-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patholed_stats.py
More file actions
81 lines (62 loc) · 3.12 KB
/
oled_stats.py
File metadata and controls
81 lines (62 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#oled_project
#author : souvik roychoudhury
#version: 4.1
#feature: with welcome screen || it has only IP, CPU, Temp, RAM and Disk information.
import time
import subprocess
import psutil
from luma.core.interface.serial import i2c
from luma.core.render import canvas
from luma.oled.device import ssd1306
from PIL import ImageFont, ImageDraw
# OLED setup
serial = i2c(port=1, address=0x3C) # Adjust the I2C address if needed
device = ssd1306(serial, width=128, height=64)
# Load fonts
default_font = ImageFont.load_default() # Small default font
ip_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 12) # Larger font for IP
welcome_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 11) # Font for welcome message
def display_welcome_message():
with canvas(device) as draw:
welcome_message = "Welcome to Raspi \nHome Media Center"
# Calculate text size and position to center the text
text_width, text_height = draw.textbbox((0, 0), welcome_message, font=welcome_font)[2:]
text_x = (device.width - text_width) // 2
text_y = (device.height - text_height) // 2
# Draw the welcome message centered
draw.multiline_text((text_x, text_y), welcome_message, font=welcome_font, fill="white", align="center")
# Display the message for 10 seconds
time.sleep(10)
def get_ip_address():
cmd = "hostname -I | cut -d' ' -f1"
return subprocess.check_output(cmd, shell=True).decode('utf-8').strip()
def get_cpu_usage():
return psutil.cpu_percent(interval=1)
def get_memory_usage():
mem = psutil.virtual_memory()
return mem.percent, mem.used / (1024 * 1024 * 1024), mem.total / (1024 * 1024 * 1024)
def get_temperature():
temp = subprocess.check_output(["vcgencmd", "measure_temp"]).decode()
return temp.replace("temp=", "").strip()
def get_disk_usage():
disk = psutil.disk_usage('/')
return disk.percent, disk.used / (1024 * 1024 * 1024), disk.total / (1024 * 1024 * 1024)
# Display the welcome message at startup
display_welcome_message()
# Main loop for displaying system stats
while True:
ip_address = get_ip_address()
cpu_usage = get_cpu_usage()
memory_usage_percent, memory_used, memory_total = get_memory_usage()
temperature = get_temperature()
disk_usage_percent, disk_used, disk_total = get_disk_usage()
with canvas(device) as draw:
# Draw IP address with larger font
draw.text((0, 0), f"IP: {ip_address}", font=ip_font, fill="yellow")
# Combine CPU and Temperature on one line
draw.text((0, 15), f"CPU: {cpu_usage}% Temp: {temperature}", font=default_font, fill="blue")
# Memory Usage in GB with percentage
draw.text((0, 30), f"RAM: {memory_used:.2f}/{memory_total:.2f}GB ({memory_usage_percent}%)", font=default_font, fill="blue")
# Disk Usage in GB with percentage
draw.text((0, 45), f"Disk: {disk_used:.2f}/{disk_total:.2f}GB ({disk_usage_percent}%)", font=default_font, fill="blue")
time.sleep(1)