代码拉取完成,页面将自动刷新
import os
import time
from PIL import Image, ImageDraw, ImageFont
import mmap
import numpy as np
import netifaces
def get_ip(ip_head='192.168.'):
all_ips = []
for interface in netifaces.interfaces():
if netifaces.AF_INET in netifaces.ifaddresses(interface):
ips = netifaces.ifaddresses(interface)[netifaces.AF_INET]
for ip_info in ips:
all_ips.append(ip_info['addr'])
for ipi in all_ips:
if ip_head in ipi:
ip=ipi
return f"{ip}"
def get_cpu_usage(lang):
if lang == "en":
cpu_percent = (
os.popen("mpstat 1 1 | awk '/Average:/ {printf \"%.1f\", 100-$12}'")
.read()
.strip()
)
elif lang == "zh":
cpu_percent = cpu_percent = (
os.popen("mpstat 1 1 | awk '/平均时间:/ {printf \"%.1f\", 100-$12}'")
.read()
.strip()
)
return f"CPU{cpu_percent}%"
def get_system_temp():
cpu_temp = (
os.popen("sensors | grep 'temp1' | head -n 1 | awk '{print $2}'").read().strip().replace("+", "")
)
return f"{cpu_temp}"
def get_network():
net_rx0, net_tx0 = get_network_speed("eth0")
net_rx1, net_tx1 = get_network_speed("enx00e04c680001")
rx_speed = net_rx0 + net_rx1
tx_speed = net_tx0 + net_tx1
if rx_speed < 1:
rx_speed_str = f"{rx_speed:.1f}"
else:
rx_speed_str = f"{int(rx_speed)}"
if tx_speed < 1:
tx_speed_str = f"{tx_speed:.1f}"
else:
tx_speed_str = f"{int(tx_speed)}"
return f"↓ {rx_speed_str}", f"↑ {tx_speed_str}M/s"
def get_network_speed(interface):
rx = os.popen(f"cat /sys/class/net/{interface}/statistics/rx_bytes").read().strip()
tx = os.popen(f"cat /sys/class/net/{interface}/statistics/tx_bytes").read().strip()
time.sleep(1)
rx_new = (
os.popen(f"cat /sys/class/net/{interface}/statistics/rx_bytes").read().strip()
)
tx_new = (
os.popen(f"cat /sys/class/net/{interface}/statistics/tx_bytes").read().strip()
)
rx_speed = (int(rx_new) - int(rx)) / 1024 / 1024
tx_speed = (int(tx_new) - int(tx)) / 1024 / 1024
return rx_speed, tx_speed
def draw_text(draw, text, position, font, fill):
draw.text(position, text, font=font, fill=fill)
def update_image(image, font_path, font_size, lang):
draw = ImageDraw.Draw(image)
font = ImageFont.truetype(font_path, font_size)
cpu_usage = get_cpu_usage(lang)
system_temp = get_system_temp()
net_rx, net_tx = get_network()
ip=get_ip('192.168.')
#net_rx1, net_tx1 = get_network_speed("enx00e04c680001")
draw_text(draw, cpu_usage, (0, 0), font, fill=(255, 10, 10))
draw_text(draw, system_temp, (90, 0), font, fill=(10, 10, 240))
draw_text(draw, ip, (2, 27), font, fill=(245, 245, 245))
draw_text(draw, net_rx, (2, 54), font, fill=(245, 245, 245))
#draw_text(draw, net_rx1, (2, 27), font, fill=(245, 245, 245))
draw_text(draw, net_tx, (62, 54), font, fill=(245, 245, 245))
return image
def rgba_to_rgb565(rgba_data):
rgb565_data = bytearray(len(rgba_data) // 2)
for i in range(0, len(rgba_data), 4):
r = rgba_data[i] & 0xF8
g = rgba_data[i + 1] & 0xFC
b = rgba_data[i + 2] & 0xF8
rgb565_data[i // 2] = r | g >> 5
rgb565_data[i // 2 + 1] = ((g & 0x1C) << 3) | b >> 3
return rgb565_data
def rgba_to_rgb565_new(rgba_bytes):
# 将字节转换为numpy数组
rgba_array = np.frombuffer(rgba_bytes, dtype=np.uint8).reshape([-1, 4])
# 提取R, G, B分量
r = rgba_array[:, 0]
g = rgba_array[:, 1]
b = rgba_array[:, 2]
# 将R、G、B分量转换为对应的5位和6位,并组合成一个16位整数
rgb565_array = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3)
# 将numpy数组转换回字节,并返回
return rgb565_array.tobytes()
def main():
width = 160
height = 80
bytes_per_pixel = 2
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
# font_path = "/usr/share/fonts/truetype/cascadia-code/CascadiaCodePL.ttf"
font_size = 18 #22
image = Image.new("RGB", (width, height), color=(0, 0, 0))
lang = "en"
locale = os.popen("locale | awk '/LANG=/'").read().strip()
if "zh" in locale:
lang = "zh"
try:
with open("/dev/fb0", "r+b") as fb:
fb_map = mmap.mmap(
fb.fileno(),
width * height * bytes_per_pixel,
mmap.MAP_SHARED,
mmap.PROT_WRITE,
)
while True:
updated_image = update_image(image.copy(), font_path, font_size, lang)
rgba_data = updated_image.convert("RGBA").tobytes()
#rgb565_data = rgba_to_rgb565(rgba_data)
rgb565_data = rgba_to_rgb565(rgba_data)
fb_map.seek(0)
fb_map.write(rgb565_data)
time.sleep(1)
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()
# install dep
# apt-get install python3 python3-pip python3-dev sysstat #fonts-dejavu lm-sensors
# pip3 install Pillow numpy -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
# if goes wrong
# apt install python3-venv
# python3 -m venv .venv
# source .venv/bin/activate
# python3 -m pip install Pillow numpy -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
# .venv/bin/python3 lcd_CPU_IP_NetSpeed.py
# use ifconfig to check network "eth0" and "eth1"
#def get_network():
# net_rx0, net_tx0 = get_network_speed("eth0")
# net_rx1, net_tx1 = get_network_speed("eth1")
#def get_network():
# net_rx0, net_tx0 = get_network_speed("eth0")
# net_rx1, net_tx1 = get_network_speed("enx00e04c680001")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。