@@ -79,7 +79,8 @@ def __init__(self, width, height, spi, cs_pin, dc_pin, sramcs_pin, rst_pin, busy
79
79
self ._buffer1_size = self ._buffer2_size = 0
80
80
self ._buffer1 = self ._buffer2 = None
81
81
self ._framebuf1 = self ._framebuf2 = None
82
- self .black_invert = self .red_invert = True
82
+ self ._colorframebuf = self ._blackframebuf = None
83
+ self ._black_inverted = self ._color_inverted = True
83
84
self .hardware_reset ()
84
85
85
86
def display (self ): # pylint: disable=too-many-branches
@@ -214,56 +215,73 @@ def set_ram_address(self, x, y):
214
215
"""Set the RAM address location, must be implemented in subclass"""
215
216
raise NotImplementedError ()
216
217
218
+ def set_black_buffer (self , index , inverted ):
219
+ """Set the index for the black buffer data (0 or 1) and whether its inverted"""
220
+ if index == 0 :
221
+ self ._blackframebuf = self ._framebuf1
222
+ elif index == 1 :
223
+ self ._blackframebuf = self ._framebuf2
224
+ else :
225
+ raise RuntimeError ("Buffer index must be 0 or 1" )
226
+ self ._black_inverted = inverted
227
+
228
+ def set_color_buffer (self , index , inverted ):
229
+ """Set the index for the color buffer data (0 or 1) and whether its inverted"""
230
+ if index == 0 :
231
+ self ._colorframebuf = self ._framebuf1
232
+ elif index == 1 :
233
+ self ._colorframebuf = self ._framebuf2
234
+ else :
235
+ raise RuntimeError ("Buffer index must be 0 or 1" )
236
+ self ._color_inverted = inverted
237
+
238
+ def _color_dup (self , func , args , color ):
239
+ black = getattr (self ._blackframebuf , func )
240
+ red = getattr (self ._colorframebuf , func )
241
+ if self ._blackframebuf is self ._colorframebuf : # monochrome
242
+ black (* args , color = (color != Adafruit_EPD .WHITE ) != self ._black_inverted )
243
+ else :
244
+ black (* args , color = (color == Adafruit_EPD .BLACK ) != self ._black_inverted )
245
+ red (* args , color = (color == Adafruit_EPD .RED ) != self ._color_inverted )
246
+
217
247
def pixel (self , x , y , color ):
218
248
"""draw a single pixel in the display buffer"""
219
- self ._framebuf1 .pixel (x , y , (color == Adafruit_EPD .BLACK ) != self .black_invert )
220
- self ._framebuf2 .pixel (x , y , (color == Adafruit_EPD .RED ) != self .red_invert )
249
+ self ._color_dup ('pixel' , (x , y ), color )
221
250
222
251
def fill (self , color ):
223
252
"""fill the screen with the passed color"""
224
- red_fill = (color == Adafruit_EPD .RED ) != self .red_invert
225
- black_fill = (color == Adafruit_EPD .BLACK ) != self .black_invert
226
- if red_fill :
227
- red_fill = 0xFF
228
- if black_fill :
229
- black_fill = 0xFF
253
+ red_fill = ((color == Adafruit_EPD .RED ) != self ._color_inverted ) * 0xFF
254
+ black_fill = ((color == Adafruit_EPD .BLACK ) != self ._black_inverted ) * 0xFF
230
255
231
256
if self .sram :
232
257
self .sram .erase (0x00 , self ._buffer1_size , black_fill )
233
258
self .sram .erase (self ._buffer1_size , self ._buffer2_size , red_fill )
234
259
else :
235
- self ._framebuf1 .fill (black_fill )
236
- self ._framebuf2 .fill (red_fill )
260
+ self ._blackframebuf .fill (black_fill )
261
+ self ._colorframebuf .fill (red_fill )
237
262
238
263
def rect (self , x , y , width , height , color ): # pylint: disable=too-many-arguments
239
264
"""draw a rectangle"""
240
- self ._framebuf1 .rect (x , y , width , height ,
241
- (color == Adafruit_EPD .BLACK ) != self .black_invert )
242
- self ._framebuf2 .rect (x , y , width , height ,
243
- (color == Adafruit_EPD .RED ) != self .red_invert )
265
+ self ._color_dup ('rect' , (x , y , width , height ), color )
244
266
245
267
def fill_rect (self , x , y , width , height , color ): # pylint: disable=too-many-arguments
246
268
"""fill a rectangle with the passed color"""
247
- self ._framebuf1 .fill_rect (x , y , width , height ,
248
- (color == Adafruit_EPD .BLACK ) != self .black_invert )
249
- self ._framebuf2 .fill_rect (x , y , width , height ,
250
- (color == Adafruit_EPD .RED ) != self .red_invert )
269
+ self ._color_dup ('fill_rect' , (x , y , width , height ), color )
251
270
252
271
def line (self , x_0 , y_0 , x_1 , y_1 , color ): # pylint: disable=too-many-arguments
253
272
"""Draw a line from (x_0, y_0) to (x_1, y_1) in passed color"""
254
- self ._framebuf1 .line (x_0 , y_0 , x_1 , y_1 ,
255
- (color == Adafruit_EPD .BLACK ) != self .black_invert )
256
- self ._framebuf2 .line (x_0 , y_0 , x_1 , y_1 ,
257
- (color == Adafruit_EPD .RED ) != self .red_invert )
273
+ self ._color_dup ('line' , (x_0 , y_0 , x_1 , y_1 ), color )
258
274
259
275
def text (self , string , x , y , color , * , font_name = "font5x8.bin" ):
260
276
"""Write text string at location (x, y) in given color, using font file"""
261
- self ._framebuf1 .text (string , x , y ,
262
- (color == Adafruit_EPD .BLACK ) != self .black_invert ,
263
- font_name = font_name )
264
- self ._framebuf2 .text (string , x , y ,
265
- (color == Adafruit_EPD .RED ) != self .red_invert ,
266
- font_name = font_name )
277
+ if self ._blackframebuf is self ._colorframebuf : # monochrome
278
+ self ._blackframebuf .text (string , x , y , font_name = font_name ,
279
+ color = (color != Adafruit_EPD .WHITE ) != self ._black_inverted )
280
+ else :
281
+ self ._blackframebuf .text (string , x , y , font_name = font_name ,
282
+ color = (color == Adafruit_EPD .BLACK ) != self ._black_inverted )
283
+ self ._colorframebuf .text (string , x , y , font_name = font_name ,
284
+ color = (color == Adafruit_EPD .RED ) != self ._color_inverted )
267
285
268
286
@property
269
287
def width (self ):
0 commit comments