Moode Forum
[How to do instruction] Use hardware momentary switches to control moOde - Printable Version

+- Moode Forum (https://moodeaudio.org/forum)
+-- Forum: moOde audio player (https://moodeaudio.org/forum/forumdisplay.php?fid=3)
+--- Forum: FAQ and Guides (https://moodeaudio.org/forum/forumdisplay.php?fid=9)
+--- Thread: [How to do instruction] Use hardware momentary switches to control moOde (/showthread.php?tid=198)

Pages: 1 2 3 4 5 6 7 8 9


RE: Use hardware momentary switches to control moOde - cyoops - 03-27-2019

(03-27-2019, 08:53 AM)remy1961 Wrote:
(03-26-2019, 11:41 PM)DRONE7 Wrote:
Quote:On the hardware side, you set your hardware button to connect the ground pin of your RPi with a GPIO pin of your choice.
So you should be able to strip the ends of a couple of dupont cables, connect the plug ends to ground and selected pin then briefly touch the bare ends together. 
Happy to test it here...

Hi. As explained here it actually depends on the "default pull status" of the GPIO: if the default pull status is low, you connect the gpio to the 3.3 volts pin (never connect with the 5 volts pin). If the default status is high, you connect to the ground. 
[Image: 2uypzex.jpg]
Remy

I have a question if we set the "default pull status" of the GPIO, can we change the  way we connect GPIO?

I set like this:


#code to manage BUTTONS
GPIO.setup(SW_PREV,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_NEXT,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_POWEROFF,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_PLAY,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_VOLUP,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_VOLDOWN,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_RON,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_RPT,GPIO.IN, pull_up_down=GPIO.PUD_UP)

and then I connect the GPIO as the default status are "High", and the buttons work in proper.


RE: Use hardware momentary switches to control moOde - Tim Curtis - 03-27-2019

(03-27-2019, 06:13 AM)cyoops Wrote:
(03-26-2019, 10:53 PM)Tim Curtis Wrote: Hi,

Here is screenie of new GPIO Config feature :-) The underlying code can be easily extended to support other actions. This interfaces with a modified version of @Cyanoazimin's nice script from post #9.



I'll need some volunteers to test this out because I'm w/o any hardware that has GPIO controlled buttons.

-Tim

Cool, that's great, but I think I think Random, Single, Power off/on are much more useful than playlist, especially the powerbutton.

Moode with buttons and pydPiper, that's a perfect local player.

Random and Single could be added but when u say Power off/on do you mean one GPIO pin that's used as a Power toggle or one pin for off and another for on?

-Tim


RE: Use hardware momentary switches to control moOde - cyoops - 03-27-2019

(03-27-2019, 02:48 PM)Tim Curtis Wrote:
(03-27-2019, 06:13 AM)cyoops Wrote:
(03-26-2019, 10:53 PM)Tim Curtis Wrote: Hi,

Here is screenie of new GPIO Config feature :-) The underlying code can be easily extended to support other actions. This interfaces with a modified version of @Cyanoazimin's nice script from post #9.



I'll need some volunteers to test this out because I'm w/o any hardware that has GPIO controlled buttons.

-Tim

Cool, that's great, but I think I think Random, Single, Power off/on are much more useful than playlist, especially the powerbutton.

Moode with buttons and pydPiper, that's a perfect local player.

Random and Single could be added but when u say Power off/on do you mean one GPIO pin that's used as a Power toggle or one pin for off and another for on?

-Tim

Before I use pydPiper as an I2C display method, I use GPIO 3 as poweroff button trigger and this button can boot from halt when  the pin connecting the GND. But now I have to connected this pin with an I2C OLED, I cannot reboot my Pi in this way anymoreSad


RE: Use hardware momentary switches to control moOde - vijapra - 03-27-2019

It works perfectly! thanks!!
Would be possible play an USB from GPIO?
Thanks again


RE: Use hardware momentary switches to control moOde - Tim Curtis - 03-29-2019

Hi,

After noodling it a bit, rather than having a fixed set of actions I've settled on an approach for mapping GPIO pins to commands that allows a pin to perform an arbitrary action limited only by the command or script associated with the pin.

[attachment=572]

The feature is named "GPIO button handler" and is accessed via System config.

[attachment=573]

Here is the script that runs in the background and monitors the GPIO pins. It reads the configuration from sql table to determine which pins to monitor and what commands to run.

Code:
#!/usr/bin/python
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License version 2 as
#   published by the Free Software Foundation.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
#    Based on the script posted in moOde Forum by @Cyanoazimin
#    2019-MM-DD TC moOde 5.0
#

import RPi.GPIO as GPIO
import sys
import time
import datetime
import os
import subprocess
import sqlite3

# Set GPIO pin numbering mode
GPIO.setmode(GPIO.BCM)

# get the configuration
db = sqlite3.connect('/var/local/www/db/moode-sqlite3.db')
db.row_factory = sqlite3.Row
db.text_factory = str
cursor = db.cursor()

# get bounce_time
cursor.execute("SELECT value FROM cfg_gpio where param='bounce_time'")
row = cursor.fetchone()
bounce_time = int(row['value'])
#print str(datetime.datetime.now())[:19] + ' bounce_time=' + str(bounce_time)

# configure the pins
cursor.execute("SELECT * FROM cfg_gpio")
for row in cursor:    
    #print str(datetime.datetime.now())[:19] + ' row id=' + str(row['id']) + ', enabled=' + row['enabled'] + ', command=' + row['command']
    if str(row['id']) == '1' and row['enabled'] == '1':
        sw_1_pin = int(row['pin'])
        sw_1_cmd = row['command'].split(' ')
        GPIO.setup(sw_1_pin,GPIO.IN, pull_up_down=GPIO.PUD_UP)
        def sw_1_event(channel):
            subprocess.call(sw_1_cmd)
        GPIO.add_event_detect(sw_1_pin, GPIO.RISING, callback = sw_1_event, bouncetime = bounce_time)
        print str(datetime.datetime.now())[:19] + ' sw_1: pin=' + str(sw_1_pin) + ', enabled=' + row['enabled'] + ', bounce_time=' + str(bounce_time) + ', cmd=' + row['command']
    elif str(row['id']) == '2' and row['enabled'] == '1':
        sw_2_pin = int(row['pin'])
        sw_2_cmd = row['command'].split(' ')
        GPIO.setup(sw_2_pin,GPIO.IN, pull_up_down=GPIO.PUD_UP)
        def sw_2_event(channel):
            subprocess.call(sw_2_cmd)
        GPIO.add_event_detect(sw_2_pin, GPIO.RISING, callback = sw_2_event, bouncetime = bounce_time)
        print str(datetime.datetime.now())[:19] + ' sw_2: pin=' + str(sw_2_pin) + ', enabled=' + row['enabled'] + ', bounce_time=' + str(bounce_time) + ', cmd=' + row['command']
    elif str(row['id']) == '3' and row['enabled'] == '1':
        sw_3_pin = int(row['pin'])
        sw_3_cmd = row['command'].split(' ')
        GPIO.setup(sw_3_pin,GPIO.IN, pull_up_down=GPIO.PUD_UP)
        def sw_3_event(channel):
            subprocess.call(sw_3_cmd)
        GPIO.add_event_detect(sw_3_pin, GPIO.RISING, callback = sw_3_event, bouncetime = bounce_time)
        print str(datetime.datetime.now())[:19] + ' sw_3: pin=' + str(sw_3_pin) + ', enabled=' + row['enabled'] + ', bounce_time=' + str(bounce_time) + ', cmd=' + row['command']
    elif str(row['id']) == '4' and row['enabled'] == '1':
        sw_4_pin = int(row['pin'])
        sw_4_cmd = row['command'].split(' ')
        GPIO.setup(sw_4_pin,GPIO.IN, pull_up_down=GPIO.PUD_UP)
        def sw_4_event(channel):
            subprocess.call(sw_4_cmd)
        GPIO.add_event_detect(sw_4_pin, GPIO.RISING, callback = sw_4_event, bouncetime = bounce_time)
        print str(datetime.datetime.now())[:19] + ' sw_4: pin=' + str(sw_4_pin) + ', enabled=' + row['enabled'] + ', bounce_time=' + str(bounce_time) + ', cmd=' + row['command']
    elif str(row['id']) == '5' and row['enabled'] == '1':
        sw_5_pin = int(row['pin'])
        sw_5_cmd = row['command'].split(' ')
        GPIO.setup(sw_5_pin,GPIO.IN, pull_up_down=GPIO.PUD_UP)
        def sw_5_event(channel):
            subprocess.call(sw_5_cmd)
        GPIO.add_event_detect(sw_5_pin, GPIO.RISING, callback = sw_5_event, bouncetime = bounce_time)
        print str(datetime.datetime.now())[:19] + ' sw_5: pin=' + str(sw_5_pin) + ', enabled=' + row['enabled'] + ', bounce_time=' + str(bounce_time) + ', cmd=' + row['command']
    elif str(row['id']) == '6' and row['enabled'] == '1':
        sw_6_pin = int(row['pin'])
        sw_6_cmd = row['command'].split(' ')
        GPIO.setup(sw_6_pin,GPIO.IN, pull_up_down=GPIO.PUD_UP)
        def sw_6_event(channel):
            subprocess.call(sw_6_cmd)
        GPIO.add_event_detect(sw_6_pin, GPIO.RISING, callback = sw_6_event, bouncetime = bounce_time)
        print str(datetime.datetime.now())[:19] + ' sw_6: pin=' + str(sw_6_pin) + ', enabled=' + row['enabled'] + ', bounce_time=' + str(bounce_time) + ', cmd=' + row['command']
    elif str(row['id']) == '7' and row['enabled'] == '1':
        sw_7_pin = int(row['pin'])
        sw_7_cmd = row['command'].split(' ')
        GPIO.setup(sw_7_pin,GPIO.IN, pull_up_down=GPIO.PUD_UP)
        def sw_7_event(channel):
            subprocess.call(sw_7_cmd)
        GPIO.add_event_detect(sw_7_pin, GPIO.RISING, callback = sw_7_event, bouncetime = bounce_time)
        print str(datetime.datetime.now())[:19] + ' sw_7: pin=' + str(sw_7_pin) + ', enabled=' + row['enabled'] + ', bounce_time=' + str(bounce_time) + ', cmd=' + row['command']
    elif str(row['id']) == '8' and row['enabled'] == '1':
        sw_8_pin = int(row['pin'])
        sw_8_cmd = row['command'].split(' ')
        GPIO.setup(sw_8_pin,GPIO.IN, pull_up_down=GPIO.PUD_UP)
        def sw_8_event(channel):
            subprocess.call(sw_8_cmd)
        GPIO.add_event_detect(sw_8_pin, GPIO.RISING, callback = sw_8_event, bouncetime = bounce_time)
        print str(datetime.datetime.now())[:19] + ' sw_8: pin=' + str(sw_8_pin) + ', enabled=' + row['enabled'] + ', bounce_time=' + str(bounce_time) + ', cmd=' + row['command']

# Main
while True:
    time.sleep(1)


-Tim


RE: Use hardware momentary switches to control moOde - TheOldPresbyope - 03-29-2019

(03-29-2019, 02:16 AM)Tim Curtis Wrote: Hi,

After noodling it a bit, rather than having a fixed set of actions I've settled on this approach for mapping GPIO pins to commands that allows a pin to perform an arbitrary action limited only by the command or script associated with the pin.



-Tim


That’s seriously cool!


RE: Use hardware momentary switches to control moOde - Tim Curtis - 03-29-2019

I've updated my post with addl info :-)


RE: Use hardware momentary switches to control moOde - remy1961 - 03-29-2019

Hi Tim. I am the obvious volunteer since I started this thread. Let me know when the thing is ready for testing.
Remy


RE: Use hardware momentary switches to control moOde - DRONE7 - 03-29-2019

So the script sets all selected pins high and a simple momentary connection to ground invokes the configured command ?
Too easy Smile


RE: Use hardware momentary switches to control moOde - Tim Curtis - 03-29-2019

The feature will be in RC1 which I hope to make available sometime this week :-)