Skip to content

Commit d5b5f0c

Browse files
committed
Switch to gpiozero fully
1 parent 20fabe2 commit d5b5f0c

File tree

5 files changed

+55
-28
lines changed

5 files changed

+55
-28
lines changed

Dynamic_RDS.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ public static function isPython3SmbusInstalled(): bool {
154154
return !ShellCommandExecutor::isEmpty($output);
155155
}
156156

157+
public static function isPython3gpiozeroInstalled(): bool {
158+
$output = ShellCommandExecutor::execute('dpkg -s python3-gpiozero | grep installed');
159+
return !ShellCommandExecutor::isEmpty($output);
160+
}
161+
157162
public static function isEngineRunning(): bool {
158163
$output = ShellCommandExecutor::execute('ps -ef | grep python.*Dynamic_RDS_Engine.py | grep -v grep');
159164
if (!ShellCommandExecutor::isEmpty($output))
@@ -285,6 +290,10 @@ function renderDynamicRDSStatus(
285290
$status->addError('python3-smbus2 is missing <button name="ReinstallScript" onClick="DynRDSScriptStream(\'dependencies\')">Reinstall plugin dependencies</button>');
286291
}
287292

293+
if ($platform === PlatformType::RASPBERRY_PI && !DependencyChecker::isPython3gpiozeroInstalled()) {
294+
$status->addError('python3-gpiozero is missing <button name="ReinstallScript" onClick="DynRDSScriptStream(\'dependencies\')">Reinstall plugin dependencies</button>');
295+
}
296+
288297
// Detect I2C bus
289298
$i2cBus = I2CBusDetector::detectBus($platform);
290299
if ($i2cBus === -1) {

basicPWM.py

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,35 +59,58 @@ def shutdown(self):
5959

6060
class softwarePWM(basicPWM):
6161
def __init__(self, pinToUse=7):
62-
logging.info('Initializing software PWM on pin %s', pinToUse)
63-
global GPIO
64-
from RPi import GPIO
62+
logging.info('Initializing software PWM on GPIO pin %s (board pin %s)', self._board_to_bcm(pinToUse), pinToUse)
63+
global PWMLED
64+
from gpiozero import PWMLED
65+
# Convert board pin to BCM GPIO number
66+
bcm_pin = self._board_to_bcm(pinToUse)
6567
self.pinToUse = pinToUse
68+
self.bcm_pin = bcm_pin
6669
self.pwm = None
67-
# TODO: Ponder if import RPi.GPIO as GPIO is a good idea
68-
# TODO: Look at switching to gpiozero
69-
GPIO.setmode(GPIO.BOARD)
70-
GPIO.setup(self.pinToUse, GPIO.OUT)
71-
GPIO.output(self.pinToUse,0)
70+
71+
# Create PWMLED device (starts at 0% duty cycle, off)
72+
self.pwm = PWMLED(bcm_pin, initial_value=0)
7273
super().__init__()
7374

75+
def _board_to_bcm(self, board_pin):
76+
"""Convert board pin number to BCM GPIO number."""
77+
# Mapping for 40-pin Raspberry Pi header (board -> BCM)
78+
board_to_bcm_map = {
79+
7: 4, 8: 14, 10: 15, 11: 17, 12: 18, 13: 27,
80+
15: 22, 16: 23, 18: 24, 19: 10, 21: 9, 22: 25,
81+
23: 11, 24: 8, 26: 7, 27: 0, 28: 1, 29: 5,
82+
31: 6, 32: 12, 33: 13, 35: 19, 36: 16, 37: 26,
83+
38: 20, 40: 21
84+
}
85+
86+
if board_pin not in board_to_bcm_map:
87+
raise ValueError(f'Invalid board pin number: {board_pin}')
88+
89+
return board_to_bcm_map[board_pin]
90+
7491
def startup(self, period=10000, dutyCycle=0):
75-
logging.debug('Starting software PWM on pin %s with period of %s', self.pinToUse, period)
76-
self.pwm = GPIO.PWM(self.pinToUse, period)
77-
logging.info('Updating software PWM on pin %s initial duty cycle to %s', self.pinToUse, round(dutyCycle/3,2))
78-
self.pwm.start(dutyCycle/3)
92+
# gpiozero uses frequency in Hz, convert from period in microseconds
93+
# frequency = 1 / (period / 1,000,000)
94+
frequency = 1_000_000 / period
95+
logging.debug('Starting software PWM on GPIO %s (board pin %s) with frequency %.2f Hz',self.bcm_pin, self.pinToUse, frequency)
96+
self.pwm.frequency = frequency
97+
initial_value = (dutyCycle / 3) / 100
98+
logging.info('Setting software PWM on GPIO %s initial duty cycle to %.2f%%', self.bcm_pin, dutyCycle / 3)
99+
self.pwm.value = initial_value
79100
super().startup()
80101

81102
def update(self, dutyCycle=0):
82-
logging.info('Updating software PWM on pin %s duty cycle to %s', self.pinToUse, round(dutyCycle/3,2))
83-
self.pwm.ChangeDutyCycle(dutyCycle/3)
103+
value = (dutyCycle / 3) / 100
104+
logging.info('Updating software PWM on GPIO %s duty cycle to %.2f%%', self.bcm_pin, dutyCycle / 3)
105+
self.pwm.value = value
84106
super().update()
85107

86108
def shutdown(self):
87-
logging.debug('Shutting down software PWM on pin %s', self.pinToUse)
88-
self.pwm.stop()
89-
logging.info('Cleaning up software PWM on pin %s', self.pinToUse)
90-
GPIO.cleanup()
109+
logging.debug('Shutting down software PWM on GPIO %s (board pin %s)', self.bcm_pin, self.pinToUse)
110+
self.pwm.off()
111+
logging.info('Cleaning up software PWM on GPIO %s', self.bcm_pin)
112+
# gpiozero handles cleanup automatically, but explicitly close
113+
self.pwm.close()
91114
super().shutdown()
92115

93116
class hardwareBBBPWM(basicPWM):

callbacks.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,12 @@ def check_engine_running():
4747
logging.debug('---')
4848
logging.debug('Args %s', argv[1:])
4949

50-
# RPi.GPIO is used for software PWM on the RPi, fail if it is missing
51-
# TODO: Look at switching to gpiozero
50+
# gpiozero is used for software PWM on the RPi, fail if it is missing
5251
if os.getenv('FPPPLATFORM', '') == 'Raspberry Pi' and config['DynRDSTransmitter'] == "QN8066":
5352
try:
54-
import RPi.GPIO
53+
import gpiozero
5554
except ImportError as impErr:
56-
logging.error("Failed to import RPi.GPIO %s", impErr.args[0])
55+
logging.error("Failed to import gpiozero %s", impErr.args[0])
5756
sys.exit(1)
5857

5958
# Environ has a few useful items when FPPD runs callbacks.py, but logging it all the time, even at debug, is too much

scripts/fpp_install.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ echo -e "\nInstalling python3-smbus2..."
77
sudo apt-get install -y python3-smbus2
88

99
if test -f /boot/firmware/config.txt; then
10-
echo -e "\nInstalling python3-rpi-lgpio..."
11-
sudo apt-get install -y python3-rpi-lgpio
10+
echo -e "\nInstalling python3-gpiozero..."
11+
sudo apt-get install -y python3-gpiozero
1212
fi
1313

1414
echo -e "\nRestarting FPP..."

scripts/fpp_uninstall.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,3 @@ else
1010
echo -e "\nLeaving modified optional config script"
1111
fi
1212

13-
if test -f /boot/firmware/config.txt; then
14-
echo -e "\nYou can manually uninstall python3-rpi-lgpio if nothing else uses it."
15-
echo "Command is: sudo apt-get remove -y python3-rpi-lgpio"
16-
fi

0 commit comments

Comments
 (0)