from machine import Pin, I2C
from time import sleep
# ==========================
# LCD I2C
# ==========================
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
I2C_ADDR = 0x27
ROWS = 2
COLS = 16
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, ROWS, COLS)
# ==========================
# LEDs & Buzzer
# ==========================
light = Pin(18, Pin.OUT)
fan = Pin(19, Pin.OUT)
door = Pin(23, Pin.OUT)
alarm = Pin(5, Pin.OUT)
# ==========================
# DEMUX Select Lines
# ==========================
S0 = Pin(15, Pin.OUT)
S1 = Pin(2, Pin.OUT)
S2 = Pin(4, Pin.OUT)
S3 = Pin(16, Pin.OUT)
# COM Pin
SIG = Pin(17, Pin.OUT)
# ==========================
# Keypad
# ==========================
rows = [
Pin(13, Pin.OUT),
Pin(12, Pin.OUT),
Pin(14, Pin.OUT),
Pin(27, Pin.OUT)
]
cols = [
Pin(26, Pin.IN, Pin.PULL_DOWN),
Pin(25, Pin.IN, Pin.PULL_DOWN),
Pin(33, Pin.IN, Pin.PULL_DOWN),
Pin(32, Pin.IN, Pin.PULL_DOWN)
]
keys = [
['1','2','3','A'],
['4','5','6','B'],
['7','8','9','C'],
['*','0','#','D']
]
selected = None
# ==========================
# LCD Startup
# ==========================
lcd.clear()
lcd.putstr("Smart Home")
sleep(2)
lcd.clear()
lcd.putstr("Select Device")
# ==========================
# Turn Everything OFF
# ==========================
def all_off():
light.off()
fan.off()
door.off()
alarm.off()
all_off()
# ==========================
# DEMUX Selection
# ==========================
def select_device(num):
global selected
selected = num
# خطوط اختيار الـ DEMUX
S0.value(num & 1)
S1.value((num >> 1) & 1)
S2.value(0)
S3.value(0)
SIG.value(1)
lcd.clear()
if num == 0:
lcd.putstr("Light Selected")
elif num == 1:
lcd.putstr("Fan Selected")
elif num == 2:
lcd.putstr("Door Selected")
elif num == 3:
lcd.putstr("Alarm Selected")
# ==========================
# Device Control
# ==========================
def device_on():
all_off()
if selected == 0:
light.on()
lcd.clear()
lcd.putstr("Light ON")
elif selected == 1:
fan.on()
lcd.clear()
lcd.putstr("Fan ON")
elif selected == 2:
door.on()
lcd.clear()
lcd.putstr("Door ON")
elif selected == 3:
alarm.on()
lcd.clear()
lcd.putstr("Alarm ON")
def device_off():
if selected == 0:
light.off()
lcd.clear()
lcd.putstr("Light OFF")
elif selected == 1:
fan.off()
lcd.clear()
lcd.putstr("Fan OFF")
elif selected == 2:
door.off()
lcd.clear()
lcd.putstr("Door OFF")
elif selected == 3:
alarm.off()
lcd.clear()
lcd.putstr("Alarm OFF")
def reset_system():
global selected
all_off()
selected = None
SIG.value(0)
lcd.clear()
lcd.putstr("System Reset")
# ==========================
# Keypad Scan
# ==========================
def read_key():
for r in range(4):
for rr in rows:
rr.value(0)
rows[r].value(1)
for c in range(4):
if cols[c].value():
sleep(0.2)
while cols[c].value():
pass
return keys[r][c]
return None
# ==========================
# Main Loop
# ==========================
while True:
key = read_key()
if key:
if key == '1':
select_device(0)
elif key == '2':
select_device(1)
elif key == '3':
select_device(2)
elif key == '4':
select_device(3)
elif key == 'A':
if selected is not None:
device_on()
elif key == 'B':
if selected is not None:
device_off()
elif key == 'D':
reset_system()
sleep(0.05)