From b943248edd116a301c4ddbb0c5b89d1afc216327 Mon Sep 17 00:00:00 2001 From: Topas Date: Sun, 12 Mar 2023 14:12:18 +0100 Subject: [PATCH] Added check for state in shutdown button handler (#1984) * Handler gets called when pushing AND releasing the button * The handler should be called only when the button is pressed, as configured here: https://github.com/MiczFlor/RPi-Jukebox-RFID/blob/develop/components/gpio_control/GPIODevices/simple_button.py#L86 --- .../GPIODevices/shutdown_button.py | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/components/gpio_control/GPIODevices/shutdown_button.py b/components/gpio_control/GPIODevices/shutdown_button.py index 30ba26b6f..ee8cb1e98 100644 --- a/components/gpio_control/GPIODevices/shutdown_button.py +++ b/components/gpio_control/GPIODevices/shutdown_button.py @@ -34,27 +34,28 @@ def set_led(self, status): logger.debug('cannot set LED to {}: no LED pin defined'.format(status)) def callbackFunctionHandler(self, *args): - logger.debug('ShutdownButton pressed, ensuring long press for {} seconds, checking each {}s'.format( - self.hold_time, self.iteration_time - )) - t_passed = 0 - led_state = True - while t_passed < self.hold_time: - self.set_led(led_state) - time.sleep(self.iteration_time) - t_passed += self.iteration_time - led_state = not led_state - if not self.is_pressed: - break - if t_passed >= self.hold_time: - # trippel off period to indicate command accepted - self.set_led(GPIO.HIGH) - time.sleep(.6) - # leave it on for the moment, it will be off when the system is down - self.when_pressed(*args) - else: - # switch off LED if pressing was cancelled early (during flashing) - self.set_led(GPIO.LOW) + if self.is_pressed: # Should not be necessary, but handler gets called on rising edge too + logger.debug('ShutdownButton pressed, ensuring long press for {} seconds, checking each {}s'.format( + self.hold_time, self.iteration_time + )) + t_passed = 0 + led_state = True + while t_passed < self.hold_time: + self.set_led(led_state) + time.sleep(self.iteration_time) + t_passed += self.iteration_time + led_state = not led_state + if not self.is_pressed: + break + if t_passed >= self.hold_time: + # trippel off period to indicate command accepted + self.set_led(GPIO.HIGH) + time.sleep(.6) + # leave it on for the moment, it will be off when the system is down + self.when_pressed(*args) + else: + # switch off LED if pressing was cancelled early (during flashing) + self.set_led(GPIO.LOW) def __repr__(self): return ''.format(