-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinkplate2.py
373 lines (297 loc) · 9.82 KB
/
inkplate2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import time
import os
from machine import ADC, I2C, SPI, Pin
from micropython import const
from shapes import Shapes
from machine import Pin as mPin
from gfx import GFX
from gfx_standard_font_01 import text_dict as std_font
# Connections between ESP32 and color Epaper
EPAPER_RST_PIN = const(19)
EPAPER_DC_PIN = const(33)
EPAPER_CS_PIN = const(27)
EPAPER_BUSY_PIN = const(32)
EPAPER_CLK = const(18)
EPAPER_DIN = const(23)
pixelMaskLUT = [0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80]
pixelMaskGLUT = [0xF, 0xF0]
# ePaper resolution
# For Inkplate2 height and width are swapped in relation to the default rotation
E_INK_HEIGHT = 212
E_INK_WIDTH = 104
E_INK_NUM_PIXELS = E_INK_HEIGHT * E_INK_WIDTH
E_INK_BUFFER_SIZE = E_INK_NUM_PIXELS // 8
busy_timeout_ms = 30000
class Inkplate:
# Colors
WHITE = 0b00000000
BLACK = 0b00000001
RED = 0b00000010
_width = E_INK_WIDTH
_height = E_INK_HEIGHT
rotation = 0
textSize = 1
_panelState = False
_framebuf_BW = bytearray([0xFF] * E_INK_BUFFER_SIZE)
_framebuf_RED = bytearray([0xFF] * E_INK_BUFFER_SIZE)
@classmethod
def begin(self):
self.wire = I2C(0, scl=Pin(22), sda=Pin(21))
self.spi = SPI(2)
self._framebuf_BW = bytearray(([0xFF] * E_INK_BUFFER_SIZE))
self._framebuf_RED = bytearray(([0xFF] * E_INK_BUFFER_SIZE))
self.GFX = GFX(
E_INK_HEIGHT,
E_INK_WIDTH,
self.writePixel,
self.writeFastHLine,
self.writeFastVLine,
self.writeFillRect,
None,
None,
)
# Wake the panel and init it
if not (self.setPanelDeepSleepState(False)):
return False
# Put it back to sleep
self.setPanelDeepSleepState(True)
# 3 is the default rotation for Inkplate 2
self.setRotation(3)
return True
@classmethod
def getPanelDeepSleepState(self):
return self._panelState
@classmethod
def setPanelDeepSleepState(self, state):
# False wakes the panel up
# True puts it to sleep
if not state:
self.spi.init(baudrate=20000000, firstbit=SPI.MSB,
polarity=0, phase=0)
self.EPAPER_BUSY_PIN = Pin(EPAPER_BUSY_PIN, Pin.IN)
self.EPAPER_RST_PIN = Pin(EPAPER_RST_PIN, Pin.OUT)
self.EPAPER_DC_PIN = Pin(EPAPER_DC_PIN, Pin.OUT)
self.EPAPER_CS_PIN = Pin(EPAPER_CS_PIN, Pin.OUT)
time.sleep_ms(10)
self.resetPanel()
# Reinit the panel
self.sendCommand(b"\x04")
_timeout = time.ticks_ms()
while not self.EPAPER_BUSY_PIN.value() and (time.ticks_ms() - _timeout) < busy_timeout_ms:
pass
self.sendCommand(b"\x00")
self.sendData(b"\x0f")
self.sendData(b"\x89")
self.sendCommand(b"\x61")
self.sendData(b"\x68")
self.sendData(b"\x00")
self.sendData(b"\xD4")
self.sendCommand(b"\x50")
self.sendData(b"\x77")
self._panelState = True
return True
else:
# Put the panel to sleep
self.sendCommand(b"\x50")
self.sendData(b"\xf7")
self.sendCommand(b"\x02")
# Wait for ePaper
_timeout = time.ticks_ms()
while not self.EPAPER_BUSY_PIN.value() and (time.ticks_ms() - _timeout) < busy_timeout_ms:
pass
self.sendCommand(b"\07")
self.sendData(b"\xA5")
time.sleep_ms(1)
# Turn off SPI
self.spi.deinit()
self.EPAPER_BUSY_PIN = Pin(EPAPER_BUSY_PIN, Pin.IN)
self.EPAPER_RST_PIN = Pin(EPAPER_RST_PIN, Pin.IN)
self.EPAPER_DC_PIN = Pin(EPAPER_DC_PIN, Pin.IN)
self.EPAPER_CS_PIN = Pin(EPAPER_CS_PIN, Pin.IN)
self._panelState = False
return False
@classmethod
def resetPanel(self):
self.EPAPER_RST_PIN.value(0)
time.sleep_ms(10)
self.EPAPER_RST_PIN.value(1)
time.sleep_ms(10)
@classmethod
def sendCommand(self, command):
self.EPAPER_DC_PIN.value(0)
self.EPAPER_CS_PIN.value(0)
self.spi.write(command)
self.EPAPER_CS_PIN.value(1)
@classmethod
def sendData(self, data):
self.EPAPER_CS_PIN.value(0)
self.EPAPER_DC_PIN.value(1)
self.spi.write(data)
self.EPAPER_CS_PIN.value(1)
time.sleep_ms(1)
@classmethod
def clearDisplay(self):
self._framebuf_BW = bytearray(([0xFF] * E_INK_BUFFER_SIZE))
self._framebuf_RED = bytearray(([0xFF] * E_INK_BUFFER_SIZE))
@classmethod
def display(self):
# Wake the display
self.setPanelDeepSleepState(False)
# Write b/w pixels
self.sendCommand(b"\x10")
self.sendData(self._framebuf_BW)
# Write red pixels
self.sendCommand(b"\x13")
self.sendData(self._framebuf_RED)
# Stop transfer
self.sendCommand(b"\x11")
self.sendData(b"\x00")
# Refresh
self.sendCommand(b"\x12")
time.sleep_ms(5)
_timeout = time.ticks_ms()
while not self.EPAPER_BUSY_PIN.value() and (time.ticks_ms() - _timeout) < busy_timeout_ms:
pass
# Put the display back to sleep
self.setPanelDeepSleepState(True)
@classmethod
def width(self):
return self._width
@classmethod
def height(self):
return self._height
# Arduino compatibility functions
@classmethod
def setRotation(self, x):
self.rotation = x % 4
if self.rotation == 0 or self.rotation == 2:
self._width = E_INK_WIDTH
self._height = E_INK_HEIGHT
elif self.rotation == 1 or self.rotation == 3:
self._width = E_INK_HEIGHT
self._height = E_INK_WIDTH
@classmethod
def getRotation(self):
return self.rotation
@classmethod
def drawPixel(self, x, y, c):
self.startWrite()
self.writePixel(x, y, c)
self.endWrite()
@classmethod
def startWrite(self):
pass
@classmethod
def writePixel(self, x, y, c):
if x > self.width() - 1 or y > self.height() - 1 or x < 0 or y < 0:
return
if (c > 2):
return
if self.rotation == 3:
x, y = y, x
y = self.width() - y - 1
elif self.rotation == 0:
x = self.width() - x - 1
y = self.height() - y - 1
elif self.rotation == 1:
x, y = y, x
x = self.height() - x - 1
elif self.rotation == 2:
pass
_x = x // 8
_x_sub = x % 8
_position = E_INK_WIDTH // 8 * y + _x
# Clear both black and red frame buffer
self._framebuf_BW[_position] |= (pixelMaskLUT[7 - _x_sub])
self._framebuf_RED[_position] |= (pixelMaskLUT[7 - _x_sub])
# Write the pixel to the according buffer
if (c < 2):
self._framebuf_BW[_position] &= ~(c << (7 - _x_sub))
else:
self._framebuf_RED[_position] &= ~(pixelMaskLUT[7 - _x_sub])
@classmethod
def writeFillRect(self, x, y, w, h, c):
for j in range(w):
for i in range(h):
self.writePixel(x + j, y + i, c)
@classmethod
def writeFastVLine(self, x, y, h, c):
for i in range(h):
self.writePixel(x, y + i, c)
@classmethod
def writeFastHLine(self, x, y, w, c):
for i in range(w):
self.writePixel(x + i, y, c)
@classmethod
def writeLine(self, x0, y0, x1, y1, c):
self.GFX.line(x0, y0, x1, y1, c)
@classmethod
def endWrite(self):
pass
@classmethod
def drawFastVLine(self, x, y, h, c):
self.startWrite()
self.writeFastVLine(x, y, h, c)
self.endWrite()
@classmethod
def drawFastHLine(self, x, y, w, c):
self.startWrite()
self.writeFastHLine(x, y, w, c)
self.endWrite()
@classmethod
def fillRect(self, x, y, w, h, c):
self.startWrite()
self.writeFillRect(x, y, w, h, c)
self.endWrite()
@classmethod
def fillScreen(self, c):
self.fillRect(0, 0, self.width(), self.height(), c)
@classmethod
def drawLine(self, x0, y0, x1, y1, c):
self.startWrite()
self.writeLine(x0, y0, x1, y1, c)
self.endWrite()
@classmethod
def drawRect(self, x, y, w, h, c):
self.GFX.rect(x, y, w, h, c)
@classmethod
def drawCircle(self, x, y, r, c):
self.GFX.circle(x, y, r, c)
@classmethod
def fillCircle(self, x, y, r, c):
self.GFX.fill_circle(x, y, r, c)
@classmethod
def drawTriangle(self, x0, y0, x1, y1, x2, y2, c):
self.GFX.triangle(x0, y0, x1, y1, x2, y2, c)
@classmethod
def fillTriangle(self, x0, y0, x1, y1, x2, y2, c):
self.GFX.fill_triangle(x0, y0, x1, y1, x2, y2, c)
@classmethod
def drawRoundRect(self, x, y, q, h, r, c):
self.GFX.round_rect(x, y, q, h, r, c)
@classmethod
def fillRoundRect(self, x, y, q, h, r, c):
self.GFX.fill_round_rect(x, y, q, h, r, c)
@classmethod
def setTextSize(self, s):
self.textSize = s
@classmethod
def setFont(self, f):
self.GFX.font = f
@classmethod
def printText(self, x, y, s, c=BLACK):
self.GFX._very_slow_text(x, y, s, self.textSize, c)
@classmethod
def drawBitmap(self, x, y, data, w, h, c=BLACK):
byteWidth = (w + 7) // 8
byte = 0
self.startWrite()
for j in range(h):
for i in range(w):
if i & 7:
byte <<= 1
else:
byte = data[j * byteWidth + i // 8]
if byte & 0x80:
self.writePixel(x + i, y + j, c)
self.endWrite()