Handle fridge door properly

This commit is contained in:
Davide Depau 2020-12-08 17:01:17 +01:00
parent f7e5330b9a
commit 04264c34fe
2 changed files with 34 additions and 6 deletions

View file

@ -1,3 +1,4 @@
pyserial pyserial
PySensors PySensors
psutil psutil
openrgb-python

View file

@ -10,9 +10,11 @@ import time
import psutil import psutil
import sensors import sensors
import serial import serial
from openrgb import OpenRGBClient
from openrgb.utils import DeviceType, RGBColor
should_stop = False should_stop = False
debug = False debug = True
structure = [ structure = [
{'pack': 'I', 'mode': 'magic', 'value': 0xAAAAAAAA}, {'pack': 'I', 'mode': 'magic', 'value': 0xAAAAAAAA},
@ -152,9 +154,35 @@ def read_serial(s: serial.Serial):
line = s.readline(1024) line = s.readline(1024)
if line.endswith(b'\n'): if line.endswith(b'\n'):
line = line[:-1] line = line[:-1]
if len(line) > 0: if len(line) == 0:
continue
if debug:
print(f"recv[{len(line):>3}]: ", line.decode(errors='replace'), ) print(f"recv[{len(line):>3}]: ", line.decode(errors='replace'), )
# Set fridge light on if the lid is open
if line.strip().startswith(b"LID "):
lid_open = None
if b"LID OPEN" in line:
lid_open = True
if b"LID CLOSED" in line:
lid_open = False
if lid_open is not None:
set_fridge_lights(lid_open)
def set_fridge_lights(on: bool):
client = None
try:
client = OpenRGBClient()
motherboard = client.get_devices_by_type(DeviceType.MOTHERBOARD)[0]
onboard_led_0 = filter(lambda x: x.name == "Onboard LED 0", motherboard.zones).__next__()
onboard_led_0.set_color(RGBColor(0x00, 0xFF if on else 0x00, 0x00))
finally:
if client is not None:
client.disconnect()
def main(): def main():
global should_stop global should_stop
@ -174,7 +202,6 @@ def main():
s = serial.Serial(port=sys.argv[1], baudrate=115200, timeout=0.5) s = serial.Serial(port=sys.argv[1], baudrate=115200, timeout=0.5)
if debug:
t = threading.Thread(target=read_serial, args=(s,)) t = threading.Thread(target=read_serial, args=(s,))
t.start() t.start()