RaspBerry pi 驱动oled12864

一、材料准备

  • 树莓派4b,安装rasbian系统的内存卡,支持IIC协议的OLED屏幕,杜邦线

    二、接线

    树莓派 oled
    3.3v –> VCC (VDD)
    GPIO2(SDL) –> SDL(SDA)
    GPIO3(SCL) –> SCL(SCK)
    GND –> GND

三、关于I2C(IIC)协议

IIC协议与OLED的使用

四、驱动OLED

  1. 安装Adafruit库
    sudo pip3 install adafruit-circuitpython-ssd1306
  2. 使用examples
    git clone https://github.com/adafruit/Adafruit_CircuitPython_SSD1306
  3. 显示各种信息
    例一、
    import time
    import subprocess
    import datetime
    from board import SCL, SDA
    import busio
    from PIL import Image, ImageDraw, ImageFont
    import adafruit_ssd1306
    i2c = busio.I2C(SCL, SDA)
    disp = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c)
    disp.fill(0)
    disp.show()
    width = disp.width
    height = disp.height
    image = Image.new("1", (width, height))
    draw = ImageDraw.Draw(image)
    draw.rectangle((0, 0, width, height), outline=0, fill=0)
    padding = -2
    top = padding
    bottom = height - padding
    font1 = ImageFont.load_default()
    font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 9)
    font2 = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 14)
    font3 = ImageFont.truetype("/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc", 22)
    while True:
        draw.rectangle((0, 0, width, height), outline=0, fill=0)
        cmd = "hostname -I | cut -d' ' -f1"
        IP = subprocess.check_output(cmd, shell=True).decode("utf-8")
        cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'"
        CPU = subprocess.check_output(cmd, shell=True).decode("utf-8")
        cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%s MB %.2f%%\", $3,$2,$3*100/$2 }'"
        MemUsage = subprocess.check_output(cmd, shell=True).decode("utf-8")
        cmd = 'df -h | awk \'$NF=="/"{printf "Disk: %d/%d GB  %s", $3,$2,$5}\''
        Disk = subprocess.check_output(cmd, shell=True).decode("utf-8")
        draw.text((x, top + 0), "IP: " + IP, font=font, fill=255)
        draw.text((x, top + 8), CPU, font=font, fill=255)
        draw.text((x, top + 16), MemUsage, font=font, fill=255)
        draw.text((x, top + 25), Disk, font=font, fill=255)
    
    #    intro = "Hello!"
    #    draw.text((0, 34), intro, font=font2, fill=255)
    #    draw.text((0, 34), u'黄家星是沙雕', font=font3, fill=255)
        now = datetime.datetime.now()
        today_date = now.strftime("%d %b %y")
        today_time = now.strftime("%H:%M:%S")
        draw.text((x, top + 35), today_date, font=font, fill=255)
        draw.text((x, top + 45), today_time, font=font, fill=255)
        disp.image(image)
        disp.show()
        time.sleep(0.1)
    例二、播放badapple
    import time
    import subprocess
    import datetime
    from board import SCL, SDA
    import busio
    from PIL import Image, ImageDraw, ImageFont
    import adafruit_ssd1306
    i2c = busio.I2C(SCL, SDA)
    
    disp = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c)
    disp.fill(0)
    disp.show()
    
    width = disp.width
    height = disp.height
    image = Image.new("1", (width, height))
    
    draw = ImageDraw.Draw(image)
    
    draw.rectangle((0, 0, width, height), outline=0, fill=0)
    while True:
        for i in range(2,6551):
            disp.show()
            image = Image.open("bad ("+str(i)+").png").convert('1')
            disp.image(image)
    
    资料