-
Notifications
You must be signed in to change notification settings - Fork 1
/
neopixelmatrix.py
135 lines (121 loc) · 4.85 KB
/
neopixelmatrix.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
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
from machine import Pin
from neopixel import NeoPixel
class NeoPixelMatrix:
"""
Helper class for NeoPixel Matrix
Class is based on MicroPython NeoPixel
"""
NUMS = [0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, # 0
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, # 1
1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, # 2
1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, # 3
1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, # 4
1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, # 5
1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, # 6
1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, # 7
1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, # 8
1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1] # 9
def __init__(self, pin: int, rows: int, cols: int):
"""
construct a NeoPixel matrix object
:param pin: gpio pin value
:param rows: number of neopixel in rows
:param cols: number of neopixel in cols
"""
self.rows = int(rows)
self.cols = int(cols)
self.number = self.rows * self.cols
self.np = NeoPixel(Pin(pin), self.number)
def write_matrix(self) -> None:
"""
writes the current NeoPixel data to matrix
:return: None
"""
self.np.write()
def fill_matrix(self, color: tuple) -> None:
"""
set all NeoPixel to same color
:param color: tuple of RGB values
:return: None
"""
self.np.fill(tuple(color))
def clear_matrix(self) -> None:
"""
set all NeoPixel off by RGB value (0, 0, 0)
:return: None
"""
self.fill_matrix((0, 0, 0))
self.write_matrix()
def set_index(self, number: int, color: tuple) -> None:
"""
set a specific NeoPixel to RGB color by index number
:param number: specific NeoPixel
:param color: tuple of RGB values
:return: None
"""
self.np[int(number)] = tuple(color)
def set_coordinate(self, x: int, y: int, color: tuple) -> None:
"""
set a specific NeoPixel to RGB color by x,y coordinate on matrix
:param x: value for x position
:param y: value for y position
:param color: tuple of RGB values
:return: None
"""
index = int(y) * int(self.cols) + int(x)
self.set_index(index, tuple(color))
def hline(self, x: int, y: int, length: int, color: tuple) -> None:
"""
set NeoPixel to RGB value as horizontal line by x,y start coordinates and length
:param x: value for x start position
:param y: value for y start position
:param length: value for length
:param color: tuple of RGB values
:return: None
"""
for item in range(int(length)):
x_cal = int(x) + item
if x_cal <= (self.cols - 1):
self.set_coordinate(x_cal, int(y), tuple(color))
def vline(self, x: int, y: int, length: int, color: tuple) -> None:
"""
set NeoPixel to RGB value as vertical line by x,y start coordinates and length
:param x: value for x start position
:param y: value for y start position
:param length: value for length
:param color: tuple of RGB values
:return: None
"""
for item in range(int(length)):
y_cal = int(y) + item
if y_cal <= (self.rows - 1):
self.set_coordinate(int(x), y_cal, tuple(color))
def draw_rectangle(self, x: int, y: int, length: int, height: int, color: tuple) -> None:
"""
set NeoPixel to RGB value as rectangle by x,y start coordinates, length and height
:param x: value for x start position
:param y: value for y start position
:param length: value for length
:param height: value for height
:param color: tuple of RGB values
:return: None
"""
self.hline(int(x), int(y), int(length), tuple(color))
self.hline(int(x), (int(y) + int(height) - 1), int(length), tuple(color))
self.vline(int(x), int(y), int(height), tuple(color))
self.vline((int(x) + int(length) - 1), int(y), int(height), tuple(color))
def set_digit(self, val: int, x: int, y: int, color: tuple) -> None:
"""
create a digit 0-9 on NeoPixels by value incl. x,y coordinates and color
:param val: number from 0 till 9
:param x: value for x coordinate on matrix
:param y: value for y coordinate on matrix
:param color: tuple of RGB values
:return: None
"""
offset = int(val) * 15
for item in range(offset, offset + 15):
if self.NUMS[item] == 1:
xt = item % 3
yt = (item - offset) // 3
self.set_coordinate(xt + int(x), yt + int(y), tuple(color))