leds/leds.py

77 lines
1.6 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-07 04:12:10 +00:00
import logging
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-07 04:12:10 +00:00
logging.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-07 04:12:10 +00:00
logging.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-03 23:51:13 +00:00
def loop():
lcd = lcd_manager.Adafruit_CharLCD()
lights = light_manager.LightStrip()
while True:
2020-11-07 04:12:10 +00:00
logging.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()