@@ -14,20 +14,22 @@ def __init__(self, port):
14
14
self .mode (2 )
15
15
self ._matrix = [[(0 , 0 ) for x in range (3 )] for y in range (3 )]
16
16
17
- def set_pixels (self , matrix ):
17
+ def set_pixels (self , matrix , display = True ):
18
18
"""Write pixel data to LED matrix
19
19
20
20
:param pixels: 3x3 list of tuples, with colour (0–10) and brightness (0–10) (see example for more detail)
21
+ :param display: Whether to update matrix or not
21
22
"""
23
+ if len (matrix ) != 3 :
24
+ raise MatrixError ("Incorrect matrix height" )
22
25
for x in range (3 ):
26
+ if len (matrix [x ]) != 3 :
27
+ raise MatrixError ("Incorrect matrix width" )
23
28
for y in range (3 ):
24
- color , brightness = matrix [x ][y ]
25
- if not (brightness >= 0 and brightness <= 10 ):
26
- raise MatrixError ("Invalid brightness specified" )
27
- if not (color >= 0 and color <= 10 ):
28
- raise MatrixError ("Invalid pixel specified" )
29
+ matrix [x ][y ] = Matrix .normalize_pixel (matrix [x ][y ]) # pylint: disable=too-many-function-args
29
30
self ._matrix = matrix
30
- self ._output ()
31
+ if display :
32
+ self ._output ()
31
33
32
34
def _output (self ):
33
35
out = [0xc2 ]
@@ -38,7 +40,14 @@ def _output(self):
38
40
self ._write1 (out )
39
41
self .deselect ()
40
42
41
- def strtocolor (self , colorstr ):
43
+ @staticmethod
44
+ def strtocolor (colorstr ):
45
+ """Return the BuldHAT's integer representation of a color string
46
+
47
+ :param colorstr: str of a valid color
48
+ :return: (0-10) representing the color
49
+ :rtype: int
50
+ """
42
51
if colorstr == "pink" :
43
52
return 1
44
53
elif colorstr == "lilac" :
@@ -63,6 +72,43 @@ def strtocolor(self, colorstr):
63
72
return 0
64
73
raise MatrixError ("Invalid color specified" )
65
74
75
+ @staticmethod
76
+ def normalize_pixel (pixel ):
77
+ """Validate a pixel tuple (color, brightness) and convert string colors to integers
78
+
79
+ :param pixel: tuple of colour (0–10) or string (ie:"red") and brightness (0–10)
80
+ :return: (color, brightness) integers
81
+ :rtype: tuple
82
+ """
83
+ if isinstance (pixel , tuple ):
84
+ c , brightness = pixel # pylint: disable=unpacking-non-sequence
85
+ if isinstance (c , str ):
86
+ c = Matrix .strtocolor (c ) # pylint: disable=too-many-function-args
87
+ if not (isinstance (brightness , int ) and isinstance (c , int )):
88
+ raise MatrixError ("Invalid pixel specified" )
89
+ if not (brightness >= 0 and brightness <= 10 ):
90
+ raise MatrixError ("Invalid brightness value specified" )
91
+ if not (c >= 0 and c <= 10 ):
92
+ raise MatrixError ("Invalid pixel color specified" )
93
+ return (c , brightness )
94
+ else :
95
+ raise MatrixError ("Invalid pixel specified" )
96
+
97
+ @staticmethod
98
+ def validate_coordinate (coord ):
99
+ """"Validate an x,y coordinate for the 3x3 Matrix
100
+
101
+ :param coord: tuple of 0-2 for the X coordinate and 0-2 for the Y coordinate
102
+ """
103
+ # pylint: disable=unsubscriptable-object
104
+ if isinstance (coord , tuple ):
105
+ if not (isinstance (coord [0 ], int ) and isinstance (coord [1 ], int )):
106
+ raise MatrixError ("Invalid coord specified" )
107
+ elif coord [0 ] > 2 or coord [0 ] < 0 or coord [1 ] > 2 or coord [1 ] < 0 :
108
+ raise MatrixError ("Invalid coord specified" )
109
+ else :
110
+ raise MatrixError ("Invalid coord specified" )
111
+
66
112
def clear (self , pixel = None ):
67
113
"""Clear matrix or set all as the same pixel
68
114
@@ -71,21 +117,15 @@ def clear(self, pixel=None):
71
117
if pixel is None :
72
118
self ._matrix = [[(0 , 0 ) for x in range (3 )] for y in range (3 )]
73
119
else :
74
- color = ()
75
- if isinstance (pixel , tuple ):
76
- c , brightness = pixel
77
- if isinstance (c , str ):
78
- c = self .strtocolor (c )
79
- if not (brightness >= 0 and brightness <= 10 ):
80
- raise MatrixError ("Invalid brightness specified" )
81
- if not (c >= 0 and c <= 10 ):
82
- raise MatrixError ("Invalid pixel specified" )
83
- color = (c , brightness )
84
- else :
85
- raise MatrixError ("Invalid pixel specified" )
120
+ color = Matrix .normalize_pixel (pixel ) # pylint: disable=too-many-function-args
86
121
self ._matrix = [[color for x in range (3 )] for y in range (3 )]
87
122
self ._output ()
88
123
124
+ def off (self ):
125
+ # Never send the "off" command to the port a Matrix is connected to
126
+ # Instead, just turn all the pixels off
127
+ self .clear ()
128
+
89
129
def level (self , level ):
90
130
"""Use the matrix as a "level" meter from 0-9
91
131
(The level meter is expressed in green which seems to be unchangeable)
@@ -143,22 +183,8 @@ def set_pixel(self, coord, pixel, display=True):
143
183
:param pixel: tuple of colour (0–10) or string and brightness (0–10)
144
184
:param display: Whether to update matrix or not
145
185
"""
146
- if isinstance (pixel , tuple ):
147
- c , brightness = pixel
148
- if isinstance (c , str ):
149
- c = self .strtocolor (c )
150
- if not (brightness >= 0 and brightness <= 10 ):
151
- raise MatrixError ("Invalid brightness specified" )
152
- if not (c >= 0 and c <= 10 ):
153
- raise MatrixError ("Invalid pixel specified" )
154
- color = (c , brightness )
155
- else :
156
- raise MatrixError ("Invalid pixel specified" )
157
- if isinstance (coord , tuple ):
158
- if not (isinstance (coord [0 ], int ) and isinstance (coord [1 ], int )):
159
- raise MatrixError ("Invalid coord specified" )
160
- else :
161
- raise MatrixError ("Invalid coord specified" )
186
+ color = Matrix .normalize_pixel (pixel ) # pylint: disable=too-many-function-args
187
+ Matrix .validate_coordinate (coord ) # pylint: disable=too-many-function-args
162
188
x , y = coord
163
189
self ._matrix [x ][y ] = color
164
190
if display :
0 commit comments