- 安裝樹莓派環境下的OLED庫:樹莓派實驗室
-
爲了更好的顯示性能,例如顯示動畫時如對幀率有要求,可以通過修改下面的參數將 I2C 頻率從默認的 100KHz 或 400KHz 提升到 1MHz。
命令:
sudo
nano
/boot/config
.txt
在文件末添加一行:
dtparam=i2c_baudrate=1000000
sudo reboot 重啓 樹莓派即可生效
- 將 badapple 視頻轉爲圖片:《MATLAB》應用 之 用 MATLAB 將 badapple 視頻轉換爲128*64分辨率圖片
- 使用 FileZilla 工具將生成的 badapple 文件夾複製到 badapple.py 文件夾下(或者使用電腦拷貝到樹莓派系統TFT卡)
badapple.py文件:
import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import subprocess
RST = None
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
# Initialize library.
disp.begin()
# Clear display.
disp.clear()
disp.display()
while True:
x = 0
y = -2
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)
font = ImageFont.truetype('123.ttf', 13)
for Test in range(1,50):
# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)
# Shell scripts for system monitoring from here : https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
cmd = "hostname -I | cut -d\' \' -f1"
IP = subprocess.check_output(cmd, shell = True )
cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'"
CPU = subprocess.check_output(cmd, shell = True )
cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%sMB %.2f%%\", $3,$2,$3*100/$2 }'"
MemUsage = subprocess.check_output(cmd, shell = True )
cmd = "df -h | awk '$NF==\"/\"{printf \"Disk: %d/%dGB %s\", $3,$2,$5}'"
Disk = subprocess.check_output(cmd, shell = True )
# Write two lines of text.
draw.text((x+32, y), "Liuzewen pi", font=font, fill=255)
draw.text((x, y+11), "Test: ", font=font, fill=255)
draw.text((x, y+22), "IP: " + str(IP), font=font, fill=255)
draw.text((x, y+32), str(CPU), font=font, fill=255)
draw.text((x, y+42), str(MemUsage), font=font, fill=255)
draw.text((x, y+52), str(Disk), font=font, fill=255)
# Display image.
disp.image(image)
disp.display()
time.sleep(.1)
for begin in range(1,10):
draw.rectangle((0,0,width,height), outline=0, fill=0)
font = ImageFont.truetype('cubic.ttf', 20)
draw.text((x+32, y+24), "Begin", font=font, fill=255)
font = ImageFont.truetype('LED Dot-Matrix.ttf', 14)
draw.text((x+115, y), str(10+1-begin) , font=font, fill=255)
# Display image.
disp.image(image)
disp.display()
time.sleep(1)
for I_image in range(1,6540):#6540
image = Image.open('badapple/'+str(I_image)+'.jpg').convert('1')
disp.image(image)
disp.display()
- 最終 OLED 屏幕 的刷新速率可以達到 30 幀
- badapple.py 中的 123.ttf 、 cubic.ttf 、 LED Dot-Matrix.ttf 均爲字體名,可自行下載字體並放在 badapple.py 目錄下,並將程序中字體修改爲對應字體即可~
字體下載網站:https://www.dafont.com/bitmap.php?page=4
-
命令:sudo python badapple.py 即可看到屏幕動了起來^-^~
- 最終播放效果:稍後上傳到 嗶哩嗶哩