Skip to content

Instantly share code, notes, and snippets.

@amnuts amnuts/volume.py
Created Jan 11, 2017

Embed
What would you like to do?
Use a rotary controller to adjust volume on a Raspberry Pi
from RPi import GPIO
from time import sleep
import subprocess
clk = 5
dt = 6
btn = 26
# vals from output of amixer cget numid=1
min = 0
max = 207
GPIO.setmode(GPIO.BCM)
GPIO.setup(clk, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(dt, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(btn, GPIO.IN, pull_up_down=GPIO.PUD_UP)
isMuted = False
preVolume = volume = 100 # give it some volume to start with
clkLastState = GPIO.input(clk)
btnLastState = GPIO.input(btn)
subprocess.call(['amixer', '-q', '-c', '0', 'cset', 'numid=1', str(volume)])
try:
while True:
btnPushed = GPIO.input(btn)
if ((not btnLastState) and btnPushed):
if isMuted:
volume = preVolume
isMuted = False
print "Unmuted"
else:
preVolume = volume
volume = 0
isMuted = True
print "Muted"
subprocess.call(['amixer', '-q', '-c', '0', 'cset', 'numid=1', str(volume)])
sleep(0.05)
else:
clkState = GPIO.input(clk)
dtState = GPIO.input(dt)
if clkState != clkLastState:
if isMuted:
isMuted = False
volume = 0
if dtState != clkState:
volume += 5
if volume > max:
volume = max
else:
volume -= 5
if volume < min:
volume = min
print "{:d} ({:.0%})".format(volume, float(volume)/float(max))
subprocess.call(['amixer', '-q', '-c', '0', 'cset', 'numid=1', str(volume)])
clkLastState = clkState
btnLastState = btnPushed
finally:
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.