Raspberry pi3でvolumioを使用しています。
GPIOを使用してボタンを押すと曲を再生するプログラムを使用しています。
※プログラムをコピペしたものです
python
1#!/usr/bin/python 2#-*- coding: utf-8 -*- 3u''' 4http://www.rpiblog.com/2012/09/using-gpio-of-raspberry-pi-to-blink-led.html 5https://github.com/if1live/rpi-mpd-controller 6http://nw-electric.way-nifty.com/blog/2014/01/volumio-1ad0.html 7''' 8 9''' 10sudo apt-get update 11sudo apt-get -y install python-rpi.gpio python-daemon 12''' 13 14import RPi.GPIO as GPIO 15import time 16import commands 17import re 18from daemon import daemon 19from daemon.pidlockfile import PIDLockFile 20 21PIN_PREV_BTN = 8 22PIN_PLAY_BTN = 10 23PIN_NEXT_BTN = 12 24PIN_STOP_BTN = 7 25PIN_VOL_UP_BTN = 11 26PIN_VOL_DW_BTN = 13 27 28class Player(object): 29 def __init__(self): 30 self.re_play = re.compile(r'^[playing].+$') 31 self.re_vol = re.compile(r'^volume:.+% .+$') 32 self.play_state = self.is_playing() 33 self.shutdown_timer = 0 34 35 def sdtimer(self): 36 if self.shutdown_timer is not 0: 37 self.shutdown_timer -= 1 38 39 def is_playing(self): 40 result = commands.getstatusoutput('mpc') 41 line_list = result[1].splitlines() 42 43 playing = False 44 for line in line_list: 45 if self.re_play.match(line) is not None: 46 playing = True 47 48 self.play_state = playing 49 50 for line in line_list: 51 if self.re_vol.match(line) is None: 52 self.vol_state = False 53# print '## vol False' 54 else: 55 self.vol_state = True 56# print '## vol True' 57 58 return playing 59 60 def play(self): 61 if self.is_playing(): 62 retval = commands.getoutput('mpc pause') 63 self.play_state = False 64 print '#pause' 65 else: 66 retval = commands.getoutput('mpc play') 67 self.play_state = True 68 print '#play' 69 print retval 70 71# result = commands.getstatusoutput('mpc') 72# line_list = retval[1].splitlines() 73# for line in line_list: 74# if self.re_vol.match(line) is None: 75# self.vol_state = False 76# print '### vol False' 77# else: 78# self.vol_state = True 79# print '### vol True' 80 81 def prev(self): 82 retval = commands.getoutput('mpc prev') 83 print '#prev' 84 print retval 85 86 def next(self): 87 retval = commands.getoutput('mpc next') 88 print '#next' 89 print retval 90 91 def stop(self): 92 if self.shutdown_timer is 0: 93 self.shutdown_timer = 20 94 retval = commands.getoutput('mpc stop') 95 print '#stop' 96 print retval 97 else: 98 retval = commands.getoutput('shutdown -h now') 99 100 def volup(self): 101 if self.vol_state is True: 102 retval = commands.getoutput('mpc volume +2') 103 print '#vol up' 104 print retval 105 106 def voldw(self): 107 if self.vol_state is True: 108 retval = commands.getoutput('mpc volume -2') 109 print '#vol dw' 110 print retval 111 112class Button(object): 113 def __init__(self, pin, func): 114 self.pin = pin 115 self.old_state = 1 116 self.func = func 117 GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) 118 119 def update(self): 120 curr_state = GPIO.input(self.pin) 121 if self.old_state is 1 and curr_state is 0: 122 self.func() 123 self.old_state = curr_state 124 125 126def main(): 127# GPIO.cleanup() 128 GPIO.setmode(GPIO.BOARD) 129 GPIO.setwarnings(False) 130 131 132 player = Player() 133 134 prev_btn = Button(PIN_PREV_BTN, lambda: player.prev()) 135 play_btn = Button(PIN_PLAY_BTN, lambda: player.play()) 136 next_btn = Button(PIN_NEXT_BTN, lambda: player.next()) 137 stop_btn = Button(PIN_STOP_BTN, lambda: player.stop()) 138 voldw_btn = Button(PIN_VOL_DW_BTN, lambda: player.voldw()) 139 volup_btn = Button(PIN_VOL_UP_BTN, lambda: player.volup()) 140 141 while True: 142 prev_btn.update() 143 play_btn.update() 144 next_btn.update() 145 stop_btn.update() 146 voldw_btn.update() 147 volup_btn.update() 148 player.sdtimer() 149 time.sleep(0.05) 150 151if __name__ == '__main__': 152 with daemon.DaemonContext(pidfile=PIDLockFile('/var/run/mpd_ctrl.pid')): 153 main() 154 GPIO.cleanup() 155
起動して、上記のプログラムを実行しボタンを押すと曲が流れます。
今回、モニターを外して使えるように、起動時に自動で上記のプログラムを実行したいと思い、下記のようにexit0の前に設定しました。
(今回のプログラムファイル mp.py)
Linux
1/usr/bin/python /home/mp.py 2 3exit 0
再起動してみましたが、実行されませんでした。
volumioを起動すると最初にloginとpwを聞かれるため、自動起動はそもそもできないものなのでしょうか?
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。