GPIO Pins to control volume

Get help for Volumio here!

Ads helps Volumio remain Free and Open Source. Please consider donating to help us continue to serve you.

GPIO Pins to control volume

Postby solaar1974 » Sat Jan 17 2015 18:26

I connected rotary encoders to control tracks and playback/pause. Used PEC11 without detents & with 24detents/24pulse (Bourns) and encoder from minidisc deck Sony MDS-S39 (ALPS). Does not work correctly not one. The script does not have time for short pulses. Tracks switch is not correct. Or no response to the command. Or randomly switching. It is possible to somehow fix at the program level ? Or look self-return rotary encoder or another switch ?
http://www.alps.com/prod/info/E/HTML/En ... 2010H.html
User avatar
solaar1974
Serious Listener
Serious Listener
 
Posts: 91
Joined: Thu Nov 20 2014 12:45
Location: Kharkiv, Ukraine

Ads helps Volumio remain Free and Open Source. Please consider donating to help us continue to serve you.


GPIO Pins to control volume

Postby Michael20 » Wed Jan 28 2015 20:57

Just installed sudo dpkg -i python-rpi.gpio_0.5.9-1_armhf.deb
But i need your Help, want to switch GPIO Outs, best over Webinterface, any ideas how to do it?
Random avatar
Michael20
Fresh off the boat
Fresh off the boat
 
Posts: 9
Joined: Tue Nov 25 2014 07:50


GPIO Pins to control volume

Postby balbuze » Tue Feb 03 2015 20:10

Hi !
@solaar1974 : Have you tried to change the time.sleep value ? Not sure it will change your problem, but....
@buckler and anyone else
I wrote a script to manage my buttons and it works perfecltly ;) but now I want to add some features :
A short press on NEXT button -> Next song with the command "
Code: Select all
subprocess.call(['mpc', 'next' ])

A long press on the NEXT button -> Seek forward (by 8 sec) with the command
Code: Select all
subprocess.call(['mpc', 'seek', '+00:00:08' ])

And the same thing for the previous button. I just try to wrote it for the next button.
It's quite similar to what you have done. But I don't use it with the rotary encoder. And there something wrong in my code I don't understand... I need help to debug it...
Thank you
I know my code is dirty, but I did plenty of test...

Code: Select all
#!/usr/bin/python
# button-rasp
# December 28, 2014
# Remix by Balbuze (balbuze@gmail.com)2014 December
# written for Raspberry B+ :for other model please re-assign gpio port!!!
# add physical buttons for a mdp player system (used with Volumio)
# Provide :   -previous
#      -next
#      -stop
#      -play
#      -volume control through rotary coder
# My first python script !!! Surely not perfect....To be improve
# To do : add code to manage lcd display
# inspirated from :
#
# Connecting a Push Switch http://razzpisampler.oreilly.com/ch07.html
# Tutorial: Raspberry Pi GPIO PINS AND PYTHON http://makezine.com/projects/tutorial-raspberry-pi-gpio-pins-and-python/
#
# radio.py 
# January 23, 2013 
# Written by Sheldon Hartling for Usual Panic. 
# MIT license, all text above must be included in any redistribution 
#

# based on code from Kyle Prier (http://wwww.youtube.com/meistervision) 
# and AdaFruit Industries (https://www.adafruit.com) 
# Kyle Prier - https://www.dropbox.com/s/w2y8xx7t6gkq8yz/radio.py 
# AdaFruit   - https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git, Adafruit_CharLCD
# Rotary class part from : Author : Bob Rathbone
# Site : http://www.bobrathbone.com


import RPi.GPIO as GPIO
import time
import sys,os
#import mpd
#import random
from rotary_class import RotaryEncoder 
import subprocess
#tglobal time_stamp
#from datetime import timedelta,datetime
#time_stamp = time.time()
#short_press = 1
#long_press = 3
time_now = ''
#button_press_timer = 0

#import pdb
#-----------------------------------------------------------
#
GPIO.setmode(GPIO.BCM) 
# Define GPIO output pins for Radio Controls 
SW_PREV = 12  # Pin 32
SW_NEXT = 16 # Pin 36
SW_STOP = 13 # Pin 33
SW_SHTDWN = 6 # Pin 31
SW_PLAY = 26 # Pin 37
# Define GPIO for rotary encoder + button
PIN_A = 23  # Pin 16
PIN_B = 24  # Pin 18
BUTTON = 25 # Pin 22
#------------------------------------------------------------
#As I have a PI-amp from Iqaudio I need to unmute it at startup
#
#A piece of code will be used to mute it at shutdown
#------------------------------------------------------------
#
#Code to manage Rotary encoder + switch

# This is the event callback routine to handle events
#pdb.set_trace()
def switch_event(event):
        if event == RotaryEncoder.CLOCKWISE:
              subprocess.call(['mpc', 'volume', '+2' ])
              time.sleep(.2)
        elif event == RotaryEncoder.ANTICLOCKWISE:
              subprocess.call(['mpc', 'volume', '-2' ])
              time.sleep(.2)
        elif event == RotaryEncoder.BUTTONDOWN:
              subprocess.call(['mpc', 'toggle' ])
   print 'toggle'
#        #elif event == RotaryEncoder.BUTTONUP:
#              #print "Button up"
        return
#pdb.set_trace()
# Define the switch
rswitch = RotaryEncoder(PIN_A,PIN_B,BUTTON,switch_event)
#----------------------------------------------------------
#
#code to manage BUTTONS previous,next,stop,play,shutdown
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_STOP,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_SHTDWN,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_PLAY,GPIO.IN, pull_up_down=GPIO.PUD_UP)
time_stamp = time.time()
button_press_timer = 0
delta = ''
while True:
   try:
      prev_switch = GPIO.input(SW_PREV)
      next_switch = GPIO.input(SW_NEXT)
      stop_switch = GPIO.input(SW_STOP)
      shtdwn_switch = GPIO.input(SW_SHTDWN)
      play_switch = GPIO.input(SW_PLAY)
      #global time_stamp         
      if prev_switch == False:
         subprocess.call(['mpc', 'prev' ])
         print "PRECED"
      elif next_switch == False:
         global time_stamp
         time_stamp=time.time()
         time.sleep(.1)
         #else next_switch != False
         if next_switch != False:
            time_now=time.time()
                       delta=int(time_now-time_stamp)
#         print button_press_timer
               #print "Button Up"
                      if delta <= 1:
            #print time_stamp
               print delta
                           subprocess.call(['mpc', 'next' ])
                 elif delta > 2:
               subprocess.call(['mpc', 'seek', '+00:00:08' ])
                elif stop_switch == False:
         subprocess.call(['mpc', 'stop' ])
                   print "stop"
      elif play_switch == False:
                        subprocess.call(['mpc', 'toggle' ])
#                        print "play"
                elif shtdwn_switch == False:
         os.system("shutdown now -h")
         print "shutdown in progress : wait 20 sec before unplug"
      time.sleep(0.5)

   except KeyboardInterrupt:
      print "\nExit"
      GPIO.setwarnings(False)
      GPIO.cleanup()
      sys.exit(0)
# End of program
User avatar
balbuze
Moderator
 
Posts: 1807
Joined: Thu May 08 2014 08:42
Location: Isère France


GPIO Pins to control volume

Postby lpt2007 » Fri Jul 17 2015 15:35

Hi

I try this script and it works great in my project, but I have one question if it possible to do this:

I need to program another button to toggle through playlists. Is this possible? How?
Random avatar
lpt2007
Fresh off the boat
Fresh off the boat
 
Posts: 1
Joined: Fri Jul 17 2015 15:27


GPIO Pins to control volume

Postby tulipo » Tue Aug 11 2015 21:31

Hi guys

I was wondering if is there any way to program a single button to do 3 different action.

Something like, 1 press = pause, 2 press = next, 3 press = shutdown.


I read something on the internet about a pulse counter to introduce in the script but i have no idea how to do that !!!

Thanks in advance guys.
Random avatar
tulipo
Fresh off the boat
Fresh off the boat
 
Posts: 5
Joined: Fri Jun 26 2015 04:35


GPIO Pins to control volume

Postby muhackl » Mon Oct 26 2015 22:28

Hi,
I totally would appreciate that too:
1 press = pause, 2 press = next, 3 press = shutdown.

Could anybody tell us, how to achieve this?

Many thanks and best regards
muhackl
Random avatar
muhackl
Fresh off the boat
Fresh off the boat
 
Posts: 4
Joined: Mon Oct 26 2015 22:27


GPIO Pins to control volume

Postby audioPhilM » Thu Nov 19 2015 14:36

Hi,

I have developed something similar featuring a rotary encoder with an embedded switch. The code is doing more than controlling mpd with encoder but this part could be easily extracted. I have not implemented the double click. What this does is : short click -> mute, long click -> off.
Double click should be possible to implement without changing the existing logic.

For more information :
https://github.com/PhilippeMeyer/ampCtl

Best
Random avatar
audioPhilM
Fresh off the boat
Fresh off the boat
 
Posts: 2
Joined: Thu Nov 19 2015 14:26


GPIO Pins to control volume

Postby audioPhilM » Sat Nov 21 2015 15:01

Hi,

Just a quick update : the double click is definitely a good idea, so good that I could not wait any longer to have it!
Now implemented see update on github

Best,
Random avatar
audioPhilM
Fresh off the boat
Fresh off the boat
 
Posts: 2
Joined: Thu Nov 19 2015 14:26


GPIO Pins to control volume

Postby Cramstyler » Wed Apr 06 2016 20:10

Hello guys,

I've been playing around with my raspberry and volumio since a couple of days, and I've got a lot of what was mentioned in this thread working. Thank you guys for that!

Now I have a question. I'd like to use one button for play and pause simultaneously. That means, when the button is pressed, I need to check whether the mpc is currently on play or pause, and then do the opposite.
Does anyone now how this would be manageable using mpc commands?

I can not find a way to read anything from mpc in the documentation.

http://linux.die.net/man/1/mpc
Random avatar
Cramstyler
Fresh off the boat
Fresh off the boat
 
Posts: 2
Joined: Wed Apr 06 2016 20:06


GPIO Pins to control volume

Postby Cramstyler » Thu Apr 07 2016 18:57

Cramstyler wrote:Hello guys,

I've been playing around with my raspberry and volumio since a couple of days, and I've got a lot of what was mentioned in this thread working. Thank you guys for that!

Now I have a question. I'd like to use one button for play and pause simultaneously. That means, when the button is pressed, I need to check whether the mpc is currently on play or pause, and then do the opposite.
Does anyone now how this would be manageable using mpc commands?

I can not find a way to read anything from mpc in the documentation.

http://linux.die.net/man/1/mpc


Alright guys, I didn't read the documentation correctly. There actually is a command called 'toggle' that switches between play and pause. Problem solved. Before that, I solved it with a variable that I set either true of false with every push of the button. That way I could switch between the commands 'play' and 'pause' with every call of the function.

Here's my (for now) final working code. Maybe it will help someone. I mainly worked with threads, including a while(true) loop only to keep the programm from terminating.

Code: Select all
import RPi.GPIO as GPIO
import subprocess
import sys, os
import time
from rotary_class import RotaryEncoder

GPIO.setmode(GPIO.BCM)

SHUTDWN = 22
SW_NEXT = 23
SW_PREV = 24
ROT_PIN_A = 17
ROT_PIN_B = 27
SW_PLAY = 10

GPIO.setup(SW_NEXT,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(SW_PREV,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(SHUTDWN,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

def nextsong(channel):
   subprocess.call(['mpc', 'next' ])
def prevsong(channel):
   subprocess.call(['mpc', 'prev' ])
def   shutdown(channel):
   os.system("shutdown now -h")
def switch_event(event):
        if event == RotaryEncoder.CLOCKWISE:
              subprocess.call(['mpc', 'volume', '+5' ])
              time.sleep(.1)
        elif event == RotaryEncoder.ANTICLOCKWISE:
              subprocess.call(['mpc', 'volume', '-5' ])
              time.sleep(.1)
        elif event == RotaryEncoder.BUTTONDOWN:
              subprocess.call(['mpc', 'toggle' ])
        #elif event == RotaryEncoder.BUTTONUP:
              #print "Button up"
        return
      
# Define the switch
rswitch = RotaryEncoder(ROT_PIN_A,ROT_PIN_B,SW_PLAY,switch_event)
      
GPIO.add_event_detect(SW_NEXT, GPIO.RISING, callback=nextsong, bouncetime=300)
GPIO.add_event_detect(SW_PREV, GPIO.RISING, callback=prevsong, bouncetime=300)
GPIO.add_event_detect(SHUTDWN, GPIO.RISING, callback=shutdown, bouncetime=300)

while True:
   try:
      pass
   except KeyboardInterrupt:
      GPIO.cleanup()


However, I still do have problems with the software-debouncing of the rotary-encoder. It still seems to randomly do what it wants when turning it quickly. I'll try do it with hardware for the moment. If I find a solution, I'll post it here.

Greetings!
Random avatar
Cramstyler
Fresh off the boat
Fresh off the boat
 
Posts: 2
Joined: Wed Apr 06 2016 20:06

PreviousNext

Return to Help

Who is online

Users browsing this forum: No registered users and 0 guests