-
Notifications
You must be signed in to change notification settings - Fork 4
/
back_panel.py
102 lines (90 loc) · 2.36 KB
/
back_panel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
# coding: utf-8
# Author: Richard Hopkins
# Date: 3 Novelner 2022
#
# This program drives the MicroPython
# back light controller; it is designed
# to be backwards compatible with
# back_lights.py
#
# The program interacts with the panel.py
# program. The panel.py program can be uploaded
# to the micropython device using:
#
# python3 pyboard.py --device /dev/tty.usbmodem387A384631342 -f cp panel.py :main.py
#
# Light schemes:
# original
# colour
# diagonal
# two
# three
# four
# six
# red
# green
# blue
# spiral
# chase_v
# chase_h
# cols
# rows
# on
# off
#
# Speeds (with ms delay)
# fastest: 50
# fast : 100
# normal : 200
# slow : 400
# slowest: 800
#
import serial
import ast
class BackLights():
def __init__(self) -> None:
self.ser = serial.Serial(
port='/dev/backpanel', # replace with your device name
baudrate = 115200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
def __write(self,text:str) -> None:
#print("Back panel:",text)
self.ser.write(str.encode(text+"\n"))
def __sw_light(self, cmd:str, lights:list) -> None:
for light in lights:
text = "light " + str(light) + " " + cmd
self.__write(text)
def cmd(self,text:str) -> None:
self.__write(text)
def on(self):
self.__write("original")
def off(self):
self.__write("off")
def turn_on(self, lights:list):
self.__sw_light("on",lights)
def turn_off(self, lights:list):
self.__sw_light("off",lights)
def toggle(self, lights:list):
self.__sw_light("toggle",lights)
def tv_on(self):
self.__write("tvon")
def tv_off(self):
self.__write("tvoff")
def get_switch_state(self) -> list:
self.switch_state = []
self.__write("switchstate")
my_input = self.ser.readlines()
#print("I heard:" + str(my_input))
try:
input_str = my_input[0].decode().strip()
except IndexError:
return self.switch_state
input_str = input_str[len('switchstate:'):]
switchstate_list = ast.literal_eval(input_str)
self.switch_state = [bool(x) for x in switchstate_list]
return self.switch_state