|
发表于 2019-10-2 11:13:20
|
显示全部楼层
已经初步实现,对我这种初学者来说非常确实吃力,把驱动源码改了一下,放进main里,不用单独import了。使用的字体名称为“han40x40_tianqi_songti.FON”,这个文件要烧录进ESP8266的,生成方法见楼主显示天气的帖子。main.py代码如下,也要烧录进ESP8266的(注意把我代码里的GPIO引脚改成你在用的)
再次感谢楼主的分享,搞定这个,后面的路就顺了。
https://www.mydigit.cn/forum.php?mod=viewthread&tid=64439
- #驱动部分
- from micropython import const
- import framebuf
- # register definitions
- SET_CONTRAST = const(0x81)
- SET_ENTIRE_ON = const(0xa4)
- SET_NORM_INV = const(0xa6)
- SET_DISP = const(0xae)
- SET_MEM_ADDR = const(0x20)
- SET_COL_ADDR = const(0x21)
- SET_PAGE_ADDR = const(0x22)
- SET_DISP_START_LINE = const(0x40)
- SET_SEG_REMAP = const(0xa0)
- SET_MUX_RATIO = const(0xa8)
- SET_COM_OUT_DIR = const(0xc0)
- SET_DISP_OFFSET = const(0xd3)
- SET_COM_PIN_CFG = const(0xda)
- SET_DISP_CLK_DIV = const(0xd5)
- SET_PRECHARGE = const(0xd9)
- SET_VCOM_DESEL = const(0xdb)
- SET_CHARGE_PUMP = const(0x8d)
- class SSD1306(framebuf.FrameBuffer):
- 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)
- super().__init__(self.buffer, self.width, self.height, framebuf.MONO_VLSB)
- self.init_display()
- def init_display(self):
- for cmd in (
- SET_DISP | 0x00, # off
- # address setting
- SET_MEM_ADDR, 0x01, # 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.fill(0)
- # self.show()
- def contrast(self, contrast):
- self.write_cmd(SET_CONTRAST)
- self.write_cmd(contrast)
- def show(self,x1,x2,x3,x4,x5):
- self.write_cmd(SET_COL_ADDR)
- self.write_cmd(x3)
- self.write_cmd(x4)
- self.write_cmd(SET_PAGE_ADDR)
- self.write_cmd(x1)
- self.write_cmd(x2)
- self.write_data(x5)
- 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)
- self.write_list = [b'\x40', None] # Co=0, D/C#=1
- super().__init__(width, height, external_vcc)
- def write_cmd(self, cmd):
- self.temp[0] = 0x80 # Co=1, D/C#=0
- self.temp[1] = cmd
- self.i2c.writeto(self.addr, self.temp)
- def write_data(self, buf):
- self.write_list[1] = buf
- self.i2c.writevto(self.addr, self.write_list)
- #我的oled的地址 60 0x3c 0b111100
- from machine import Pin, I2C
- i2c = I2C(scl=Pin(12), sda=Pin(14), freq=400000)
- oled = SSD1306_I2C(128, 64, i2c, addr=0x3c, external_vcc=False)
- # #垂直水平清屏
- show_buffer = bytearray([0x0]*8*128)
- oled.show(0,7,0,127,show_buffer)
- # #页地址清屏
- # for x in range(8):
- # show_buffer = bytearray([0x00]*128)
- # # oled.pag_show(0,x,show_buffer)
- dic_han_40 = {'℃': 0, '↑': 200, '↓': 400, '★': 600, '。': 800, '一': 1000, '不': 1200, '中': 1400, '云': 1600, '今': 1800, '会': 2000, '伞': 2200, '伴': 2400, '低': 2600, '充': 2800, '光': 3000, '内': 3200, '冰': 3400, '冷': 3600, '冻': 3800, '出': 4000, '升': 4200, '卷': 4400, '国': 4600, '外': 4800, '多': 5000, '大': 5200, '天': 5400, '太': 5600, '夹': 5800, '宜': 6000, '寒': 6200, '小': 6400, '尘': 6600, '带': 6800, '度': 7000, '弱': 7200, '强': 7400, '很': 7600, '或': 7800, '扬': 8000, '旅': 8200, '明': 8400, '晴': 8600, '暴': 8800, '最': 9000, '有': 9200, '未': 9400, '气': 9600, '沙': 9800, '没': 10000, '洗': 10200, '浮': 10400, '温': 10600, '游': 10800, '炎': 11000, '热': 11200, '特': 11400, '的': 11600, '知': 11800, '示': 12000, '穿': 12200, '等': 12400, '紫': 12600, '级': 12800, '线': 13000, '者': 13200, '舒': 13400, '般': 13600, '衣': 13800, '表': 14000, '说': 14200, '足': 14400, '车': 14600, '较': 14800, '适': 15000, '部': 15200, '门': 15400, '间': 15600, '阳': 15800, '阴': 16000, '阵': 16200, '降': 16400, '雨': 16600, '雪': 16800, '雷': 17000, '雹': 17200, '雾': 17400, '霾': 17600, '风': 17800, '飓': 18000, '高': 18200, '龙': 18400, '!': 18600, ',': 18800, '?': 19000}
- import time
- #汉字 40*40点阵,每次取一列 5个字节 5*8 = 40 逐列扫描
- #首先开辟一块显存,对应要显示的面积 x*y*字数/8 一个字节8个点
- buff = [0x00]*(40*40*3//8)#40pix x 40 pix 三个字 共600个bytes
- han_font_file = open('han40x40_tianqi_songti.fon',"rb")
- # en_font_file = open('en_20x40_kaiti.FON',"rb")
- nubBytes = 5 #每次写入5个字节(5为步进) 5*8 = 40 一列40个点
- str = '明天晴天'
- for one_char in str:
- if ord(one_char) <= 128:
- # offset = (ord(one_char))*100
- # en_font_file.seek(offset)
- # for i in range(20):#每个英文100个bytes
- # han_tem=en_font_file.read(nubBytes)#将一个汉字的5个字节取出
- # buff += han_tem# 将这5个bytes加到尾部
- # del buff[0:5]#删除前面的5个bytes
- # show_buffer = (bytearray(buff))#整个缓存转为字节串
- # oled.show(2,6,0,127,show_buffer)#将整个缓存写入
- # time.sleep_ms(1)
- pass
- else:
- offset = dic_han_40[one_char]
- han_font_file.seek(offset+100)
- for i in range(40):#每个汉字200个bytes 每次写入5个字节(5为步进) 5*8 = 40 一列40个点
- han_tem=han_font_file.read(nubBytes)#将一个汉字的5个字节取出
- buff += han_tem# 将这5个bytes加到尾部
- del buff[0:5]#删除前面的5个bytes
- show_buffer = (bytearray(buff))#整个缓存转为字节串
- oled.show(2,6,0,127,show_buffer)#将整个缓存写入
- time.sleep_ms(1)
- for j in range(0,2):#写入10列作为两个字之间的空白
- buff += [0x00]*5
- del buff[0:5]
- show_buffer = (bytearray(buff))
- oled.show(2,6,0,127,show_buffer)
- time.sleep_ms(1)
- han_font_file.close()
- # en_font_file.close()
复制代码
|
|