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 Buckler » Tue Dec 16 2014 13:11

Peter,

Nice script and setup!
For what kind o f project are you using your gadaget, i read something about train, Xmas, werbung..... etc?

Could you publish your shutdown script?
I have a pythonscript running but it shutdown random without pressing a button!!!!

Greetings

Harry
User avatar
Buckler
Serious Listener
Serious Listener
 
Posts: 65
Joined: Sun Sep 14 2014 08:34
Location: The Netherlands

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


GPIO Pins to control volume?

Postby dakota127 » Wed Dec 17 2014 10:30

Hi Harry

I am currently away fom home but will respond to your question soon.

Peter
Random avatar
dakota127
Fresh off the boat
Fresh off the boat
 
Posts: 9
Joined: Thu Nov 20 2014 16:44


GPIO Pins to control volume

Postby dakota127 » Fri Dec 19 2014 12:28

Hi Harry

I added a short description of my Volumio project in the DIY section:

http://volumio.org/forum/christmas-t2281.html

You can find the shutdown script there.

Hope this helps.

Greetings, Peter
Random avatar
dakota127
Fresh off the boat
Fresh off the boat
 
Posts: 9
Joined: Thu Nov 20 2014 16:44


GPIO Pins to control volume

Postby balbuze » Wed Dec 24 2014 10:52

Hi!
For a new project, I start to think about what I need to drive the system.
What I want :
A lcd display, I orderd that one : http://www.amazon.fr/gp/product/B00AT8K ... ge_o08_s00
I2C interface : http://www.amazon.fr/gp/product/B00GBSW ... ge_o02_s00 (not sure I need it...)
a play/ pause button
a next button
a previous button
a stop button
a shutdown button
a rotary encoder to set volume (I tried a script written in python and it works)
So I'm looking for a script (or several ) to do all that!
I saw some with additionnal features but without some I need :(
And I am not programmer I need something easy to modify.(if needed)
A question : as I used a I2C dac, will it work with the i2c interface ?
Voila ! If you get this, please let me know ! :D
User avatar
balbuze
Moderator
 
Posts: 1807
Joined: Thu May 08 2014 08:42
Location: Isère France


GPIO Pins to control volume

Postby solaar1974 » Sun Dec 28 2014 18:01

Buckler ,
Why your script VolumeKnob.py don*t work (((( :?
Code: Select all
pi@volumio:~$ sudo python /home/pi/VolumeKnob.py
  File "/home/pi/VolumeKnob.py", line 10
    import subprocess
    ^
IndentationError: unexpected indent
pi@volumio:~$

You know what the problem is ??? :?
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 solaar1974 » Sun Dec 28 2014 18:44

Code: Select all
pi@volumio:~$ sudo python /home/pi/VolumeKnob.py
Traceback (most recent call last):
  File "/home/pi/VolumeKnob.py", line 13, in <module>
    from rotary_class import RotaryEncoder
ImportError: No module named rotary_class
pi@volumio:~$


What you say ???
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 Buckler » Sun Dec 28 2014 19:23

@solaar1974

Did you use the "Select all" option? If so when you paste the code you will have extra spaces at each line and create an indentation error.

Voorbeeld.jpg
Voorbeeld.jpg (47.92 KiB) Viewed 8538 times


Just select the code and paste.


Rotary Class
Did you download the rotary_class.py from http://www.bobrathbone.com/raspberrypi_rotary.htm

Here is the code, save as: /home/pi/rotary_class.py

Code: Select all
#!/usr/bin/env python
#
# Raspberry Pi Rotary Encoder Class
# $Id: rotary_class.py,v 1.2 2014/01/31 13:34:48 bob Exp $
#
# Author : Bob Rathbone
# Site   : http://www.bobrathbone.com
#
# This class uses standard rotary encoder with push switch
#
#

import RPi.GPIO as GPIO

class RotaryEncoder:

   CLOCKWISE=1
   ANTICLOCKWISE=2
   BUTTONDOWN=3
   BUTTONUP=4

   rotary_a = 0
   rotary_b = 0
   rotary_c = 0
   last_state = 0
   direction = 0

   # Initialise rotary encoder object
   def __init__(self,pinA,pinB,button,callback):
      self.pinA = pinA
      self.pinB = pinB
      self.button = button
      self.callback = callback

      GPIO.setmode(GPIO.BCM)
      
      # The following lines enable the internal pull-up resistors
      # on version 2 (latest) boards
      GPIO.setwarnings(False)
      GPIO.setup(self.pinA, GPIO.IN, pull_up_down=GPIO.PUD_UP)
      GPIO.setup(self.pinB, GPIO.IN, pull_up_down=GPIO.PUD_UP)
      GPIO.setup(self.button, GPIO.IN, pull_up_down=GPIO.PUD_UP)

      # For version 1 (old) boards comment out the above four lines
      # and un-comment the following 3 lines
      #GPIO.setup(self.pinA, GPIO.IN)
      #GPIO.setup(self.pinB, GPIO.IN)
      #GPIO.setup(self.button, GPIO.IN)

      # Add event detection to the GPIO inputs
      GPIO.add_event_detect(self.pinA, GPIO.FALLING, callback=self.switch_event)
      GPIO.add_event_detect(self.pinB, GPIO.FALLING, callback=self.switch_event)
      GPIO.add_event_detect(self.button, GPIO.BOTH, callback=self.button_event, bouncetime=200)
      return

   # Call back routine called by switch events
   def switch_event(self,switch):
      if GPIO.input(self.pinA):
         self.rotary_a = 1
      else:
         self.rotary_a = 0

      if GPIO.input(self.pinB):
         self.rotary_b = 1
      else:
         self.rotary_b = 0

      self.rotary_c = self.rotary_a ^ self.rotary_b
      new_state = self.rotary_a * 4 + self.rotary_b * 2 + self.rotary_c * 1
      delta = (new_state - self.last_state) % 4
      self.last_state = new_state
      event = 0

      if delta == 1:
         if self.direction == self.CLOCKWISE:
            # print "Clockwise"
            event = self.direction
         else:
            self.direction = self.CLOCKWISE
      elif delta == 3:
         if self.direction == self.ANTICLOCKWISE:
            # print "Anticlockwise"
            event = self.direction
         else:
            self.direction = self.ANTICLOCKWISE
      if event > 0:
         self.callback(event)
      return


   # Push button up event
   def button_event(self,button):
      if GPIO.input(button):
         event = self.BUTTONUP
      else:
         event = self.BUTTONDOWN
      self.callback(event)
      return

   # Get a switch state
   def getSwitchState(self, switch):
      return  GPIO.input(switch)

# End of RotaryEncoder class


We carefull with the indentation.... ;)
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 » Sun Dec 28 2014 20:31

Thank you !! !! !!

Works !
Only was mistake and I changed volume control mixer on position "Software".

Why "Pause" button? Why not "Mute" ?

And if you put 2 encoder:
1 - swivel 360 degrees encoder (Volume left-DOWN & right-UP and push button Mute),
2 - swing-return encoder (left-Previous - right-Next and push button Play/Pause) , like "Sony" (Jog-Dial, in my opinion is called) ?

http://en.wikipedia.org/wiki/Jog_dial

Image

That would be great and still to this script would someone? :D:D:D

...And then I would have finished my project ))) "Brick"... :D:D:D ...waiting for parts from China )...
Last edited by solaar1974 on Tue Dec 30 2014 10:31, edited 3 times 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 » Mon Dec 29 2014 20:50

Edit january 03 2015 import sys ->import sys,os
EDIT : UPDATE CODE
Hi!
I'm proud to show you my code to add physical buttons to my Volumio project.
Code is inspirated from what I read here and on the web.
It provide buttons for :
# -previous
# -next
# -stop
# -play
# -stop
# -volume control through rotary coder
It is written for Raspberry b+, so for other models need to be adapted.
Script is not finished yet but it works !!!! :D :D
Now, I have to manage for the display but I'm waiting for my order to arrive...
It's my first python script ... so if you see error or something wrong, let me know ;)
Now the code
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 improved
# 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,os
#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 = 12  # Pin 32
SW_NEXT = 16 # Pin 36
SW_STOP = 20 # Pin 38
SW_SHTDWN = 21 # Pin 40
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
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

# 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)
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)         
      if prev_switch == False:
         subprocess.call(['mpc', 'prev' ])
         print "PRECED"
      elif next_switch == False:
         subprocess.call(['mpc', 'next' ])
         print "SUIVANT"
      elif stop_switch == False:
         subprocess.call(['mpc', 'stop' ])
                   print "stop"
      elif play_switch == False:
                        subprocess.call(['mpc', 'play' ])
                        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 solaar1974 » Mon Dec 29 2014 21:28

I little don't understand this :
Code: Select all
# This is the event callback routine to handle left knob events

What is it "left knob" ?
User avatar
solaar1974
Serious Listener
Serious Listener
 
Posts: 91
Joined: Thu Nov 20 2014 12:45
Location: Kharkiv, Ukraine

PreviousNext

Return to Help

Who is online

Users browsing this forum: No registered users and 4 guests