add oled to home pi

This commit is contained in:
2019-06-02 20:03:47 +10:00
parent 71aaf6e9c4
commit bc749b4147
3 changed files with 94 additions and 10 deletions
+1
View File
@@ -0,0 +1 @@
.vscode
+2 -2
View File
@@ -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
+91 -8
View File
@@ -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)