1.3寸oled 4针 sh1106驱动
本人萌新,在淘宝买了4针oled,使用之前0.92存的代码出现花屏的情况,然后根据商家介绍使用了sh1106进行驱动而不是ssd1315,且没有提供micropython相关驱动,因此在git找到相关代码,这里仅做分享,若有错误或版权问题请及时与我联系,原文链接:GitHub - robert-hh/SH1106:用于 SH1106 OLED 控制器的 MicroPython 驱动程序
原文I2C使用的是GPIO5和GPIO4,我将其改为了GPIO22和GPIO21
屏幕引脚 | ESP32对应GPIO口 |
---|---|
Vcc | 3V |
Gnd | Gnd |
SCL | GPIO22 |
SDA | GPIO21 |
驱动文件 sh1106.py :
#
# MicroPython SH1106 OLED driver, I2C and SPI interfaces
#
# The MIT License (MIT)
#
# Copyright (c) 2016 Radomir Dopieralski (@deshipu),
# 2017-2021 Robert Hammelrath (@robert-hh)
# 2021 Tim Weber (@scy)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# Sample code sections for ESP8266 pin assignments
# ------------ SPI ------------------
# Pin Map SPI
# - 3v - xxxxxx - Vcc
# - G - xxxxxx - Gnd
# - D7 - GPIO 13 - Din / MOSI fixed
# - D5 - GPIO 14 - Clk / Sck fixed
# - D8 - GPIO 4 - CS (optional, if the only connected device)
# - D2 - GPIO 5 - D/C
# - D1 - GPIO 2 - Res
#
# for CS, D/C and Res other ports may be chosen.
#
# from machine import Pin, SPI
# import sh1106
# spi = SPI(1, baudrate=1000000)
# display = sh1106.SH1106_SPI(128, 64, spi, Pin(5), Pin(2), Pin(4))
# display.sleep(False)
# display.fill(0)
# display.text('Testing 1', 0, 0, 1)
# display.show()
#
# --------------- I2C ------------------
#
# Pin Map I2C
# - 3v - xxxxxx - Vcc
# - G - xxxxxx - Gnd
# - D2 - GPIO 22 - SCK / SCL
# - D1 - GPIO 21 - DIN / SDA
# - D0 - GPIO 16 - Res
# - G - xxxxxx CS
# - G - xxxxxx D/C
#
# Pin's for I2C can be set almost arbitrary
#
# from machine import Pin, I2C
# import sh1106
#
# i2c = I2C(scl=Pin(5), sda=Pin(4), freq=400000)
# display = sh1106.SH1106_I2C(128, 64, i2c, Pin(16), 0x3c)
# display.sleep(False)
# display.fill(0)
# display.text('Testing 1', 0, 0, 1)
# display.show()
from micropython import const
import utime as time
import framebuf
# a few register definitions
_SET_CONTRAST = const(0x81)
_SET_NORM_INV = const(0xa6)
_SET_DISP = const(0xae)
_SET_SCAN_DIR = const(0xc0)
_SET_SEG_REMAP = const(0xa0)
_LOW_COLUMN_ADDRESS = const(0x00)
_HIGH_COLUMN_ADDRESS = const(0x10)
_SET_PAGE_ADDRESS = const(0xB0)
class SH1106(framebuf.FrameBuffer):
def __init__(self, width, height, external_vcc, rotate=0):
self.width = width
self.height = height
self.external_vcc = external_vcc
self.flip_en = rotate == 180 or rotate == 270
self.rotate90 = rotate == 90 or rotate == 270
self.pages = self.height // 8
self.bufsize = self.pages * self.width
self.renderbuf = bytearray(self.bufsize)
self.pages_to_update = 0
if self.rotate90:
self.displaybuf = bytearray(self.bufsize)
# HMSB is required to keep the bit order in the render buffer
# compatible with byte-for-byte remapping to the display buffer,
# which is in VLSB. Else we'd have to copy bit-by-bit!
super().__init__(self.renderbuf, self.height, self.width,
framebuf.MONO_HMSB)
else:
self.displaybuf = self.renderbuf
super().__init__(self.renderbuf, self.width, self.height,
framebuf.MONO_VLSB)
# flip() was called rotate() once, provide backwards compatibility.
self.rotate = self.flip
self.init_display()
def init_display(self):
self.reset()
self.fill(0)
self.show()
self.poweron()
# rotate90 requires a call to flip() for setting up.
self.flip(self.flip_en)
def poweroff(self):
self.write_cmd(_SET_DISP | 0x00)
def poweron(self):
self.write_cmd(_SET_DISP | 0x01)
if self.delay:
time.sleep_ms(self.delay)
def flip(self, flag=None, update=True):
if flag is None:
flag = not self.flip_en
mir_v = flag ^ self.rotate90
mir_h = flag
self.write_cmd(_SET_SEG_REMAP | (0x01 if mir_v else 0x00))
self.write_cmd(_SET_SCAN_DIR | (0x08 if mir_h else 0x00))
self.flip_en = flag
if update:
self.show(True) # full update
def sleep(self, value):
self.write_cmd(_SET_DISP | (not value))
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))
def show(self, full_update = False):
# self.* lookups in loops take significant time (~4fps).
(w, p, db, rb) = (self.width, self.pages,
self.displaybuf, self.renderbuf)
if self.rotate90:
for i in range(self.bufsize):
db[w * (i % p) + (i // p)] = rb[i]
if full_update:
pages_to_update = (1 << self.pages) - 1
else:
pages_to_update = self.pages_to_update
#print("Updating pages: {:08b}".format(pages_to_update))
for page in range(self.pages):
if (pages_to_update & (1 << page)):
self.write_cmd(_SET_PAGE_ADDRESS | page)
self.write_cmd(_LOW_COLUMN_ADDRESS | 2)
self.write_cmd(_HIGH_COLUMN_ADDRESS | 0)
self.write_data(db[(w*page):(w*page+w)])
self.pages_to_update = 0
def pixel(self, x, y, color=None):
if color is None:
return super().pixel(x, y)
else:
super().pixel(x, y , color)
page = y // 8
self.pages_to_update |= 1 << page
def text(self, text, x, y, color=1):
super().text(text, x, y, color)
self.register_updates(y, y+7)
def line(self, x0, y0, x1, y1, color):
super().line(x0, y0, x1, y1, color)
self.register_updates(y0, y1)
def hline(self, x, y, w, color):
super().hline(x, y, w, color)
self.register_updates(y)
def vline(self, x, y, h, color):
super().vline(x, y, h, color)
self.register_updates(y, y+h-1)
def fill(self, color):
super().fill(color)
self.pages_to_update = (1 << self.pages) - 1
def blit(self, fbuf, x, y, key=-1, palette=None):
super().blit(fbuf, x, y, key, palette)
self.register_updates(y, y+self.height)
def scroll(self, x, y):
# my understanding is that scroll() does a full screen change
super().scroll(x, y)
self.pages_to_update = (1 << self.pages) - 1
def fill_rect(self, x, y, w, h, color):
super().fill_rect(x, y, w, h, color)
self.register_updates(y, y+h-1)
def rect(self, x, y, w, h, color):
super().rect(x, y, w, h, color)
self.register_updates(y, y+h-1)
#画椭圆,参数:起始圆心坐标,x半径,y半径,颜色默认为亮,是否填充,显示象限(0-15的数字)
def ellipse(self,x,y,xr,yr,c=1,f=False,m=15):
self.framebuf.ellipse(x,y,xr,yr,c,f,m)
#画立方体,左上前点的坐标,边长
def cube(self,x,y,l):
self.rect(x,y,l,l)
self.rect(x+int(0.5*l),int(y-0.5*l),l,l)
self.line(x,y,int(x+0.5*l),int(y-0.5*l)) #感觉画的是左边下面的一条边
self.line(x+l-1,y,int(x+1.5*l-1),int(y-0.5*l)) #这里可能是右边底面的一条边,因为x加上了边长
self.line(x-1,y+l,int(x+0.5*l),int(y+0.5*l-1)) #这里的y加上了边长,可能是左上的边
self.line(x+l-1,y+l-1,int(x+1.5*l-1),int(y+0.5*l-1)) #右上
#画8*8的图,列行
def p8(self,page,x,y):
for e in range(8):#遍历每一个page
byte=bin(page[e]).replace('0b','')
while len(byte)<8: #当长度小于8时用0填充到8位
byte='0'+byte
for i in range(8):#遍历page中的每一个bit
if byte[i]=='1': #如果对应的位置为1就将对应的像素点点亮
self.pixel(x+e,y+i,int(byte[i])) #xy就是左上角
#画16*16的图,列行
def p16(self,page,x,y):
for e in range(32):
byte=bin(page[e]).replace('0b','')
while len(byte)<8:
byte='0'+byte
for i in range(8):
if byte[i] and e<16:
self.pixel(x+e,y+i,int(byte[i])) #有一个关键的问题,显示器左上角为0,0,右下角为127,63,所以这里先画了图像的上半部分
elif byte[i] and e>=16:
self.pixel(x-16+e,y+8+i,int(byte[i])) #再画图像的下半部分
#画32*32的图,列行
def p32(self,page,x,y):
for e in range(128):
byte=bin(page[e]).replace('0b','')
while len(byte)<8:
byte='0'+byte
for i in range(8):
if byte[i] and e<32: #从上到下,从左往右
self.pixel(x+e,y+i,int(byte[i]))
elif byte[i] and 32<=e<64:
self.pixel(x+e-32,y+8+i,int(byte[i]))
elif byte[i] and 64<=e<96:
self.pixel(x+e-64,y+16+i,int(byte[i]))
elif byte[i] and 96<=e<128:
self.pixel(x+e-96,y+24+i,int(byte[i]))
#画64*64的图,列行
def p64(self,page,x,y):
for e in range(512):
byte=bin(page[e]).replace('0b','')
while len(byte)<8:
byte='0'+byte
for i in range(8):
if byte[i] and e<64: #从上到下,从左往右
self.pixel(x+e,y+i,int(byte[i]))
elif byte[i] and 64<=e<128:
self.pixel(x+e-64,y+8+i,int(byte[i]))
elif byte[i] and 128<=e<192:
self.pixel(x+e-128,y+16+i,int(byte[i]))
elif byte[i] and 192<=e<256:
self.pixel(x+e-192,y+24+i,int(byte[i]))
elif byte[i] and 256<=e<320:
self.pixel(x+e-256,y+32+i,int(byte[i]))
elif byte[i] and 320<=e<384:
self.pixel(x+e-320,y+40+i,int(byte[i]))
elif byte[i] and 384<=e<448:
self.pixel(x+e-384,y+48+i,int(byte[i]))
elif byte[i] and 448<=e<512:
self.pixel(x+e-448,y+56+i,int(byte[i]))
def register_updates(self, y0, y1=None):
# this function takes the top and optional bottom address of the changes made
# and updates the pages_to_change list with any changed pages
# that are not yet on the list
start_page = max(0, y0 // 8)
end_page = max(0, y1 // 8) if y1 is not None else start_page
# rearrange start_page and end_page if coordinates were given from bottom to top
if start_page > end_page:
start_page, end_page = end_page, start_page
for page in range(start_page, end_page+1):
self.pages_to_update |= 1 << page
def reset(self, res):
if res is not None:
res(1)
time.sleep_ms(1)
res(0)
time.sleep_ms(20)
res(1)
time.sleep_ms(20)
class SH1106_I2C(SH1106):
def __init__(self, width, height, i2c, res=None, addr=0x3c,
rotate=0, external_vcc=False, delay=0):
self.i2c = i2c
self.addr = addr
self.res = res
self.temp = bytearray(2)
self.delay = delay
if res is not None:
res.init(res.OUT, value=1)
super().__init__(width, height, external_vcc, rotate)
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.i2c.writeto(self.addr, b'\x40'+buf)
def reset(self):
super().reset(self.res)
class SH1106_SPI(SH1106):
def __init__(self, width, height, spi, dc, res=None, cs=None,
rotate=0, external_vcc=False, delay=0):
dc.init(dc.OUT, value=0)
if res is not None:
res.init(res.OUT, value=0)
if cs is not None:
cs.init(cs.OUT, value=1)
self.spi = spi
self.dc = dc
self.res = res
self.cs = cs
self.delay = delay
super().__init__(width, height, external_vcc, rotate)
def write_cmd(self, cmd):
if self.cs is not None:
self.cs(1)
self.dc(0)
self.cs(0)
self.spi.write(bytearray([cmd]))
self.cs(1)
else:
self.dc(0)
self.spi.write(bytearray([cmd]))
def write_data(self, buf):
if self.cs is not None:
self.cs(1)
self.dc(1)
self.cs(0)
self.spi.write(buf)
self.cs(1)
else:
self.dc(1)
self.spi.write(buf)
def reset(self):
super().reset(self.res)
代码在原本的基础上增加了显示点阵图的方法p8-p64,分布表示8x8~64x64的点阵图像
测试文件 oled_13_sh1106.py
from machine import Pin, I2C
from sh1106 import SH1106_I2C
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=400000)
oled = SH1106_I2C(128, 64, i2c, Pin(16), 0x3c)
oled.sleep(False)
oled.fill(0)
oled.text('hello world!!!', 0, 0, 1)
oled.show()
显示效果
测试文件64*64_sh1106.py
from sh1106 import SH1106_I2C
from machine import Pin,I2C
i2c=I2C(0,scl=Pin(22),sda=Pin(21))
oled = SH1106_I2C(128, 64, i2c, Pin(16), 0x3c)
pic=[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x07, 0x06, 0x0E, 0x0C, 0x1C, 0x18, 0x39, 0x30, 0x32, 0x70, 0x30, 0x30, 0x30, 0x30, 0x30, 0x11, 0x3B, 0x60, 0xE0, 0xC1, 0x83, 0x83, 0x81, 0xC0, 0xC0, 0xC0, 0x60, 0x60, 0x30, 0x38, 0x18, 0x1C, 0x0E, 0x07, 0x03, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x0F, 0x1E, 0x3C, 0x38, 0x70, 0xE0, 0xC0, 0x81, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0xF9, 0xB9, 0x71, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF4, 0x70, 0x78, 0x3E, 0x0F, 0x07, 0x01, 0x18, 0x1C, 0x0F, 0x07, 0x01, 0x80, 0xC0, 0xE0, 0x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