development:python:micropython:oled
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
development:python:micropython:oled [2024/08/15 15:10] – tungnt | development:python:micropython:oled [2024/08/15 15:15] (current) – tungnt | ||
---|---|---|---|
Line 6: | Line 6: | ||
{{ : | {{ : | ||
+ | |||
+ | **Thư viện:** [[https:// | ||
<file python boot.py> | <file python boot.py> | ||
Line 36: | Line 38: | ||
sleep(1) | sleep(1) | ||
</ | </ | ||
- | |||
- | <file python ssd1306.py> | ||
- | # | ||
- | |||
- | import time | ||
- | import framebuf | ||
- | |||
- | # register definitions | ||
- | SET_CONTRAST | ||
- | SET_ENTIRE_ON | ||
- | SET_NORM_INV | ||
- | SET_DISP | ||
- | SET_MEM_ADDR | ||
- | SET_COL_ADDR | ||
- | SET_PAGE_ADDR | ||
- | SET_DISP_START_LINE = const(0x40) | ||
- | SET_SEG_REMAP | ||
- | SET_MUX_RATIO | ||
- | SET_COM_OUT_DIR | ||
- | SET_DISP_OFFSET | ||
- | SET_COM_PIN_CFG | ||
- | SET_DISP_CLK_DIV | ||
- | SET_PRECHARGE | ||
- | SET_VCOM_DESEL | ||
- | SET_CHARGE_PUMP | ||
- | |||
- | |||
- | class SSD1306: | ||
- | def __init__(self, | ||
- | self.width = width | ||
- | self.height = height | ||
- | self.external_vcc = external_vcc | ||
- | self.pages = self.height // 8 | ||
- | # Note the subclass must initialize self.framebuf to a framebuffer. | ||
- | # This is necessary because the underlying data buffer is different | ||
- | # between I2C and SPI implementations (I2C needs an extra byte). | ||
- | self.poweron() | ||
- | self.init_display() | ||
- | |||
- | def init_display(self): | ||
- | for cmd in ( | ||
- | SET_DISP | 0x00, # off | ||
- | # address setting | ||
- | SET_MEM_ADDR, | ||
- | # resolution and layout | ||
- | SET_DISP_START_LINE | 0x00, | ||
- | SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0 | ||
- | SET_MUX_RATIO, | ||
- | SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0 | ||
- | SET_DISP_OFFSET, | ||
- | SET_COM_PIN_CFG, | ||
- | # timing and driving scheme | ||
- | SET_DISP_CLK_DIV, | ||
- | SET_PRECHARGE, | ||
- | SET_VCOM_DESEL, | ||
- | # display | ||
- | SET_CONTRAST, | ||
- | SET_ENTIRE_ON, | ||
- | SET_NORM_INV, | ||
- | # charge pump | ||
- | SET_CHARGE_PUMP, | ||
- | SET_DISP | 0x01): # on | ||
- | self.write_cmd(cmd) | ||
- | self.fill(0) | ||
- | self.show() | ||
- | |||
- | def poweroff(self): | ||
- | self.write_cmd(SET_DISP | 0x00) | ||
- | |||
- | def contrast(self, | ||
- | self.write_cmd(SET_CONTRAST) | ||
- | self.write_cmd(contrast) | ||
- | |||
- | def invert(self, | ||
- | self.write_cmd(SET_NORM_INV | (invert & 1)) | ||
- | |||
- | def show(self): | ||
- | x0 = 0 | ||
- | x1 = self.width - 1 | ||
- | if self.width == 64: | ||
- | # displays with width of 64 pixels are shifted by 32 | ||
- | x0 += 32 | ||
- | x1 += 32 | ||
- | self.write_cmd(SET_COL_ADDR) | ||
- | self.write_cmd(x0) | ||
- | self.write_cmd(x1) | ||
- | self.write_cmd(SET_PAGE_ADDR) | ||
- | self.write_cmd(0) | ||
- | self.write_cmd(self.pages - 1) | ||
- | self.write_framebuf() | ||
- | |||
- | def fill(self, col): | ||
- | self.framebuf.fill(col) | ||
- | |||
- | def pixel(self, x, y, col): | ||
- | self.framebuf.pixel(x, | ||
- | |||
- | def scroll(self, | ||
- | self.framebuf.scroll(dx, | ||
- | |||
- | def text(self, string, x, y, col=1): | ||
- | self.framebuf.text(string, | ||
- | |||
- | |||
- | class SSD1306_I2C(SSD1306): | ||
- | def __init__(self, | ||
- | self.i2c = i2c | ||
- | self.addr = addr | ||
- | self.temp = bytearray(2) | ||
- | # Add an extra byte to the data buffer to hold an I2C data/ | ||
- | # to use hardware-compatible I2C transactions. | ||
- | # buffer is used to mask this byte from the framebuffer operations | ||
- | # (without a major memory hit as memoryview doesn' | ||
- | # buffer). | ||
- | self.buffer = bytearray(((height // 8) * width) + 1) | ||
- | self.buffer[0] = 0x40 # Set first byte of data buffer to Co=0, D/C=1 | ||
- | self.framebuf = framebuf.FrameBuffer1(memoryview(self.buffer)[1: | ||
- | super().__init__(width, | ||
- | |||
- | def write_cmd(self, | ||
- | self.temp[0] = 0x80 # Co=1, D/C#=0 | ||
- | self.temp[1] = cmd | ||
- | self.i2c.writeto(self.addr, | ||
- | |||
- | def write_framebuf(self): | ||
- | # Blast out the frame buffer using a single I2C transaction to support | ||
- | # hardware I2C interfaces. | ||
- | self.i2c.writeto(self.addr, | ||
- | |||
- | def poweron(self): | ||
- | pass | ||
- | |||
- | |||
- | class SSD1306_SPI(SSD1306): | ||
- | def __init__(self, | ||
- | self.rate = 10 * 1024 * 1024 | ||
- | dc.init(dc.OUT, | ||
- | res.init(res.OUT, | ||
- | cs.init(cs.OUT, | ||
- | self.spi = spi | ||
- | self.dc = dc | ||
- | self.res = res | ||
- | self.cs = cs | ||
- | self.buffer = bytearray((height // 8) * width) | ||
- | self.framebuf = framebuf.FrameBuffer1(self.buffer, | ||
- | super().__init__(width, | ||
- | |||
- | def write_cmd(self, | ||
- | self.spi.init(baudrate=self.rate, | ||
- | self.cs.high() | ||
- | self.dc.low() | ||
- | self.cs.low() | ||
- | self.spi.write(bytearray([cmd])) | ||
- | self.cs.high() | ||
- | |||
- | def write_framebuf(self): | ||
- | self.spi.init(baudrate=self.rate, | ||
- | self.cs.high() | ||
- | self.dc.high() | ||
- | self.cs.low() | ||
- | self.spi.write(self.buffer) | ||
- | self.cs.high() | ||
- | |||
- | def poweron(self): | ||
- | self.res.high() | ||
- | time.sleep_ms(1) | ||
- | self.res.low() | ||
- | time.sleep_ms(10) | ||
- | self.res.high() | ||
- | |||
- | </ | ||
<file python oled.py> | <file python oled.py> | ||
Line 260: | Line 91: | ||
self.oled.show() | self.oled.show() | ||
</ | </ | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | {{ : |
development/python/micropython/oled.1723734659.txt.gz · Last modified: 2024/08/15 15:10 by tungnt