Skip to content

Instantly share code, notes, and snippets.

@susumuota
Created December 13, 2017 15:40
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 susumuota/fb08cd618fa92ae8014522beff63dc5f to your computer and use it in GitHub Desktop.
Save susumuota/fb08cd618fa92ae8014522beff63dc5f to your computer and use it in GitHub Desktop.
#!/usr/bin/python
#-*- coding: utf-8 -*-
u'''
http://www.rpiblog.com/2012/09/using-gpio-of-raspberry-pi-to-blink-led.html
https://github.com/if1live/rpi-mpd-controller
http://nw-electric.way-nifty.com/blog/2014/01/volumio-1ad0.html
'''
'''
sudo apt-get update
sudo apt-get -y install python-rpi.gpio python-daemon
'''
import RPi.GPIO as GPIO
import time
import commands
import re
import json
#import subprocess
#from daemon import daemon
#from daemon.pidlockfile import PIDLockFile
PIN_PREV_BTN = 7
PIN_PLAY_BTN = 11
PIN_NEXT_BTN = 13
PIN_STOP_BTN = 32
PIN_VOL_UP_BTN = 33
PIN_VOL_DW_BTN = 31
class Player(object):
def __init__(self):
self.re_play = re.compile(r'^\[playing\].+$')
self.re_vol = re.compile(r'^volume:.+% .+$')
self.play_state = self.is_playing()
self.shutdown_timer = 0
def sdtimer(self):
if self.shutdown_timer is not 0:
self.shutdown_timer -= 1
def is_playing(self):
# result = commands.getstatusoutput('mpc')
# result = commands.getstatusoutput('mpc')
result = commands.getstatusoutput('volumio status')
# line_list = result[1].splitlines()
playing = False
# for line in line_list:
# if self.re_play.match(line) is not None:
# playing = True
try:
js = json.loads(result[1])
except:
js = None
playing = js and (js['status'] == 'play')
self.play_state = playing
# for line in line_list:
# if self.re_vol.match(line) is None:
# self.vol_state = False
## print '## vol False'
# else:
# self.vol_state = True
## print '## vol True'
self.vol_state = (js and js['volume']) is not None
return playing
def play(self,cmd):
if self.is_playing():
# if self.play_state is True:
# retval = commands.getoutput('mpc pause')
retval = commands.getoutput('volumio pause')
self.play_state = False
print '#pause'
else:
# retval = commands.getoutput('mpc play')
retval = commands.getoutput('volumio play')
self.play_state = True
print '#play'
print retval
# result = commands.getstatusoutput('mpc')
# line_list = retval[1].splitlines()
# for line in line_list:
# if self.re_vol.match(line) is None:
# self.vol_state = False
# print '### vol False'
# else:
# self.vol_state = True
# print '### vol True'
def prev(self,cmd):
if cmd is 2:
retval = commands.getoutput('ifconfig wlan0 down')
print '##ifconfig wlan0 down '
time.sleep(1)
retval = commands.getoutput('systemctl stop hostapd')
print '##systemctl stop hostapd'
# print retval
else:
# retval = commands.getoutput('mpc prev')
retval = commands.getoutput('volumio previous')
print '#prev'
print retval
def next(self,cmd):
if cmd is 2:
retval = commands.getoutput('ifconfig wlan0 up')
print '##ifconfig wlan0 up '
# print retval
time.sleep(5)
# retval = subprocess.check_output('/usr/sbin/hostapd /etc/hostapd/hostapd.conf', shell=True)
retval = commands.getoutput('systemctl start hostapd')
# print retval
print '##systemctl start hostapd'
else:
# retval = commands.getoutput('mpc next')
retval = commands.getoutput('volumio next')
print '#next'
print retval
def stop(self,cmd):
if cmd is 2:
retval = commands.getoutput('shutdown -h now')
else:
# retval = commands.getoutput('mpc stop')
retval = commands.getoutput('volumio stop')
print '#stop'
print retval
def volup(self,cmd):
if self.vol_state is True:
# retval = commands.getoutput('mpc volume +2') # for general
# retval = commands.getoutput('amixer -c 1 sset SoftMaster 1%+') # for volumio2
retval = commands.getoutput('volumio volume plus')
print '#vol up'
print retval
def voldw(self,cmd):
if self.vol_state is True:
# retval = commands.getoutput('mpc volume -2')# for general
# retval = commands.getoutput('amixer -c 1 sset SoftMaster 1%-') # for volumio2
retval = commands.getoutput('volumio volume minus')
print '#vol dw'
print retval
class Button(object):
def __init__(self, pin, func):
self.pin = pin
self.old_state = 1
self.func = func
self.up_timer = 0
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def update(self):
curr_state = GPIO.input(self.pin)
if self.old_state is 1 and curr_state is 0:
self.func(1)
self.up_timer = 0
if self.old_state is 0 and curr_state is 0:
self.up_timer += 1
if self.up_timer > 20:
self.func(2)
self.up_timer = 18
self.old_state = curr_state
def main():
# GPIO.cleanup()
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
player = Player()
prev_btn = Button(PIN_PREV_BTN, lambda x: player.prev(x))
play_btn = Button(PIN_PLAY_BTN, lambda x: player.play(x))
next_btn = Button(PIN_NEXT_BTN, lambda x: player.next(x))
stop_btn = Button(PIN_STOP_BTN, lambda x: player.stop(x))
voldw_btn = Button(PIN_VOL_DW_BTN, lambda x: player.voldw(x))
volup_btn = Button(PIN_VOL_UP_BTN, lambda x: player.volup(x))
while True:
prev_btn.update()
play_btn.update()
next_btn.update()
stop_btn.update()
voldw_btn.update()
volup_btn.update()
player.sdtimer()
time.sleep(0.05)
if __name__ == '__main__':
# with daemon.DaemonContext(pidfile=PIDLockFile('/var/run/mpd_ctrl.pid')):
main()
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment