-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_helper.py
151 lines (120 loc) · 6.13 KB
/
matrix_helper.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"""
Class MatrixHelper as used in PortaBrick Arcade project
Copyright <2023> <LC-jrx>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the “Software”), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
from pybricks.parameters import Color
from pybricks.pupdevices import ColorLightMatrix
from pybricks.tools import wait
from detect_devices import DetectDevices
class ResolutionException(Exception):
def __init__(self, resolution):
self.resolution = resolution
class MatrixHelper:
"""
The MatrixHelper class controls the overall matrix consisting of 3x3 individual matrices.
The number of matrices is freely selectable and is determined by the desired resolution.
"""
__res_x = None
__res_y = None
def __init__(self, game_res_x, game_res_y):
self.__matrix_id = None
self.__new_y = None
self.__new_x = None
self.__res_x = game_res_x
self.__res_y = game_res_y
self.__pix_color = None
detect_devices = DetectDevices()
self.__matrix_available = detect_devices.matrix_available
self.__matrix_ports = detect_devices.matrix_ports
self.__matrix_count = int(self.__calc_matrix_count())
# Check if available matrix modules fit to given resolution
try:
if self.__matrix_count > self.__matrix_available:
print("\n"
"Given resolution does not match available matrix resolution.\n"
"Please adapt resolution or add",
self.__matrix_count - self.__matrix_available,
"more Spike ColorLightMatrix modules.\n")
raise Exception
except Exception:
raise
# Catch x or y equals 0, and x or y not multiples of 3
try:
if self.__res_x % 3 != 0 or self.__res_x == 0:
raise ResolutionException(self.__res_x)
elif self.__res_y % 3 != 0 or self.__res_y == 0:
raise ResolutionException(self.__res_y)
except ResolutionException as e:
print('Resolution of ' + str(e) + ' pixel is not suitable for Spike ColorLightMatrix modules. '
'Must not be 0 and must be a multiple of 3.')
raise
self.__pixels = [] # Empty list, will hold the lists of each module
# self.pix_black = [[Color.NONE, Color.NONE, Color.NONE],
# [Color.NONE, Color.NONE, Color.NONE],
# [Color.NONE, Color.NONE, Color.NONE]] # List with color info to turn module dark
# Pre-set all pixels black (aka off), first initialize nested list
for i in range(self.__matrix_count):
# here each module get's its still empty pixel list linewise
self.__pixels.append([[], [], []])
# now fill the emty lists with default entries for black
# self.pixels.append(self.pix_black.copy())
# self.pixels.append(list(self.pix_black))
self.matrix_off()
def __recalc_coordinates(self, abs_x, abs_y):
"""Converts a coordinate of the given resolution to the coordinates of a
single matrix of the display composed of matrix modules."""
self.__new_x = abs_x % 3
self.__new_y = abs_y % 3
self.__matrix_id = (abs_y // 3 * self.__res_x / 3) + abs_x // 3
return self.__new_x, self.__new_y, int(self.__matrix_id)
def __calc_matrix_count(self):
"""Calculates the necessary number of individual modules from the given total resolution."""
return (self.__res_x / 3) * (self.__res_y / 3)
def __matrix2pixel(self, index):
"""Converts the given 'array' to a list"""
dot = []
for y in range(3):
for x in range(3):
dot.append(self.__pixels[index][y][x])
return dot
def pixel_on(self, input_x, input_y, input_color):
temp = self.__recalc_coordinates(input_x, input_y)
self.__pixels[temp[2]][temp[1]][temp[0]] = input_color
ColorLightMatrix(self.__matrix_ports[temp[2]]).on(self.__matrix2pixel(temp[2]))
def pixel_off(self, input_x, input_y):
temp = self.__recalc_coordinates(input_x, input_y)
self.__pixels[temp[2]][temp[1]][temp[0]] = Color.NONE
ColorLightMatrix(self.__matrix_ports[temp[2]]).on(self.__matrix2pixel(temp[2]))
def draw_pixel_graphic(self, picture, color):
for i in range(len(picture)):
self.pixel_on(picture[i][0], picture[i][1], color)
def matrix_off(self):
for i in range(self.__matrix_count):
pix_black = [[Color.NONE, Color.NONE, Color.NONE],
[Color.NONE, Color.NONE, Color.NONE],
[Color.NONE, Color.NONE, Color.NONE]]
self.__pixels[i] = pix_black.copy()
ColorLightMatrix(self.__matrix_ports[i]).off()
if __name__ == "__main__":
x_res = 6 # set resolution x
y_res = 6 # set resolution y
matrix = MatrixHelper(x_res, y_res)
for y in range(y_res):
for x in range(x_res):
matrix.pixel_on(x, y, Color.RED)
wait(150)
for y in range(y_res):
for x in range(x_res):
matrix.pixel_off(x, y)
wait(150)
matrix.matrix_off()