树莓派0.91 128x32 SSD1306 OLED教程


【资料下载】

[] 原理图

[] ssd1306_oled_128x32.py

[] bad_apple_128x32.gif


【准备工作】

在开始之前,您需要以下物品:

[] 树莓派开发板

[] SSD1306 OLED屏幕-I2C显示接口

[] Python编程环境


硬件连接




打开I2C接口

在树莓派上确保开启I2C接口之后重启树莓派


查找I2C设备地址

您可以使用以下命令查找连接到树莓派上的I2C设备的地址:

sudo i2cdetect -y 1

运行结果如图所示(一般ssd1306oled的默认i2c地址为0x3c)


安装库

接下来,您需要在树莓派上安装luma.oled库。可以通过以下命令在终端中进行安装:

sudo pip3 install luma.oled

sudo apt-get install ttf-mscorefonts-installer


测试Python程序

运行代码之前需要在树莓派新建一个名为ssd1306_oled_128x32_bad_apple.py的python文件(文件名可自定义)

sudo nano ssd1306_oled_128x32_bad_apple.py

组合以上步骤,并使用luma.oled库驱动SSD1306 OLED屏幕,以下是一个完整的Python程序,用于在树莓派上驱动SSD1306 OLED屏幕:

import os

import time

import threading

from PIL import Image, ImageSequence

from PIL import ImageFont, ImageDraw

from luma.core.interface.serial import i2c

from luma.core.render import canvas

from luma.oled.device import ssd1306


serial = i2c(port=1, address=0x3C)

device = ssd1306(serial, width=128, height=32)


# Return CPU temperature as a character string

def getCPUtemperature():

    res = os.popen('vcgencmd measure_temp').readline()

    return(res.replace("temp=","").replace("'C\n",""))


# Return RAM information (unit=kb) in a list

# Index 0: total RAM

# Index 1: used RAM

# Index 2: free RAM

def getRAMinfo():

    p = os.popen('free')

    i = 0

    while 1:

        i = i + 1

        line = p.readline()

        if i==2:

            return(line.split()[1:4])


# Return % of CPU used by user as a character string

def getCPUuse():

    return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip()))


# Return information about disk space as a list (unit included)

# Index 0: total disk space

# Index 1: used disk space

# Index 2: remaining disk space

# Index 3: percentage of disk used

def getDiskSpace():

    p = os.popen("df -h /")

    i = 0

    while 1:

        i = i +1

        line = p.readline()

        if i==2:

            return(line.split()[1:5])


def draw_text(text, width, height, fontsize):

    font = ImageFont.truetype("arial.ttf", fontsize)

    with canvas(device) as draw:

        draw.rectangle(device.bounding_box, outline="white", fill="black")

        # Calculate the width of the text using textlength() instead of textsize()

        w = font.getlength(text)

        # Calculate the height of the text by subtracting the font descent from the font ascent

        ascent, descent = font.getmetrics()

        h = ascent - descent

        # Calculate the position of the text

        x = (width - w) // 2

        y = (height - h) // 2

        # Draw the text using the new width and height values

        draw.text((10, 1), text, font=font, fill="white")




def image_gif():

    # Display each image with an idea

    for frame in frames:

        device.display(frame)

        time.sleep(0.01)


def computer_resources(flag):

    while not flag.is_set():

        # CPU informatiom

        CPU_use = getCPUuse()

        CPU_tmp = getCPUtemperature()

        # RAM information

        # Output is in kb, here I convert it in Mb for readability

        RAM_stats = getRAMinfo()

        RAM_total = round(int(RAM_stats[0]) / 1000,1)

        RAM_used = round(int(RAM_stats[1]) / 1000,1)

        RAM_free = round(int(RAM_stats[2]) / 1000,1)


        # Disk information

        DISK_stats = getDiskSpace()

        DISK_total = DISK_stats[0]

        DISK_used = DISK_stats[1]

        DISK_perc = DISK_stats[3]

        #

        status_text = "CPU_use:  "+CPU_use+"\nCPU_tmp:  "+CPU_tmp


        draw_text(status_text, device.width, device.height, 12)


if __name__ == '__main__':

    # Open a GIF file and create an Image object for each frame

    frames = []

    with Image.open("./bad_apple_128x32.gif") as img:

        for frame in ImageSequence.Iterator(img):

            frames.append(frame.convert("1").resize((128,32)))

    # Create a threading.Event() object for synchronization

    flag = threading.Event()

    # Create a thread to compute system resources usage

    t = threading.Thread(target=computer_resources,args=(flag,))

    while True:

        # Show some text and wait for 2 seconds

        draw_text("www.spotpear.cn\nhttp://www.spotpear.com\n", device.width, device.height, 12)

        time.sleep(2)

        #Start the thread to compute system resources usage,wait for 5 seconds

        t.start()

        time.sleep(5)

        # Set the flag to stop the thread

        flag.set()

        # Wait for the thread to terminate

        t.join()

        # Clear the flag and create a new thread for next loop

        flag.clear()

        t = threading.Thread(target=computer_resources,args=(flag,))

        time.sleep(2)

        # Display Bad Apple animation as GIF images

        image_gif()


请注意,我们在i2c对象中使用了地址"0x3C",这是默认的SSD1306 OLED屏幕地址。如果您使用的是其他类型的I2C设备,请使用命令

sudo i2cdetect -y 1查找其地址,并将其地址替换为i2c对象中的地址。

该程序实现了gif的显示效果,所以必须得有对应的gif文件,否则程序无法正常运行。 如何转换视频为gif参考这个教程:MP4视频如何转化为gif


程序执行效果

上述代码输入到python文件内,保存并且退出,在树莓派终端输入以下代码即可执行程序:

sudo python3 ssd1306_oled_128x32_bad_apple.py