From d1c453320006ee221f87a5cb46800d18f74916c3 Mon Sep 17 00:00:00 2001 From: Andrew McOlash Date: Wed, 11 Sep 2024 20:56:35 -0700 Subject: [PATCH] change from rpi.gpio to gpiozero --- README.md | 28 +++++++++------------------- src/python/display.py | 19 ++++++++----------- src/python/power.py | 11 ++++------- tests/interrupt_test.py | 25 +++++-------------------- 4 files changed, 26 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 76c8ab6..99ee83c 100644 --- a/README.md +++ b/README.md @@ -4,33 +4,19 @@ LEDs for my piano! ## Requirements -- `RPi-GPIO` - installed by default +- `python3-gpiozero` - `libopenjp2-7-dev` - `libjack-jackd2-dev` -### NOTE: On New PiOS versions, you need to remove the legacy version of this in favor of the newer version -``` -sudo apt remove python3-rpi.gpio -sudo apt install python3-rpi-lgpio -``` - ### Dev Requirements -- entr (`sudo apt install entr`) - -### Combined - -`sudo apt remove python3-rpi.gpio && sudo apt-get install python3-rpi-lgpio libopenjp2-7-dev libjack-jackd2-dev entr` +- `entr` -## Python Dependencies can be installed with `pip`. +### Install everything at ounce -Install pip if necessary +`sudo apt-get python3-gpiozero libopenjp2-7-dev libjack-jackd2-dev python3-pip entr` -`sudo apt install python3-pip` - -Then install the dependencies as root using `sudo` Since this will likely be the only python program, it should be fine :fingers-crossed:. - -`sudo pip install --break-system-packages adafruit-circuitpython-ssd1306 jsonpickle mido python-rtmidi pytz rpi_ws281x smbus2 pillow` +## Python dependencies - adafruit-circuitpython-ssd1306 - jsonpickle @@ -40,3 +26,7 @@ Then install the dependencies as root using `sudo` Since this will likely be the - rpi_ws281x - smbus2 - pillow + +Install the dependencies as root using `sudo` Since this will likely be the only python program running on the pi, it should be fine. + +`sudo pip install --break-system-packages adafruit-circuitpython-ssd1306 jsonpickle mido python-rtmidi pytz rpi_ws281x smbus2 pillow` diff --git a/src/python/display.py b/src/python/display.py index c637c4b..c48a517 100644 --- a/src/python/display.py +++ b/src/python/display.py @@ -2,7 +2,7 @@ from board import SCL, SDA import busio from PIL import Image, ImageDraw, ImageFont, ImageOps -import RPi.GPIO as GPIO +from gpiozero import Button import sys import time @@ -64,16 +64,13 @@ def __init__(self): self.lastNotesTime = time.time() # Set up GPIO Pins - GPIO.setup(DOWN_BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP) - GPIO.setup(ENTER_BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP) - GPIO.setup(UP_BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP) - - try: - GPIO.add_event_detect(DOWN_BUTTON, GPIO.FALLING, callback=self.button_callback, bouncetime=300) - GPIO.add_event_detect(ENTER_BUTTON, GPIO.FALLING, callback=self.button_callback, bouncetime=300) - GPIO.add_event_detect(UP_BUTTON, GPIO.FALLING, callback=self.button_callback, bouncetime=300) - except RuntimeError as e: - print(f"Error: {e}") + down = Button(DOWN_BUTTON, bounce_time=0.3) + enter = Button(ENTER_BUTTON, bounce_time=0.3) + up = Button(UP_BUTTON, bounce_time=0.3) + + down.when_pressed = lambda: self.button_callback(DOWN_BUTTON) + enter.when_pressed = lambda: self.button_callback(ENTER_BUTTON) + up.when_pressed = lambda: self.button_callback(UP_BUTTON) # Init i2c bus i2c = busio.I2C(SCL, SDA) diff --git a/src/python/power.py b/src/python/power.py index 936ae8d..ea5a427 100644 --- a/src/python/power.py +++ b/src/python/power.py @@ -1,4 +1,4 @@ -import RPi.GPIO as GPIO +from gpiozero import DigitalOutputDevice from midi_ports import MidiPorts import time @@ -8,16 +8,13 @@ class Power: @classmethod def init(cls): # Set power pin to output - GPIO.setup(POWER_PIN, GPIO.OUT) - - # Set default power state to off - GPIO.output(POWER_PIN, GPIO.LOW) + cls.power = DigitalOutputDevice(POWER_PIN, initial_value=False) @classmethod def toggle(cls): - GPIO.output(POWER_PIN, GPIO.HIGH) + cls.power.on() time.sleep(0.5) - GPIO.output(POWER_PIN, GPIO.LOW) + cls.power.off() @classmethod def on(cls): diff --git a/tests/interrupt_test.py b/tests/interrupt_test.py index e3b80f5..36761e8 100644 --- a/tests/interrupt_test.py +++ b/tests/interrupt_test.py @@ -1,22 +1,7 @@ -import RPi.GPIO as GPIO +from gpiozero import Button -BUTTON = 12 # Board pin number +button = Button(7) +button.when_pressed = lambda: print("Pressed") -GPIO.cleanup() - -GPIO.setmode(GPIO.BOARD) -GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP) - -try: - GPIO.add_event_detect(BUTTON, GPIO.FALLING, bouncetime=300) - print("Event detection added successfully") -except RuntimeError as e: - print(f"Error: {e}") - -# Keep the program running -try: - while True: - if GPIO.event_detected(BUTTON): - print("Edge detected") -except KeyboardInterrupt: - GPIO.cleanup() +while True: + pass