2020-11-03 18:51:13 -05: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-07 22:33:55 -05:00
|
|
|
import sys
|
2020-11-06 23:00:23 -05:00
|
|
|
import lcd_manager
|
|
|
|
import light_manager
|
2020-11-08 02:16:30 -05:00
|
|
|
import pattern
|
2020-11-08 02:31:45 -05:00
|
|
|
import logger
|
2020-11-08 02:16:30 -05:00
|
|
|
import threading
|
2020-11-08 23:15:31 -05:00
|
|
|
import socket
|
2020-11-03 20:44:08 -05:00
|
|
|
|
2020-11-07 22:33:55 -05:00
|
|
|
this = sys.modules[__name__]
|
|
|
|
|
|
|
|
this.level = 0
|
|
|
|
this.level_max = 14
|
|
|
|
this.level_state = 0
|
|
|
|
this.color_state = 0
|
2020-11-07 22:28:58 -05:00
|
|
|
|
2020-11-03 20:44:08 -05:00
|
|
|
|
2020-11-03 20:38:58 -05:00
|
|
|
def lightlevel(lcd, level):
|
2020-11-08 02:37:40 -05:00
|
|
|
logger.debug("display level")
|
2020-11-03 20:38:58 -05:00
|
|
|
lcd.clear()
|
|
|
|
lcd.message("Light Level:\n]" + "-"*level + "[")
|
|
|
|
|
2020-11-03 20:44:08 -05:00
|
|
|
|
2020-11-07 22:28:58 -05:00
|
|
|
def query():
|
|
|
|
level_state = 2
|
2020-11-03 20:44:08 -05:00
|
|
|
|
2020-11-03 20:38:58 -05:00
|
|
|
|
2020-11-06 20:22:15 -05:00
|
|
|
def color(lcd):
|
2020-11-03 20:38:58 -05:00
|
|
|
lcd.clear()
|
2020-11-06 20:22:15 -05:00
|
|
|
lcd.message("new pattern loaded.")
|
2020-11-08 02:37:40 -05:00
|
|
|
logger.debug("NYI")
|
2020-11-03 20:38:58 -05:00
|
|
|
|
|
|
|
|
2020-11-07 22:28:58 -05:00
|
|
|
def get_state():
|
2020-11-07 22:33:55 -05:00
|
|
|
if this.level_state > 0:
|
|
|
|
this.level_state -= 1
|
2020-11-07 22:28:58 -05:00
|
|
|
return "level"
|
2020-11-07 22:33:55 -05:00
|
|
|
elif this.color_state > 0:
|
|
|
|
this.color_state -= 1
|
2020-11-07 22:28:58 -05:00
|
|
|
return "color"
|
|
|
|
else:
|
|
|
|
return "idle"
|
|
|
|
|
|
|
|
|
|
|
|
def displayon(lcd):
|
|
|
|
if lcd.displaycontrol & lcd.LCD_DISPLAYON != lcd.displaycontrol:
|
|
|
|
lcd.display()
|
|
|
|
|
|
|
|
|
2020-11-08 02:16:30 -05:00
|
|
|
def socket_loop():
|
|
|
|
injected = False
|
2020-11-08 23:15:31 -05:00
|
|
|
s = socket.create_server(('0.0.0.0', 29999))
|
2020-11-08 02:16:30 -05:00
|
|
|
while True:
|
|
|
|
if not injected:
|
|
|
|
this.lights.set_pattern(pattern.pat)
|
2020-11-08 23:15:31 -05:00
|
|
|
injected = True
|
|
|
|
sock, addr = s.accept()
|
|
|
|
with sock:
|
|
|
|
length_data = sock.recv(4)
|
2020-11-09 01:38:02 -05:00
|
|
|
logger.debug(length_data)
|
2020-11-08 23:15:31 -05:00
|
|
|
length = int(length_data.decode())
|
2020-11-09 01:38:02 -05:00
|
|
|
logger.debug(length)
|
2020-11-08 23:15:31 -05:00
|
|
|
pattern_data = sock.recv(length)
|
2020-11-09 01:38:02 -05:00
|
|
|
logger.debug(pattern_data)
|
2020-11-08 23:15:31 -05:00
|
|
|
pattern.parse(pattern_data.decode())
|
2020-11-10 17:59:59 -05:00
|
|
|
this.color_state = 7
|
2020-11-08 02:16:30 -05:00
|
|
|
|
|
|
|
|
2020-11-03 18:51:13 -05:00
|
|
|
def loop():
|
2020-11-08 02:43:11 -05:00
|
|
|
socket_thread = threading.Thread(target=socket_loop)
|
2020-11-06 23:00:23 -05:00
|
|
|
lcd = lcd_manager.Adafruit_CharLCD()
|
2020-12-28 23:28:24 -05:00
|
|
|
this.lights = light_manager.LightStrip(string_length=450, brightness=0.6, max_changes=10)
|
2020-11-08 02:48:12 -05:00
|
|
|
socket_thread.start()
|
2020-11-03 20:38:58 -05:00
|
|
|
while True:
|
2020-11-08 02:37:40 -05:00
|
|
|
logger.debug("loop")
|
2020-11-08 02:45:47 -05:00
|
|
|
this.lights.tick()
|
2020-11-07 22:28:58 -05:00
|
|
|
query()
|
|
|
|
state = get_state()
|
|
|
|
|
|
|
|
if state == "level":
|
2020-11-08 02:45:47 -05:00
|
|
|
if this.lights.get_light_level() != (this.level / this.level_max):
|
|
|
|
this.lights.set_light_level(this.level / this.level_max)
|
2020-11-07 22:33:55 -05:00
|
|
|
lightlevel(lcd, this.level)
|
2020-11-07 22:28:58 -05:00
|
|
|
elif state == "color":
|
|
|
|
color(lcd)
|
2020-11-03 20:44:08 -05:00
|
|
|
else:
|
2020-11-07 22:28:58 -05:00
|
|
|
lcd.noDisplay()
|
2020-11-06 23:17:02 -05:00
|
|
|
sleep(0.1)
|
2020-11-03 20:38:58 -05:00
|
|
|
|
2020-11-03 18:51:13 -05:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-12-05 23:41:31 -05:00
|
|
|
loop()
|