|
| 1 | +from micropython import const |
| 2 | +from machine import Pin |
| 3 | +from utime import sleep_ms |
| 4 | +import framebuf |
| 5 | + |
| 6 | + |
| 7 | +disp_width = const(128) |
| 8 | +disp_height = const(64) |
| 9 | +disp_pages = const((disp_height >> 3)) |
| 10 | + |
| 11 | +# Constants |
| 12 | +SSD1309_SET_CONTRAST = const(0x81) |
| 13 | +SSD1309_DISPLAY_ALL_ON_RESUME = const(0xA4) |
| 14 | +SSD1309_DISPLAY_ALL_ON = const(0xA5) |
| 15 | +SSD1309_NORMAL_DISPLAY = const(0xA6) |
| 16 | +SSD1309_INVERT_DISPLAY = const(0xA7) |
| 17 | +SSD1309_DISPLAY_OFF = const(0xAE) |
| 18 | +SSD1309_DISPLAY_ON = const(0xAF) |
| 19 | +SSD1309_SET_DISPLAY_OFFSET = const(0xD3) |
| 20 | +SSD1309_SET_COM_PINS = const(0xDA) |
| 21 | +SSD1309_SET_VCOM_DETECT = const(0xDB) |
| 22 | +SSD1309_SET_DISPLAY_CLOCK_DIV = const(0xD5) |
| 23 | +SSD1309_SET_PRECHARGE = const(0xD9) |
| 24 | +SSD1309_SET_MULTIPLEX = const(0xA8) |
| 25 | +SSD1309_SET_LOW_COLUMN = const(0x00) |
| 26 | +SSD1309_SET_HIGH_COLUMN = const(0x10) |
| 27 | +SSD1309_SET_START_LINE = const(0x40) |
| 28 | +SSD1309_MEMORY_MODE = const(0x20) |
| 29 | +SSD1309_COLUMN_ADDR = const(0x21) |
| 30 | +SSD1309_PAGE_ADDR = const(0x22) |
| 31 | +SSD1309_COM_SCAN_INC = const(0xC0) |
| 32 | +SSD1309_COM_SCAN_DEC = const(0xC8) |
| 33 | +SSD1309_SEG_REMAP = const(0xA0) |
| 34 | +SSD1309_CHARGE_PUMP = const(0x8D) |
| 35 | +SSD1309_EXTERNAL_VCC = const(0x01) |
| 36 | +SSD1309_SWITCH_CAP_VCC = const(0x02) |
| 37 | + |
| 38 | +# Scrolling constants |
| 39 | +SSD1309_ACTIVATE_SCROLL = const(0x2F) |
| 40 | +SSD1309_DEACTIVATE_SCROLL = const(0x2E) |
| 41 | +SSD1309_SET_VERTICAL_SCROLL_AREA = const(0xA3) |
| 42 | +SSD1309_RIGHT_HORIZONTAL_SCROLL = const(0x26) |
| 43 | +SSD1309_LEFT_HORIZONTAL_SCROLL = const(0x27) |
| 44 | +SSD1309_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL = const(0x29) |
| 45 | +SSD1309_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL = const(0x2A) |
| 46 | + |
| 47 | +LOW = const(0) |
| 48 | +HIGH = const(1) |
| 49 | + |
| 50 | +CMD = LOW |
| 51 | +DAT = HIGH |
| 52 | + |
| 53 | + |
| 54 | +class OLED1309(framebuf.FrameBuffer): |
| 55 | + |
| 56 | + def __init__(self, _spi, _dc, _rst, _cs): |
| 57 | + self.width = disp_width |
| 58 | + self.height = disp_height |
| 59 | + self._pages = disp_pages |
| 60 | + |
| 61 | + self.WHITE = 1 |
| 62 | + self.BLACK = 0 |
| 63 | + |
| 64 | + self.spi = _spi |
| 65 | + self.cs = Pin(_cs, Pin.OUT) |
| 66 | + self.rst = Pin(_rst, Pin.OUT) |
| 67 | + self.dc = Pin(_dc, Pin.OUT) |
| 68 | + |
| 69 | + self.buffer = bytearray(self._pages * self.width) |
| 70 | + super().__init__(self.buffer, self.width, self.height, framebuf.MONO_VLSB) |
| 71 | + self.init_display() |
| 72 | + |
| 73 | + |
| 74 | + def write_command(self, value): |
| 75 | + self.dc.value(CMD) |
| 76 | + self.cs.value(LOW) |
| 77 | + self.spi.write(bytearray([value])) |
| 78 | + self.cs.value(HIGH) |
| 79 | + |
| 80 | + |
| 81 | + def reset(self): |
| 82 | + self.rst.value(HIGH) |
| 83 | + sleep_ms(1) |
| 84 | + self.rst.value(LOW) |
| 85 | + sleep_ms(10) |
| 86 | + self.rst.value(HIGH) |
| 87 | + |
| 88 | + |
| 89 | + def init_display(self): |
| 90 | + self.reset() |
| 91 | + |
| 92 | + self.write_command(SSD1309_DISPLAY_OFF) |
| 93 | + self.write_command(SSD1309_SET_DISPLAY_CLOCK_DIV) |
| 94 | + self.write_command(0x80) |
| 95 | + |
| 96 | + self.write_command(SSD1309_SET_MULTIPLEX) |
| 97 | + self.write_command((self.height - 1)) |
| 98 | + |
| 99 | + self.write_command(SSD1309_SET_DISPLAY_OFFSET) |
| 100 | + self.write_command(0x00) |
| 101 | + |
| 102 | + self.write_command((SSD1309_SET_START_LINE | 0x00)) |
| 103 | + |
| 104 | + self.write_command(SSD1309_CHARGE_PUMP) |
| 105 | + self.write_command(0x14) # 0x10 # 0x14 |
| 106 | + |
| 107 | + self.write_command(SSD1309_MEMORY_MODE) |
| 108 | + self.write_command(0x00) |
| 109 | + |
| 110 | + self.write_command((SSD1309_SEG_REMAP | 0x01)) |
| 111 | + self.write_command(SSD1309_COM_SCAN_DEC) |
| 112 | + self.write_command(SSD1309_SET_COM_PINS) |
| 113 | + self.write_command(0x12) |
| 114 | + |
| 115 | + self.write_command(SSD1309_SET_CONTRAST) |
| 116 | + self.write_command(0xCF) # 0x9F # 0xCF |
| 117 | + |
| 118 | + self.write_command(SSD1309_SET_PRECHARGE) |
| 119 | + self.write_command(0xF1) # 0x22 # 0xF1 |
| 120 | + |
| 121 | + self.write_command(SSD1309_SET_VCOM_DETECT) |
| 122 | + self.write_command(0x40) |
| 123 | + |
| 124 | + self.write_command(SSD1309_DISPLAY_ALL_ON_RESUME) |
| 125 | + self.write_command(SSD1309_NORMAL_DISPLAY) |
| 126 | + self.write_command(SSD1309_DISPLAY_ON) |
| 127 | + |
| 128 | + |
| 129 | + def show(self): |
| 130 | + x0 = 0 |
| 131 | + x1 = self.width - 1 |
| 132 | + if (self.width == 64): |
| 133 | + # displays with width of 64 pixels are shifted by 32 |
| 134 | + x0 += 32 |
| 135 | + x1 += 32 |
| 136 | + |
| 137 | + self.write_command(SSD1309_COLUMN_ADDR) |
| 138 | + self.write_command(x0) |
| 139 | + self.write_command(x1) |
| 140 | + self.write_command(SSD1309_PAGE_ADDR) |
| 141 | + self.write_command(0x00) |
| 142 | + self.write_command((self._pages - 1)) |
| 143 | + |
| 144 | + self.dc.value(DAT) |
| 145 | + self.cs.value(LOW) |
| 146 | + self.spi.write(self.buffer) |
| 147 | + self.cs.value(HIGH) |
0 commit comments