-
Notifications
You must be signed in to change notification settings - Fork 4
/
Grove_OLED.py
380 lines (368 loc) · 11.8 KB
/
Grove_OLED.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
374
375
376
377
378
379
380
# A port of SeeedStudio's Library for the 96x96 OLED Display
# http://www.seeedstudio.com/depot/Grove-OLED-Display-096-p-824.html
# http://www.seeedstudio.com/wiki/Grove_-_OLED_Display_96*96
# https://github.com/Seeed-Studio/OLED_Display_96X96
import pyb
import time
from pyb import I2C
i2c = I2C(1, I2C.MASTER)
i2c.init(I2C.MASTER, baudrate=400000)
BasicFont = [
[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
[0x00,0x00,0x5F,0x00,0x00,0x00,0x00,0x00],
[0x00,0x00,0x07,0x00,0x07,0x00,0x00,0x00],
[0x00,0x14,0x7F,0x14,0x7F,0x14,0x00,0x00],
[0x00,0x24,0x2A,0x7F,0x2A,0x12,0x00,0x00],
[0x00,0x23,0x13,0x08,0x64,0x62,0x00,0x00],
[0x00,0x36,0x49,0x55,0x22,0x50,0x00,0x00],
[0x00,0x00,0x05,0x03,0x00,0x00,0x00,0x00],
[0x00,0x1C,0x22,0x41,0x00,0x00,0x00,0x00],
[0x00,0x41,0x22,0x1C,0x00,0x00,0x00,0x00],
[0x00,0x08,0x2A,0x1C,0x2A,0x08,0x00,0x00],
[0x00,0x08,0x08,0x3E,0x08,0x08,0x00,0x00],
[0x00,0xA0,0x60,0x00,0x00,0x00,0x00,0x00],
[0x00,0x08,0x08,0x08,0x08,0x08,0x00,0x00],
[0x00,0x60,0x60,0x00,0x00,0x00,0x00,0x00],
[0x00,0x20,0x10,0x08,0x04,0x02,0x00,0x00],
[0x00,0x3E,0x51,0x49,0x45,0x3E,0x00,0x00],
[0x00,0x00,0x42,0x7F,0x40,0x00,0x00,0x00],
[0x00,0x62,0x51,0x49,0x49,0x46,0x00,0x00],
[0x00,0x22,0x41,0x49,0x49,0x36,0x00,0x00],
[0x00,0x18,0x14,0x12,0x7F,0x10,0x00,0x00],
[0x00,0x27,0x45,0x45,0x45,0x39,0x00,0x00],
[0x00,0x3C,0x4A,0x49,0x49,0x30,0x00,0x00],
[0x00,0x01,0x71,0x09,0x05,0x03,0x00,0x00],
[0x00,0x36,0x49,0x49,0x49,0x36,0x00,0x00],
[0x00,0x06,0x49,0x49,0x29,0x1E,0x00,0x00],
[0x00,0x00,0x36,0x36,0x00,0x00,0x00,0x00],
[0x00,0x00,0xAC,0x6C,0x00,0x00,0x00,0x00],
[0x00,0x08,0x14,0x22,0x41,0x00,0x00,0x00],
[0x00,0x14,0x14,0x14,0x14,0x14,0x00,0x00],
[0x00,0x41,0x22,0x14,0x08,0x00,0x00,0x00],
[0x00,0x02,0x01,0x51,0x09,0x06,0x00,0x00],
[0x00,0x32,0x49,0x79,0x41,0x3E,0x00,0x00],
[0x00,0x7E,0x09,0x09,0x09,0x7E,0x00,0x00],
[0x00,0x7F,0x49,0x49,0x49,0x36,0x00,0x00],
[0x00,0x3E,0x41,0x41,0x41,0x22,0x00,0x00],
[0x00,0x7F,0x41,0x41,0x22,0x1C,0x00,0x00],
[0x00,0x7F,0x49,0x49,0x49,0x41,0x00,0x00],
[0x00,0x7F,0x09,0x09,0x09,0x01,0x00,0x00],
[0x00,0x3E,0x41,0x41,0x51,0x72,0x00,0x00],
[0x00,0x7F,0x08,0x08,0x08,0x7F,0x00,0x00],
[0x00,0x41,0x7F,0x41,0x00,0x00,0x00,0x00],
[0x00,0x20,0x40,0x41,0x3F,0x01,0x00,0x00],
[0x00,0x7F,0x08,0x14,0x22,0x41,0x00,0x00],
[0x00,0x7F,0x40,0x40,0x40,0x40,0x00,0x00],
[0x00,0x7F,0x02,0x0C,0x02,0x7F,0x00,0x00],
[0x00,0x7F,0x04,0x08,0x10,0x7F,0x00,0x00],
[0x00,0x3E,0x41,0x41,0x41,0x3E,0x00,0x00],
[0x00,0x7F,0x09,0x09,0x09,0x06,0x00,0x00],
[0x00,0x3E,0x41,0x51,0x21,0x5E,0x00,0x00],
[0x00,0x7F,0x09,0x19,0x29,0x46,0x00,0x00],
[0x00,0x26,0x49,0x49,0x49,0x32,0x00,0x00],
[0x00,0x01,0x01,0x7F,0x01,0x01,0x00,0x00],
[0x00,0x3F,0x40,0x40,0x40,0x3F,0x00,0x00],
[0x00,0x1F,0x20,0x40,0x20,0x1F,0x00,0x00],
[0x00,0x3F,0x40,0x38,0x40,0x3F,0x00,0x00],
[0x00,0x63,0x14,0x08,0x14,0x63,0x00,0x00],
[0x00,0x03,0x04,0x78,0x04,0x03,0x00,0x00],
[0x00,0x61,0x51,0x49,0x45,0x43,0x00,0x00],
[0x00,0x7F,0x41,0x41,0x00,0x00,0x00,0x00],
[0x00,0x02,0x04,0x08,0x10,0x20,0x00,0x00],
[0x00,0x41,0x41,0x7F,0x00,0x00,0x00,0x00],
[0x00,0x04,0x02,0x01,0x02,0x04,0x00,0x00],
[0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00],
[0x00,0x01,0x02,0x04,0x00,0x00,0x00,0x00],
[0x00,0x20,0x54,0x54,0x54,0x78,0x00,0x00],
[0x00,0x7F,0x48,0x44,0x44,0x38,0x00,0x00],
[0x00,0x38,0x44,0x44,0x28,0x00,0x00,0x00],
[0x00,0x38,0x44,0x44,0x48,0x7F,0x00,0x00],
[0x00,0x38,0x54,0x54,0x54,0x18,0x00,0x00],
[0x00,0x08,0x7E,0x09,0x02,0x00,0x00,0x00],
[0x00,0x18,0xA4,0xA4,0xA4,0x7C,0x00,0x00],
[0x00,0x7F,0x08,0x04,0x04,0x78,0x00,0x00],
[0x00,0x00,0x7D,0x00,0x00,0x00,0x00,0x00],
[0x00,0x80,0x84,0x7D,0x00,0x00,0x00,0x00],
[0x00,0x7F,0x10,0x28,0x44,0x00,0x00,0x00],
[0x00,0x41,0x7F,0x40,0x00,0x00,0x00,0x00],
[0x00,0x7C,0x04,0x18,0x04,0x78,0x00,0x00],
[0x00,0x7C,0x08,0x04,0x7C,0x00,0x00,0x00],
[0x00,0x38,0x44,0x44,0x38,0x00,0x00,0x00],
[0x00,0xFC,0x24,0x24,0x18,0x00,0x00,0x00],
[0x00,0x18,0x24,0x24,0xFC,0x00,0x00,0x00],
[0x00,0x00,0x7C,0x08,0x04,0x00,0x00,0x00],
[0x00,0x48,0x54,0x54,0x24,0x00,0x00,0x00],
[0x00,0x04,0x7F,0x44,0x00,0x00,0x00,0x00],
[0x00,0x3C,0x40,0x40,0x7C,0x00,0x00,0x00],
[0x00,0x1C,0x20,0x40,0x20,0x1C,0x00,0x00],
[0x00,0x3C,0x40,0x30,0x40,0x3C,0x00,0x00],
[0x00,0x44,0x28,0x10,0x28,0x44,0x00,0x00],
[0x00,0x1C,0xA0,0xA0,0x7C,0x00,0x00,0x00],
[0x00,0x44,0x64,0x54,0x4C,0x44,0x00,0x00],
[0x00,0x08,0x36,0x41,0x00,0x00,0x00,0x00],
[0x00,0x00,0x7F,0x00,0x00,0x00,0x00,0x00],
[0x00,0x41,0x36,0x08,0x00,0x00,0x00,0x00],
[0x00,0x02,0x01,0x01,0x02,0x01,0x00,0x00],
[0x00,0x02,0x05,0x05,0x02,0x00,0x00,0x00]
]
VERTICAL_MODE=01
HORIZONTAL_MODE=02
SeeedGrayOLED_Address=0x3c
SeeedGrayOLED_Command_Mode=0x80
SeeedGrayOLED_Data_Mode=0x40
SeeedGrayOLED_Display_Off_Cmd=0xAE
SeeedGrayOLED_Display_On_Cmd=0xAF
SeeedGrayOLED_Normal_Display_Cmd=0xA4
SeeedGrayOLED_Inverse_Display_Cmd=0xA7
SeeedGrayOLED_Activate_Scroll_Cmd=0x2F
SeeedGrayOLED_Dectivate_Scroll_Cmd=0x2E
SeeedGrayOLED_Set_ContrastLevel_Cmd=0x81
Scroll_Left=0x00
Scroll_Right=0x01
Scroll_2Frames=0x7
Scroll_3Frames=0x4
Scroll_4Frames=0x5
Scroll_5Frames=0x0
Scroll_25Frames=0x6
Scroll_64Frames=0x1
Scroll_128Frames=0x2
Scroll_256Frames=0x3
class SeeedGrayOLED():
grayH=0xF0
grayL=0x0F
def __init__(self):
self.sendCommand(0xFD)
# Unlock OLED driver IC MCU interface from entering command. i.e: Accept commands
self.sendCommand(0x12);
self.sendCommand(0xAE)
# Set display off
self.sendCommand(0xA8)
# set multiplex ratio
self.sendCommand(0x5F)
# 96
self.sendCommand(0xA1)
# set display start line
self.sendCommand(0x00);
self.sendCommand(0xA2)
# set display offset
self.sendCommand(0x60);
self.sendCommand(0xA0)
# set remap
self.sendCommand(0x46);
self.sendCommand(0xAB)
# set vdd internal
self.sendCommand(0x01)
#
self.sendCommand(0x81)
# set contrasr
self.sendCommand(0x53)
# 100 nit
self.sendCommand(0xB1)
# Set Phase Length
self.sendCommand(0X51)
#
self.sendCommand(0xB3)
# Set Display Clock Divide Ratio/Oscillator Frequency
self.sendCommand(0x01);
self.sendCommand(0xB9)
#
self.sendCommand(0xBC)
# set pre_charge voltage/VCOMH
self.sendCommand(0x08)
# (0x08);
self.sendCommand(0xBE)
# set VCOMH
self.sendCommand(0X07)
# (0x07);
self.sendCommand(0xB6)
# Set second pre-charge period
self.sendCommand(0x01)
#
self.sendCommand(0xD5)
# enable second precharge and enternal vsl
self.sendCommand(0X62)
# (0x62);
self.sendCommand(0xA4)
# Set Normal Display Mode
self.sendCommand(0x2E)
# Deactivate Scroll
self.sendCommand(0xAF)
# Switch on display
time.sleep(.100)
self.sendCommand(0x75)
#Set Row Address
self.sendCommand(0x00)
#Start 0
self.sendCommand(0x5f)
#End 95
self.sendCommand(0x15)
#Set Column Address
self.sendCommand(0x08)
#Start from 8th Column of driver IC. This is 0th Column for OLED
self.sendCommand(0x37)
#End at (8 + 47)th column. Each Column has 2 pixels(segments)
#Init gray level for text. Default:Brightest White
def sendCommand(self, command):
data = bytearray(2)
data[0]=SeeedGrayOLED_Command_Mode
data[1]=command
i2c.send(data, addr=SeeedGrayOLED_Address)
def setContrastLevel(self, ContrastLevel):
self.sendCommand(SeeedGrayOLED_Set_ContrastLevel_Cmd)
self.sendCommand(ContrastLevel)
def setHorizontalMode(self):
self.sendCommand(0xA0)
# remap to
self.sendCommand(0x42)
# horizontal mode
#Row Address
self.sendCommand(0x75)
# Set Row Address
self.sendCommand(0x00)
# Start 0
self.sendCommand(0x5f)
# End 95
#Column Address
self.sendCommand(0x15)
# Set Column Address
self.sendCommand(0x08)
# Start from 8th Column of driver IC. This is 0th Column for OLED
self.sendCommand(0x37)
# End at (8 + 47)th column. Each Column has 2 pixels(or segments)
def setVerticalMode(self):
self.sendCommand(0xA0)
#remap to Vertical mode
self.sendCommand(0x46)
def setTextXY(self, Row, Column):
#Column Address
self.sendCommand(0x15)
#Set Column Address
self.sendCommand(0x08+(Column*4))
#Start Column: Start from 8
self.sendCommand(0x37)
#End Column
#Row Address
self.sendCommand(0x75)
#Set Row Address
self.sendCommand(0x00+(Row*8))
#Start Row
self.sendCommand(0x07+(Row*8))
#End Row
def clearDisplay(self):
for j in range(0, 48):
for i in range(0, 96):
#clear all columns
self.sendData([0x00])
def sendData(self, toSend):
ln=len(toSend)
data = bytearray(ln+1)
data[0]=SeeedGrayOLED_Data_Mode
for i in range(0, ln):
data[i+1]=toSend[i]
i2c.send(data, addr=SeeedGrayOLED_Address)
def setGrayLevel(self, grayLevel):
self.grayH = (grayLevel << 4) & 0xF0
self.grayL = grayLevel & 0x0F
def putChar(self, C):
if ord(C) < 32 or ord(C) > 127:
#Ignore non-printable ASCII characters. This can be modified for multilingual font.
C=' '
D=ord(C)
for i in [0, 2, 4, 6]:
for j in range(0, 8):
#Character is constructed two pixel at a time using vertical mode from the default 8x8 font
c=0x00
bit1=(BasicFont[D-32][i] >> j) & 0x01
bit2=(BasicFont[D-32][i+1] >> j) & 0x01
#Each bit is changed to a nibble
if(bit1>0):
c|=self.grayH
if(bit2>0):
c|=self.grayL
self.sendData([c])
def putString(self, s):
for i in s:
self.putChar(i)
def putNumber(self, long_num):
char_buffer=[0]*10
i = 0
f = 0
if long_num < 0:
f=1
self.putChar('-')
long_num = -long_num
elif long_num == 0:
f=1
self.putChar('0')
return f
while (long_num > 0):
char_buffer[i] = long_num % 10
i+=1
long_num /= 10
f=f+i
while(i>0):
self.putChar(48+char_buffer[i - 1])
i-=1
return f
def setNormalDisplay(self):
self.sendCommand(SeeedGrayOLED_Normal_Display_Cmd)
def setInverseDisplay(self):
self.sendCommand(SeeedGrayOLED_Inverse_Display_Cmd)
def activateScroll(self):
self.sendCommand(SeeedGrayOLED_Activate_Scroll_Cmd)
def deactivateScroll(self):
self.sendCommand(SeeedGrayOLED_Dectivate_Scroll_Cmd)
def drawBitmap(self, bitmaparray, bytes):
localAddressMode = addressingMode
if(localAddressMode != HORIZONTAL_MODE):
#Bitmap is drawn in horizontal mode
setHorizontalMode()
for i in range (0, bytes):
for j in [0, 2, 4, 6]:
c=0x00
bit1=bitmaparray[i] << j & 0x80
bit2=bitmaparray[i] << (j+1) & 0x80
#Each bit is changed to a nibble
if(bit1>0):
c|=self.grayH
if(bit2>0):
c|=self.grayL
self.sendData(c)
if(localAddressMode == VERTICAL_MODE):
#If Vertical Mode was used earlier, restore it.
setVerticalMode()
def setHorizontalScrollProperties(direction, startRow, endRow, startColumn, endColumn, scrollSpeed):
#Use the following defines for 'direction':
# Scroll_Left
# Scroll_Right
#Use the following defines for 'scrollSpeed':
# Scroll_2Frames
# Scroll_3Frames
# Scroll_4Frames
# Scroll_5Frames
# Scroll_25Frames
# Scroll_64Frames
# Scroll_128Frames
# Scroll_256Frames
if(Scroll_Right == direction):
self.sendCommand(0x27)
else:
self.sendCommand(0x26)
#Dummmy byte
self.sendCommand(0x00)
self.sendCommand(startRow)
self.sendCommand(scrollSpeed)
self.sendCommand(endRow)
self.sendCommand(startColumn+8)
self.sendCommand(endColumn+8)
#Dummmy byte
self.sendCommand(0x00)
OLED=SeeedGrayOLED()
#Initialize Object
OLED.clearDisplay()
OLED.setNormalDisplay()
OLED.setVerticalMode()
for i in range(0, 12):
OLED.setTextXY(i,0)
OLED.setGrayLevel(i)
OLED.putString("Hello World")