aboutsummaryrefslogtreecommitdiffstats
path: root/rasp_startup.py
blob: 4a1a426bfb3d33e7d3ffff5cbbda064cd4a3862e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import RPi.GPIO as GPIO
import time
import thread
import subprocess
from subprocess import Popen, PIPE, STDOUT

# pin-setup
GPIO.setmode(GPIO.BOARD)
led_pin = 12
blk_pin = 16
red_pin = 18

# set volume
cmd_amixer1 = 'amixer -c 0 set PCM 98%'
cmd_amixer2 = 'amixer -c 0 set Speaker 98%'

# the process we want to start and its argument
#cmd_player = 'mplayer'
cmd_player = 'ogg123'
arg_player = []
url_player = 'http://192.168.1.33:8000/mpd.ogg'
process = 0

def playerThreadFunc():
   # we need in every thread, so it is a global
   global process
   # kill if running
   if process != 0:
      #if process.poll() == None:
         #process.communicate(input='q')
         if process.poll() == None:
            process.terminate()
            list_cmdarg = []
            list_cmdarg.append('killall')
            list_cmdarg.append(cmd_player)
            subprocess.call(list_cmdarg, shell=False)
         time.sleep(1)
   # turn on led
   GPIO.output(led_pin, True)
   list_cmdarg = []
   list_cmdarg.append(cmd_player)
   list_cmdarg.extend(arg_player)
   list_cmdarg.append(url_player)
   # start process and wait for termination
   process = subprocess.Popen(list_cmdarg)
   #process = subprocess.Popen(list_cmdarg, stdin=PIPE)
   process.wait()
   # turn off led
   GPIO.output(led_pin, False)

# callback function
def callBackFunc(channel=0):
   thread.start_new_thread(playerThreadFunc, ())

# init
GPIO.setup(led_pin, GPIO.OUT)
GPIO.setup(blk_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(red_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# add event
GPIO.add_event_detect(blk_pin, GPIO.RISING, callback=callBackFunc, bouncetime=300)

while True:
   subprocess.call(cmd_amixer1, shell=True)
   subprocess.call(cmd_amixer2, shell=True)
   thread.start_new_thread(playerThreadFunc, ())
   GPIO.wait_for_edge(red_pin, GPIO.FALLING)
   break

# clean
GPIO.cleanup()

subprocess.call('shutdown -h now', shell=True)