@@ -5,13 +5,21 @@ def strip(_rpi_ws281x):
5
5
from rpi_ws281x import PixelStrip
6
6
strip = PixelStrip (10 , 20 )
7
7
strip .begin ()
8
+ strip .off ()
8
9
yield strip
9
10
10
11
def test_setup (_rpi_ws281x ):
11
12
from rpi_ws281x import PixelStrip
12
13
strip = PixelStrip (10 , 20 )
13
14
strip .begin ()
14
15
16
+ def test_setup_compat (_rpi_ws281x ):
17
+ from rpi_ws281x import PixelStrip
18
+ strip = PixelStrip (10 , 20 , strip_type = [1.0 ]* 256 )
19
+ strip .begin ()
20
+ _rpi_ws281x .ws2811_channel_t_strip_type_set .assert_called_with (strip ._channel ,
21
+ _rpi_ws281x .WS2811_STRIP_GRB )
22
+ _rpi_ws281x .ws2811_channel_t_gamma_set .assert_called_with (strip ._channel ,[1.0 ]* 256 )
15
23
16
24
def test_setup_init_fail (_rpi_ws281x ):
17
25
from rpi_ws281x import PixelStrip
@@ -20,13 +28,50 @@ def test_setup_init_fail(_rpi_ws281x):
20
28
with pytest .raises (RuntimeError ):
21
29
strip .begin ()
22
30
31
+ def test_cleanup (_rpi_ws281x , strip ):
32
+ assert _rpi_ws281x .ws2811_fini .call_count == 0
33
+ assert _rpi_ws281x .delete_ws2811_t .call_count == 0
34
+ strip ._cleanup ()
35
+ assert _rpi_ws281x .ws2811_fini .call_count == 1
36
+ assert _rpi_ws281x .delete_ws2811_t .call_count == 1
37
+ assert strip ._leds is None
38
+ assert strip ._channel is None
39
+
40
+ def test_show (_rpi_ws281x , strip ):
41
+ assert _rpi_ws281x .ws2811_render .call_count == 1
42
+ strip .show ()
43
+ assert _rpi_ws281x .ws2811_render .call_count == 2
44
+ sub = strip .createPixelSubStrip (5 , last = 10 )
45
+ sub .show ()
46
+ assert _rpi_ws281x .ws2811_render .call_count == 3
47
+
48
+ _rpi_ws281x .ws2811_render .return_value = 1
49
+ with pytest .raises (RuntimeError ):
50
+ sub .show ()
51
+
52
+ def test_brightness (_rpi_ws281x , strip ):
53
+ assert _rpi_ws281x .ws2811_channel_t_brightness_get .call_count == 0
54
+ strip .getBrightness ()
55
+ assert _rpi_ws281x .ws2811_channel_t_brightness_get .call_count == 1
56
+ calls = _rpi_ws281x .ws2811_channel_t_brightness_set .call_count
57
+ strip .setBrightness (20 )
58
+ assert _rpi_ws281x .ws2811_channel_t_brightness_set .call_count == calls + 1
59
+
60
+ def test_gamma (_rpi_ws281x , strip ):
61
+ calls = _rpi_ws281x .ws2811_channel_t_gamma_set .call_count
62
+ strip .setGamma (1.0 )
63
+ assert _rpi_ws281x .ws2811_channel_t_gamma_set .call_count == calls
64
+ strip .setGamma ([1.0 , 1.0 ])
65
+ assert _rpi_ws281x .ws2811_channel_t_gamma_set .call_count == calls
66
+ strip .setGamma ([1.0 ] * 256 )
67
+ assert _rpi_ws281x .ws2811_channel_t_gamma_set .call_count == calls + 1
68
+
23
69
24
70
def test_num_pixels (strip ):
25
71
assert len (strip [:]) == 10
26
72
assert len (strip ) == 10
27
73
assert strip .numPixels () == 10
28
74
29
-
30
75
def test_set_pixel (strip ):
31
76
from rpi_ws281x import RGBW
32
77
strip [0 ] = RGBW (255 , 0 , 0 )
@@ -36,14 +81,132 @@ def test_set_pixel(strip):
36
81
assert strip .getPixelColorRGBW (0 ) == RGBW (255 , 0 , 0 )
37
82
assert strip .getPixelColorRGBW (0 ).r == 255
38
83
39
-
40
84
def test_set_multiple (strip ):
41
85
from rpi_ws281x import RGBW
42
86
strip [:] = RGBW (255 , 0 , 0 )
43
87
assert strip [:] == [RGBW (255 , 0 , 0 )] * 10
44
88
89
+ def test_set_pixel_slice (strip ):
90
+ from rpi_ws281x import RGBW
91
+ colours = [RGBW (i * 10 , 0 , 0 ) for i in range (0 , 10 )]
92
+ strip [0 :10 ] = colours
93
+ for i in range (0 , 10 ):
94
+ assert strip [i ] == RGBW (i * 10 , 0 , 0 )
45
95
46
96
def test_set_odd (strip ):
47
97
from rpi_ws281x import RGBW
48
98
strip [::2 ] = RGBW (255 , 0 , 0 )
49
99
assert strip [:] == [RGBW (255 , 0 , 0 ), RGBW (0 , 0 , 0 )] * 5
100
+
101
+
102
+ def test_create_substrip (strip ):
103
+ from rpi_ws281x import InvalidStrip
104
+ with pytest .raises (InvalidStrip ):
105
+ sub = strip .createPixelSubStrip (100 )
106
+ with pytest .raises (InvalidStrip ):
107
+ sub = strip .createPixelSubStrip (5 )
108
+ with pytest .raises (InvalidStrip ):
109
+ sub = strip .createPixelSubStrip (5 , 11 )
110
+ with pytest .raises (InvalidStrip ):
111
+ sub = strip .createPixelSubStrip (5 , - 2 )
112
+ with pytest .raises (InvalidStrip ):
113
+ sub = strip .createPixelSubStrip (5 , num = 6 )
114
+ with pytest .raises (InvalidStrip ):
115
+ sub = strip .createPixelSubStrip (- 1 , num = 5 )
116
+ with pytest .raises (InvalidStrip ):
117
+ sub = strip .createPixelSubStrip (5 , num = - 2 )
118
+ sub = strip .createPixelSubStrip (5 , last = 10 )
119
+ assert len (sub [:]) == 5
120
+ assert len (sub ) == 5
121
+ assert sub .numPixels () == 5
122
+ sub = strip .createPixelSubStrip (5 , num = 5 )
123
+ assert len (sub [:]) == 5
124
+ assert len (sub ) == 5
125
+ assert sub .numPixels () == 5
126
+
127
+ def test_adjust_pos (strip ):
128
+ sub = strip .createPixelSubStrip (5 , 10 )
129
+ assert sub ._adjust_pos (3 ) == 8
130
+ assert sub ._adjust_pos (- 2 ) == 8
131
+ assert sub ._adjust_pos (slice (None , None )) == slice (5 , 10 ) # [:]
132
+ assert sub ._adjust_pos (slice (0 , None )) == slice (5 , 10 ) # [0:]
133
+ assert sub ._adjust_pos (slice (None , 5 )) == slice (5 , 10 ) # [:5]
134
+
135
+ assert sub ._adjust_pos (slice (- 1 , None )) == slice (9 , 10 ) # [-1:] (start, step default to None)
136
+ assert sub ._adjust_pos (slice (- 2 , None )) == slice (8 , 10 ) # [-2:]
137
+ assert sub ._adjust_pos (slice (None , - 1 )) == slice (5 , 9 ) # [:-1]
138
+ assert sub ._adjust_pos (slice (None , - 2 )) == slice (5 , 8 ) # [:-2]
139
+ assert sub ._adjust_pos (slice (- 1 , 0 )) == slice (9 , 5 ) # [-1:0]
140
+
141
+ assert sub ._adjust_pos (slice (2 , 4 )) == slice (7 , 9 ) # [2:4]
142
+ assert sub ._adjust_pos (slice (None , None , - 2 )) == slice (5 , 10 , - 2 ) # [::-2]
143
+
144
+ def test_substrip_set (strip ):
145
+ from rpi_ws281x import RGBW
146
+ sub = strip .createPixelSubStrip (5 , 10 )
147
+
148
+ sub [0 ] = RGBW (1 , 1 , 1 )
149
+ assert strip [5 ] == RGBW (1 , 1 , 1 )
150
+
151
+ sub .setPixelColor (2 , RGBW (2 , 2 , 2 ))
152
+ assert strip [7 ] == RGBW (2 , 2 , 2 )
153
+
154
+ sub .setPixelColorRGB (3 , 3 , 4 , 5 )
155
+ assert strip [8 ] == RGBW (3 , 4 , 5 )
156
+
157
+ strip .off ()
158
+ sub [1 :4 ] = RGBW (1 , 2 , 3 )
159
+ assert strip [6 :9 ] == sub [1 :4 ]
160
+ assert strip [6 :9 ] == [RGBW (1 , 2 , 3 )] * 3
161
+ assert strip .getPixels () == [RGBW (0 , 0 , 0 )] * 6 + [RGBW (1 , 2 , 3 )] * 3 + [RGBW (0 , 0 , 0 )]
162
+
163
+ strip .off ()
164
+ colours = [RGBW (i , i , i ) for i in range (1 , 4 )]
165
+ sub [1 :4 ] = colours
166
+ print (f"strip is { strip [:]} " )
167
+
168
+ assert strip [6 :9 ] == sub [1 :4 ]
169
+ assert strip [6 :9 ] == colours
170
+
171
+ strip .off ()
172
+ sub = strip .createPixelSubStrip (2 , 10 )
173
+
174
+ sub [- 1 :0 :- 2 ] = [RGBW (i , i , i ) for i in range (1 , 9 )]
175
+ assert strip [:] == ([RGBW (0 , 0 , 0 )]
176
+ + [RGBW (0 , 0 , 0 )]
177
+ + [RGBW (0 , 0 , 0 )]
178
+ + [RGBW (4 , 4 , 4 )]
179
+ + [RGBW (0 , 0 , 0 )]
180
+ + [RGBW (3 , 3 , 3 )]
181
+ + [RGBW (0 , 0 , 0 )]
182
+ + [RGBW (2 , 2 , 2 )]
183
+ + [RGBW (0 , 0 , 0 )]
184
+ + [RGBW (1 , 1 , 1 )])
185
+
186
+ strip .off ()
187
+ sub [- 1 :2 :- 2 ] = [RGBW (i , i , i ) for i in range (1 , 9 )]
188
+ assert strip [:] == ([RGBW (0 , 0 , 0 )]
189
+ + [RGBW (0 , 0 , 0 )]
190
+ + [RGBW (0 , 0 , 0 )]
191
+ + [RGBW (0 , 0 , 0 )]
192
+ + [RGBW (0 , 0 , 0 )]
193
+ + [RGBW (3 , 3 , 3 )]
194
+ + [RGBW (0 , 0 , 0 )]
195
+ + [RGBW (2 , 2 , 2 )]
196
+ + [RGBW (0 , 0 , 0 )]
197
+ + [RGBW (1 , 1 , 1 )])
198
+
199
+ def test_RGBW ():
200
+ from rpi_ws281x import RGBW
201
+ c = RGBW (0x50 )
202
+ assert c == 0x50
203
+ assert c .r == 0x00 and c .g == 0x00 and c .b == 0x50 and c .w == 0x0
204
+ c = RGBW (0x50 , g = 0x60 )
205
+ assert c .r == 0x50 and c .g == 0x60 and c .b == 0x00 and c .w == 0x00
206
+ c = RGBW (0x50 , b = 0x60 )
207
+ assert c .r == 0x50 and c .g == 0x00 and c .b == 0x60 and c .w == 0x00
208
+ c = RGBW (0x50 , 0x60 , 0x70 )
209
+ assert c .r == 0x50 and c .g == 0x60 and c .b == 0x70 and c .w == 0x00
210
+ c = RGBW (0x50 , 0x60 , 0x70 , 0x80 )
211
+ assert c .r == 0x50 and c .g == 0x60 and c .b == 0x70 and c .w == 0x80
212
+ assert c == (0x80 << 24 ) + (0x50 << 16 ) + (0x60 << 8 ) + 0x70
0 commit comments