/
oled.py
489 lines (437 loc) · 16.6 KB
/
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
from smbus2 import SMBus
from smbus2 import SMBusWrapper
from lib.logging import *
import time
import xbmcgui
import gpio
import spi
CHARGEPUMP = 0x8D
COLUMNADDR = 0x21
COMSCANDEC = 0xC8
COMSCANINC = 0xC0
DISPLAYALLON = 0xA5
DISPLAYALLON_RESUME = 0xA4
DISPLAYOFF = 0xAE
DISPLAYON = 0xAF
EXTERNALVCC = 0x1
INVERTDISPLAY = 0xA7
MEMORYMODE = 0x20
NORMALDISPLAY = 0xA6
PAGEADDR = 0x22
SEGREMAP = 0xA0
SETCOMPINS = 0xDA
SETCONTRAST = 0x81
SETDISPLAYCLOCKDIV = 0xD5
SETDISPLAYOFFSET = 0xD3
SETHIGHCOLUMN = 0x10
SETLOWCOLUMN = 0x00
SETMULTIPLEX = 0xA8
SETPRECHARGE = 0xD9
SETSEGMENTREMAP = 0xA1
SETSTARTLINE = 0x40
SETVCOMDETECT = 0xDB
SWITCHCAPVCC = 0x2
BUSNUMBER = 2
class Oled:
def __init__(self, i2c, displayType, flipDisplay):
self._width = 128
self._height = 64
self._i2c = i2c
self._flipDisplay = flipDisplay
self._displayType = displayType
self._pages = self._height // 8
self._image = [0] * (self._width * self._height)
self.bus = None
if self._displayType != "128x64-ssd1309 spi":
try:
self.bus = SMBus(BUSNUMBER)
except IOError:
xbmcgui.Dialog().notification("OLED IO Error", "Please check your I2C address and controller type.", xbmcgui.NOTIFICATION_ERROR, 5000)
else:
self.spi= spi.SPI("/dev/spidev32766.0")
self.spi.mode = spi.SPI.MODE_0
self.spi.bits_per_word = 8
self.spi.speed = 5000000
gpio.initGPIO()
gpio.gpioWriteDC(0)
gpio.gpioDoReset()
if self._displayType == "128x64-sh1106":
self.displayHeight32 = False
self._initSH1106()
self.display = self._displaySH1106
elif self._displayType == "128x64-ssd1306":
self.displayHeight32 = False
self._initSSD1306_64()
self.display = self._displaySSD1306
elif self._displayType == "128x32-ssd1306":
self.displayHeight32 = True
self._initSSD1306_32()
self.display = self._displaySSD1306
elif self._displayType == "128x64-ssd1309 spi":
self.displayHeight32 = False
self._initSSD1309SPI()
self.display = self._displaySSD1309SPI
self.clear()
self.display()
def isDisplayHeight32(self):
return self.displayHeight32
def _command(self, c):
try:
self.bus.write_byte_data(self._i2c, 0x00, c)
except:
pass
def _commandSPI(self, val):
self.spi.transfer([val])
def _initSH1106(self):
self._command(DISPLAYOFF)
self._command(NORMALDISPLAY)
self._command(SETDISPLAYCLOCKDIV)
self._command(0x80)
self._command(SETMULTIPLEX)
self._command(0x3F)
self._command(SETDISPLAYOFFSET)
self._command(0x00)
self._command(SETSTARTLINE | 0x00)
self._command(CHARGEPUMP)
self._command(0x14)
self._command(MEMORYMODE)
self._command(0x00)
if (self._flipDisplay):
self._command(SEGREMAP)
self._command(SETCOMPINS)
self._command(PAGEADDR)
self._command(COMSCANINC)
else:
self._command(SEGREMAP | 0x1)
self._command(SETCOMPINS)
self._command(PAGEADDR)
self._command(COMSCANDEC)
self._command(SETCONTRAST)
self._command(0xCF)
self._command(SETPRECHARGE)
self._command(0xF1)
self._command(SETVCOMDETECT)
self._command(0x00)
self._command(DISPLAYALLON_RESUME)
self._command(NORMALDISPLAY)
self._command(0x2e)
self._command(DISPLAYON)
def _initSSD1306_64(self):
self._command(DISPLAYOFF)
self._command(SETDISPLAYCLOCKDIV)
self._command(0x80)
self._command(SETMULTIPLEX)
self._command(0x3F)
self._command(SETDISPLAYOFFSET)
self._command(0x0)
self._command(SETSTARTLINE | 0x0)
self._command(CHARGEPUMP)
self._command(0x14) # 0x14 0x2f
self._command(MEMORYMODE)
self._command(0x00)
if (self._flipDisplay):
self._command(SEGREMAP)
self._command(COMSCANINC)
else:
self._command(SEGREMAP | 0x1)
self._command(COMSCANDEC)
self._command(SETCOMPINS)
self._command(0x12)
self._command(SETCONTRAST)
self._command(0xCF)
self._command(SETPRECHARGE)
self._command(0xF1)
self._command(SETVCOMDETECT)
self._command(0x00) # 0x40 0x00
self._command(DISPLAYALLON_RESUME)
self._command(NORMALDISPLAY)
self._command(DISPLAYON)
def _initSSD1309SPI(self):
gpio.gpioWriteDC(0)
self._commandSPI(DISPLAYOFF)
self._commandSPI(SETDISPLAYCLOCKDIV)
self._commandSPI(0x80)
self._commandSPI(SETMULTIPLEX)
self._commandSPI(0x3F)
self._commandSPI(SETDISPLAYOFFSET)
self._commandSPI(0x0)
self._commandSPI(SETSTARTLINE | 0x0)
self._commandSPI(CHARGEPUMP)
self._commandSPI(0x14) # 0x14 0x2f
self._commandSPI(MEMORYMODE)
self._commandSPI(0x00)
if (self._flipDisplay):
self._commandSPI(SEGREMAP)
self._commandSPI(COMSCANINC)
else:
self._commandSPI(SEGREMAP | 0x1)
self._commandSPI(COMSCANDEC)
self._commandSPI(SETCOMPINS)
self._commandSPI(0x12)
self._commandSPI(SETCONTRAST)
self._commandSPI(0xCF) #0xCF
self._commandSPI(SETPRECHARGE)
self._commandSPI(0x22) #0xF1
self._commandSPI(SETVCOMDETECT)
self._commandSPI(0x00) # 0x40 0x00
self._commandSPI(DISPLAYALLON_RESUME)
self._commandSPI(NORMALDISPLAY)
self._commandSPI(DISPLAYON)
def _initSSD1306_32(self):
self._command(DISPLAYOFF)
self._command(SETDISPLAYCLOCKDIV)
self._command(0x80)
self._command(SETMULTIPLEX)
self._command(0x1F)
self._command(SETDISPLAYOFFSET)
self._command(0x0)
self._command(SETSTARTLINE | 0x0)
self._command(CHARGEPUMP)
self._command(0x14) # 0x14 0x2f
self._command(MEMORYMODE)
self._command(0x00)
if (self._flipDisplay):
self._command(SEGREMAP)
self._command(COMSCANINC)
else:
self._command(SEGREMAP | 0x1)
self._command(COMSCANDEC)
self._command(SETCOMPINS)
self._command(0x02)
self._command(SETCONTRAST)
self._command(0xCF)
self._command(SETPRECHARGE)
self._command(0xF1)
self._command(SETVCOMDETECT)
self._command(0x00) # 0x40 0x00
self._command(DISPLAYALLON_RESUME)
self._command(NORMALDISPLAY)
self._command(DISPLAYON)
def _displaySH1106(self):
page = 0xB0
step = self._width * 8
for y in xrange(0, step * 8, step):
self._command(page)
self._command(0x02)
self._command(0x10)
page += 1
buffer = []
for x in xrange(self._width):
byte = 0
for n in xrange(0, step, self._width):
byte |= (self._image[x + y + n] & 0x01) << 8
byte >>= 1
buffer.append(byte)
# Write buffer data
for i in xrange(0, len(buffer), 16):
with SMBusWrapper(2) as bus:
bus.write_i2c_block_data(self._i2c, 0x40, buffer[i:i+16])
def _displaySSD1306(self):
buffer = []
for page in xrange(self._pages):
for x in xrange(self._width):
bits = 0
for bit in [0, 1, 2, 3, 4, 5, 6, 7]:
bits = bits << 1
bits |= self._image[x + ((page * 8 + 7 - bit) * self._width)]
buffer.append(bits)
self._command(COLUMNADDR)
self._command(0) # Column start address. (0 = reset)
self._command(self._width - 1) # Column end address.
self._command(PAGEADDR)
self._command(0) # Page start address. (0 = reset)
self._command(self._pages - 1) # Page end address.
# Write buffer data
for i in xrange(0, len(buffer), 16):
with SMBusWrapper(2) as bus:
bus.write_i2c_block_data(self._i2c, 0x40, buffer[i:i+16])
def _displaySSD1309SPI(self):
buffer = []
for page in xrange(self._pages):
for x in xrange(self._width):
bits = 0
for bit in [0, 1, 2, 3, 4, 5, 6, 7]:
bits = bits << 1
bits |= self._image[x + ((page * 8 + 7 - bit) * self._width)]
buffer.append(bits)
gpio.gpioWriteDC(0)
self._commandSPI(COLUMNADDR)
self._commandSPI(0) # Column start address. (0 = reset)
self._commandSPI(self._width - 1) # Column end address.
self._commandSPI(PAGEADDR)
self._commandSPI(0) # Page start address. (0 = reset)
self._commandSPI(self._pages - 1) # Page end address.
# Write buffer data
gpio.gpioWriteDC(1)
for i in xrange(0, len(buffer), 8):
self.spi.transfer(buffer[i:i+8])
def clear(self):
for i in xrange(self._width * self._height):
self._image[i] = 0
def close(self):
self.clear()
self.display()
try:
self.bus.close()
except:
pass
def setBrightness(self, brightness):
if self._displayType == "128x64-ssd1309 spi":
gpio.gpioWriteDC(0)
self._commandSPI(SETCONTRAST)
if brightness == 16:
self._commandSPI(255)
else:
self._commandSPI((brightness * 16) - 16)
else:
self._command(SETCONTRAST)
if brightness == 16:
self._command(255)
else:
self._command((brightness * 16) - 16)
def getStringWidth(self, str, charset):
charStride = charset[0][3]
dashFix = 0
for c in str:
char = ord(c)
if char == 45:
dashFix += -3
if char == 46:
dashFix += -4
return len(str) * charStride + dashFix + 3
def drawIcons(self, info, x, y, center, solid, charset):
if (center):
width = 0
for i in xrange(0, len(info)):
w = self.getStringWidth(info[i], charset)
width += w + 4
x = ((128 - width) // 2) + 4
for i in xrange(0, len(info)):
pos = self.drawString(
info[i], x, y, solid, charset)
x += pos + 4
def drawString(self, str, x, y, solid, charset):
charWidth = charset[0][0]
charHeight = charset[0][1]
charByteHeight = charset[0][2]
charStride = charset[0][3]
strWidth = len(str) * charStride - 1
px = x
py = y
if solid:
border = False
for by in xrange(y - 2, y + 1 + charHeight + 1):
for bx in xrange(x - 2, x + strWidth + 2):
self._image[bx + (by * self._width)
] = 0
else:
border = True
dashFix = 0
for c in str:
char = ord(c)
if char == 32:
px += charStride
if char == 45:
self._image[px + (py + 3) * self._width] = 1
self._image[px + 1 + (py + 3) * self._width] = 1
px += 3
dashFix += -3
if char == 46:
self._image[px + (py + 6) * self._width] = 1
px += 2
dashFix += -4
if char >= 48 and char <= 57:
self._drawChar(char - 47, px, py, charWidth,
charByteHeight, charset)
px += charStride
elif char >= 65 and char <= 90:
self._drawChar(char - 54, px, py, charWidth,
charByteHeight, charset)
px += charStride
if border:
for bx in xrange(x - 2, x + strWidth + 2 + dashFix):
self._image[bx + ((y - 3) * self._width)] = 1
self._image[bx + ((y + 2 + charHeight) * self._width)] = 1
for by in xrange(y - 2, y + 2 + charHeight):
self._image[x - 3 + (by * self._width)] = 1
self._image[x + 2 + strWidth + dashFix + (by * self._width)] = 1
if solid:
for by in xrange(y - 2, y + 1 + charHeight + 1):
for bx in xrange(x - 2, x + strWidth + 2 + dashFix):
self._image[bx + (by * self._width)
] = not self._image[bx + (by * self._width)]
self._image[x - 2 +
((y - 2) * self._width)] = 0
self._image[x - 2 +
((y + charHeight + 1) * self._width)] = 0
self._image[x + strWidth + 1 + dashFix +
((y - 2) * self._width)] = 0
self._image[x + strWidth + 1 + dashFix +
((y + charHeight + 1) * self._width)] = 0
return len(str) * charStride + dashFix + 3
def _drawChar(self, char, x, y, cw, cbh, chset):
for sx in xrange(0, cw):
for sy in xrange(0, cbh):
dy = y
chdata = chset[char][sx + sy * cw]
for bit in [0, 1, 2, 3, 4, 5, 6, 7]:
self._image[(sx + x) + ((8 * sy) + dy) *
self._width] = (chdata >> bit) & 0x01
dy += 1
def drawProgress(self, seconds, totalSeconds):
if self.displayHeight32:
y = 31
else:
y = 56
for py in xrange(y - 3, y + 4):
self._image[10 + (py * self._width)] = 1
self._image[117 + (py * self._width)] = 1
if seconds > 0 and totalSeconds > 0:
progress = float(float(seconds) / float(totalSeconds) * 107.0)
for x in range(107):
if x <= progress:
self._image[(x + 10) + ((y - 1) * self._width)] = 1
self._image[(x + 10) + (y * self._width)] = 1
self._image[(x + 10) + ((y + 1) * self._width)] = 1
else:
self._image[(x + 10) + ((y - 1) * self._width)] = 0
self._image[(x + 10) + (y * self._width)] = 0
self._image[(x + 10) + ((y + 1) * self._width)] = 0
def drawTime(self, seconds, x, y, charset, fullsize=True, center=True):
hours = seconds // 3600
seconds %= 3600
minutes = seconds // 60
seconds = seconds % 60
cw = charset[11][0]
ch = charset[11][1]
digit_stride = charset[11][2]
colon_stride = charset[11][3]
if fullsize and center:
w = (digit_stride * 4) + colon_stride + colon_stride + cw
x = (128 - w) / 2
elif not fullsize and center:
w = (digit_stride * 3) + colon_stride + cw
x = (128 - w) / 2
pos = x
if fullsize:
self._drawChar(hours, pos, y, cw, ch, charset) # hours
pos += digit_stride
self._drawChar(10, pos, y, cw, ch, charset) # :
pos += colon_stride
self._drawChar(minutes // 10, pos, y, cw, ch, charset) # min 10
pos += digit_stride
self._drawChar(minutes % 10, pos, y, cw, ch, charset) # min 1
pos += digit_stride
self._drawChar(10, pos, y, cw, ch, charset) # :
pos += colon_stride
self._drawChar(seconds // 10, pos, y, cw, ch, charset) # sec 10
pos += digit_stride
self._drawChar(seconds % 10, pos, y, cw, ch, charset) # sec 1
def drawTrack(self, track, x, y, charset):
cw = charset[11][0]
ch = charset[11][1]
digit_stride = charset[11][2]
pos = x
self._drawChar(track // 10, pos, y, cw, ch, charset) # Track 10
pos += digit_stride
self._drawChar(track % 10, pos, y, cw, ch, charset) # Track 1