Skip to content

Commit 3ec5464

Browse files
committed
python: Sample usage of a new python API
Signed-off-by: Daniel Schaefer <dhs@frame.work>
1 parent 61e45b1 commit 3ec5464

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

python/sample.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import time
2+
from datetime import datetime
3+
#from inputmodule.ledmatrix import LedMatrix, Pattern
4+
5+
# TODO: Import
6+
from enum import IntEnum
7+
class PatternVals(IntEnum):
8+
FullBrightness = 0x05
9+
class CommandVals(IntEnum):
10+
Brightness = 0x00
11+
Pattern = 0x01
12+
13+
class Pattern:
14+
width = 9
15+
height = 34
16+
vals = []
17+
18+
def percentage(p):
19+
Pattern()
20+
21+
def from_string(s):
22+
Pattern()
23+
24+
def set(self, x, y, val):
25+
pass
26+
27+
class InputModule:
28+
pass
29+
30+
class LedMatrix(object):
31+
def __init__(self, name):
32+
self.name = name
33+
self.fw_version = "0.1.9"
34+
self.sleeping = True
35+
self.brightness = 100
36+
37+
def find_all():
38+
return [1, 2]
39+
40+
def __enter__(self):
41+
#print('entering')
42+
return self
43+
44+
def __exit__(self, exc_type, exc_val, exc_tb):
45+
pass
46+
#print('leaving')
47+
48+
def enter_bootloader(self):
49+
pass
50+
51+
def raw_command(self, command, vals):
52+
pass
53+
54+
def set_bw(self, pattern):
55+
pass
56+
57+
def set_grayscale(self, pattern):
58+
pass
59+
60+
# TODO: Properties for things like sleeping, brightness, ...
61+
@property
62+
def radius(self):
63+
"""The radius property."""
64+
print("Get radius")
65+
return self._radius
66+
67+
@radius.setter
68+
def radius(self, value):
69+
print("Set radius")
70+
self._radius = value
71+
72+
73+
#
74+
matrices = LedMatrix.find_all()
75+
print(f"{len(matrices)} LED Matrices connected to the system")
76+
77+
with LedMatrix("COM1") as matrix:
78+
print(f"Firmware version: {matrix.fw_version}")
79+
80+
print(f"Sleep status: {matrix.sleeping}")
81+
print(f"Going to sleep and back")
82+
matrix.sleeping = True
83+
matrix.sleeping = False
84+
85+
print(f"Current brightness: {matrix.brightness}")
86+
print("Setting 100% brightness")
87+
matrix.brightness = 100
88+
print("Setting 50% brightness")
89+
matrix.brightness = 50
90+
91+
# TODO
92+
# matrix.pwm_freq
93+
# matrix.animate
94+
# matrix.animate = True
95+
# matrix.animate = False
96+
97+
# Enter bootloader to prepare for flashing
98+
# To exit bootloader, either flash the firmware or re-plug the device
99+
# matrix.enter_bootloader()
100+
101+
print("Iterating through a couple of built-in patterns, 1s each")
102+
pattern_commands = [
103+
PatternVals.FullBrightness,
104+
#PatternVals.Gradient,
105+
#PatternVals.DoubleGradient,
106+
#PatternVals.DisplayLotus,
107+
#PatternVals.ZigZag,
108+
#PatternVals.DisplayPanic,
109+
#PatternVals.DisplayLotus2
110+
]
111+
for pattern in pattern_commands:
112+
matrix.raw_command(CommandVals.Pattern, [pattern])
113+
time.sleep(1)
114+
115+
print("Iterating through a couple of black/white patterns, 1s each")
116+
bw_patterns = [
117+
Pattern.percentage(50),
118+
]
119+
for pattern in bw_patterns:
120+
matrix.set_bw(pattern)
121+
time.sleep(1)
122+
123+
# Demonstrate gray-scale pattern
124+
print("Show all 255 brightness levels, one per LED")
125+
pattern = Pattern()
126+
for x in range(0, pattern.width):
127+
for y in range(pattern.height):
128+
brightness = x + pattern.width * y
129+
if brightness > 255:
130+
pattern.set(x, y, 0)
131+
else:
132+
pattern.set(x, y, brightness)
133+
matrix.set_grayscale(pattern)
134+
135+
# Show current time
136+
current_time = datetime.now().strftime("%H:%M")
137+
print("Current Time =", current_time)
138+
matrix.set_bw(Pattern.from_string(current_time))

0 commit comments

Comments
 (0)