diff --git a/README.md b/README.md index 1d725f5..83c129f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,79 @@ # PiDisplay Setup and use the Pi OLED HAT by Waveshare + +128x64, 1.3inch OLED display HAT for Raspberry Pi + +The **1.3" OLED Display HAT for [Raspberry Pi](https://www.raspberrypi.org/)** is a display HAT of the +same size of a [Pi Zero](https://www.raspberrypi.org/products/raspberry-pi-zero/). The information +provided by the manufacturer [Waveshare Electronics](https://www.waveshare.com/) is somewhat awkward, +I decided to document my journey here, along with the Python script I adopted to control the display. + +## Hardware +The display attaches to a Pi Zero via the GPIO. It is a 128 x 64 1.3" blue OLED display with a 5-ways +joystick on one side, and 3 buttons on the other: +* Driver: SH1106 +* Interface: 4-wire SPI, 3-wire SPI, I2C +* Display color: blue +* Resolution: 128 x 64 +* Operating voltage: 3.3V +The display has some jumpers at the back to be soldered to enable/disable the interfaces it uses. +I decided to use the default 4-wire SPI which require no soldering at all. + +## Setup +I decided to use Python to control my display. There are other options from the manufacturer but I +didn't explore them. + +_**Note: The following steps are deduced from the documents from the manufacturer. I performed them +in the exact order listed below, but I'm not sure if the ordering is necessary**_ + +### Prepare raspbian +* `sudo apt-get update` +* `sudo apt-get install python-dev` + +### Install RPi.GPIO +* Download RPi.GPIO from [https://pypi.python.org/pypi/RPi.GPIO](https://pypi.python.org/pypi/RPi.GPIO) to somewhere. +* Extract the content (`tar xvfz RPi.GPIO-x.y.z.tar.gz`) +* `cd RPi.GPIO-x.y.z` +* `sudo python setup.py install` + +### Install spidev +* Download spidev from [https://pypi.python.org/pypi/spidev](https://pypi.python.org/pypi/spidev) to somewhere. +* Extract the content (`tar xvfz spidev-x.y.tar.gz`) +* `sudo apt-get install python-smbus` +* `sudo apt-get install python-serial` +* `cd spidev-x.y` +* `sudo python setup.py install` + +### Install Python Imaging +* `sudo apt-get install python-imaging` + +### Enable the SPI interface +* `sudo raspi-config` +* Go to '5 Interfacing Options' +* Go to 'P4 SPI' +* Choose 'Yes' + +### Enable the I2C interface +I'm not sure if this is necessary, but well what the hack... +* `sudo raspi-config` +* Go to '5 Interfacing Options' +* Go to 'P5 I2C' +* Choose 'Yes' + +### Install the luma.oled driver +* `sudo apt-get install python-pip libfreetype6-dev libjpeg-dev` +* `sudo -H pip install --upgrade pip` +* `sudo apt-get purge python-pip` +* `sudo -H pip install --upgrade luma.oled` + +## Test +That's it, the display is ready to use. + +### Sample python app +* Download the sample apps from [https://www.waveshare.com/wiki/File:1.3inch-OLED-HAT-Code.7z](https://www.waveshare.com/wiki/File:1.3inch-OLED-HAT-Code.7z) to somewhere +* Extract the content somehow, I'm too lazy to find out how to handle 7z on Linux... +* `cd` to the subfolder containing the Python sample +* `sudo python demo.py` + +## Next step +The Python code in this repo are adopted from `demo.py` by the manufacturer. \ No newline at end of file diff --git a/monitor.py b/monitor.py new file mode 100644 index 0000000..4671102 --- /dev/null +++ b/monitor.py @@ -0,0 +1,94 @@ +# -*- coding:utf-8 -*- + +from luma.core.interface.serial import i2c, spi +from luma.core.render import canvas +from luma.core import lib + +from luma.oled.device import sh1106 +import RPi.GPIO as GPIO + +import time +import subprocess + +from PIL import Image +from PIL import ImageDraw +from PIL import ImageFont + +#GPIO define +RST_PIN = 25 +CS_PIN = 8 +DC_PIN = 24 +KEY_UP_PIN = 6 +KEY_DOWN_PIN = 19 +KEY_LEFT_PIN = 5 +KEY_RIGHT_PIN = 26 +KEY_PRESS_PIN = 13 +KEY1_PIN = 21 +KEY2_PIN = 20 +KEY3_PIN = 16 + +#init GPIO +GPIO.setmode(GPIO.BCM) +GPIO.setup(KEY_UP_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up +GPIO.setup(KEY_DOWN_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up +GPIO.setup(KEY_LEFT_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up +GPIO.setup(KEY_RIGHT_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up +GPIO.setup(KEY_PRESS_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up +GPIO.setup(KEY1_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up +GPIO.setup(KEY2_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up +GPIO.setup(KEY3_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up + +font = ImageFont.load_default() +width = 128 +height = 64 + +padding = -2 +top = padding +bottom = height-padding +xpos = 0 + +state = 0 +press = 0 +try: + while 1: + if state == 0: + if GPIO.input(KEY_PRESS_PIN): # button is release + if press == 1: + #print("state 0 -> 1") + press = 0 + state = 1 + serial = spi(device=0, port=0, bus_speed_hz = 8000000, transfer_size = 4096, gpio_DC = DC_PIN, gpio_RST = RST_PIN) + device = sh1106(serial, rotate=2) #sh1106 + image = Image.new('1', (width, height)) + draw = ImageDraw.Draw(image) + draw.rectangle((0,0,width,height), outline=0, fill=0) + else: # button is pressed: + press = 1 + + if state == 1: + with canvas(device) as draw: + 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 ) + + draw.text((xpos, top), "IP: " + str(IP), font=font, fill=255) + draw.text((xpos, top+8), str(CPU), font=font, fill=255) + draw.text((xpos, top+16), str(MemUsage), font=font, fill=255) + draw.text((xpos, top+25), str(Disk), font=font, fill=255) + + if GPIO.input(KEY_PRESS_PIN): # button is released + if press == 1: + #print("state 1 -> 0") + GPIO.output(RST_PIN,GPIO.LOW) + press = 0 + state = 0 + else: # button is pressed: + press = 1 +except: + print("except") +GPIO.cleanup() \ No newline at end of file