目录
简介
我重新写了一个驱动,增加了一些功能,由于我的硬件是128*64oled单色I2C,我只适配了我的硬件。如果你的硬件和我相同,你可以使用我的ssd1306驱动.
我的oled似乎不是ssd1306驱动芯片,而是ssd1315驱动芯片,不过两者差别很小,仅在滚动那块有些许出入
更新时间:2024年12月18日
代码
import framebuf
class SSD1306():
def __init__(self,i2c, addr=0x3c, external_vcc=False):
self.external_vcc = external_vcc
self.i2c = i2c
self.addr = addr
self.temp = bytearray(2)
# buffer需要8 * 128的显示字节加1字节命令
self.buffer = bytearray(8 * 128 + 1)
self.buffer[0] = 0x40 # Co=0, D/C=1
self.framebuf = framebuf.FrameBuffer1(memoryview(self.buffer)[1:], 128, 64)
self.init_display()
def init_display(self):
for cmd in (
0xae, # 熄屏
0x20, 0x00, # 水平寻址
0x40, # 显示起始行地址
0xa1, # 正常列扫描
0xa8, 63, # 复用率
0xc8, # 正常行扫描
0xd3, 0x00, #设置COM偏移量,即屏幕像上偏移的行数
0xda, 0x12, #使用备选引脚配置,并禁用左右反置
0xd5, 0x80, # 设置分频因子与振荡频率
0xd9, 0x22 if self.external_vcc else 0xf1,
0xdb, 0x30, # 设置vcomh电压为0.83*Vcc
0x81, 0xff, # 亮度最大
0xa4, # 使用GDDRAM中的数据显示
0xa6, # 设置GDDRAM中的0对应于像素点的暗
# 关闭电荷泵
0x8d, 0x10 if self.external_vcc else 0x14,
0x2e, # 禁止滚动
0xaf): #开屏
self.write_cmd(cmd)
self.fill(0)
self.show()
# 中文到字节的转化函数
def cn_to_byte(self,cn:str)->bytearray:
cn_to_byte_dict={
'蓝':bytearray(b'\x04\x04\xe4\x04\x0f\xf4\x04\x04\xf4\x44\xcf\x44\x44\x44\x04\x00\x40\x40\x7d\x44\x44\x7d\x44\x45\x44\x7c\x44\x45\x7c\x40\x40\x00'),
'牙':bytearray(b'\x00\x00\xc2\xb2\x82\x82\x82\x82\x82\xfe\x82\x82\x82\x82\x80\x00\x00\x20\x20\x10\x08\x04\x02\x41\x80\x7f\x00\x00\x00\x00\x00\x00'),
'模':bytearray(b'\x10\x10\xd0\xff\x90\x14\xe4\xaf\xa4\xa4\xa4\xaf\xe4\x04\x00\x00\x04\x03\x00\xff\x00\x89\x4b\x2a\x1a\x0e\x1a\x2a\x4b\x88\x80\x00'),
'式':bytearray(b'\x10\x10\x90\x90\x90\x90\x90\x10\x10\xff\x10\x10\x11\x16\x10\x00\x00\x20\x60\x20\x3f\x10\x10\x10\x00\x03\x0c\x10\x20\x40\xf8\x00'),
'离':bytearray(b'\x04\x04\x04\xf4\x84\xd4\xa5\xa6\xa4\xd4\x84\xf4\x04\x04\x04\x00\x00\xfe\x02\x02\x12\x3a\x16\x13\x12\x1a\x32\x42\x82\x7e\x00\x00'),
'线':bytearray(b'\x20\x30\xac\x63\x20\x18\x80\x90\x90\xff\x90\x49\x4a\x48\x40\x00\x22\x67\x22\x12\x12\x12\x40\x40\x20\x13\x0c\x14\x22\x41\xf8\x00'),
'设':bytearray(b'\x40\x40\x42\xcc\x00\x40\xa0\x9e\x82\x82\x82\x9e\xa0\x20\x20\x00\x00\x00\x00\x3f\x90\x88\x40\x43\x2c\x10\x28\x46\x41\x80\x80\x00'),
'置':bytearray(b'\x00\x17\x15\xd5\x55\x57\x55\x7d\x55\x57\x55\xd5\x15\x17\x00\x00\x40\x40\x40\x7f\x55\x55\x55\x55\x55\x55\x55\x7f\x40\x40\x40\x00'),
'版':bytearray(b'\x00\xfe\x20\x20\x3f\x20\x00\xfc\x24\xe4\x24\x22\x23\xe2\x00\x00\x80\x7f\x01\x01\xff\x80\x60\x1f\x80\x41\x26\x18\x26\x41\x80\x00'),
'本':bytearray(b'\x00\x10\x10\x10\x10\xd0\x30\xff\x30\xd0\x10\x10\x10\x10\x00\x00\x10\x08\x04\x02\x09\x08\x08\xff\x08\x08\x09\x02\x04\x08\x10\x00'),
'连':bytearray(b'\x40\x40\x42\xcc\x00\x04\x44\x64\x5c\x47\xf4\x44\x44\x44\x04\x00\x00\x40\x20\x1f\x20\x44\x44\x44\x44\x44\x7f\x44\x44\x44\x44\x00'),
'接':bytearray(b'\x10\x10\x10\xff\x10\x50\x44\x54\x65\xc6\x44\x64\x54\x44\x40\x00\x04\x44\x82\x7f\x01\x82\x82\x4a\x56\x23\x22\x52\x4e\x82\x02\x00'),
'使':bytearray(b'\x80\x60\xf8\x07\x04\xe4\x24\x24\x24\xff\x24\x24\x24\xe4\x04\x00\x00\x00\xff\x00\x80\x81\x45\x29\x11\x2f\x41\x41\x81\x81\x80\x00'),
'用':bytearray(b'\x00\x00\xfe\x22\x22\x22\x22\xfe\x22\x22\x22\x22\xfe\x00\x00\x00\x80\x60\x1f\x02\x02\x02\x02\x7f\x02\x02\x42\x82\x7f\x00\x00\x00'),
'说':bytearray(b'\x40\x40\x42\xcc\x00\x00\xf1\x16\x10\x10\x18\x14\xf3\x00\x00\x00\x00\x00\x00\x3f\x90\x48\x21\x19\x07\x01\x3f\x41\x41\x40\x70\x00'),
'明':bytearray(b'\x00\xfc\x44\x44\x44\xfc\x00\x00\xfe\x22\x22\x22\x22\xfe\x00\x00\x00\x0f\x04\x04\x04\x8f\x40\x30\x0f\x02\x02\x42\x82\x7f\x00\x00'),
'开':bytearray(b'\x80\x82\x82\x82\xfe\x82\x82\x82\x82\x82\xfe\x82\x82\x82\x80\x00\x00\x80\x40\x30\x0f\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00'),
'始':bytearray(b'\x10\x10\xf0\x1f\x10\xf0\x00\x40\xe0\x58\x47\x40\x50\x60\xc0\x00\x40\x22\x15\x08\x16\x21\x00\x00\xfe\x42\x42\x42\x42\xfe\x00\x00'),
'校':bytearray(b'\x10\x10\xd0\xff\x90\x10\x80\x48\x28\x09\x0e\x08\x28\x48\x88\x00\x04\x03\x00\xff\x00\x01\x80\x80\x43\x2c\x10\x2c\x43\x80\x80\x00'),
'准':bytearray(b'\x00\x02\x0c\xe0\x40\x20\xf8\x4f\x48\x49\xfe\x48\x48\x48\x08\x00\x04\x04\x7f\x00\x00\x00\xff\x22\x22\x22\x3f\x22\x22\x22\x20\x00'),
'滴':bytearray(b'\x10\x60\x02\x8c\x00\xe4\x24\xac\xb5\xe6\xb4\xac\x24\xe4\x00\x00\x04\x04\x7e\x01\x00\xff\x00\x1e\x12\x13\x12\x5e\x80\x7f\x00\x00'),
'定':bytearray(b'\x10\x0c\x44\x44\x44\x44\x45\xc6\x44\x44\x44\x44\x44\x14\x0c\x00\x80\x40\x20\x1e\x20\x40\x40\x7f\x44\x44\x44\x44\x44\x40\x40\x00'),
'检':bytearray(b'\x10\x10\xd0\xff\x90\x50\x20\x50\x4c\x43\x4c\x50\x20\x40\x40\x00\x04\x03\x00\xff\x00\x41\x44\x58\x41\x4e\x60\x58\x47\x40\x40\x00'),
'查':bytearray(b'\x40\x44\x24\xa4\x94\x8c\x84\xff\x84\x8c\x94\xa4\x24\x44\x40\x00\x40\x40\x40\x5f\x4a\x4a\x4a\x4a\x4a\x4a\x4a\x5f\x40\x40\x40\x00'),
'更':bytearray(b'\x02\x02\xf2\x92\x92\x92\x92\xfe\x92\x92\x92\x92\xf2\x02\x02\x00\x80\x80\x87\x4c\x54\x24\x34\x4f\x44\x44\x84\x84\x87\x80\x80\x00'),
'新':bytearray(b'\x40\x44\x54\x65\xc6\x64\x54\x44\x00\xfc\x44\x44\xc4\x42\x40\x00\x20\x12\x4a\x82\x7f\x02\x0a\x92\x60\x1f\x00\x00\xff\x00\x00\x00'),
'速':bytearray(b'\x40\x40\x42\xcc\x00\x04\xf4\x94\x94\xff\x94\x94\xf4\x04\x00\x00\x00\x40\x20\x1f\x20\x48\x44\x42\x41\x5f\x41\x42\x44\x48\x40\x00'),
'度':bytearray(b'\x00\x00\xfc\x24\x24\x24\xfc\x25\x26\x24\xfc\x24\x24\x24\x04\x00\x40\x30\x8f\x80\x84\x4c\x55\x25\x25\x25\x55\x4c\x80\x80\x80\x00'),
'中':bytearray(b'\x00\x00\xf0\x10\x10\x10\x10\xff\x10\x10\x10\x10\xf0\x00\x00\x00\x00\x00\x0f\x04\x04\x04\x04\xff\x04\x04\x04\x04\x0f\x00\x00\x00'),
'打':bytearray(b'\x10\x10\x10\xff\x10\x90\x04\x04\x04\x04\xfc\x04\x04\x04\x04\x00\x04\x44\x82\x7f\x01\x00\x00\x00\x40\x80\x7f\x00\x00\x00\x00\x00'),
'小':bytearray(b'\x00\x00\x00\xe0\x00\x00\x00\xff\x00\x00\x00\x20\x40\x80\x00\x00\x08\x04\x03\x00\x00\x40\x80\x7f\x00\x00\x00\x00\x00\x01\x0e\x00'),
'程':bytearray(b'\x24\x24\xa4\xfe\x23\x22\x00\x3e\x22\x22\x22\x22\x22\x3e\x00\x00\x08\x06\x01\xff\x01\x06\x40\x49\x49\x49\x7f\x49\x49\x49\x41\x00'),
'序':bytearray(b'\x00\x00\xfc\x04\x04\x04\x14\x15\x56\x94\x54\x34\x14\x04\x04\x00\x40\x30\x0f\x00\x01\x01\x01\x41\x81\x7f\x01\x01\x01\x05\x03\x00'),
'自':bytearray(b'\x00\x00\x00\xf8\x88\x8c\x8a\x89\x88\x88\x88\xf8\x00\x00\x00\x00\x00\x00\x00\xff\x44\x44\x44\x44\x44\x44\x44\xff\x00\x00\x00\x00'),
'动':bytearray(b'\x40\x44\xc4\x44\x44\x44\x40\x10\x10\xff\x10\x10\x10\xf0\x00\x00\x1

博主用Python重新写了一个适配128*64 oled单色I2C硬件的驱动,虽硬件芯片是ssd1315,但与ssd1306差别小。该驱动有显示字符、图片,画图形,翻转、反相、滚动等功能,部分功能存在一些bug。
最低0.47元/天 解锁文章
1278

被折叠的 条评论
为什么被折叠?



