Parent: [86a38f] (diff)

Download this file

swgpio.py    82 lines (64 with data), 1.9 kB

 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
74
75
76
77
78
79
80
81
#!/usr/bin/env python
from __future__ import print_function
import sys
import time
import conftree
def perr(s):
print("%s"%s, file=sys.stderr)
try:
import RPi.GPIO as GPIO
except Exception as err:
perr("Error importing RPi.GPIO!: %s" % err)
sys.exit(1)
# We do things in several executions. A channel already setup is normal
def init():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
fconfig = conftree.ConfSimple('/etc/audioswitch.conf')
global speaker_channel, line_channel
speaker_channel = int(fconfig.get("speaker_channel"))
line_channel = int(fconfig.get("line_channel"))
def setup_gpio():
GPIO.setup(speaker_channel, GPIO.OUT)
GPIO.setup(line_channel, GPIO.OUT)
def reset_gpio():
GPIO.cleanup((speaker_channel, line_channel))
def spkon():
GPIO.output(speaker_channel, True)
def spkoff():
GPIO.output(speaker_channel, False)
def lineon():
GPIO.output(line_channel, True)
def lineoff():
GPIO.output(line_channel, False)
init()
# It seems that we need to always setup the channels even if this was
# done by a previous invocation, so we have no need or use for a
# separate setup command
setup_gpio()
##########
if __name__ == '__main__':
def usage():
perr("swgpio.py: Usage: swgpio.py <cmd> [<param> ...]")
perr("cmd:")
perr(" setup, reset, spkon, spkoff, lineon, lineoff")
perr
sys.exit(1)
if len(sys.argv) <= 1:
usage()
for cmd in sys.argv[1:]:
if cmd == "reset":
reset_gpio()
elif cmd == "spkon":
spkon()
elif cmd == "spkoff":
spkoff()
elif cmd == "lineon":
lineon()
elif cmd == "lineoff":
lineoff()
elif cmd == "sleep":
time.sleep(1)
else:
usage()
sys.exit(0)