Skip to content

Commit

Permalink
change from rpi.gpio to gpiozero
Browse files Browse the repository at this point in the history
  • Loading branch information
amcolash committed Sep 12, 2024
1 parent f92faaf commit d1c4533
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 57 deletions.
28 changes: 9 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`
19 changes: 8 additions & 11 deletions src/python/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
11 changes: 4 additions & 7 deletions src/python/power.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import RPi.GPIO as GPIO
from gpiozero import DigitalOutputDevice
from midi_ports import MidiPorts
import time

Expand All @@ -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):
Expand Down
25 changes: 5 additions & 20 deletions tests/interrupt_test.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d1c4533

Please sign in to comment.