forked from IotaHydrae/pico-py-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssd1681.py
260 lines (206 loc) · 6.55 KB
/
ssd1681.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
import time
from machine import SPI, Pin
SSD1681_PIN_SCL = 18
SSD1681_PIN_SDA = 19
SSD1681_PIN_RES = 15
SSD1681_PIN_DC = 14
SSD1681_PIN_CS = 13
SSD1681_PIN_BUSY = 12
SSD1681_BLACK = True
SSD1681_WHITE = False
SSD1681_UPDATE_MODE_PART = 0
SSD1681_UPDATE_MODE_FULL = 1
class spi_ifce(object):
def __init__(self, speed=400000, sck=18, mosi=19, miso=16, csPin=13):
self.speed = speed
self.sck = sck
self.mosi = mosi
self.miso = miso
self.csPin = csPin
self.spi = SPI(0,
baudrate=self.speed,
sck=Pin(self.sck),
mosi=Pin(self.mosi),
miso=Pin(self.miso))
self.cs = Pin(self.csPin, mode=Pin.OUT, value=1)
def cs_select(self):
self.cs.value(0)
def cs_deselect(self):
self.cs.value(1)
def surround_cs(self, fun):
def wrapper(*args, **kwargs):
self.cs_select()
rc = fun(*args, **kwargs)
self.cs_deselect()
return rc
return wrapper
# @surround_cs
# def read_byte(self):
# self.spi.read(1, 0x00)
# pass
# @surround_cs
def write_byte(self, val):
val = val.to_bytes(1, 'little')
self.cs_select()
self.spi.write(val)
self.cs_deselect()
pass
class ssd1681(spi_ifce):
def __init__(self, sck, mosi, csPin, resPin, dcPin, busyPin):
self.csPin = csPin
self.resPin = resPin
self.dcPin = dcPin
self.busyPin = busyPin
super().__init__(csPin=self.csPin)
self.res = Pin(self.resPin, mode=Pin.OUT, value=1)
self.dc = Pin(self.dcPin, mode=Pin.OUT, value=0)
self.busy = Pin(self.busyPin, mode=Pin.IN)
self.fb = list(range(200 * 25))
for i in self.fb:
self.fb[i] = 0xff
def res_set(self):
self.res.value(1)
def res_clr(self):
self.res.value(0)
def dc_set(self):
self.dc.value(1)
def dc_clr(self):
self.dc.value(0)
def dc_cmd(self, fun):
def wrapper(*args, **kwargs):
self.dc_clr()
rc = fun(*args, **kwargs)
self.dc_set()
return rc
return wrapper
def dc_data(self, fun):
def wrapper(*args, **kwargs):
self.dc_set()
rc = fun(*args, **kwargs)
self.dc_clr()
return rc
return wrapper
# @dc_cmd
def write_reg(self, reg):
# print("reg :", reg)
self.dc_clr()
super().write_byte(reg)
self.dc_set()
# @dc_data
def write_data(self, val):
# print("data :", val)
self.dc_set()
super().write_byte(val)
self.dc_clr()
def wait_for_busy(self):
# print("wait for busy...")
while self.busy.value():
pass
def device_init(self):
# init sequence
self.res_clr()
time.sleep_ms(20)
self.res_set()
time.sleep_ms(20)
self.wait_for_busy()
self.write_reg(0x12) # Software reset
self.wait_for_busy()
self.write_reg(0x01)
self.write_data(0xC7)
self.write_data(0x00)
self.write_data(0x01)
self.write_reg(0x11)
self.write_data(0x01)
self.write_reg(0x44)
self.write_data(0x00)
self.write_data(0x18) # 0x0F -->(15 + 1) * 8 = 128
self.write_reg(0x45)
self.write_data(0xC7) # 0xF9 -->(249 + 1) = 250
self.write_data(0x00)
self.write_data(0x00)
self.write_data(0x00)
self.write_reg(0x3C)
self.write_data(0x05)
self.write_reg(0x18) # Read built-in temperature sensor
self.write_data(0x80)
self.write_reg(0x4E) # set RAM x address count to 0;
self.write_data(0x00)
self.write_data(0x4F) # set RAM y address count to 0X199;
self.write_data(0xC7)
self.write_data(0x00)
self.wait_for_busy()
def update(self, update_mode: int = SSD1681_UPDATE_MODE_FULL):
self.write_reg(0x22)
if update_mode == SSD1681_UPDATE_MODE_FULL:
self.write_data(0xf7)
elif update_mode == SSD1681_UPDATE_MODE_PART:
self.write_data(0xff)
else:
print("""given update mode is incompatible,
using default update mode: SSD1681_UPDATE_MODE_FULL""")
self.write_data(0xf7)
self.write_reg(0x20)
self.wait_for_busy()
def clear(self):
self.write_reg(0x24)
for i in range(200 * 25):
self.write_data(0xff)
self.update()
# TODO: need a logic to decide to use full or part update
def flush(self):
self.write_reg(0x24)
for i in range(200 * 25):
self.write_data(self.fb[i])
self.update()
def draw_pixel(self, x, y, color: bool):
"""
This function used to draw a single pixel on
given (x,y) pos with 'color'
:param x: pos on x
:param y: pos on y
:param color: color of this pixel
:return: none
"""
if color:
self.fb[y * 25 + int(x / 8)] &= ~(1 << (7 - x % 8))
else:
self.fb[y * 25 + int(x / 8)] |= (1 << (7 - x % 8))
def draw_rectangle(self, x1, y1, x2, y2, color: bool):
for x in range(x1, x2):
for y in range(y1, y2):
self.draw_pixel(x, y, color)
pass
def main():
display = ssd1681(
sck=SSD1681_PIN_SCL,
mosi=SSD1681_PIN_SDA,
resPin=SSD1681_PIN_RES,
dcPin=SSD1681_PIN_DC,
csPin=SSD1681_PIN_CS,
busyPin=SSD1681_PIN_BUSY
)
display.device_init()
# display.write_reg(0x24)
# for i in range(200 * 25):
# display.write_data(0x00)
# display.update()
#
# display.write_reg(0x24)
# for i in range(200 * 25):
# display.write_data(0xff)
# display.update()
# for x in range(50, 100):
# for y in range(50, 100):
# display.draw_pixel(x, y, 1)
display.clear()
display.draw_rectangle(0, 0, 50, 50, SSD1681_BLACK)
display.draw_rectangle(50, 50, 100, 100, SSD1681_BLACK)
display.draw_rectangle(100, 100, 150, 150, SSD1681_BLACK)
display.draw_rectangle(150, 150, 200, 200, SSD1681_BLACK)
display.draw_rectangle(100, 0, 150, 50, SSD1681_BLACK)
display.draw_rectangle(150, 50, 200, 100, SSD1681_BLACK)
display.draw_rectangle(0, 100, 50, 150, SSD1681_BLACK)
display.draw_rectangle(50, 150, 100, 200, SSD1681_BLACK)
display.flush()
if __name__ == '__main__':
main()