本篇主要参考:
https://blog.csdn.net/ki1381/article/details/79291138
(懒得打字了,要不咱就去大佬那边康康吧)
亿点小整理:
1.为了方便多场景调用,我加了几个参数进去。
//oledinit()是初始化函数,oleddisplay()是显示函数。如果不需要显示IP,只截这两个就行了
//oledIP() 和 它调用的getIP()是专门显示IP地址的函数
from luma.core.interface.serial import i2c, spi
from luma.core.render import canvas
from luma.oled.device import ssd1306, ssd1325, ssd1331, sh1106
import time
import socket
import fcntl
import struct
serial = i2c(port=1, address=0x3C)
oled = sh1106(serial)
def getIP(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15].encode('utf-8'))
)[20:24])
def oledIP():
with canvas(oled) as draw:
draw.text((2, 5), "IP: " + getIP("wlan0") + '\n' + "I'm working\nbut no people", fill=255)
def oledinit():
serial = i2c(port=1, address=0x3C)
oled = sh1106(serial)
def oleddisplay(In,x,y):
with canvas(oled) as draw:
#draw.rectangle(device.bounding_box, outline="white", fill="black")
draw.text((x, y), In, fill="white")
if __name__ == "__main__":
oledinit()
#oledIP()
In = "hello "
oleddisplay(In,15,20)
2.some wrongs
我之前看错了调用的是ssd1306()应该用sh1106,虽然能正常显示,但是在刷新屏幕的时候,可能会出现黑屏的问题。。。。困扰了我。。一个周末。。气死