Added timer script

This commit is contained in:
2025-12-13 15:13:33 +01:00
parent b9a87309e4
commit 47fa075a62
2 changed files with 127 additions and 0 deletions

89
rpi/lcd-screen/lcd.py Normal file
View File

@@ -0,0 +1,89 @@
import time,sys
from grove_rgb_lcd import lcd
if sys.platform == 'uwp':
import winrt_smbus as smbus
bus = smbus.SMBus(1)
else:
import smbus
import RPi.GPIO as GPIO
rev = GPIO.RPI_REVISION
if rev == 2 or rev == 3:
bus = smbus.SMBus(1)
else:
bus = smbus.SMBus(0)
# this device has two I2C addresses
DISPLAY_RGB_ADDR = 0x62
DISPLAY_TEXT_ADDR = 0x3e
# set backlight to (R,G,B) (values from 0..255 for each)
def setRGB(r,g,b):
bus.write_byte_data(DISPLAY_RGB_ADDR,0,0)
bus.write_byte_data(DISPLAY_RGB_ADDR,1,0)
bus.write_byte_data(DISPLAY_RGB_ADDR,0x08,0xaa)
bus.write_byte_data(DISPLAY_RGB_ADDR,4,r)
bus.write_byte_data(DISPLAY_RGB_ADDR,3,g)
bus.write_byte_data(DISPLAY_RGB_ADDR,2,b)
# send command to display (no need for external use)
def textCommand(cmd):
bus.write_byte_data(DISPLAY_TEXT_ADDR,0x80,cmd)
# set display text \n for second line(or auto wrap)
def setText(text):
textCommand(0x01) # clear display
time.sleep(.05)
textCommand(0x08 | 0x04) # display on, no cursor
textCommand(0x28) # 2 lines
time.sleep(.05)
count = 0
row = 0
for c in text:
if c == '\n' or count == 16:
count = 0
row += 1
if row == 2:
break
textCommand(0xc0)
if c == '\n':
continue
count += 1
bus.write_byte_data(DISPLAY_TEXT_ADDR,0x40,ord(c))
#Update the display without erasing the display
def setText_norefresh(text):
textCommand(0x02) # return home
time.sleep(.05)
textCommand(0x08 | 0x04) # display on, no cursor
textCommand(0x28) # 2 lines
time.sleep(.05)
count = 0
row = 0
while len(text) < 32: #clears the rest of the screen
text += ' '
for c in text:
if c == '\n' or count == 16:
count = 0
row += 1
if row == 2:
break
textCommand(0xc0)
if c == '\n':
continue
count += 1
bus.write_byte_data(DISPLAY_TEXT_ADDR,0x40,ord(c))
# example code
if __name__=="__main__":
setRGB(255, 255, 255)
# Total time in seconds
total_seconds = 5 * 60
while total_seconds >= 0:
minutes = total_seconds // 60
seconds = total_seconds % 60
setText(f"{minutes:02d}:{seconds:02d}")
time.sleep(1)
total_seconds -= 1

38
rpi/lcd-screen/main.py Normal file
View File

@@ -0,0 +1,38 @@
#import lcd
import time
whiteTime = 5 * 60
blackTime = 5 * 60
current = "white"
def compute_seconds(total_seconds):
return total_seconds % 60
def compute_minutes(total_seconds):
return total_seconds // 60
def update_timer(whiteSeconds, blackSeconds):
white_minutes = compute_minutes(whiteSeconds)
white_seconds = compute_seconds(whiteSeconds)
black_minutes = compute_minutes(blackSeconds)
black_seconds = compute_seconds(blackSeconds)
print(f"W {white_minutes:02d}:{white_seconds:02d} || B {black_minutes:02d}:{black_seconds:02d}")
#lcd.setText(f"W {white_minutes:02d}:{white_seconds:02d} || B {black_minutes:02d}:{black_seconds:02d}")
if __name__ == "__main__":
while whiteTime > 0 and blackTime > 0:
currentTime = 0
if current == "white":
current = "black"
whiteTime -= 1
else:
current = "white"
blackTime -= 1
update_timer(whiteTime, blackTime)
time.sleep(1)
#lcd.setText("Time up!")