庆祝中华人民共和国成立70周年

高举旗帜 团结一致 锐意进取

python 驱动ssd1306

# -*- coding: utf-8 -*-
import time
import datetime
import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
from PIL import Image,ImageDraw,ImageFont
import psutil
import os
import commands
import re

os.chdir("/home/python/ssd1306")


# 引脚配置,按照上面的接线来配置
RST=17
DC=22
# 因为连的是CE0,这里的PORT和DEVICE也设置为0
SPI_PORT=0
SPI_DEVICE=0

#根据自己的oled型号进行初始化,我的是128X64、SPI的oled,使用SSD1306_128_64初始化
disp=Adafruit_SSD1306.SSD1306_128_64(rst=RST,dc=DC,spi=SPI.SpiDev(SPI_PORT,SPI_DEVICE,max_speed_hz=8000000))

disp.begin()
disp.clear()
disp.display() #清屏

#image 1,绘制了几个图形
width=disp.width
height=disp.height

# # image 3,显示一些文字
image1=Image.new('1',(width,height))
draw1=ImageDraw.Draw(image1)
# line1=ImageFont.load_default()
line1=ImageFont.truetype('fonts/hwls.ttf',20,encoding="unic")
line2=ImageFont.truetype('fonts/hwls.ttf',39,encoding="unic")
line3=ImageFont.truetype('fonts/msyh.ttf',12,encoding="unic")

line1_pos=(0,0)
line2_pos=(0,15)
line3_pos=(0,49)
line3_pos2=(60,49)

def get_data():
    date_str=time.strftime("%Y年%m月%d日",time.localtime())
    return unicode(date_str,'UTF-8')

def get_time():
    time_str=time.strftime("%H:%M:%S",time.localtime()) 
    # time_str=datetime.datetime.now().strftime('%H:%M:%S.%f')
    return unicode(time_str,'UTF-8')

def get_cpu():
    system_str="cpu:"+str(psutil.cpu_percent())+"%"
    return unicode(system_str,'UTF-8')

def get_mem():
    phymem = psutil.virtual_memory()
    system_str = "内存:"+str(phymem.percent)+"%"
    return unicode(system_str,'UTF-8')

def get_ip(device):
    (status, output) = commands.getstatusoutput('ifconfig '+device)
    try:
        pattern = re.compile(r'inet (\d+.\d+.\d+.\d+)')
        ip = pattern.findall(output)
        return ip[0]
    except :
        return unicode("未连接到此网络",'UTF-8')

try:
    i=0
    for i in range(1,101):
        image1=Image.open("start/video_"+str(i)+".jpg").resize((128,64)).convert('1')
        disp.image(image1)
        disp.display()
        time.sleep(0.02)
    time.sleep(1)
    i=0
    while True:
        image1=Image.new('1',(width,height))
        draw1=ImageDraw.Draw(image1)

        date_str=time.strftime("%Y年%m月%d日",time.localtime()) 
        time_str=time.strftime("%H:%M:%S",time.localtime()) 
        draw1.text(line1_pos,get_data(),font=line1,fill=1)
        draw1.text(line2_pos,get_time(),font=line2,fill=1)

        if(int(i/5)%3==1):
            draw1.text(line3_pos,get_cpu(),font=line3,fill=1)
            draw1.text(line3_pos2,get_mem(),font=line3,fill=1)
        elif (int(i/5)%3==2):
            draw1.text(line3_pos,"eth0:"+get_ip("eth0"),font=line3,fill=1)
        else:
            draw1.text(line3_pos,"wlan0:"+get_ip("wlan0"),font=line3,fill=1)

        disp.image(image1)
        disp.display()
        time.sleep(1)
        i+=1
except IOError as e:
    # disp.clear()
    # disp.display()
    image1=Image.new('1',(width,height))
    draw1=ImageDraw.Draw(image1)
    draw1.text((0,17),e,font=font2,fill=1)

    disp.image(image1)
    disp.display()

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注