-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatrix_api.py
58 lines (47 loc) · 1.97 KB
/
matrix_api.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
from rgbmatrix import graphics, RGBMatrix, RGBMatrixOptions
from dataclasses import dataclass
from PIL import Image
font = graphics.Font()
font.LoadFont("./tom-thumb.bdf")
@dataclass
class Color:
r: int
g: int
b: int
class Matrix:
"""API for interacting with a matrix. After drawing what is desired, tick must be called to actually update the display."""
def __init__(self):
options = RGBMatrixOptions()
options.show_refresh_rate = 0
options.rows = 32
options.cols = 64
options.drop_privileges = False
options.brightness = 100
options.hardware_mapping = "adafruit-hat-pwm"
# options.limit_refresh_rate_hz = 60
options.pwm_bits = 3
self.matrix = RGBMatrix(options = options)
self.canvas = self.matrix.CreateFrameCanvas()
def drawText(self, x: int, y: int, color: Color, text: str):
graphics.DrawText(self.canvas, font, x, y, graphics.Color(color.r, color.g, color.b), text)
def drawCircle(self, x: int, y: int, radius: int, color: Color):
graphics.DrawCircle(self.canvas, x, y, radius, graphics.Color(color.r, color.g, color.b))
def drawFilledCircle(self, x: int, y: int, radius: int, color: Color):
for x_diff in range(-radius, radius):
y_diff = radius - abs(x_diff)
graphics.DrawLine(
self.canvas,
x + x_diff,
y - y_diff,
x + x_diff,
y + y_diff,
graphics.Color(color.r, color.g, color.b)
)
def drawLine(self, x: int, y: int, x2: int, y2: int, color: Color):
graphics.DrawLine(self.canvas, x, y, x2, y2, graphics.Color(color.r, color.g, color.b))
def tick(self):
self.canvas = self.matrix.SwapOnVSync(self.canvas)
self.canvas.Clear()
def setImage(self, path: str, x: int, y: int):
image = Image.open(path)
self.canvas.SetImage(image.convert('RGB'), x, y)