Moode Forum
GPIO buttons that fire on connect. - Printable Version

+- Moode Forum (https://moodeaudio.org/forum)
+-- Forum: moOde audio player (https://moodeaudio.org/forum/forumdisplay.php?fid=3)
+--- Forum: Support (https://moodeaudio.org/forum/forumdisplay.php?fid=7)
+--- Thread: GPIO buttons that fire on connect. (/showthread.php?tid=3993)



GPIO buttons that fire on connect. - the_bertrum - 07-07-2021

I would like to use a rotary switch to select playlists on one of my moOde players.  I've got scripts mapped to the GPIO button handler that load and play the playlists as I need, but because the script attached to the button fires when the button disconnects my rotary switch will play the playlist attached to the position it just left, not the one for the position it is in.
Is there a way to get the buttons to run the attached scripts when the contact is made, rather than when the contact is broken?

Is it as simple as:

Code:
GPIO.add_event_detect(sw_1_pin, GPIO.RISING, callback=sw_1_event, bouncetime=bounce_time)
becoming

Code:
GPIO.add_event_detect(sw_1_pin, GPIO.FALLING, callback=sw_1_event, bouncetime=bounce_time)
in the gpio-buttons.py


RE: GPIO buttons that fire on connect. - TheOldPresbyope - 07-07-2021

@the_bertrum

Real hackers don't ask for directions. They hack first and brag (or complain) after Smile 

I expect the sense of the transition you want to trigger on depends on how you've wired your switch to the GPIO. Experimentation is par for the course and what you suggest seems reasonable. 

Rotary switches are generally "noisy" so it's possible you have to deal with switch bounce as well.

Regards,
Kent


RE: GPIO buttons that fire on connect. - the_bertrum - 07-07-2021

Cry
But I'm all nervous Kent, I don't want to break my system!

But I did go a-hacking anyway and find that:

Code:
           if GPIO.input(channel) == 0:
rather than:

Code:
           if GPIO.input(channel) == 1:
in the gpio-buttons.py file has the desired effect.

Now to work out how that works because my current understanding suggests it shouldn't be enough on its own.


RE: GPIO buttons that fire on connect. - Tim Curtis - 07-07-2021

IIRC in the code the var channel is the GPIO PIN number and GPIO.input(channel) can return either 0 or 1 (false / true) where 0 = pin level is LOW, 1 = pin level is HIGH.