Skip to content

Instantly share code, notes, and snippets.

@amnuts
Created January 11, 2017 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amnuts/d44643b6ab951e2d9cd6ccfbf1eb5b78 to your computer and use it in GitHub Desktop.
Save amnuts/d44643b6ab951e2d9cd6ccfbf1eb5b78 to your computer and use it in GitHub Desktop.
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