leds/leds.py

101 lines
2.3 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-09 04:15:31 +00:00
import socket
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
2020-11-09 04:15:31 +00:00
s = socket.create_server(('0.0.0.0', 29999))
2020-11-08 07:16:30 +00:00
while True:
if not injected:
this.lights.set_pattern(pattern.pat)
2020-11-09 04:15:31 +00:00
injected = True
sock, addr = s.accept()
with sock:
length_data = sock.recv(4)
2020-11-09 06:38:02 +00:00
logger.debug(length_data)
2020-11-09 04:15:31 +00:00
length = int(length_data.decode())
2020-11-09 06:38:02 +00:00
logger.debug(length)
2020-11-09 04:15:31 +00:00
pattern_data = sock.recv(length)
2020-11-09 06:38:02 +00:00
logger.debug(pattern_data)
2020-11-09 04:15:31 +00:00
pattern.parse(pattern_data.decode())
2020-11-10 22:59:59 +00:00
this.color_state = 7
2020-11-08 07:16:30 +00:00
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)
lcd = lcd_manager.Adafruit_CharLCD()
2020-11-08 07:45:47 +00:00
this.lights = light_manager.LightStrip()
2020-11-08 07:48:12 +00:00
socket_thread.start()
while True:
2020-11-08 07:37:40 +00:00
logger.debug("loop")
2020-11-08 07:45:47 +00:00
this.lights.tick()
2020-11-08 03:28:58 +00:00
query()
state = get_state()
if state == "level":
2020-11-08 07:45:47 +00:00
if this.lights.get_light_level() != (this.level / this.level_max):
this.lights.set_light_level(this.level / this.level_max)
2020-11-08 03:33:55 +00:00
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()