]>
vault307.fbx.one Git - PicoTamachibi.git/blob - ssd1306.py
1 # MicroPython SSD1306 OLED driver, I2C and SPI interfaces
3 from micropython
import const
8 SET_CONTRAST
= const(0x81)
9 SET_ENTIRE_ON
= const(0xA4)
10 SET_NORM_INV
= const(0xA6)
11 SET_DISP
= const(0xAE)
12 SET_MEM_ADDR
= const(0x20)
13 SET_COL_ADDR
= const(0x21)
14 SET_PAGE_ADDR
= const(0x22)
15 SET_DISP_START_LINE
= const(0x40)
16 SET_SEG_REMAP
= const(0xA0)
17 SET_MUX_RATIO
= const(0xA8)
18 SET_COM_OUT_DIR
= const(0xC0)
19 SET_DISP_OFFSET
= const(0xD3)
20 SET_COM_PIN_CFG
= const(0xDA)
21 SET_DISP_CLK_DIV
= const(0xD5)
22 SET_PRECHARGE
= const(0xD9)
23 SET_VCOM_DESEL
= const(0xDB)
24 SET_CHARGE_PUMP
= const(0x8D)
26 # Subclassing FrameBuffer provides support for graphics primitives
27 # http://docs.micropython.org/en/latest/pyboard/library/framebuf.html
28 class SSD1306(framebuf
.FrameBuffer
):
29 def __init__(self
, width
, height
, external_vcc
):
32 self
.external_vcc
= external_vcc
33 self
.pages
= self
.height
// 8
34 self
.buffer = bytearray(self
.pages
* self
.width
)
35 super().__init
__(self
.buffer, self
.width
, self
.height
, framebuf
.MONO_VLSB
)
38 def init_display(self
):
40 SET_DISP |
0x00, # off
44 # resolution and layout
45 SET_DISP_START_LINE |
0x00,
46 SET_SEG_REMAP |
0x01, # column addr 127 mapped to SEG0
49 SET_COM_OUT_DIR |
0x08, # scan from COM[N] to COM0
53 0x02 if self
.width
> 2 * self
.height
else 0x12,
54 # timing and driving scheme
58 0x22 if self
.external_vcc
else 0xF1,
64 SET_ENTIRE_ON
, # output follows RAM contents
65 SET_NORM_INV
, # not inverted
68 0x10 if self
.external_vcc
else 0x14,
76 self
.write_cmd(SET_DISP |
0x00)
79 self
.write_cmd(SET_DISP |
0x01)
81 def contrast(self
, contrast
):
82 self
.write_cmd(SET_CONTRAST
)
83 self
.write_cmd(contrast
)
85 def invert(self
, invert
):
86 self
.write_cmd(SET_NORM_INV |
(invert
& 1))
92 # displays with width of 64 pixels are shifted by 32
95 self
.write_cmd(SET_COL_ADDR
)
98 self
.write_cmd(SET_PAGE_ADDR
)
100 self
.write_cmd(self
.pages
- 1)
101 self
.write_data(self
.buffer)
104 class SSD1306_I2C(SSD1306
):
105 def __init__(self
, width
, height
, i2c
, addr
=0x3C, external_vcc
=False):
108 self
.temp
= bytearray(2)
109 self
.write_list
= [b
"\x40", None] # Co=0, D/C#=1
110 super().__init
__(width
, height
, external_vcc
)
112 def write_cmd(self
, cmd
):
113 self
.temp
[0] = 0x80 # Co=1, D/C#=0
115 self
.i2c
.writeto(self
.addr
, self
.temp
)
117 def write_data(self
, buf
):
118 self
.write_list
[1] = buf
119 self
.i2c
.writevto(self
.addr
, self
.write_list
)
122 class SSD1306_SPI(SSD1306
):
123 def __init__(self
, width
, height
, spi
, dc
, res
, cs
, external_vcc
=False):
124 self
.rate
= 10 * 1024 * 1024
125 dc
.init(dc
.OUT
, value
=0)
126 res
.init(res
.OUT
, value
=0)
127 cs
.init(cs
.OUT
, value
=1)
139 super().__init
__(width
, height
, external_vcc
)
141 def write_cmd(self
, cmd
):
142 self
.spi
.init(baudrate
=self
.rate
, polarity
=0, phase
=0)
146 self
.spi
.write(bytearray([cmd
]))
149 def write_data(self
, buf
):
150 self
.spi
.init(baudrate
=self
.rate
, polarity
=0, phase
=0)