From bc749b4147c656bda67b3d1700631b06c9664f3e Mon Sep 17 00:00:00 2001 From: Junv Zhao Date: Sun, 2 Jun 2019 20:03:47 +1000 Subject: [PATCH] add oled to home pi --- .gitignore | 1 + stats.py | 4 +-- x.py | 99 +++++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 94 insertions(+), 10 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..722d5e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode diff --git a/stats.py b/stats.py index ce1cb63..664b3c2 100644 --- a/stats.py +++ b/stats.py @@ -44,7 +44,7 @@ SPI_DEVICE = 0 # SPI_DEVICE = 0 # 128x32 display with hardware I2C: -disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST) +# disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST) # 128x64 display with hardware I2C: # disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST) @@ -60,7 +60,7 @@ disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST) # disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST, dc=DC, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=8000000)) # 128x64 display with hardware SPI: -# disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, dc=DC, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=8000000)) +disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, dc=DC, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=8000000)) # Alternatively you can specify a software SPI implementation by providing # digital GPIO pin numbers for all the required display pins. For example diff --git a/x.py b/x.py index 8c072e9..b834628 100644 --- a/x.py +++ b/x.py @@ -2,6 +2,15 @@ import usb.core import usb.util import time import RPi.GPIO as GPIO +import Adafruit_GPIO.SPI as SPI +import Adafruit_SSD1306 + +from PIL import Image +from PIL import ImageDraw +from PIL import ImageFont + +import subprocess + USB_IF = 0 # Interface USB_TIMEOUT = 5 # Timeout in MS @@ -18,6 +27,13 @@ BTN_EXIT = 32 # ESC USB_VENDOR = 0x045E # Rii USB_PRODUCT = 0x028E # Mini Wireless Keyboard +# Raspberry Pi pin configuration: +RST = None # on the PiOLED this pin isnt used +# Note the following are only used with SPI: +DC = 23 +SPI_PORT = 0 +SPI_DEVICE = 0 + ForwardRight = 20 BackwardRight = 26 @@ -25,13 +41,6 @@ ForwardLeft = 18 BackwardLeft = 17 -GPIO.setmode(GPIO.BCM) -GPIO.setup(ForwardRight, GPIO.OUT) -GPIO.setup(BackwardRight, GPIO.OUT) -GPIO.setup(ForwardLeft, GPIO.OUT) -GPIO.setup(BackwardLeft, GPIO.OUT) - - def forward(): GPIO.output(ForwardLeft, GPIO.HIGH) GPIO.output(ForwardRight, GPIO.HIGH) @@ -63,6 +72,79 @@ def clear(): GPIO.output(BackwardLeft, GPIO.LOW) +def init_display(): + # create display instance + disp = Adafruit_SSD1306.SSD1306_128_64( + rst=RST, dc=DC, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=8000000) + ) + + # Initialize library. + disp.begin() + + # Clear display. + disp.clear() + disp.display() + + # Create blank image for drawing. + # Make sure to create image with mode '1' for 1-bit color. + width = disp.width + height = disp.height + image = Image.new("1", (width, height)) + + # Get drawing object to draw on image. + draw = ImageDraw.Draw(image) + + # Draw a black filled box to clear the image. + draw.rectangle((0, 0, width, height), outline=0, fill=0) + return disp, draw, image + + +def draw_stats(disp, draw, image): + width = disp.width + height = disp.height + # Draw a black filled box to clear the image. + draw.rectangle((0, 0, width, height), outline=0, fill=0) + + # Shell scripts for system monitoring from here : https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load + cmd = "hostname -I | cut -d' ' -f1" + IP = subprocess.check_output(cmd, shell=True) + cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'" + CPU = subprocess.check_output(cmd, shell=True) + cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%sMB %.2f%%\", $3,$2,$3*100/$2 }'" + MemUsage = subprocess.check_output(cmd, shell=True) + cmd = 'df -h | awk \'$NF=="/"{printf "Disk: %d/%dGB %s", $3,$2,$5}\'' + Disk = subprocess.check_output(cmd, shell=True) + + # Write two lines of text. + + draw.text((x, top), "IP: " + str(IP), font=font, fill=255) + draw.text((x, top + 8), str(CPU), font=font, fill=255) + draw.text((x, top + 16), str(MemUsage), font=font, fill=255) + draw.text((x, top + 25), str(Disk), font=font, fill=255) + + # Display image. + disp.image(image) + disp.display() + time.sleep(0.1) + + +GPIO.setmode(GPIO.BCM) +GPIO.setup(ForwardRight, GPIO.OUT) +GPIO.setup(BackwardRight, GPIO.OUT) +GPIO.setup(ForwardLeft, GPIO.OUT) +GPIO.setup(BackwardLeft, GPIO.OUT) + +disp, draw, image = init_display() +# First define some constants to allow easy resizing of shapes. +padding = -2 +top = padding +bottom = height - padding +# Move left to right keeping track of the current x position for drawing shapes. +x = 0 +# Load default font. +font = ImageFont.load_default() + +# main logic starts dev = usb.core.find(idVendor=USB_VENDOR, idProduct=USB_PRODUCT) endpoint = dev[0][(0, 0)][0] @@ -73,6 +155,7 @@ if dev.is_kernel_driver_active(USB_IF) is True: usb.util.claim_interface(dev, USB_IF) while True: + draw_stats(disp, draw, image) control = None try: @@ -109,6 +192,6 @@ while True: clear() exit() else: + # Let CTRL+C actually exit clear() - # Let CTRL+C actually exit time.sleep(SLEEP_TIME)