forked from hosaka/micropython-st7735
-
Notifications
You must be signed in to change notification settings - Fork 9
/
st7735.py
342 lines (291 loc) · 10.5 KB
/
st7735.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
# MicroPython ST7735 TFT display driver
class ST7735(object):
# command definitions
CMD_NOP = const(0x00) # No Operation
CMD_SWRESET = const(0x01) # Software reset
CMD_RDDID = const(0x04) # Read Display ID
CMD_RDDST = const(0x09) # Read Display Status
CMD_SLPIN = const(0x10) # Sleep in & booster off
CMD_SLPOUT = const(0x11) # Sleep out & booster on
CMD_PTLON = const(0x12) # Partial mode on
CMD_NORON = const(0x13) # Partial off (Normal)
CMD_INVOFF = const(0x20) # Display inversion off
CMD_INVON = const(0x21) # Display inversion on
CMD_DISPOFF = const(0x28) # Display off
CMD_DISPON = const(0x29) # Display on
CMD_CASET = const(0x2A) # Column address set
CMD_RASET = const(0x2B) # Row address set
CMD_RAMWR = const(0x2C) # Memory write
CMD_RAMRD = const(0x2E) # Memory read
CMD_PTLAR = const(0x30) # Partial start/end address set
CMD_COLMOD = const(0x3A) # Interface pixel format
CMD_MADCTL = const(0x36) # Memory data access control
CMD_RDID1 = const(0xDA) # Read ID1
CMD_RDID2 = const(0xDB) # Read ID2
CMD_RDID3 = const(0xDC) # Read ID3
CMD_RDID4 = const(0xDD) # Read ID4
# panel function commands
CMD_FRMCTR1 = const(0xB1) # In normal mode (Full colors)
CMD_FRMCTR2 = const(0xB2) # In Idle mode (8-colors)
CMD_FRMCTR3 = const(0xB3) # In partial mode + Full colors
CMD_INVCTR = const(0xB4) # Display inversion control
CMD_PWCTR1 = const(0xC0) # Power control settings
CMD_PWCTR2 = const(0xC1) # Power control settings
CMD_PWCTR3 = const(0xC2) # In normal mode (Full colors)
CMD_PWCTR4 = const(0xC3) # In Idle mode (8-colors)
CMD_PWCTR5 = const(0xC4) # In partial mode + Full colors
CMD_VMCTR1 = const(0xC5) # VCOM control
CMD_GMCTRP1 = const(0xE0)
CMD_GMCTRN1 = const(0xE1)
# colors
COLOR_BLACK = const(0x0000)
COLOR_BLUE = const(0x001F)
COLOR_RED = const(0xF800)
COLOR_GREEN = const(0x07E0)
COLOR_CYAN = const(0x07FF)
COLOR_MAGENTA = const(0xF81F)
COLOR_YELLOW = const(0xFFE0)
COLOR_WHITE = const(0xFFFF)
def __init__(self, width, height):
# self.tab = tab
self.width = width
self.height = height
self.power_on = True
self.inverted = False
self.backlight_on = True
# default margins, set yours in HAL init
self.margin_row = 0
self.margin_col = 0
def _set_window(self, x0, y0, x1, y1, rotate=0):
"""
Set window frame boundaries.
Any pixels written to the display will start from this area.
"""
if self.rotate == 90:
y2 = x0
y3 = x1
x1 = self.height - 1 - y0
x0 = self.height - 1 - y1
y0 = y2
y1 = y3
if self.rotate == 270:
y2 = self.width - 1 - x0
y3 = self.width - 1 - x1
x0 = y0
x1 = y1
y0 = y2
y1 = y3
if self.rotate == 180:
x3 = x1
y3 = y1
x1 = self.width - 1 - x0
x0 = self.width - 1 - x3
y1 = self.height - 1 - y0
y0 = self.height - 1 - y3
# set row XSTART/XEND
self.write_cmd(CMD_RASET)
self.write_data(bytearray(
[0x00, y0 + self.margin_row, 0x00, y1 + self.margin_row])
)
# set column XSTART/XEND
self.write_cmd(CMD_CASET)
self.write_data(bytearray(
[0x00, x0 + self.margin_col, 0x00, x1 + self.margin_col])
)
# write addresses to RAM
self.write_cmd(CMD_RAMWR)
def changeRotate(self, r):
if self.rotate == 270 or self.rotate == 90:
if r == 0 or r == 180:
self.height, self.width = self.width, self.height
else:
if r == 90 or r == 270:
self.height, self.width = self.width, self.height
self.rotate = r
def power(self, state=None):
"""
Get/set display power.
"""
if state is None:
return self.power_on
self.write_cmd(CMD_DISPON if state else CMD_DISPOFF)
self.power_on = state
def clear(self, color=COLOR_WHITE):
"""
Clear the display filling it with color.
"""
self.rect(0, 0, self.width, self.height, color)
def invert(self, state=None):
"""
Get/set display color inversion.
"""
if state is None:
return self.inverted
self.write_cmd(CMD_INVON if state else CMD_INVOFF)
self.inverted = state
def rgbcolor(self, r, g, b):
"""
Pack 24-bit RGB into 16-bit value.
"""
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3)
def pixel(self, x, y, color):
"""
Draw a single pixel on the display with given color.
"""
self._set_window(x, y, x + 1, y + 1)
self.write_pixels(1, bytearray([color >> 8, color]))
def rect(self, x, y, w, h, color):
"""
Draw a rectangle with specified coordinates/size and fill with color.
"""
# check the coordinates and trim if necessary
if (x >= self.width) or (y >= self.height):
return
if (x + w - 1) >= self.width:
w = self.width - x
if (y + h - 1) >= self.height:
h = self.height - y
self._set_window(x, y, x + w - 1, y + h - 1)
self.write_pixels((w*h), bytearray([color >> 8, color]))
def line(self, x0, y0, x1, y1, color):
# line is vertical
if x0 == x1:
# use the smallest y
start, end = (x1, y1) if y1 < y0 else (x0, y0)
self.vline(start, end, abs(y1 - y0) + 1, color)
# line is horizontal
elif y0 == y1:
# use the smallest x
start, end = (x1, y1) if x1 < x0 else (x0, y0)
self.hline(start, end, abs(x1 - x0) + 1, color)
else:
# Bresenham's algorithm
dx = abs(x1 - x0)
dy = abs(y1 - y0)
inx = 1 if x1 - x0 > 0 else -1
iny = 1 if y1 - y0 > 0 else -1
# steep line
if (dx >= dy):
dy <<= 1
e = dy - dx
dx <<= 1
while (x0 != x1):
# draw pixels
self.pixel(x0, y0, color)
if (e >= 0):
y0 += iny
e -= dx
e += dy
x0 += inx
# not steep line
else:
dx <<= 1
e = dx - dy
dy <<= 1
while(y0 != y1):
# draw pixels
self.pixel(x0, y0, color)
if (e >= 0):
x0 += inx
e -= dy
e += dx
y0 += iny
def hline(self, x, y, w, color):
if (x >= self.width) or (y >= self.height):
return
if (x + w - 1) >= self.width:
w = self.width - x
self._set_window(x, y, x + w - 1, y)
self.write_pixels(x+w-1, bytearray([color >> 8, color]))
def vline(self, x, y, h, color):
if (x >= self.width) or (y >= self.height):
return
if (y + h -1) >= self.height:
h = self.height - y
self._set_window(x, y, x, y + h - 1)
self.write_pixels(y+h-1, bytearray([color >> 8, color]))
def text(self, x, y, string, font, color, size=1):
"""
Draw text at a given position using the user font.
Font can be scaled with the size parameter.
"""
if font is None:
return
width = size * font['width'] + 1
px = x
for c in string:
self.char(px, y, c, font, color, size, size)
px += width
# wrap the text to the next line if it reaches the end
if px + width > self.width:
y += font['height'] * size + 1
px = x
def char(self, x, y, char, font, color, sizex=1, sizey=1):
"""
Draw a character at a given position using the user font.
Font is a data dictionary, can be scaled with sizex and sizey.
"""
if font is None:
return
startchar = font['start']
endchar = font['end']
ci = ord(char)
if (startchar <= ci <= endchar):
width = font['width']
height = font['height']
ci = (ci - startchar) * width
ch = font['data'][ci:ci + width]
# no font scaling
px = x
if (sizex <= 1 and sizey <= 1):
for c in ch:
py = y
for _ in range(height):
if c & 0x01:
self.pixel(px, py, color)
py += 1
c >>= 1
px += 1
# scale to given sizes
else:
for c in ch:
py = y
for _ in range(height):
if c & 0x01:
self.rect(px, py, sizex, sizey, color)
py += sizey
c >>= 1
px += sizex
else:
# character not found in this font
return
def init(self):
"""
HAL: Init your "tab" version of the display.
"""
raise NotImplementedError
def reset(self):
"""
HAL: Display reset command.
"""
raise NotImplementedError
def backlight(self, state):
"""
HAL: Toggle display backlight depending on given state.
"""
raise NotImplementedError
def write_pixels(self, count, color):
"""
HAL: Write individual pixels to the display.
"""
raise NotImplementedError
def write_cmd(self, cmd):
"""
HAL: Write a command to the display.
"""
raise NotImplementedError
def write_data(self, data):
"""
HAL: Write data to the display.
"""
raise NotImplementedError