#!/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)