from luma.core.interface.serial import i2c, spi
from luma.core.render import canvas
from luma.oled.device import ssd1306
import socket
import fcntl
import struct
import subprocess
import psutil
import time
import netifaces
import datetime
# 创建 IIC 设备
serial = i2c(port=1, address=0x3C)
# 如果使用 SPI,换成这个
# serial = spi(device=0, port=0)
# 创建屏幕的驱动实例
device = ssd1306(serial)
def get_system_stats():
# 通过 shell 命令获取 CPU 使用率和内存使用率
cpu_command = "top -bn1 | grep '%Cpu(s)' | awk '{print $2}'"
memory_command = "free | grep Mem | awk '{print $3/$2 * 100.0}'"
# 执行命令并获取输出结果
cpu_result = subprocess.check_output(cpu_command, shell=True).decode().strip()
memory_result = subprocess.check_output(memory_command, shell=True).decode().strip()
# 转换结果为浮点数
cpu_usage = float(cpu_result)
memory_usage = float(memory_result)
# 返回 CPU 使用率和内存使用率
return cpu_usage, memory_usage
def get_local_ip_address():
ip_address = 'no network'
try:
# 获取网络接口列表
interfaces = netifaces.interfaces()
# 查找第一个非本地回环接口的IP地址
for interface in interfaces:
if interface == 'lo':
continue
addresses = netifaces.ifaddresses(interface)
ip_addresses = addresses.get(netifaces.AF_INET)
if ip_addresses:
ip_address = ip_addresses[0]['addr']
break
except:
pass
return ip_address
def networkShow():
# 测网速
last_total_bytes_sent = psutil.net_io_counters().bytes_sent
last_total_bytes_recv = psutil.net_io_counters().bytes_recv
time.sleep(1)
total_bytes_sent = psutil.net_io_counters().bytes_sent
total_bytes_recv = psutil.net_io_counters().bytes_recv
diff_bytes_sent = (total_bytes_sent - last_total_bytes_sent) / 1024
diff_bytes_recv = (total_bytes_recv - last_total_bytes_recv) / 1024
download_speed = diff_bytes_recv / 1
upload_speed = diff_bytes_sent / 1
return networkFormat(download_speed), networkFormat(upload_speed)
def networkFormat(speed):
if speed < 1024:
return str(int(speed)) + "K"
elif 1024 < speed < 1024**2:
return str(round(speed/1024, 1)) + "M"
else:
return str(round(speed/(1024**2), 1)) + "G"
# 开始往屏幕上绘图。draw 是 Pillow 的实例,它里面还有非常多的绘图 API。
def run():
with canvas(device) as draw:
ip_address = get_local_ip_address()
mem = psutil.virtual_memory()
total = round(mem.total / 1024 / 1024 / 1024, 1)
used = round(mem.used / 1024 / 1024 / 1024, 1)
free = round(mem.free / 1024 / 1024 / 1024, 1)
temps = psutil.sensors_temperatures()
cpu_percent = psutil.cpu_percent(interval=1)
if temps is None:
temp = 0
else:
for name, entries in temps.items():
for entry in entries:
temp = entry.current
break
break
download_speed, upload_speed = networkShow()
draw.rectangle(device.bounding_box, outline="white", fill="black")
draw.text((1, 1), "ip:" + ip_address, fill="white")
draw.text((1, 14), "cpu:" + str(int(temp)) + "°C ", fill="white")
draw.text((60, 14), "use:" + str(int(cpu_percent)) + "%", fill="white")
draw.text((1, 28), "mem: t" + str(total) + " u" + str(used) + " f" + str(free) + "G", fill="white")
draw.text((1, 38), "up:" + upload_speed + " down:" + download_speed, fill="white")
draw.text((1, 50), datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), fill="white")
# 这行是为了阻止程序退出,因为退出的时候会调用析构函数,清空屏幕。防止一闪而过,什么也看不到。
while (True):
run()
time.sleep(1)
树莓派驱动屏幕显示系统信息
最新推荐文章于 2024-06-01 22:51:14 发布