| 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): |
| 30 | self.width = width |
| 31 | self.height = height |
| 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) |
| 36 | self.init_display() |
| 37 | |
| 38 | def init_display(self): |
| 39 | for cmd in ( |
| 40 | SET_DISP | 0x00, # off |
| 41 | # address setting |
| 42 | SET_MEM_ADDR, |
| 43 | 0x00, # horizontal |
| 44 | # resolution and layout |
| 45 | SET_DISP_START_LINE | 0x00, |
| 46 | SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0 |
| 47 | SET_MUX_RATIO, |
| 48 | self.height - 1, |
| 49 | SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0 |
| 50 | SET_DISP_OFFSET, |
| 51 | 0x00, |
| 52 | SET_COM_PIN_CFG, |
| 53 | 0x02 if self.width > 2 * self.height else 0x12, |
| 54 | # timing and driving scheme |
| 55 | SET_DISP_CLK_DIV, |
| 56 | 0x80, |
| 57 | SET_PRECHARGE, |
| 58 | 0x22 if self.external_vcc else 0xF1, |
| 59 | SET_VCOM_DESEL, |
| 60 | 0x30, # 0.83*Vcc |
| 61 | # display |
| 62 | SET_CONTRAST, |
| 63 | 0xFF, # maximum |
| 64 | SET_ENTIRE_ON, # output follows RAM contents |
| 65 | SET_NORM_INV, # not inverted |
| 66 | # charge pump |
| 67 | SET_CHARGE_PUMP, |
| 68 | 0x10 if self.external_vcc else 0x14, |
| 69 | SET_DISP | 0x01, |
| 70 | ): # on |
| 71 | self.write_cmd(cmd) |
| 72 | self.fill(0) |
| 73 | self.show() |
| 74 | |
| 75 | def poweroff(self): |
| 76 | self.write_cmd(SET_DISP | 0x00) |
| 77 | |
| 78 | def poweron(self): |
| 79 | self.write_cmd(SET_DISP | 0x01) |
| 80 | |
| 81 | def contrast(self, contrast): |
| 82 | self.write_cmd(SET_CONTRAST) |
| 83 | self.write_cmd(contrast) |
| 84 | |
| 85 | def invert(self, invert): |
nothing calls this directly
no outgoing calls
no test coverage detected