@@ -131,15 +131,28 @@ def page(self, p):
131
131
132
132
133
133
134
- def __getitem__ (self , key ):
135
- print (key )
134
+ def __getitem__ (self , led ):
135
+ if not 0 <= led <= 350 :
136
+ raise ValueError ("LED must be 0 ~ 350" )
137
+ if led < 180 :
138
+ self .page = 0
139
+ self ._buf [0 ] = led
140
+ else :
141
+ self .page = 1
142
+ self ._buf [0 ] = led - 180
143
+
144
+ with self .i2c_device as i2c :
145
+ i2c .write_then_readinto (self ._buf , self ._buf ,
146
+ out_start = 0 , out_end = 1 ,
147
+ in_start = 1 , in_end = 2 )
148
+ return self ._buf [1 ]
136
149
137
150
def __setitem__ (self , led , pwm ):
138
151
if not 0 <= led <= 350 :
139
152
raise ValueError ("LED must be 0 ~ 350" )
140
153
if not 0 <= pwm <= 255 :
141
154
raise ValueError ("PWM must be 0 ~ 255" )
142
- print (led , pwm )
155
+ # print(led, pwm)
143
156
144
157
if led < 180 :
145
158
self .page = 0
@@ -150,16 +163,13 @@ def __setitem__(self, led, pwm):
150
163
self ._buf [1 ] = pwm
151
164
with self .i2c_device as i2c :
152
165
i2c .write (self ._buf )
153
-
154
-
155
-
156
-
157
166
158
167
# This function must be replaced for each board
159
168
@staticmethod
160
- def pixel_addr (x , y ):
169
+ def pixel_addrs (x , y ):
161
170
"""Calulate the offset into the device array for x,y pixel"""
162
- return x + y * 16
171
+ raise NotImplementedError ("Supported in subclasses only" )
172
+
163
173
164
174
# pylint: disable-msg=too-many-arguments
165
175
def pixel (self , x , y , color = None ):
@@ -174,24 +184,20 @@ def pixel(self, x, y, color=None):
174
184
return None
175
185
if not 0 <= y <= self .height :
176
186
return None
177
- pixel = self .pixel_addr (x , y )
178
- if color is None and blink is None :
179
- return self ._register (self ._frame , pixel )
180
- if frame is None :
181
- frame = self ._frame
182
- if color is not None :
183
- if not 0 <= color <= 255 :
184
- raise ValueError ("Color out of range" )
185
- self ._register (frame , _COLOR_OFFSET + pixel , color )
186
- if blink is not None :
187
- addr , bit = divmod (pixel , 8 )
188
- bits = self ._register (frame , _BLINK_OFFSET + addr )
189
- if blink :
190
- bits |= 1 << bit
191
- else :
192
- bits &= ~ (1 << bit )
193
- self ._register (frame , _BLINK_OFFSET + addr , bits )
194
- return None
187
+ addrs = self .pixel_addrs (x , y )
188
+ print (addrs )
189
+ if color is not None : # set the color
190
+ self [addrs [0 ]] = (color >> 16 ) & 0xFF
191
+ self [addrs [1 ]] = (color >> 8 ) & 0xFF
192
+ self [addrs [2 ]] = color & 0xFF
193
+ return None
194
+ # we want to fetch the color
195
+ color = self [addrs [0 ]]
196
+ color <<= 8
197
+ color |= self [addrs [1 ]]
198
+ color <<= 8
199
+ color |= self [addrs [2 ]]
200
+ return color
195
201
196
202
# pylint: enable-msg=too-many-arguments
197
203
@@ -203,8 +209,8 @@ def image(self, img, blink=None, frame=None):
203
209
:param blink: True to blink
204
210
:param frame: the frame to set the image
205
211
"""
206
- if img .mode != "L " :
207
- raise ValueError ("Image must be in mode L ." )
212
+ if img .mode != "RGB " :
213
+ raise ValueError ("Image must be in mode RGB ." )
208
214
imwidth , imheight = img .size
209
215
if imwidth != self .width or imheight != self .height :
210
216
raise ValueError (
@@ -218,4 +224,4 @@ def image(self, img, blink=None, frame=None):
218
224
# Iterate through the pixels
219
225
for x in range (self .width ): # yes this double loop is slow,
220
226
for y in range (self .height ): # but these displays are small!
221
- self .pixel (x , y , pixels [(x , y )], blink = blink , frame = frame )
227
+ self .pixel (x , y , pixels [(x , y )])
0 commit comments