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 MobeyDuck » Mon Dec 29 2014 22:10

Looking at your script makes me wonder about the imported modules. I believe there are more imported modules then you use.

mpd - not used?
random - not used?
os - only used to do a shutdown, cant this be done using subprocess?
getopt - not used?

GPIO.setmode
shouldn't this be used to set it to board or bcm, which one is default? The script doesn't state what is used.

I must admit i haven't read all the previous scripts, you might have copied and removed parts you don't need but missed these. Or there critical could be.


In the notes and your post, you state stop two times one button should be enough ;) maybe it should be pause :D
User avatar
MobeyDuck
Moderator
 
Posts: 482
Joined: Sun Jan 05 2014 20:28
Location: The Netherlands

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


Re: GPIO Pins to control volume

Postby solaar1974 » Mon Dec 29 2014 22:48

Now I have to "Mute" mode instead of a "Pause" mode on my rotary encoder №1:

Code: Select all
#!/usr/bin/env python
#
# Raspberry Pi Rotary Test Encoder Class
#
# Author : Bob Rathbone
# Site   : http://www.bobrathbone.com
#
# This class uses a standard rotary encoder with push switch
#
import subprocess
import sys
import time
from rotary_class import RotaryEncoder

# Define GPIO inputs (BCM)
PIN_A = 23              # Pin 16
PIN_B = 24             # Pin 18
BUTTON = 25     # Pin 22

# This is the event callback routine to handle events
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', 'volume', '-25' ])
        #elif event == RotaryEncoder.BUTTONUP:
              #print "Button up"
        return

# Define the switch
rswitch = RotaryEncoder(PIN_A,PIN_B,BUTTON,switch_event)

while True:
        time.sleep(0.5)


But if pressing it again makes a return to the original volume... And since you have after you just have to turn the knob to the right (volume+).

...Now I find rotary encoder №2 ( ? ) < Left - Right > in only ONE step with push button ( <Prew - Next>,Pause/Play)... :roll: :roll: :roll:
User avatar
solaar1974
Serious Listener
Serious Listener
 
Posts: 91
Joined: Thu Nov 20 2014 12:45
Location: Kharkiv, Ukraine


Re: GPIO Pins to control volume

Postby MobeyDuck » Mon Dec 29 2014 23:35

solaar1974 wrote:Now I have to "Mute" mode instead of a "Pause" mode. But if pressing it again makes a return to the original volume... And since you have after you just have to turn the knob to the right (volume+).


Personally i prefer pause over mute since mute keeps playing the song.

Right.. you updated your post while i was typing...

I guess you want the script to do something like:
Code: Select all
On mute press:
- read "mute variable"
-- If muted
--- set volume to "current volume"
--- set "mute variable" to unmuted
-- else
--- read the "current volume"
--- save "current volume" in a variable
--- set volume to 0
--- set "mute variable" to muted


This would then end up around here
Code: Select all
elif event == RotaryEncoder.BUTTONDOWN:
             subprocess.call
User avatar
MobeyDuck
Moderator
 
Posts: 482
Joined: Sun Jan 05 2014 20:28
Location: The Netherlands


GPIO Pins to control volume

Postby solaar1974 » Tue Dec 30 2014 08:00

I had enough of this. :D

More to add, when rotary volume knob displayed here is

Image

and after the termination of turning the standard mpd info displayed again

Image

:geek: :geek: :geek: :roll: :roll: :roll:
User avatar
solaar1974
Serious Listener
Serious Listener
 
Posts: 91
Joined: Thu Nov 20 2014 12:45
Location: Kharkiv, Ukraine


GPIO Pins to control volume

Postby balbuze » Tue Dec 30 2014 09:59

@MobeyDuck @solaar1974
Thank you guys for your comments ! I will correct it !
@solaar1974 have you already have a script for the lcd display ?
User avatar
balbuze
Moderator
 
Posts: 1807
Joined: Thu May 08 2014 08:42
Location: Isère France


Re: GPIO Pins to control volume

Postby solaar1974 » Tue Dec 30 2014 10:07

No. I don't have a script. I'm not a programmer. I'm a techie. Only now has it:
how-launch-oled-display-with-now-playing-t2260.html
But I think it's time to learn python :)
Last edited by solaar1974 on Tue Dec 30 2014 10:42, edited 1 time in total.
User avatar
solaar1974
Serious Listener
Serious Listener
 
Posts: 91
Joined: Thu Nov 20 2014 12:45
Location: Kharkiv, Ukraine


GPIO Pins to control volume

Postby balbuze » Tue Dec 30 2014 10:30

I have updated my script. Still job to do.... ;)
User avatar
balbuze
Moderator
 
Posts: 1807
Joined: Thu May 08 2014 08:42
Location: Isère France


GPIO Pins to control volume

Postby Buckler » Tue Dec 30 2014 20:11

I have tried to add some extra function to the button of the rotary encoder. The script is far from perfect but i want to share it and hope to get some feedback to improve the result.

FUNCTIONS:
a short press = (de)activates pause
a long press = at this moment nothing but I would like to add the option that the function of the knob changes from volume-control to station/song-control (or get into a menu with different options but i prefer to keep it simple)
a very long press = shutdown

VolumioKnob.py
Code: Select all
#!/usr/bin/env python

#import os
import subprocess
import sys
import time
from rotary_class import RotaryEncoder

global time_stamp
time_stamp = ''
time_now = ''

# Define GPIO inputs
PIN_A = 23      # Pin 8
PIN_B = 24      # Pin 10
BUTTON = 25     # Pin 7


# This is the event callback routine to handle events
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:
              global time_stamp
              time_stamp=time.time()
              time.sleep(.1)
              #print "Button Down"
        elif event == RotaryEncoder.BUTTONUP:
               time_now=time.time()
               delta=int(time_now-time_stamp)
               #print delta
               #print "Button Up"
               if delta<=1:
                   subprocess.call(['mpc', 'toggle' ])
                   #print " Toggle"
                   #time.sleep(.2)   
               elif delta > 2:
                   #print "Shutdown"
                   subprocess.call(['mpc', 'stop' ])
                   subprocess.call(['sudo', 'halt' ])
               #elif 1 < delta <= 2:
                   #print "do something else"
                   #subprocess.call(['mpc', 'stop' ])
                   
        return

# Define the switch
rswitch = RotaryEncoder(PIN_A,PIN_B,BUTTON,switch_event)

while True:
        time.sleep(0.1)


...so still some work to do :D
User avatar
Buckler
Serious Listener
Serious Listener
 
Posts: 65
Joined: Sun Sep 14 2014 08:34
Location: The Netherlands


GPIO Pins to control volume

Postby solaar1974 » Wed Dec 31 2014 00:52

I left so :
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 sys
#import mpd
import time
#import random
from rotary_class import RotaryEncoder
import subprocess

#-----------------------------------------------------------
#
GPIO.setmode(GPIO.BCM)
# Define GPIO output pins for Radio Controls
SW_PREV = 17  # Pin 11
SW_NEXT = 27 # Pin 13
SW_SHTDWN = 4 # Pin 7
SW_PAUSE = 22 # Pin 15
# 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
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', 'volume', '-25' ])
              #print 'toggle'
        #elif event == RotaryEncoder.BUTTONUP:
              #print "Button up"
        return

# 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_SHTDWN,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_PAUSE,GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
   try:
      prev_switch = GPIO.input(SW_PREV)
      next_switch = GPIO.input(SW_NEXT)
      shtdwn_switch = GPIO.input(SW_SHTDWN)
      pause_switch = GPIO.input(SW_PAUSE)         
      if prev_switch == False:
         subprocess.call(['mpc', 'prev' ])
         print "PRECED"
      elif next_switch == False:
         subprocess.call(['mpc', 'next' ])
         print "SUIVANT"
      elif pause_switch == False:
         subprocess.call(['mpc', 'pause' ])
         print "pause"
      elif shtdwn_switch == False:
         subprocess.call(['sudo', 'halt' ])
         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
solaar1974
Serious Listener
Serious Listener
 
Posts: 91
Joined: Thu Nov 20 2014 12:45
Location: Kharkiv, Ukraine


GPIO Pins to control volume

Postby balbuze » Sat Jan 03 2015 17:40

I re-update my script so that shutdown works....
User avatar
balbuze
Moderator
 
Posts: 1807
Joined: Thu May 08 2014 08:42
Location: Isère France

PreviousNext

Return to Help

Who is online

Users browsing this forum: No registered users and 0 guests