import time, math
from machine import Pin, I2C
import asyncio
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
import ds1307
time.sleep(0.1) # Wait for USB to become ready
# Define LCD I2C pins/BUS/address
SDA = 26
SCL = 27
I2C_BUS = 1
LCD_ADDR = 0x27
RTC_ADDR = 0x68
#define LCD rows/columns
LCD_NUM_ROWS = 4
LCD_NUM_COLS = 20
#setup LCD I2C
lcdi2c = I2C(I2C_BUS, sda=Pin(SDA), scl=Pin(SCL), freq=400000)
lcd = I2cLcd(lcdi2c, LCD_ADDR, LCD_NUM_ROWS, LCD_NUM_COLS)
# lcd.putstr("Automatic Lights and Race Control System")
# Setup the ds1307RTC
ds = ds1307.DS1307(lcdi2c)
ds.halt(False)
# Setup the buttons
select_btn = Pin(15, Pin.IN, Pin.PULL_UP)
run_btn = Pin(13, Pin.IN, Pin.PULL_UP)
horn_btn = Pin(14, Pin.IN, Pin.PULL_UP)
shorten_btn = Pin(12, Pin.IN, Pin.PULL_UP)
general_btn = Pin(10, Pin.IN, Pin.PULL_UP)
individual_btn = Pin(9, Pin.IN, Pin.PULL_UP)
postpone_btn = Pin(8, Pin.IN, Pin.PULL_UP)
# Setup the LEDs
blu_lt = Pin(21, Pin.OUT)
red_lt = Pin(20, Pin.OUT)
grn_lt = Pin(19, Pin.OUT)
yel_lt = Pin(18, Pin.OUT)
horn = Pin(17, Pin.OUT)
#blu_lt.value(True)
#red_lt.value(True)
#grn_lt.value(True)
#yel_lt.value(True)
#horn.value(True)
count = 0
lcd_new = ["LSC Start System", "LSC Start System", "Line2", "Line3"]
lcd_old = ["a", "b", "c", "d"]
def out_fn(old, new):
"""
Displays a message on the LCD with four strings for four lines.
To reduce time spent updating the LCD screen the old strings are
compared with the new strings, and the update sent over I2C only
if there is a difference.
:param old: Tuple containing previous four strings sent to LCD
:param new: Tuple containing current set of strings to be checked
and LCD updated as required.
:return: Tuple containing last four strings sent to LCD
"""
msg_old = ' | '.join(old)
msg_new = ' | '.join(new)
if msg_old != msg_new:
print(msg_new)
for x in range(0, 4): # iterate through each line of LCD
if old[x] != new[x]:
old[x] = new[x]
lcd.move_to(0,x)
lcd.putstr(new[x][:20])
return [old[0], old[1], old[2], old[3]]
def rtc_check():
# Checks the RTC and returns string of hh:mm:ss
tm = ds.datetime()
hr = tm[4]
mn = tm[5]
sc = tm[6]
string = ' {:02}:{:02}:{:02}'.format(hr, mn, sc)
return string
def tm_fmt_fn(secs):
"""
Converts a time difference in seconds to a
string formatted as hh:mm:ss
:param: secs Number of seconds to convert
:return: Dictionary with numerical values for hh:MM:ss
"""
sec_value = abs(secs) % (24 * 3600)
hour_value = sec_value // 3600
sec_value %= 3600
minute_value = sec_value // 60
sec_value %= 60
sec_value = math.floor(sec_value)
return {'ss': sec_value, 'hh': hour_value, 'mm': minute_value}
# out_fn(rtc_check())
#lcd_new[0] = rtc_check()
#lcd_old = out_fn(lcd_old, lcd_new)
#print(lcd_old)
def lights_set(blue: bool = False,
grn: bool = False,
yel: bool = False,
red: bool = False):
blu_lt.value = blue
grn_lt.value = grn
yel_lt.value = yel
red_lt.value = red
async def blink_green_led():
while True:
grn_lt.toggle()
await asyncio.sleep(2)
async def blink_blue_led():
while True:
blu_lt.toggle()
await asyncio.sleep(0.5)
async def main():
global lcd_new
global lcd_old
asyncio.create_task(blink_blue_led())
asyncio.create_task(blink_green_led())
# lcd_old = asyncio.create_task(out_fn(lcd_old, lcd_new))
#loop = asyncio.get_event_loop()
#loop.create_task(main()) # Create a task to run the main function
#loop.run_forever() # Run the event loop indefinitely
Init_tm = time.ticks_ms()
print(Init_tm)
while True:
lcd_new[0] = rtc_check()
lcd_old = out_fn(lcd_old, lcd_new)
time.sleep(1)
"""
while False:
if select_btn.value() == False:
blu_lt.value(True)
count = count + 1
out_fn(str(count))
else:
blu_lt.value(False)
time.sleep(0.1)
if select_btn.value() ==True:
btn = 'Select'
if blu_lt.value() == True:
blu_lt.value(False)
else:
blu_lt.value(True)
out_fn(btn)
elif run_btn.value() == True:
btn = 'Run'
out_fn(btn)
elif shorten_btn.value() == True:
btn = 'Shorten'
out_fn(btn)
elif general_btn.value() == True:
btn = 'General'
out_fn(btn)
elif individual_btn.value() == True:
btn = 'Individual'
out_fn(btn)
elif postpone_btn.value() == True:
btn = 'Postpone'
out_fn(btn)
elif horn_btn.value() == True:
btn = 'Horn'
out_fn(btn)
"""