leds/leds.py

89 lines
1.8 KiB
Python
Raw Normal View History

2020-11-03 23:51:13 +00:00
#!/usr/bin/env python
#
# based on code from lrvick and LiquidCrystal
# lrvic - https://github.com/lrvick/raspi-hd44780/blob/master/hd44780.py
# LiquidCrystal - https://github.com/arduino/Arduino/blob/master/libraries/LiquidCrystal/LiquidCrystal.cpp
#
from time import sleep
2020-11-08 03:33:55 +00:00
import sys
import lcd_manager
import light_manager
2020-11-08 07:16:30 +00:00
import pattern
2020-11-08 07:31:45 +00:00
import logger
2020-11-08 07:16:30 +00:00
import threading
2020-11-04 01:44:08 +00:00
2020-11-08 03:33:55 +00:00
this = sys.modules[__name__]
this.level = 0
this.level_max = 14
this.level_state = 0
this.color_state = 0
2020-11-08 03:28:58 +00:00
2020-11-04 01:44:08 +00:00
def lightlevel(lcd, level):
2020-11-08 07:37:40 +00:00
logger.debug("display level")
lcd.clear()
lcd.message("Light Level:\n]" + "-"*level + "[")
2020-11-04 01:44:08 +00:00
2020-11-08 03:28:58 +00:00
def query():
level_state = 2
2020-11-04 01:44:08 +00:00
2020-11-07 01:22:15 +00:00
def color(lcd):
lcd.clear()
2020-11-07 01:22:15 +00:00
lcd.message("new pattern loaded.")
2020-11-08 07:37:40 +00:00
logger.debug("NYI")
2020-11-08 03:28:58 +00:00
def get_state():
2020-11-08 03:33:55 +00:00
if this.level_state > 0:
this.level_state -= 1
2020-11-08 03:28:58 +00:00
return "level"
2020-11-08 03:33:55 +00:00
elif this.color_state > 0:
this.color_state -= 1
2020-11-08 03:28:58 +00:00
return "color"
else:
return "idle"
def displayon(lcd):
if lcd.displaycontrol & lcd.LCD_DISPLAYON != lcd.displaycontrol:
lcd.display()
2020-11-08 07:16:30 +00:00
def socket_loop():
injected = False
while True:
if not injected:
this.lights.set_pattern(pattern.pat)
2020-11-03 23:51:13 +00:00
def loop():
2020-11-08 07:43:11 +00:00
socket_thread = threading.Thread(target=socket_loop)
socket_thread.start()
lcd = lcd_manager.Adafruit_CharLCD()
lights = light_manager.LightStrip()
2020-11-08 07:16:30 +00:00
this.lights = lights
while True:
2020-11-08 07:37:40 +00:00
logger.debug("loop")
lights.tick()
2020-11-08 03:28:58 +00:00
query()
state = get_state()
if state == "level":
2020-11-08 03:33:55 +00:00
if lights.get_light_level() != (this.level / this.level_max):
lights.set_light_level(this.level / this.level_max)
lightlevel(lcd, this.level)
2020-11-08 03:28:58 +00:00
elif state == "color":
color(lcd)
2020-11-04 01:44:08 +00:00
else:
2020-11-08 03:28:58 +00:00
lcd.noDisplay()
2020-11-07 04:17:02 +00:00
sleep(0.1)
2020-11-03 23:51:13 +00:00
if __name__ == '__main__':
loop()