Advertisement
Guest User

stats3.py

a guest
Feb 19th, 2024
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | Source Code | 0 0
  1. # Import necessary libraries
  2. from board import SCL, SDA
  3. import busio
  4. from PIL import Image, ImageDraw, ImageFont
  5. import adafruit_ssd1306
  6. import subprocess
  7. import psutil
  8.  
  9. # Create the I2C interface
  10. i2c = busio.I2C(SCL, SDA)
  11.  
  12. # Create the SSD1306 OLED class
  13. display = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c)
  14.  
  15. # Clear display
  16. display.fill(0)
  17. display.show()
  18.  
  19. # Create blank image for drawing with mode '1' for 1-bit color
  20. width = display.width
  21. height = display.height
  22. image = Image.new('1', (width, height))
  23.  
  24. # Get drawing object to draw on image
  25. draw = ImageDraw.Draw(image)
  26.  
  27. # Draw a black filled box to clear the image
  28. draw.rectangle((0, 0, width, height), outline=0, fill=0)
  29.  
  30. # Define some constants to allow easy resizing of shapes
  31. padding = -2
  32. top = padding
  33. bottom = height - padding
  34.  
  35. # Move left to right keeping track of the current x position for drawing shapes
  36. x = 0
  37.  
  38. # Load a default font
  39. font = ImageFont.load_default()
  40.  
  41. # Collect system information
  42. def get_system_info():
  43.     # CPU usage
  44.     cpu_usage = f"CPU: {psutil.cpu_percent()}%"
  45.     # RAM usage
  46.     ram = psutil.virtual_memory()
  47.     ram_usage = f"RAM: {ram.percent}%"
  48.     # IP address
  49.     cmd = "hostname -I | cut -d' ' -f1"
  50.     ip_address = subprocess.check_output(cmd, shell=True).decode("utf-8").strip()
  51.     ip_info = f"IP: {ip_address}"
  52.  
  53.     return cpu_usage, ram_usage, ip_info
  54.  
  55. # Display system information on OLED
  56. while True:
  57.     # Draw a black filled box to clear the image
  58.     draw.rectangle((0, 0, width, height), outline=0, fill=0)
  59.  
  60.     # Get system info
  61.     cpu_usage, ram_usage, ip_info = get_system_info()
  62.  
  63.     # Write lines of text
  64.     draw.text((x, top + 0), cpu_usage, font=font, fill=255)
  65.     draw.text((x, top + 8), ram_usage, font=font, fill=255)
  66.     draw.text((x, top + 16), ip_info, font=font, fill=255)
  67.  
  68.     # Display image
  69.     display.image(image)
  70.     display.show()
  71.  
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement