1
1
from .devices import Device
2
2
from .exc import MatrixError
3
+ from typing import List , Any , Tuple
3
4
4
5
5
6
class Matrix (Device ):
@@ -14,7 +15,7 @@ def __init__(self, port):
14
15
self .mode (2 )
15
16
self ._matrix = [[(0 , 0 ) for x in range (3 )] for y in range (3 )]
16
17
17
- def set_pixels (self , matrix , display = True ):
18
+ def set_pixels (self , matrix : List [ List [ Tuple [ Any , int ]]] , display = True ):
18
19
"""Write pixel data to LED matrix
19
20
20
21
:param pixels: 3x3 list of tuples, with colour (0–10) and brightness (0–10) (see example for more detail)
@@ -26,7 +27,7 @@ def set_pixels(self, matrix, display=True):
26
27
if len (matrix [x ]) != 3 :
27
28
raise MatrixError ("Incorrect matrix width" )
28
29
for y in range (3 ):
29
- matrix [x ][y ] = Matrix .normalize_pixel (matrix [x ][y ])
30
+ matrix [x ][y ] = Matrix .normalize_pixel (matrix [x ][y ]) # pylint: disable=too-many-function-args
30
31
self ._matrix = matrix
31
32
if display :
32
33
self ._output ()
@@ -40,6 +41,7 @@ def _output(self):
40
41
self ._write1 (out )
41
42
self .deselect ()
42
43
44
+ @classmethod
43
45
def strtocolor (colorstr ):
44
46
"""Return the BuldHAT's integer representation of a color string
45
47
@@ -71,18 +73,19 @@ def strtocolor(colorstr):
71
73
return 0
72
74
raise MatrixError ("Invalid color specified" )
73
75
74
- def normalize_pixel (pixel ):
76
+ @classmethod
77
+ def normalize_pixel (pixel : Tuple [Any , int ]):
75
78
"""Validate a pixel tuple (color, brightness) and convert string colors to integers
76
79
77
80
:param pixel: tuple of colour (0–10) or string (ie:"red") and brightness (0–10)
78
81
:return: (color, brightness) integers
79
82
:rtype: tuple
80
83
"""
81
84
if isinstance (pixel , tuple ):
82
- c , brightness = pixel
85
+ c , brightness = pixel # pylint: disable=unpacking-non-sequence
83
86
if isinstance (c , str ):
84
- c = Matrix .strtocolor (c )
85
- if not ( isinstance (brightness , int ) and isinstance (c , int )):
87
+ c = Matrix .strtocolor (c ) # pylint: disable=too-many-function-args
88
+ if not (isinstance (brightness , int ) and isinstance (c , int )):
86
89
raise MatrixError ("Invalid pixel specified" )
87
90
if not (brightness >= 0 and brightness <= 10 ):
88
91
raise MatrixError ("Invalid brightness value specified" )
@@ -92,28 +95,30 @@ def normalize_pixel(pixel):
92
95
else :
93
96
raise MatrixError ("Invalid pixel specified" )
94
97
95
- def validate_coordinate (coord ):
98
+ @classmethod
99
+ def validate_coordinate (coord : Tuple [int , int ]):
96
100
""""Validate an x,y coordinate for the 3x3 Matrix
97
101
98
102
:param coord: tuple of 0-2 for the X coordinate and 0-2 for the Y coordinate
99
103
"""
104
+ # pylint: disable=unsubscriptable-object
100
105
if isinstance (coord , tuple ):
101
106
if not (isinstance (coord [0 ], int ) and isinstance (coord [1 ], int )):
102
107
raise MatrixError ("Invalid coord specified" )
103
108
elif coord [0 ] > 2 or coord [0 ] < 0 or coord [1 ] > 2 or coord [1 ] < 0 :
104
- raise MatrixError ("Invalid coord specified" )
109
+ raise MatrixError ("Invalid coord specified" )
105
110
else :
106
111
raise MatrixError ("Invalid coord specified" )
107
112
108
- def clear (self , pixel = None ):
113
+ def clear (self , pixel : Tuple [ Any , int ] = None ):
109
114
"""Clear matrix or set all as the same pixel
110
115
111
116
:param pixel: tuple of colour (0–10) or string and brightness (0–10)
112
117
"""
113
118
if pixel is None :
114
119
self ._matrix = [[(0 , 0 ) for x in range (3 )] for y in range (3 )]
115
120
else :
116
- color = Matrix .normalize_pixel (pixel )
121
+ color = Matrix .normalize_pixel (pixel ) # pylint: disable=too-many-function-args
117
122
self ._matrix = [[color for x in range (3 )] for y in range (3 )]
118
123
self ._output ()
119
124
@@ -172,15 +177,15 @@ def set_transition(self, transition):
172
177
self .mode (2 ) # The rest of the Matrix code seems to expect this to be always set
173
178
self .deselect ()
174
179
175
- def set_pixel (self , coord , pixel , display = True ):
180
+ def set_pixel (self , coord : Tuple [ int , int ], pixel : Tuple [ Any , int ] , display = True ):
176
181
"""Write pixel to coordinate
177
182
178
183
:param coord: (0,0) to (2,2)
179
184
:param pixel: tuple of colour (0–10) or string and brightness (0–10)
180
185
:param display: Whether to update matrix or not
181
186
"""
182
- color = Matrix .normalize_pixel (pixel )
183
- Matrix .validate_coordinate (coord )
187
+ color = Matrix .normalize_pixel (pixel ) # pylint: disable=too-many-function-args
188
+ Matrix .validate_coordinate (coord ) # pylint: disable=too-many-function-args
184
189
x , y = coord
185
190
self ._matrix [x ][y ] = color
186
191
if display :
0 commit comments