-
Notifications
You must be signed in to change notification settings - Fork 0
/
OLED_Driver2.py
313 lines (261 loc) · 7.61 KB
/
OLED_Driver2.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
# -*- coding:UTF-8 -*-
# same as Driver 1 except for second dipslay on the second SPI bus of the Jetson Nano. Refer to Driver 1 code for missing documentation. Please contact Waveshare for any additional inquiries
import spidev
import Jetson.GPIO as GPIO
import time
import numpy as np
import cv2
#SSD1351
SSD1351_WIDTH = 128
SSD1351_HEIGHT = 128
SSD1351_CMD_SETCOLUMN = 0x15
SSD1351_CMD_SETROW = 0x75
SSD1351_CMD_WRITERAM = 0x5C
SSD1351_CMD_READRAM = 0x5D
SSD1351_CMD_SETREMAP = 0xA0
SSD1351_CMD_STARTLINE = 0xA1
SSD1351_CMD_DISPLAYOFFSET = 0xA2
SSD1351_CMD_DISPLAYALLOFF = 0xA4
SSD1351_CMD_DISPLAYALLON = 0xA5
SSD1351_CMD_NORMALDISPLAY = 0xA6
SSD1351_CMD_INVERTDISPLAY = 0xA7
SSD1351_CMD_FUNCTIONSELECT = 0xAB
SSD1351_CMD_DISPLAYOFF = 0xAE
SSD1351_CMD_DISPLAYON = 0xAF
SSD1351_CMD_PRECHARGE = 0xB1
SSD1351_CMD_DISPLAYENHANCE = 0xB2
SSD1351_CMD_CLOCKDIV = 0xB3
SSD1351_CMD_SETVSL = 0xB4
SSD1351_CMD_SETGPIO = 0xB5
SSD1351_CMD_PRECHARGE2 = 0xB6
SSD1351_CMD_SETGRAY = 0xB8
SSD1351_CMD_USELUT = 0xB9
SSD1351_CMD_PRECHARGELEVEL = 0xBB
SSD1351_CMD_VCOMH = 0xBE
SSD1351_CMD_CONTRASTABC = 0xC1
SSD1351_CMD_CONTRASTMASTER = 0xC7
SSD1351_CMD_MUXRATIO = 0xCA
SSD1351_CMD_COMMANDLOCK = 0xFD
SSD1351_CMD_HORIZSCROLL = 0x96
SSD1351_CMD_STOPSCROLL = 0x9E
SSD1351_CMD_STARTSCROLL = 0x9F
#color
BLACK = 0x0000
BLUE = 0x001F
RED = 0xF800
GREEN = 0x07E0
CYAN = 0x07FF
MAGENTA = 0xF81F
YELLOW = 0xFFE0
WHITE = 0xFFFF
#buffer
color_byte = [0x00, 0x00]
color_fill_byte = [0x00, 0x00]*(SSD1351_WIDTH)
#GPIO Set
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
OLED_RST_PIN = 17 # 22 17
OLED_DC_PIN = 13 # 6 13
OLED_CS_PIN = 24 # 8 24
GPIO.setup(OLED_RST_PIN, GPIO.OUT)
GPIO.setup(OLED_DC_PIN, GPIO.OUT)
GPIO.setup(OLED_CS_PIN, GPIO.OUT)
#GPIO init
GPIO.setwarnings(False)
GPIO.setup(OLED_RST_PIN, GPIO.OUT)
GPIO.setup(OLED_DC_PIN, GPIO.OUT)
GPIO.setup(OLED_CS_PIN, GPIO.OUT)
#SPI init
SPI = spidev.SpiDev()
SPI.open(1,0)
SPI.max_speed_hz = 30*1000000
SPI.mode = 0b00
def Set_Color(color):
color_byte[0] = (color >> 8) & 0xff
color_byte[1] = color & 0xff
def OLED_RST(x):
if x == 1:
GPIO.output(OLED_RST_PIN,GPIO.HIGH)
elif x == 0:
GPIO.output(OLED_RST_PIN,GPIO.LOW)
def OLED_DC(x):
if x == 1:
GPIO.output(OLED_DC_PIN,GPIO.HIGH)
elif x == 0:
GPIO.output(OLED_DC_PIN,GPIO.LOW)
def OLED_CS(x):
if x == 1:
GPIO.output(OLED_CS_PIN,GPIO.HIGH)
elif x == 0:
GPIO.output(OLED_CS_PIN,GPIO.LOW)
def SPI_WriteByte(byte):
SPI.writebytes(byte)
def Write_Command(cmd):
OLED_CS(0)
OLED_DC(0)
SPI_WriteByte([cmd])
OLED_CS(1)
def Write_Data(dat):
OLED_CS(0)
OLED_DC(1)
SPI_WriteByte([dat])
OLED_CS(1)
def Write_Datas(data):
OLED_CS(0)
OLED_DC(1)
SPI_WriteByte(data)
OLED_CS(1)
def RAM_Address():
Write_Command(0x15)
Write_Data(0x00)
Write_Data(0x7f)
Write_Command(0x75)
Write_Data(0x00)
Write_Data(0x7f)
def Fill_Color(color):
RAM_Address()
Write_Command(0x5c)
Set_Color(color)
color_fill_byte = color_byte*SSD1351_WIDTH
OLED_CS(0)
OLED_DC(1)
for i in range(0,SSD1351_HEIGHT):
SPI_WriteByte(color_fill_byte)
OLED_CS(1)
def Clear_Screen():
RAM_Address()
Write_Command(0x5c)
color_fill_byte = [0x00, 0x00]*SSD1351_WIDTH
OLED_CS(0)
OLED_DC(1)
for i in range(0,SSD1351_HEIGHT):
SPI_WriteByte(color_fill_byte)
OLED_CS(1)
def Draw_Pixel(x, y):
# Bounds check.
if ((x >= SSD1351_WIDTH) or (y >= SSD1351_HEIGHT)):
return
if ((x < 0) or (y < 0)):
return
Set_Address(x, y)
# transfer data
Write_Datas(color_byte)
def Set_Coordinate(x, y):
if((x >= SSD1351_WIDTH) or (y >= SSD1351_HEIGHT)):
return
# Set x and y coordinate
Write_Command(SSD1351_CMD_SETCOLUMN)
Write_Data(x)
Write_Data(SSD1351_WIDTH-1)
Write_Command(SSD1351_CMD_SETROW)
Write_Data(y)
Write_Data(SSD1351_HEIGHT-1)
Write_Command(SSD1351_CMD_WRITERAM)
def Set_Address(column, row):
Write_Command(SSD1351_CMD_SETCOLUMN)
Write_Data(column) #X start
Write_Data(column) #X end
Write_Command(SSD1351_CMD_SETROW)
Write_Data(row) #Y start
Write_Data(row+7) #Y end
Write_Command(SSD1351_CMD_WRITERAM)
def Write_text(dat):
for i in range(0,8):
if(dat & 0x01):
Write_Datas(color_byte)
else:
Write_Datas([0x00,0x00])
dat = dat >> 1
def Invert(v):
if(v):
Write_Command(SSD1351_CMD_INVERTDISPLAY)
else:
Write_Command(SSD1351_CMD_NORMALDISPLAY)
def Draw_Pixel(x, y):
# Bounds check.
if((x >= SSD1351_WIDTH) or (y >= SSD1351_HEIGHT)):
return
if((x < 0) or (y < 0)):
return
Set_Address(x, y)
# transfer data
Write_Datas(color_byte)
def Delay(x):
time.sleep(x / 1000.0)
def Device_Init():
OLED_CS(0)
OLED_RST(0)
Delay(500)
OLED_RST(1)
Delay(500)
Write_Command(0xfd) # command lock
Write_Data(0x12)
Write_Command(0xfd) # command lock
Write_Data(0xB1)
Write_Command(0xae) # display off
Write_Command(0xa4) # Normal Display mode
Write_Command(0x15) # set column address
Write_Data(0x00) # column address start 00
Write_Data(0x7f) # column address end 95
Write_Command(0x75) # set row address
Write_Data(0x00) # row address start 00
Write_Data(0x7f) # row address end 63
Write_Command(0xB3)
Write_Data(0xF1)
Write_Command(0xCA)
Write_Data(0x7F)
Write_Command(0xa0) # set re-map & data format
Write_Data(0x74) # Horizontal address increment
Write_Command(0xa1) # set display start line
Write_Data(0x00) # start 00 line
Write_Command(0xa2) # set display offset
Write_Data(0x00)
Write_Command(0xAB)
Write_Command(0x01)
Write_Command(0xB4)
Write_Data(0xA0)
Write_Data(0xB5)
Write_Data(0x55)
Write_Command(0xC1)
Write_Data(0xC8)
Write_Data(0x80)
Write_Data(0xC0)
Write_Command(0xC7)
Write_Data(0x0F)
Write_Command(0xB1)
Write_Data(0x32)
Write_Command(0xB2)
Write_Data(0xA4)
Write_Data(0x00)
Write_Data(0x00)
Write_Command(0xBB)
Write_Data(0x17)
Write_Command(0xB6)
Write_Data(0x01)
Write_Command(0xBE)
Write_Data(0x05)
Write_Command(0xA6)
Clear_Screen()
Write_Command(0xaf)
# Draw a horizontal line ignoring any screen rotation.
def Display_Image(Image):
if(Image == None):
return
w, h = Image.size
Set_Coordinate(0,0)
buffer1 = Image.load()
if h is 64:
for j in range(0, 64):
for i in range(0,64):
color1 = ((buffer1[-i+1,-j+1][0] & 0xF8)|(buffer1[-i+1,-j+1][1] >> 5))
color2 = (((buffer1[-i+1,-j+1][1] << 3) & 0xE0)|(buffer1[-i+1,-j+1][2] >> 3))
color_fill_byte[i * 4 ], color_fill_byte[i * 4 + 2] = color1, color1
color_fill_byte[i * 4 + 1], color_fill_byte[i * 4 + 3] = color2, color2
Write_Datas(color_fill_byte)
Write_Datas(color_fill_byte)
else:
for j in range(0, SSD1351_WIDTH):
for i in range(0, SSD1351_HEIGHT):
color_fill_byte[i*2] = ((buffer1[i,j][0] & 0xF8)|(buffer1[i,j][1] >> 5))
color_fill_byte[i*2+1] = (((buffer1[i,j][1] << 3) & 0xE0)|(buffer1[i,j][2] >> 3))
Write_Datas(color_fill_byte)