This repository was archived by the owner on May 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssd1306_i2c.py
More file actions
59 lines (47 loc) · 2.01 KB
/
ssd1306_i2c.py
File metadata and controls
59 lines (47 loc) · 2.01 KB
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
import framebuf
class Display:
_byte = bytearray(1)
_word = bytearray(2)
def __init__(self, i2c, address=0x3c, width=128, height=64):
self._i2c = i2c
self._address = address
self.width = width
self.height = height
pages = height // 8
self._command = bytearray(b'\x21\x00\x7f\x22\x00\x0f')
self._command[2] = width - 1
if width == 64:
self._command[1] += 32
self._command[2] += 32
self._command[5] = pages - 1
self._i2c.writeto_mem(self._address, 0x00, b'\xae\x20\x00\x40\x00\xa1'
b'\xc8\xd3\x00\xd5\x80\xd9\xf1\xdb\x30\x8d\x14'
b'\x81\xff\xa4\xa6')
self._word[0] = 0xa8
self._word[1] = height - 1
self._i2c.writeto_mem(self._address, 0x00, self._word)
self._word[0] = 0xda
self._word[1] = 0x02 if height == 32 else 0x12
self._i2c.writeto_mem(self._address, 0x00, self._word)
self.active(True)
buffer = bytearray(width * pages)
self.fb = framebuf.FrameBuffer(buffer, width, height, framebuf.MVLSB)
self._buffer = buffer
def active(self, val):
self._i2c.writeto_mem(self._address, 0x00, b'\xaf' if val else b'\xae')
def inverse(self, val):
self._i2c.writeto_mem(self._address, 0x00, b'\xa7' if val else b'\xa6')
def vscroll(self, dy):
self._byte[0] = 0x40 | dy & 0x3f
self._i2c.writeto_mem(self._address, 0x00, self._byte)
def flip(self, val):
self._i2c.writeto_mem(self._address, 0x00, b'\xc0' if val else b'\xc8')
def mirror(self, val):
self._i2c.writeto_mem(self._address, 0x00, b'\xa0' if val else b'\xa1')
def contrast(self, val):
self._word[0] = 0x81
self._word[1] = val & 0xff
self._i2c.writeto_mem(self._address, 0x00, self._word)
def update(self):
self._i2c.writeto_mem(self._address, 0x00, self._command)
self._i2c.writeto_mem(self._address, 0x40, self._buffer)