from machine import Pin, PWM
from time import sleep_ms
import machine
from machine import Pin, SoftI2C, PWM
from machine import I2C
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
from ir_rx import NEC_16
from time import sleep
R=Pin(12, Pin.OUT)
B=Pin(27, Pin.OUT)
G=Pin(14, Pin.OUT)
ir_data=0
R_st=0
B_st=0
G_st=0
def callback(data, ctrl):
global ir_data
if data>0:
ir_data=data
print('Data {:.02x}'.format(data))
ir=NEC_16(Pin(13, Pin.IN), callback)
I2C_ADDR = 0x27
totalRows = 2
totalColumns = 16
sdaPIN=machine.Pin(21)
sclPIN=machine.Pin(22)
i2c=machine.I2C(0, sda=sdaPIN, scl=sclPIN, freq=10000)
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
segment_pins = {
'A': Pin(19, Pin.OUT),
'B': Pin(18, Pin.OUT),
'C': Pin(17, Pin.OUT),
'D': Pin(16, Pin.OUT),
'E': Pin(4, Pin.OUT),
'F': Pin(2, Pin.OUT),
'G': Pin(15, Pin.OUT),
'DP':Pin(5,Pin.OUT)
}
digit_map = {
'0': ['A', 'B', 'C', 'D', 'E', 'F'],
'1': ['B', 'C'],
'2': ['A', 'B', 'G', 'E', 'D'],
'3': ['A', 'B', 'C', 'D', 'G'],
'4': ['F', 'G', 'B', 'C'],
'5': ['A', 'F', 'G', 'C', 'D'],
'6': ['A', 'F', 'E', 'D', 'C', 'G'],
'7': ['A', 'B', 'C'],
'8': ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
'9': ['A', 'B', 'C', 'D', 'F', 'G']
}
def display_digit(digit):
segments = digit_map.get(str(digit), [])
for seg, pin in segment_pins.items():
pin.value(1 if seg in segments else 0)
while True:
if ir_data==0x30:
digit = '1'
print("Pressed value 1")
display_digit(digit)
lcd.move_to(1,0)
lcd.clear()
lcd.putstr("Pressed value 1")
if R_st==1 and B_st==0 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Red Color")
elif R_st==0 and B_st==1 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Blue Color")
elif R_st==0 and B_st==0 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Green Color")
elif R_st==1 and B_st==1 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Magenta Color")
elif R_st==1 and B_st==0 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Yellow Color")
elif R_st==0 and B_st==1 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Cyan Color")
elif R_st==1 and B_st==1 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Blue Color")
else:
lcd.move_to(0,1)
lcd.putstr("No Color")
ir_data=0
elif ir_data==0x18:
digit = '2'
print("Pressed value 2")
display_digit(digit)
lcd.move_to(1,0)
lcd.clear()
lcd.putstr("Pressed value 2")
if R_st==1 and B_st==0 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Red Color")
elif R_st==0 and B_st==1 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Blue Color")
elif R_st==0 and B_st==0 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Green Color")
elif R_st==1 and B_st==1 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Magenta Color")
elif R_st==1 and B_st==0 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Yellow Color")
elif R_st==0 and B_st==1 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Cyan Color")
elif R_st==1 and B_st==1 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Blue Color")
else:
lcd.move_to(0,1)
lcd.putstr("No Color")
ir_data=0
elif ir_data==0x7a:
digit = '3'
print("Pressed value 3")
display_digit(digit)
lcd.move_to(1,0)
lcd.clear()
lcd.putstr("Pressed value 3")
if R_st==1 and B_st==0 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Red Color")
elif R_st==0 and B_st==1 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Blue Color")
elif R_st==0 and B_st==0 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Green Color")
elif R_st==1 and B_st==1 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Magenta Color")
elif R_st==1 and B_st==0 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Yellow Color")
elif R_st==0 and B_st==1 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Cyan Color")
elif R_st==1 and B_st==1 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Blue Color")
else:
lcd.move_to(0,1)
lcd.putstr("No Color")
ir_data=0
elif ir_data==0x10:
digit = '4'
print("Pressed value 4")
display_digit(digit)
lcd.move_to(1,0)
lcd.clear()
lcd.putstr("Pressed value 4")
if R_st==1 and B_st==0 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Red Color")
elif R_st==0 and B_st==1 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Blue Color")
elif R_st==0 and B_st==0 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Green Color")
elif R_st==1 and B_st==1 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Magenta Color")
elif R_st==1 and B_st==0 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Yellow Color")
elif R_st==0 and B_st==1 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Cyan Color")
elif R_st==1 and B_st==1 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Blue Color")
else:
lcd.move_to(0,1)
lcd.putstr("No Color")
ir_data=0
elif ir_data==0x38:
digit = '5'
print("Pressed value 5")
display_digit(digit)
lcd.move_to(1,0)
lcd.clear()
lcd.putstr("Pressed value 5")
if R_st==1 and B_st==0 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Red Color")
elif R_st==0 and B_st==1 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Blue Color")
elif R_st==0 and B_st==0 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Green Color")
elif R_st==1 and B_st==1 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Magenta Color")
elif R_st==1 and B_st==0 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Yellow Color")
elif R_st==0 and B_st==1 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Cyan Color")
elif R_st==1 and B_st==1 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Blue Color")
else:
lcd.move_to(0,1)
lcd.putstr("No Color")
ir_data=0
elif ir_data==0x5a:
digit = '6'
print("Pressed value 6")
display_digit(digit)
lcd.move_to(1,0)
lcd.clear()
lcd.putstr("Pressed value 6")
if R_st==1 and B_st==0 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Red Color")
elif R_st==0 and B_st==1 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Blue Color")
elif R_st==0 and B_st==0 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Green Color")
elif R_st==1 and B_st==1 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Magenta Color")
elif R_st==1 and B_st==0 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Yellow Color")
elif R_st==0 and B_st==1 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Cyan Color")
elif R_st==1 and B_st==1 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Blue Color")
else:
lcd.move_to(0,1)
lcd.putstr("No Color")
ir_data=0
elif ir_data==0x42:
digit = '7'
print("Pressed value 7")
display_digit(digit)
lcd.move_to(1,0)
lcd.clear()
lcd.putstr("Pressed value 7")
if R_st==1 and B_st==0 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Red Color")
elif R_st==0 and B_st==1 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Blue Color")
elif R_st==0 and B_st==0 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Green Color")
elif R_st==1 and B_st==1 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Magenta Color")
elif R_st==1 and B_st==0 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Yellow Color")
elif R_st==0 and B_st==1 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Cyan Color")
elif R_st==1 and B_st==1 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Blue Color")
else:
lcd.move_to(0,1)
lcd.putstr("No Color")
ir_data=0
elif ir_data==0x4a:
digit = '8'
print("Pressed value 8")
display_digit(digit)
lcd.move_to(1,0)
lcd.clear()
lcd.putstr("Pressed value 8")
if R_st==1 and B_st==0 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Red Color")
elif R_st==0 and B_st==1 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Blue Color")
elif R_st==0 and B_st==0 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Green Color")
elif R_st==1 and B_st==1 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Magenta Color")
elif R_st==1 and B_st==0 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Yellow Color")
elif R_st==0 and B_st==1 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Cyan Color")
elif R_st==1 and B_st==1 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Blue Color")
else:
lcd.move_to(0,1)
lcd.putstr("No Color")
ir_data=0
elif ir_data==0x52:
digit = '9'
print("Pressed value 9")
display_digit(digit)
lcd.move_to(1,0)
lcd.clear()
lcd.putstr("Pressed value 9")
if R_st==1 and B_st==0 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Red Color")
elif R_st==0 and B_st==1 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Blue Color")
elif R_st==0 and B_st==0 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Green Color")
elif R_st==1 and B_st==1 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Magenta Color")
elif R_st==1 and B_st==0 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Yellow Color")
elif R_st==0 and B_st==1 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Cyan Color")
elif R_st==1 and B_st==1 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Blue Color")
else:
lcd.move_to(0,1)
lcd.putstr("No Color")
ir_data=0
elif ir_data==0x68:
digit = '0'
print("Pressed value 0")
display_digit(digit)
lcd.move_to(1,0)
lcd.clear()
lcd.putstr("Pressed value 0")
if R_st==1 and B_st==0 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Red Color")
elif R_st==0 and B_st==1 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Blue Color")
elif R_st==0 and B_st==0 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Green Color")
elif R_st==1 and B_st==1 and G_st==0:
lcd.move_to(0,1)
lcd.putstr("Magenta Color")
elif R_st==1 and B_st==0 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Yellow Color")
elif R_st==0 and B_st==1 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Cyan Color")
elif R_st==1 and B_st==1 and G_st==1:
lcd.move_to(0,1)
lcd.putstr("Blue Color")
else:
lcd.move_to(0,1)
lcd.putstr("No Color")
ir_data=0
elif ir_data==0xe0:
R_st=not R_st
R.value(R_st)
print("Red on" if R_st else "Red OFF")
if R_st==0:
lcd.move_to(0, 0)
lcd.clear()
lcd.putstr("RED OFF")
elif R_st==1:
lcd.move_to(4,0)
lcd.clear()
lcd.putstr("RED ON")
ir_data=0
elif ir_data==0xa8:
G_st=not G_st
G.value(G_st)
print("Green on" if G_st else "Green OFF")
if G_st==0:
lcd.move_to(0, 0)
lcd.clear()
lcd.putstr("Green OFF")
elif G_st==1:
lcd.move_to(4,0)
lcd.clear()
lcd.putstr("Green ON")
ir_data=0
elif ir_data==0x90:
B_st=not B_st
B.value(B_st)
print("Blue on" if B_st else "Blue OFF")
if B_st==0:
lcd.move_to(0, 0)
lcd.clear()
lcd.putstr("Blue OFF")
elif B_st==1:
lcd.move_to(4,0)
lcd.clear()
lcd.putstr("Blue ON")
ir_data=0
elif ir_data==0xb0:
digit = '8','DP'
display_digit(digit)
R.off()
B.off()
G.off()
lcd.clear()
lcd.move_to(0,0)
lcd.putstr("All Clear")
sleep(2)
lcd.clear()
ir_data=0