development:python:micropython:oled
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
development:python:micropython:oled [2024/08/15 14:45] – created tungnt | development:python:micropython:oled [2024/08/15 15:15] (current) – tungnt | ||
---|---|---|---|
Line 7: | Line 7: | ||
{{ : | {{ : | ||
- | <file python | + | **Thư viện:** [[https:// |
- | # | + | |
- | import | + | <file python boot.py> |
- | import | + | import gc |
+ | import esp | ||
+ | from oled import | ||
+ | from machine | ||
- | # register definitions | + | esp.osdebug(None) |
- | SET_CONTRAST | + | gc.collect() |
- | 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 | + | |
+ | oled21 = OLed(scl=22, | ||
+ | </ | ||
- | class SSD1306: | + | <file python main.py> |
- | def __init__(self, | + | import image_asset |
- | self.width = width | + | from utime import sleep |
- | | + | |
- | | + | |
- | 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): | + | while True: |
- | 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): | + | sleep(1) |
- | self.write_cmd(SET_DISP | 0x00) | + | |
- | def contrast(self, contrast): | + | oled21.display_menu(0, 5, 0) |
- | self.write_cmd(SET_CONTRAST) | + | |
- | self.write_cmd(contrast) | + | |
- | def invert(self, invert): | + | sleep(1) |
- | self.write_cmd(SET_NORM_INV | (invert & 1)) | + | |
- | def show(self): | + | oled21.set_images(image_asset.girls) |
- | 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): | + | sleep(1) |
- | 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 190: | Line 51: | ||
self.oled = ssd1306.SSD1306_I2C(128, | self.oled = ssd1306.SSD1306_I2C(128, | ||
| | ||
- | self.menu_items = ["Profile", "Setup Wifi", " | + | self.menu_items = ["Option 1", "Option 2", " |
self.menu_selected_index = 0 | self.menu_selected_index = 0 | ||
self.start_index = 0 | self.start_index = 0 | ||
Line 227: | Line 88: | ||
self.oled.text(" | self.oled.text(" | ||
else: | else: | ||
- | self.oled.text(self.menu_items[i], | + | self.oled.text(self.menu_items[i], |
self.oled.show() | self.oled.show() | ||
</ | </ | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | {{ : |
development/python/micropython/oled.1723733132.txt.gz · Last modified: 2024/08/15 14:45 by tungnt