27
27
"""
28
28
29
29
import time
30
+ from micropython import const
30
31
from digitalio import Direction
31
- import adafruit_framebuf
32
32
from adafruit_epd import mcp_sram
33
33
34
- class Adafruit_EPD :
34
+ class Adafruit_EPD : # pylint: disable=too-many-instance-attributes
35
35
"""Base class for EPD displays
36
36
"""
37
- BLACK = 0
38
- WHITE = 1
39
- INVERSE = 2
40
- RED = 3
41
- DARK = 4
42
- LIGHT = 5
43
-
44
- # pylint: disable=too-many-arguments
45
- def __init__ (self , width , height , spi , cs_pin , dc_pin , sramcs_pin , rst_pin , busy_pin ):
46
- # pylint: enable=too-many-arguments
37
+ BLACK = const (0 )
38
+ WHITE = const (1 )
39
+ INVERSE = const (2 )
40
+ RED = const (3 )
41
+ DARK = const (4 )
42
+ LIGHT = const (5 )
43
+
44
+
45
+ def __init__ (self , width , height , spi , cs_pin , dc_pin , sramcs_pin , rst_pin , busy_pin ): # pylint: disable=too-many-arguments
47
46
self ._width = width
48
47
self ._height = height
49
48
@@ -74,8 +73,11 @@ def __init__(self, width, height, spi, cs_pin, dc_pin, sramcs_pin, rst_pin, busy
74
73
if sramcs_pin :
75
74
self .sram = mcp_sram .Adafruit_MCP_SRAM (sramcs_pin , spi )
76
75
76
+ self ._buf = bytearray (3 )
77
77
self ._buffer1_size = self ._buffer2_size = 0
78
78
self ._buffer1 = self ._buffer2 = None
79
+ self ._framebuf1 = self ._framebuf2 = None
80
+ self .black_invert = self .red_invert = True
79
81
self .hardware_reset ()
80
82
81
83
def display (self ):
@@ -89,9 +91,11 @@ def display(self):
89
91
pass
90
92
self .sram .cs_pin .value = False
91
93
#send read command
92
- self .spi_device . write ( bytearray ([ mcp_sram .Adafruit_MCP_SRAM .SRAM_READ ]))
94
+ self ._buf [ 0 ] = mcp_sram .Adafruit_MCP_SRAM .SRAM_READ
93
95
#send start address
94
- self .spi_device .write (bytearray ([0x00 , 0x00 ]))
96
+ self ._buf [1 ] = 0
97
+ self ._buf [2 ] = 0
98
+ self .spi_device .write (self ._buf , end = 3 )
95
99
self .spi_device .unlock ()
96
100
97
101
#first data byte from SRAM will be transfered in at the
@@ -122,9 +126,11 @@ def display(self):
122
126
pass
123
127
self .sram .cs_pin .value = False
124
128
#send read command
125
- self .spi_device . write ( bytearray ([ mcp_sram .Adafruit_MCP_SRAM .SRAM_READ ]))
129
+ self ._buf [ 0 ] = mcp_sram .Adafruit_MCP_SRAM .SRAM_READ
126
130
#send start address
127
- self .spi_device .write (bytearray ([(self ._buffer1_size >> 8 ), (self ._buffer1_size & 0xFF )]))
131
+ self ._buf [1 ] = self ._buffer1_size >> 8
132
+ self ._buf [2 ] = self ._buffer1_size
133
+ self .spi_device .write (self ._buf , end = 3 )
128
134
self .spi_device .unlock ()
129
135
130
136
#first data byte from SRAM will be transfered in at the
@@ -151,7 +157,7 @@ def display(self):
151
157
152
158
153
159
def hardware_reset (self ):
154
- # if we have a reset pin, do a hardware reset
160
+ """If we have a reset pin, do a hardware reset by toggling it"""
155
161
if self ._rst :
156
162
self ._rst .value = False
157
163
time .sleep (0.1 )
@@ -178,6 +184,28 @@ def command(self, cmd, data=None, end=True):
178
184
179
185
return outbuf [0 ]
180
186
187
+ def power_up (self ):
188
+ """Power up the display in preparation for writing RAM and updating.
189
+ must be implemented in subclass"""
190
+ raise NotImplementedError ()
191
+
192
+ def power_down (self ):
193
+ """Power down the display, must be implemented in subclass"""
194
+ raise NotImplementedError ()
195
+
196
+ def update (self ):
197
+ """Update the display from internal memory, must be implemented in subclass"""
198
+ raise NotImplementedError ()
199
+
200
+ def write_ram (self , index ):
201
+ """Send the one byte command for starting the RAM write process. Returns
202
+ the byte read at the same time over SPI. index is the RAM buffer, can be
203
+ 0 or 1 for tri-color displays. must be implemented in subclass"""
204
+ raise NotImplementedError ()
205
+
206
+ def set_ram_address (self , x , y ):
207
+ """Set the RAM address location, must be implemented in subclass"""
208
+ raise NotImplementedError ()
181
209
182
210
def pixel (self , x , y , color ):
183
211
"""draw a single pixel in the display buffer"""
@@ -200,40 +228,54 @@ def fill(self, color):
200
228
self ._framebuf1 .fill (black_fill )
201
229
self ._framebuf2 .fill (red_fill )
202
230
203
- def rect (self , x , y , width , height , color ):
231
+ def rect (self , x , y , width , height , color ): # pylint: disable=too-many-arguments
204
232
"""draw a rectangle"""
205
- self ._framebuf1 .rect (x , y , width , height , (color == Adafruit_EPD .BLACK ) != self .black_invert )
206
- self ._framebuf2 .rect (x , y , width , height , (color == Adafruit_EPD .RED ) != self .red_invert )
233
+ self ._framebuf1 .rect (x , y , width , height ,
234
+ (color == Adafruit_EPD .BLACK ) != self .black_invert )
235
+ self ._framebuf2 .rect (x , y , width , height ,
236
+ (color == Adafruit_EPD .RED ) != self .red_invert )
207
237
208
- # pylint: disable=too-many-arguments
209
- def fill_rect (self , x , y , width , height , color ):
238
+ def fill_rect (self , x , y , width , height , color ): # pylint: disable=too-many-arguments
210
239
"""fill a rectangle with the passed color"""
211
- self ._framebuf1 .fill_rect (x , y , width , height , (color == Adafruit_EPD .BLACK ) != self .black_invert )
212
- self ._framebuf2 .fill_rect (x , y , width , height , (color == Adafruit_EPD .RED ) != self .red_invert )
213
-
214
- def line (self , x_0 , y_0 , x_1 , y_1 , color ):
215
- self ._framebuf1 .line (x_0 , y_0 , x_1 , y_1 , (color == Adafruit_EPD .BLACK ) != self .black_invert )
216
- self ._framebuf2 .line (x_0 , y_0 , x_1 , y_1 , (color == Adafruit_EPD .RED ) != self .red_invert )
240
+ self ._framebuf1 .fill_rect (x , y , width , height ,
241
+ (color == Adafruit_EPD .BLACK ) != self .black_invert )
242
+ self ._framebuf2 .fill_rect (x , y , width , height ,
243
+ (color == Adafruit_EPD .RED ) != self .red_invert )
244
+
245
+ def line (self , x_0 , y_0 , x_1 , y_1 , color ): # pylint: disable=too-many-arguments
246
+ """Draw a line from (x_0, y_0) to (x_1, y_1) in passed color"""
247
+ self ._framebuf1 .line (x_0 , y_0 , x_1 , y_1 ,
248
+ (color == Adafruit_EPD .BLACK ) != self .black_invert )
249
+ self ._framebuf2 .line (x_0 , y_0 , x_1 , y_1 ,
250
+ (color == Adafruit_EPD .RED ) != self .red_invert )
217
251
218
252
def text (self , string , x , y , color , * , font_name = "font5x8.bin" ):
219
- self ._framebuf1 .text (string , x , y , (color == Adafruit_EPD .BLACK ) != self .black_invert , font_name = font_name )
220
- self ._framebuf2 .text (string , x , y , (color == Adafruit_EPD .RED ) != self .red_invert , font_name = font_name )
253
+ """Write text string at location (x, y) in given color, using font file"""
254
+ self ._framebuf1 .text (string , x , y ,
255
+ (color == Adafruit_EPD .BLACK ) != self .black_invert ,
256
+ font_name = font_name )
257
+ self ._framebuf2 .text (string , x , y ,
258
+ (color == Adafruit_EPD .RED ) != self .red_invert ,
259
+ font_name = font_name )
221
260
222
261
@property
223
262
def width (self ):
263
+ """The width of the display, accounting for rotation"""
224
264
if self .rotation in (0 , 2 ):
225
265
return self ._width
226
266
return self ._height
227
267
228
268
@property
229
269
def height (self ):
270
+ """The height of the display, accounting for rotation"""
230
271
if self .rotation in (0 , 2 ):
231
272
return self ._height
232
273
return self ._width
233
274
234
275
@property
235
276
def rotation (self ):
236
- return self ._framebuf1 ._rotation
277
+ """The rotation of the display, can be one of (0, 1, 2, 3)"""
278
+ return self ._framebuf1 .rotation
237
279
238
280
@rotation .setter
239
281
def rotation (self , val ):
@@ -271,7 +313,7 @@ def image(self, image):
271
313
addr = int (((self ._width - x ) * self ._height + y )/ 8 )
272
314
273
315
if pixel == (0xFF , 0 , 0 ):
274
- addr = addr + self .bw_bufsize
316
+ addr = addr + self ._buffer1_size
275
317
current = self .sram .read8 (addr )
276
318
277
319
if pixel in ((0xFF , 0 , 0 ), (0 , 0 , 0 )):
0 commit comments