Repository navigation
Expand file tree
/
Copy path__init__.py
More file actions
152 lines (119 loc) · 3.39 KB
/
Copy path__init__.py
File metadata and controls
152 lines (119 loc) · 3.39 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from enum import IntEnum
import serial
# TODO: Make independent from GUI
from inputmodule.gui.gui_threading import disconnect_dev
FWK_MAGIC = [0x32, 0xAC]
FWK_VID = 0x32AC
LED_MATRIX_PID = 0x20
QTPY_PID = 0x001F
INPUTMODULE_PIDS = [LED_MATRIX_PID, QTPY_PID]
class CommandVals(IntEnum):
Brightness = 0x00
Pattern = 0x01
BootloaderReset = 0x02
Sleep = 0x03
Animate = 0x04
Panic = 0x05
Draw = 0x06
StageGreyCol = 0x07
DrawGreyColBuffer = 0x08
SetText = 0x09
StartGame = 0x10
GameControl = 0x11
GameStatus = 0x12
SetColor = 0x13
DisplayOn = 0x14
InvertScreen = 0x15
SetPixelColumn = 0x16
FlushFramebuffer = 0x17
ClearRam = 0x18
ScreenSaver = 0x19
SetFps = 0x1A
SetPowerMode = 0x1B
PwmFreq = 0x1E
DebugMode = 0x1F
Version = 0x20
class Game(IntEnum):
Snake = 0x00
Pong = 0x01
Tetris = 0x02
GameOfLife = 0x03
class PatternVals(IntEnum):
Percentage = 0x00
Gradient = 0x01
DoubleGradient = 0x02
DisplayLotus = 0x03
ZigZag = 0x04
FullBrightness = 0x05
DisplayPanic = 0x06
DisplayLotus2 = 0x07
class GameOfLifeStartParam(IntEnum):
Currentmatrix = 0x00
Pattern1 = 0x01
Blinker = 0x02
Toad = 0x03
Beacon = 0x04
Glider = 0x05
def __str__(self):
return self.name.lower()
def __repr__(self):
return str(self)
@staticmethod
def argparse(s):
try:
return GameOfLifeStartParam[s.lower().capitalize()]
except KeyError:
return s
class GameControlVal(IntEnum):
Up = 0
Down = 1
Left = 2
Right = 3
Quit = 4
RESPONSE_SIZE = 32
def bootloader(dev):
"""Reboot into the bootloader to flash new firmware"""
send_command(dev, CommandVals.BootloaderReset, [0x00])
def brightness(dev, b: int):
"""Adjust the brightness scaling of the entire screen."""
send_command(dev, CommandVals.Brightness, [b])
def get_brightness(dev):
"""Adjust the brightness scaling of the entire screen."""
res = send_command(dev, CommandVals.Brightness, with_response=True)
return int(res[0])
def get_version(dev):
"""Get the device's firmware version"""
res = send_command(dev, CommandVals.Version, with_response=True)
if not res:
return 'Unknown'
major = res[0]
minor = (res[1] & 0xF0) >> 4
patch = res[1] & 0xF
pre_release = res[2]
version = f"{major}.{minor}.{patch}"
if pre_release:
version += " (Pre-release)"
return version
def send_command(dev, command, parameters=[], with_response=False):
return send_command_raw(dev, FWK_MAGIC + [command] + parameters, with_response)
def send_command_raw(dev, command, with_response=False):
"""Send a command to the device.
Opens new serial connection every time"""
# print(f"Sending command: {command}")
try:
with serial.Serial(dev.device, 115200) as s:
s.write(command)
if with_response:
res = s.read(RESPONSE_SIZE)
# print(f"Received: {res}")
return res
except (IOError, OSError) as _ex:
disconnect_dev(dev.device)
# print("Error: ", ex)
def send_serial(dev, s, command):
"""Send serial command by using existing serial connection"""
try:
s.write(command)
except (IOError, OSError) as _ex:
disconnect_dev(dev.device)
# print("Error: ", ex)