28
28
29
29
import time
30
30
import digitalio
31
+ import adafruit_framebuf
31
32
from adafruit_epd import mcp_sram
32
33
33
34
class Adafruit_EPD :
@@ -46,41 +47,47 @@ def __init__(self, width, height, spi, cs_pin, dc_pin, sramcs_pin, rst_pin, busy
46
47
self .width = width
47
48
self .height = height
48
49
49
- # Setup reset pin.
50
+ # Setup reset pin, if we have one
50
51
self ._rst = rst_pin
51
52
if rst_pin :
52
53
self ._rst .direction = digitalio .Direction .OUTPUT
53
54
54
- # Setup busy pin.
55
+ # Setup busy pin, if we have one
55
56
self ._busy = busy_pin
56
57
if busy_pin :
57
58
self ._busy .direction = digitalio .Direction .INPUT
58
59
59
- # Setup dc pin.
60
+ # Setup dc pin (required)
60
61
self ._dc = dc_pin
61
62
self ._dc .direction = digitalio .Direction .OUTPUT
62
63
self ._dc .value = False
63
64
64
- # Setup cs pin.
65
+ # Setup cs pin (required)
65
66
self ._cs = cs_pin
66
67
self ._cs .direction = digitalio .Direction .OUTPUT
67
68
self ._cs .value = True
68
69
70
+ # SPI interface (required)
69
71
self .spi_device = spi
70
72
71
73
if sramcs_pin :
72
74
self .sram = mcp_sram .Adafruit_MCP_SRAM (sramcs_pin , spi )
73
75
else :
74
76
self .sram = None
75
- self .bw_buffer = bytearray ((width // 8 ) * height )
76
- self .red_buffer = bytearray ((width // 8 ) * height )
77
+ self ._bw_buffer = bytearray ((width // 8 ) * height )
78
+ self ._red_buffer = bytearray ((width // 8 ) * height )
79
+ # since we have *two* framebuffers - one for red and one for black, we dont subclass but manage manually
80
+ self ._red_framebuf = adafruit_framebuf .FrameBuffer (self ._red_buffer , width , height , buf_format = adafruit_framebuf .MHMSB )
81
+ self ._bw_framebuf = adafruit_framebuf .FrameBuffer (self ._bw_buffer , width , height , buf_format = adafruit_framebuf .MHMSB )
77
82
83
+ # if we hav ea reset pin, do a hardware reset
78
84
if self ._rst :
79
85
self ._rst .value = False
80
86
time .sleep (.1 )
81
87
self ._rst .value = True
82
88
time .sleep (.1 )
83
89
90
+
84
91
def command (self , cmd , data = None , end = True ):
85
92
"""Send command byte to display."""
86
93
self ._cs .value = True
@@ -109,42 +116,34 @@ def data(self, dat):
109
116
self ._cs .value = True
110
117
self .spi_device .unlock ()
111
118
112
- def draw_pixel (self , x , y , color ):
119
+ def fill (self , color ):
120
+ #This should be overridden in the subclass
121
+ self ._bw_framebuf .fill ((color == Adafruit_EPD .BLACK ) != self .black_invert )
122
+ self ._red_framebuf .fill ((color == Adafruit_EPD .RED ) != self .red_invert )
123
+
124
+ def pixel (self , x , y , color = None ):
113
125
"""This should be overridden in the subclass"""
114
- pass
126
+ self ._bw_framebuf .pixel (x , y , (color == Adafruit_EPD .BLACK ) != self .black_invert )
127
+ self ._red_framebuf .pixel (x , y , (color == Adafruit_EPD .RED ) != self .red_invert )
115
128
116
- #framebuf methods
117
- def fill ( self , color ):
118
- """fill the screen with the passed color"""
119
- self .fill_rect ( 0 , 0 , self . width , self . height , color )
129
+ def rect ( self , x , y , width , height , color ):
130
+ """draw a rectangle"""
131
+ self . _bw_framebuf . rect ( x , y , width , height , ( color == Adafruit_EPD . BLACK ) != self . black_invert )
132
+ self ._red_framebuf . rect ( x , y , width , height , ( color == Adafruit_EPD . RED ) != self . red_invert )
120
133
121
134
# pylint: disable=too-many-arguments
122
135
def fill_rect (self , x , y , width , height , color ):
123
136
"""fill a rectangle with the passed color"""
124
- if width < 1 or height < 1 or (x + width ) <= 0 :
125
- return
126
- if (y + height ) <= 0 or y >= self .height or x >= self .width :
127
- return
128
- xend = min (self .width , x + width )
129
- yend = min (self .height , y + height )
130
- x = max (x , 0 )
131
- y = max (y , 0 )
132
- for _x in range (xend - x ):
133
- for _y in range (yend - y ):
134
- self .draw_pixel (x + _x , y + _y , color )
135
- return
137
+ self ._bw_framebuf .fill_rect (x , y , width , height , (color == Adafruit_EPD .BLACK ) != self .black_invert )
138
+ self ._red_framebuf .fill_rect (x , y , width , height , (color == Adafruit_EPD .RED ) != self .red_invert )
136
139
137
- def pixel (self , x , y , color = None ):
138
- """draw a pixel"""
139
- if x < 0 or x >= self .width or y < 0 or y >= self .height :
140
- return None
141
- #TODO: figure this out when we know what framebuffer we
142
- # will actually use
143
- #if color is None:
144
- # return self.get_pixel(self, x, y)
140
+ def line (self , x_0 , y_0 , x_1 , y_1 , color ):
141
+ self ._bw_framebuf .line (x_0 , y_0 , x_1 , y_1 , (color == Adafruit_EPD .BLACK ) != self .black_invert )
142
+ self ._red_framebuf .line (x_0 , y_0 , x_1 , y_1 , (color == Adafruit_EPD .RED ) != self .red_invert )
145
143
146
- self .draw_pixel (x , y , color )
147
- return None
144
+ def text (self , string , x , y , color , * , font_name = "font5x8.bin" ):
145
+ self ._bw_framebuf .text (string , x , y , (color == Adafruit_EPD .BLACK ) != self .black_invert , font_name )
146
+ self ._red_framebuf .text (string , x , y , (color == Adafruit_EPD .RED ) != self .red_invert , font_name )
148
147
149
148
def hline (self , x , y , width , color ):
150
149
"""draw a horizontal line"""
@@ -153,10 +152,3 @@ def hline(self, x, y, width, color):
153
152
def vline (self , x , y , height , color ):
154
153
"""draw a vertical line"""
155
154
self .fill_rect (x , y , 1 , height , color )
156
-
157
- def rect (self , x , y , width , height , color ):
158
- """draw a rectangle"""
159
- self .fill_rect (x , y , width , 1 , color )
160
- self .fill_rect (x , y + height , width , 1 , color )
161
- self .fill_rect (x , y , 1 , height , color )
162
- self .fill_rect (x + width , y , 1 , height , color )
0 commit comments