1 Star 0 Fork 0

mumu / Esp8266MicroPythonOLEDProject

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
oledsc.py 5.98 KB
一键复制 编辑 原始数据 按行查看 历史
mumu 提交于 2022-02-04 22:23 . 可用
# MicroPython SSD1306 OLED driver, I2C and SPI interfaces
# from micropython import const
# import font
# register definitions
SET_CONTRAST = 0x81
SET_ENTIRE_ON = 0xA4
SET_NORM_INV = 0xA6
SET_DISP = 0xAE
SET_MEM_ADDR = 0x20
SET_COL_ADDR = 0x21
SET_PAGE_ADDR = 0x22
SET_DISP_START_LINE = 0x40
SET_SEG_REMAP = 0xA0
SET_MUX_RATIO = 0xA8
SET_COM_OUT_DIR = 0xC0
SET_DISP_OFFSET = 0xD3
SET_COM_PIN_CFG = 0xDA
SET_DISP_CLK_DIV = 0xD5
SET_PRECHARGE = 0xD9
SET_VCOM_DESEL = 0xDB
SET_CHARGE_PUMP = 0x8D
class SSD1306(object):
def __init__(self, width, height, external_vcc):
self.width = width
self.height = height
self.external_vcc = external_vcc
self.pages = self.height // 8
self.buffer = bytearray(self.pages * self.width)
self.init_display()
def init_display(self):
for cmd in (
SET_DISP | 0x00, # off
# address setting
SET_MEM_ADDR,
0x00, # horizontal
# resolution and layout
SET_DISP_START_LINE | 0x00,
SET_SEG_REMAP | 0x01, # column addr 127 mapped to SEG0
SET_MUX_RATIO,
self.height - 1,
SET_COM_OUT_DIR | 0x08, # scan from COM[N] to COM0
SET_DISP_OFFSET,
0x00,
SET_COM_PIN_CFG,
0x02 if self.height == 32 else 0x12,
# timing and driving scheme
SET_DISP_CLK_DIV,
0x80,
SET_PRECHARGE,
0x22 if self.external_vcc else 0xF1,
SET_VCOM_DESEL,
0x30, # 0.83*Vcc
# display
SET_CONTRAST,
0xFF, # maximum
SET_ENTIRE_ON, # output follows RAM contents
SET_NORM_INV, # not inverted
# charge pump
SET_CHARGE_PUMP,
0x10 if self.external_vcc else 0x14,
SET_DISP | 0x01,
): # on
self.write_cmd(cmd)
self.show()
def poweroff(self):
self.write_cmd(SET_DISP | 0x00)
def poweron(self):
self.write_cmd(SET_DISP | 0x01)
def contrast(self, contrast):
self.write_cmd(SET_CONTRAST)
self.write_cmd(contrast)
def invert(self, invert):
self.write_cmd(SET_NORM_INV | (invert & 1))
@micropython.native
def show(self):
for i in range(8):
self.write_cmd(0xb0+i)
self.write_cmd(0)
self.write_cmd(0x10)
for j in range(128):
self.write_data(self.buffer[j*8 + i])
def draw_pixel(self,x, y, t=True):
if(x > 127):
return
if(y>63):
return
pos = y // 8
bx = y%8
temp = 1<<bx
if(t):
self.buffer[x*8 + pos] |= temp
else:
self.buffer[x*8 + pos] &= (~temp)
@micropython.native
def show_char(self, x, y, chr, size=16):
csize = (size//8)*(size//2) if size%8 == 0 else (size//8 + 1)*(size//2)
chr = ord(chr) - ord(' ')
y0=y
temp = bytes(0)
from font import asc2_1608
for i in range(csize):
if size == 12:
from font import asc2_1206
temp = asc2_1206[chr][i]
elif size == 16:
temp = asc2_1608[chr][i]
elif size == 24:
from font import asc2_2412
temp = asc2_2412[chr][i]
else:
return
for j in range(8):
if( (temp&0x80) != 0):
self.draw_pixel(x,y,True)
else:
self.draw_pixel(x,y,False)
temp<<=1
y+=1
if (y - y0) == size:
y=y0
x+=1
break
# @micropython.native
def show_text(self,x,y,text,size = 16):
for chr in text:
self.show_char(x,y,chr,size)
x += (size//2)
def clear(self):
self.buffer = bytearray(self.pages * self.width)
self.show()
def draw_line(self, x0,y0,x1,y1):
if x0 == x1:
for point in range(y0, y1):
self.draw_pixel(x0, point)
elif y0 == y1:
for point in range(x0, x1):
self.draw_pixel(point, y0)
else:
k = (y1-y0)/(x1-x0)
if x0 < x1:
for point in range(x0, x1):
y = int(y0 + point*k)
self.draw_pixel(point, y)
else:
for point in range(x1, x0):
y = int(y1 + point*k)
self.draw_pixel(point, y)
class SSD1306_I2C(SSD1306):
def __init__(self, width, height, i2c, addr=0x3C, external_vcc=False):
self.i2c = i2c
self.addr = addr
self.temp = bytearray(2)
super().__init__(width, height, external_vcc)
def write_cmd(self, cmd):
self.temp[0] = 0x00 # Co=1, D/C#=0
self.temp[1] = cmd
self.i2c.writeto(self.addr, self.temp)
def write_data(self, data):
self.temp[0] = 0x40
self.temp[1] = data
self.i2c.writeto(self.addr, self.temp)
class SSD1306_SPI(SSD1306):
def __init__(self, width, height, spi, dc, res, cs, external_vcc=False):
self.rate = 10 * 1024 * 1024
dc.init(dc.OUT, value=0)
res.init(res.OUT, value=0)
cs.init(cs.OUT, value=1)
self.spi = spi
self.dc = dc
self.res = res
self.cs = cs
import time
self.res(1)
time.sleep_ms(1)
self.res(0)
time.sleep_ms(10)
self.res(1)
super().__init__(width, height, external_vcc)
def write_cmd(self, cmd):
self.spi.init(baudrate=self.rate, polarity=0, phase=0)
self.cs(1)
self.dc(0)
self.cs(0)
self.spi.write(bytearray([cmd]))
self.cs(1)
def write_data(self, buf):
self.spi.init(baudrate=self.rate, polarity=0, phase=0)
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(buf)
self.cs(1)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/MUMU-DADA/esp8266-micro-python-oledproject.git
git@gitee.com:MUMU-DADA/esp8266-micro-python-oledproject.git
MUMU-DADA
esp8266-micro-python-oledproject
Esp8266MicroPythonOLEDProject
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891