-
Notifications
You must be signed in to change notification settings - Fork 0
/
st7789.py
264 lines (228 loc) · 8.1 KB
/
st7789.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
from machine import Pin,SPI,PWM
import framebuf
# pin asignment
BL = const(13)
DC = const(8)
RST = const(12)
MOSI = const(11)
SCK = const(10)
CS = const(9)
class LCD_1inch14(framebuf.FrameBuffer):
def __init__(self, rotation:int=0, brightness:int=16384):
# add rotation
if rotation in (0,90,180,270):
self.rotation = rotation
else:
self.rotation = 0
if (self.rotation == 0 or self.rotation == 180):
self.width = 240
self.height = 135
else:
self.width = 135
self.height = 240
# the 2 "missing" rotations will be done mirroring X & Y
self.brightness = brightness
self.cs = Pin(CS,Pin.OUT)
self.rst = Pin(RST,Pin.OUT)
self.cs(1)
self.spi = SPI(1)
self.spi = SPI(1,1000_000)
self.spi = SPI(1,10000_000,polarity=0, phase=0,sck=Pin(SCK),mosi=Pin(MOSI),miso=None)
self.dc = Pin(DC,Pin.OUT)
self.dc(1)
self.buffer = bytearray(self.height * self.width * 2)
super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
self.init_display()
self.pwm = PWM(Pin(BL))
self.pwm.freq(1000)
self.set_brightness(self.brightness)
# Waveshare 1.14" LCD keys
self.keyA = Pin(15,Pin.IN,Pin.PULL_UP) # A
self.keyB = Pin(17,Pin.IN,Pin.PULL_UP) # B
self.keyC = Pin(3,Pin.IN,Pin.PULL_UP) # joystick center
# joystick keys are defined according to screen rotation
if self.rotation == 0:
self.keyUP = Pin(2,Pin.IN,Pin.PULL_UP)
self.keyLEFT = Pin(16,Pin.IN,Pin.PULL_UP)
self.keyDOWN = Pin(18,Pin.IN,Pin.PULL_UP)
self.keyRIGHT = Pin(20,Pin.IN,Pin.PULL_UP)
elif self.rotation == 90:
self.keyUP = Pin(20,Pin.IN,Pin.PULL_UP)
self.keyLEFT = Pin(2,Pin.IN,Pin.PULL_UP)
self.keyDOWN = Pin(16,Pin.IN,Pin.PULL_UP)
self.keyRIGHT = Pin(18,Pin.IN,Pin.PULL_UP)
elif self.rotation == 180:
self.keyUP = Pin(18 ,Pin.IN,Pin.PULL_UP)
self.keyLEFT = Pin(20 ,Pin.IN,Pin.PULL_UP)
self.keyDOWN = Pin(2 ,Pin.IN,Pin.PULL_UP)
self.keyRIGHT = Pin(16 ,Pin.IN,Pin.PULL_UP)
elif self.rotation == 270:
self.keyUP = Pin(16,Pin.IN,Pin.PULL_UP)
self.keyLEFT = Pin(18,Pin.IN,Pin.PULL_UP)
self.keyDOWN = Pin(20,Pin.IN,Pin.PULL_UP)
self.keyRIGHT = Pin(2,Pin.IN,Pin.PULL_UP)
# colors coded as RGB565 (swapped bytes)
self.red = 0x00F8
self.green = 0xE007
self.blue = 0x1F00
self.yellow = 0xE0FF
self.magenta = 0x1FF8
self.cyan = 0xFF07
self.black = 0x0000
self.white = 0xFFFF
def set_brightness(self, brightness:int):
"""Set LCD brightness from 0 to 65535"""
self.pwm.duty_u16(brightness)
def write_cmd(self, cmd):
self.cs(1)
self.dc(0)
self.cs(0)
self.spi.write(bytearray([cmd]))
self.cs(1)
def write_data(self, buf):
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(bytearray([buf]))
self.cs(1)
def init_display(self):
"""Initialize display"""
self.rst(1)
self.rst(0)
self.rst(1)
self.write_cmd(0x36)
if self.rotation == 0:
# default (original Waveshare code)
self.write_data(0x70)
elif self.rotation == 90:
# 270 rotation and X&Y mirror
self.write_data(0xC0)
elif self.rotation == 180:
# 0 rotation and X&Y mirror
self.write_data(0xB0)
elif self.rotation == 270:
self.write_data(0x00)
self.write_cmd(0x3A)
self.write_data(0x05)
self.write_cmd(0xB2)
self.write_data(0x0C)
self.write_data(0x0C)
self.write_data(0x00)
self.write_data(0x33)
self.write_data(0x33)
self.write_cmd(0xB7)
self.write_data(0x35)
self.write_cmd(0xBB)
self.write_data(0x19)
self.write_cmd(0xC0)
self.write_data(0x2C)
self.write_cmd(0xC2)
self.write_data(0x01)
self.write_cmd(0xC3)
self.write_data(0x12)
self.write_cmd(0xC4)
self.write_data(0x20)
self.write_cmd(0xC6)
self.write_data(0x0F)
self.write_cmd(0xD0)
self.write_data(0xA4)
self.write_data(0xA1)
self.write_cmd(0xE0)
self.write_data(0xD0)
self.write_data(0x04)
self.write_data(0x0D)
self.write_data(0x11)
self.write_data(0x13)
self.write_data(0x2B)
self.write_data(0x3F)
self.write_data(0x54)
self.write_data(0x4C)
self.write_data(0x18)
self.write_data(0x0D)
self.write_data(0x0B)
self.write_data(0x1F)
self.write_data(0x23)
self.write_cmd(0xE1)
self.write_data(0xD0)
self.write_data(0x04)
self.write_data(0x0C)
self.write_data(0x11)
self.write_data(0x13)
self.write_data(0x2C)
self.write_data(0x3F)
self.write_data(0x44)
self.write_data(0x51)
self.write_data(0x2F)
self.write_data(0x1F)
self.write_data(0x1F)
self.write_data(0x20)
self.write_data(0x23)
self.write_cmd(0x21)
self.write_cmd(0x11)
self.write_cmd(0x29)
def show(self):
if self.rotation == 0:
# default (original Waveshare code)
self.write_cmd(0x2A)
self.write_data(0x00)
self.write_data(0x28)
self.write_data(0x01)
self.write_data(0x17)
self.write_cmd(0x2B)
self.write_data(0x00)
self.write_data(0x35)
self.write_data(0x00)
self.write_data(0xBB)
elif self.rotation == 90:
self.write_cmd(0x2A)
self.write_data(0x00)
self.write_data(0x35)
self.write_data(0x00)
self.write_data(0xBB)
self.write_cmd(0x2B)
self.write_data(0x00)
self.write_data(0x28)
self.write_data(0x01)
self.write_data(0x17)
if self.rotation == 180:
self.write_cmd(0x2A)
self.write_data(0x00)
self.write_data(0x28)
self.write_data(0x01)
self.write_data(0x17)
self.write_cmd(0x2B)
self.write_data(0x00)
self.write_data(0x34)
self.write_data(0x00)
self.write_data(0xBA)
elif self.rotation == 270:
self.write_cmd(0x2A)
self.write_data(0x00)
self.write_data(0x34)
self.write_data(0x00)
self.write_data(0xBA)
self.write_cmd(0x2B)
self.write_data(0x00)
self.write_data(0x28)
self.write_data(0x01)
self.write_data(0x17)
self.write_cmd(0x2C)
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(self.buffer)
self.cs(1)
def off(self):
"""DISPOFF command and brightness to 0"""
self.write_cmd(0x28)
self.set_brightness(0)
def on(self):
"""DISPON command and restore brightness"""
self.write_cmd(0x29)
self.set_brightness(self.brightness)
def rgb565(self,r: int,g: int,b: int) -> int:
"""Encodes RGB888 to RGB565 swapping its bytes"""
return ((g & 0x1c) << 11) | (g >> 5) | ((b & 0xf8) << 5) | (r & 0xf8)
def gray565(self,gray: int) -> int:
"""Encodes 8-bit grayscale to RGB565 swapping its bytes"""
return ((gray & 0x1c) << 11) | (gray >> 5) | ((gray & 0xf8) << 5) | (gray & 0xf8)