1# MicroPython SSD1306 OLED driver, I2C and SPI interfaces
2
3from micropython import const
4import framebuf
5
6
7# register definitions
8SET_CONTRAST = const(0x81)
9SET_ENTIRE_ON = const(0xA4)
10SET_NORM_INV = const(0xA6)
11SET_DISP = const(0xAE)
12SET_MEM_ADDR = const(0x20)
13SET_COL_ADDR = const(0x21)
14SET_PAGE_ADDR = const(0x22)
15SET_DISP_START_LINE = const(0x40)
16SET_SEG_REMAP = const(0xA0)
17SET_MUX_RATIO = const(0xA8)
18SET_IREF_SELECT = const(0xAD)
19SET_COM_OUT_DIR = const(0xC0)
20SET_DISP_OFFSET = const(0xD3)
21SET_COM_PIN_CFG = const(0xDA)
22SET_DISP_CLK_DIV = const(0xD5)
23SET_PRECHARGE = const(0xD9)
24SET_VCOM_DESEL = const(0xDB)
25SET_CHARGE_PUMP = const(0x8D)
26
27# Subclassing FrameBuffer provides support for graphics primitives
28# http://docs.micropython.org/en/latest/pyboard/library/framebuf.html
29class SSD1306(framebuf.FrameBuffer):
30    def __init__(self, width, height, external_vcc):
31        self.width = width
32        self.height = height
33        self.external_vcc = external_vcc
34        self.pages = self.height // 8
35        self.buffer = bytearray(self.pages * self.width)
36        super().__init__(self.buffer, self.width, self.height, framebuf.MONO_VLSB)
37        self.init_display()
38
39    def init_display(self):
40        for cmd in (
41            SET_DISP,  # display off
42            # address setting
43            SET_MEM_ADDR,
44            0x00,  # horizontal
45            # resolution and layout
46            SET_DISP_START_LINE,  # start at line 0
47            SET_SEG_REMAP | 0x01,  # column addr 127 mapped to SEG0
48            SET_MUX_RATIO,
49            self.height - 1,
50            SET_COM_OUT_DIR | 0x08,  # scan from COM[N] to COM0
51            SET_DISP_OFFSET,
52            0x00,
53            SET_COM_PIN_CFG,
54            0x02 if self.width > 2 * self.height else 0x12,
55            # timing and driving scheme
56            SET_DISP_CLK_DIV,
57            0x80,
58            SET_PRECHARGE,
59            0x22 if self.external_vcc else 0xF1,
60            SET_VCOM_DESEL,
61            0x30,  # 0.83*Vcc
62            # display
63            SET_CONTRAST,
64            0xFF,  # maximum
65            SET_ENTIRE_ON,  # output follows RAM contents
66            SET_NORM_INV,  # not inverted
67            SET_IREF_SELECT,
68            0x30,  # enable internal IREF during display on
69            # charge pump
70            SET_CHARGE_PUMP,
71            0x10 if self.external_vcc else 0x14,
72            SET_DISP | 0x01,  # display on
73        ):  # on
74            self.write_cmd(cmd)
75        self.fill(0)
76        self.show()
77
78    def poweroff(self):
79        self.write_cmd(SET_DISP)
80
81    def poweron(self):
82        self.write_cmd(SET_DISP | 0x01)
83
84    def contrast(self, contrast):
85        self.write_cmd(SET_CONTRAST)
86        self.write_cmd(contrast)
87
88    def invert(self, invert):
89        self.write_cmd(SET_NORM_INV | (invert & 1))
90
91    def rotate(self, rotate):
92        self.write_cmd(SET_COM_OUT_DIR | ((rotate & 1) << 3))
93        self.write_cmd(SET_SEG_REMAP | (rotate & 1))
94
95    def show(self):
96        x0 = 0
97        x1 = self.width - 1
98        if self.width != 128:
99            # narrow displays use centred columns
100            col_offset = (128 - self.width) // 2
101            x0 += col_offset
102            x1 += col_offset
103        self.write_cmd(SET_COL_ADDR)
104        self.write_cmd(x0)
105        self.write_cmd(x1)
106        self.write_cmd(SET_PAGE_ADDR)
107        self.write_cmd(0)
108        self.write_cmd(self.pages - 1)
109        self.write_data(self.buffer)
110
111
112class SSD1306_I2C(SSD1306):
113    def __init__(self, width, height, i2c, addr=0x3C, external_vcc=False):
114        self.i2c = i2c
115        self.addr = addr
116        self.temp = bytearray(2)
117        self.write_list = [b"\x40", None]  # Co=0, D/C#=1
118        super().__init__(width, height, external_vcc)
119
120    def write_cmd(self, cmd):
121        self.temp[0] = 0x80  # Co=1, D/C#=0
122        self.temp[1] = cmd
123        self.i2c.writeto(self.addr, self.temp)
124
125    def write_data(self, buf):
126        self.write_list[1] = buf
127        self.i2c.writevto(self.addr, self.write_list)
128
129
130class SSD1306_SPI(SSD1306):
131    def __init__(self, width, height, spi, dc, res, cs, external_vcc=False):
132        self.rate = 10 * 1024 * 1024
133        dc.init(dc.OUT, value=0)
134        res.init(res.OUT, value=0)
135        cs.init(cs.OUT, value=1)
136        self.spi = spi
137        self.dc = dc
138        self.res = res
139        self.cs = cs
140        import time
141
142        self.res(1)
143        time.sleep_ms(1)
144        self.res(0)
145        time.sleep_ms(10)
146        self.res(1)
147        super().__init__(width, height, external_vcc)
148
149    def write_cmd(self, cmd):
150        self.spi.init(baudrate=self.rate, polarity=0, phase=0)
151        self.cs(1)
152        self.dc(0)
153        self.cs(0)
154        self.spi.write(bytearray([cmd]))
155        self.cs(1)
156
157    def write_data(self, buf):
158        self.spi.init(baudrate=self.rate, polarity=0, phase=0)
159        self.cs(1)
160        self.dc(1)
161        self.cs(0)
162        self.spi.write(buf)
163        self.cs(1)
164