Thank you for your donation!


Instruction Guide Use hardware momentary switches to control moOde
#41
Tim,
I love the Moode player looking to modify my old tuner with a display and reuse some of the buttons to control the moode player

So far so good - have the player running, have been experimenting with switches and the new GPIO handler is great.  I want to create buttons for my most frequent radio stations - so have created playlists e.g BBC>R3 and tried to follow your instructions to crate a script test.sh but it is not working correctly

pi@moode:~ $ sudo cat test.sh
#!/bin/bash
mpc clear
mpc load BBC>R3
mpc play
pi@moode:~ $ 


I am sure I am doing something wrong with the script.
When I program 3 switches:
mpd clear
mpd load BBC>R3
mpd play

and press them is sequence I can get the functions to work

Any help appreciated
Reply
#42
On my way out the door to a robotics competition but at a glance your playlist name BBC>R3 looks suspect. In a bash shell script,

Code:
mpc load BBC>R3

 says load file BBC and redirect the output of the command to file R3.

If BBC>R3 is really the playlist name, try putting it in quotes.

Regards,
Kent
Reply
#43
(01-11-2020, 12:06 PM)TheOldPresbyope Wrote: On my way out the door to a robotics competition but at a glance your playlist name BBC>R3 looks suspect. In a bash shell script,

Code:
mpc load BBC>R3

 says load file BBC and redirect the output of the command to file R3.

If BBC>R3 is really the playlist name, try putting it in quotes.

Regards,
Kent
Thank you - stupid error
Changed the Playlist names to remove the > and all is good

Next task is to work out how I can get inputs from the 8 presets and drive the 8 indicator LEDs and I don't have enough GPIOs - using my old RPI B.  after the DAC and the OLED I will only have maybe 9 GPIO left
Thought about using a binary encoder to get the 8 presets into 3 bits, then a decoder to get the status back out to the LEDs

Thanks again
Reply
#44
(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

Tim, are Random and Repeat possible - if so what would the commands be in the GPIO handler?
Also if I wanted to have more playlists can I extend the number of inputs beyond the 8 in the GPIO handler?
Reply
#45
(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 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.



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



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

Tim,
I realize this is an older post, however could you provide a list of the possible commands that could be executed from the GPIO handler please or direct me to the list if you have already provided.
I am particularly looking for Random and repeat. Thanks
Reply
#46
mpc help
Reply
#47
Hello everybody.
I need your help to get this up and running.
Could you clarify the following questions:
a) Is it necessary to create the directory hwbuttons and put the python script from post #25 in it and name it buttons.py ?
b) If so then I think its also necessary to change the rc.local file to load the buttons.py script, right?
c) Inside the configuration (m > configure > system) I set the GPIO handler to ON and configure just button 1 as following (Switch to ON, PIN 27, CMD mpc toggle). But after saving the configuration and pressing the hardware button nothing happens. So is it the GPIO Number (27) or the Raspberry PI Pin (13) that must be set into the config?

I'm using the OLD Pi1 Model B+ Rev. 2 where Pin 13 is assigned to be GPIO27.
Kindly regards
The Herb Guy
Reply
#48
Tim, I want to use more GPIO than you have in the GPIO handler - where do I find the config file. I assume I leave the GPIO button handler off in the system config menu. I have 8 radio presets that are encoded as BCD, so use 3 GPIO, but this requires a little more programming of the GPIO.
Can you point me in the right direction please as to the file I need to create and where I will find it.
Reply
#49
The moOde repo https://github.com/moode-player/moode
Reply
#50
Big Grin 
- issue solved -
false alarm
Reply


Forum Jump: