Skip to content

Instantly share code, notes, and snippets.

@littilewing
Last active February 18, 2016 06:19
Show Gist options
  • Save littilewing/bc18b340d03c916ee237 to your computer and use it in GitHub Desktop.
Save littilewing/bc18b340d03c916ee237 to your computer and use it in GitHub Desktop.
song select function added.
# -*- coding: utf-8 -*-
# Attach to auto start
# add hayashi 160213
# /etc/rc.local
# sudo python /home/pi/Documents/docomo/jack.py > /dev/null 2>&1 &
import RPi.GPIO as GPIO
import time
import os
import datetime
import logging
songid = 1
def readFile():
global songid
f = open('/home/pi/Documents/docomo/songid.txt', 'r')
for line in f:
songid = int(line)
f.close()
def writeFile(id):
f = open("/home/pi/Documents/docomo/songid.txt","w")
f.write( str(id) )
f.close()
###
### GPIO Settings
###
ch_jack = 5
ch_ledout = 13
ch_pwsw = 21
GPIO.setmode(GPIO.BCM)
#jack pinをプルアップする
GPIO.setup(ch_jack, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#LEDを光らせる
GPIO.setup(ch_ledout, GPIO.OUT )
GPIO.output(ch_ledout,GPIO.HIGH)
#電源スイッチをプルアップする
GPIO.setup(ch_pwsw,GPIO.IN,pull_up_down=GPIO.PUD_UP)
###
### Logrfile Settings
###
#rootロガーを取得
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
#出力のフォーマットを定義
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
#ファイルへ出力するハンドラーを定義
fh = logging.FileHandler(filename='/home/pi/Documents/docomo/log.txt')
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
#rootロガーにハンドラーを登録する
logger.addHandler(fh)
###
### functions
###
def switch_callback(gpio_pin):
if GPIO.input(gpio_pin):
logging.debug("HIi -")
sound_play()
else:
logging.debug("LOW - ")
sound_play()
# mp3を再生する
def sound_play():
global songid
todaydetail = datetime.datetime.today()
GPIO.output(ch_ledout,GPIO.LOW)
os.system('killall mpg321')
time.sleep(0.5)
filename = "/home/pi/Documents/docomo/mp3/voice%02d.mp3" % int(songid)
logging.info( "{} {}".format (todaydetail.strftime("%Y/%m/%d %H:%M:%S\tStartPlay") ,filename))
os.system('mpg321 -q {} &'.format(filename))
GPIO.output(ch_ledout,GPIO.HIGH)
def changesong():
global songid
songid = int(songid) + 1
if(songid > 7):
songid = 1
writeFile(songid)
logging.debug("change song to {}".format(songid))
sound_play()
def switchdown_callback(gpio_pin):
logging.debug("power is low pin:{} state:{}".format(gpio_pin,GPIO.input(gpio_pin)))
# シャットダウンスイッチ用の変数
button_current = 1 #ボタンの押下状態 0:ON 1:OFF
button_previous = 1 #ループ一回前のボタンの状態
flag_pressed = 0 #連続した押下状態
button_count = 0 #ボタンの連続押しをカウント。この数で制御を切り分ける
reboot_count = 30 # rebootモードに入るまでのbutton_countの閾値
while True:
button_current = GPIO.input(gpio_pin)
flag_pressed = button_previous + button_current
if( button_count >= reboot_count and button_count < 100 ):
if(button_current):
logging.info("----------REBOOT message")
GPIO.output(ch_ledout,GPIO.HIGH)
os.system("sudo shutdown -r now")
break
elif( (not flag_pressed) and button_count == 100):
logging.info("----------SHUTDOWN message")
GPIO.output(ch_ledout,GPIO.LOW)
os.system("sudo shutdown -h now")
break
elif(button_count < reboot_count and button_current):
changesong()
break
button_previous = button_current
if( button_count >= reboot_count):
if( button_count % 2 == 1):
GPIO.output(ch_ledout,GPIO.HIGH)
else:
GPIO.output(ch_ledout,GPIO.LOW)
if (not(flag_pressed)):
button_count += 1
else:
button_count = 0
time.sleep(0.03)
###
### main
###
readFile();
logging.info("START jack.py awake songid:{}----------------------------".format(songid) )
sound_play()
GPIO.add_event_detect(ch_jack, GPIO.BOTH, callback=switch_callback,bouncetime=800)
GPIO.add_event_detect(ch_pwsw, GPIO.FALLING, callback=switchdown_callback,bouncetime=800)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
logging.info("STOP jack.py ----------------------")
GPIO.output(ch_ledout,GPIO.LOW)
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment