I have the following code, and I cannot figure out how to make
when_released work. The callback functions just do not run to the press and release.
On the other hand, properties such as
is_pressed do work as expected.
MyDoor(object): ... def function1(self, gpio_button, held_duration): ds = DoorSound() gpio_button.when_pressed = ds.start_door_sound # gpio_button.when_pressed = lambda: ds.start_door_sound() # <-- I have also tried this alternative gpio_button.when_released = ds.stop_door_sound # gpio_button.when_released = lambda: ds.stop_door_sound() do_loop = True while do_loop: ... # do more stuff and at some point set do_loop to False ...
DoorSound is defined as follows:
class DoorSound(object): def __init__(self): self.file_path = 'sounds/door_sound.mp3' self.pygame_door = pygame.mixer def start_door_sound(self): print('Start door sound!') self.pygame_door.init() self.pygame_door.music.load('sounds/door_sound.mp3') self.pygame_door.music.play() time.sleep(0.1) def stop_door_sound(self): print('Stop door sound!') self.pygame_door.music.stop() time.sleep(0.1)
I also tried:
stop_door_soundfunctions inside the
- the following simple function, that when it is called directly, instead of as a callback, works. And not trying to stop it.
def door_sound(self): """Reproduce a DOOR SOUND""" print('♩ ♪ ♫ ♬ Door sound ♩ ♪ ♫ ♬') print() pygame.mixer.init() pygame.mixer.music.load('sounds/door_sound.mp3') pygame.mixer.music.play() while pygame.mixer.music.get_busy() == True: continue
but none of them made any difference.
The text inside the functions is never printed, so I guess the problem is that somehow the callback function is never called.
I read somewhere that
when_released functions might not work well inside loops, but unfortunately, the documentation is not very extensive so I don't know if that might be the case.
In any case,
function1 runs inside some
Does anybody know what the problem is?