#
# startserialdisk.py
#
# This Python script uses the GPIO library to decide which RK05 packs to use with SerialDisk.
#
# The easiest way to wire this up is to use a single-pole centre-off two-position switch. One end of the switch
# gets wired to LSB-Pin. The other end gets wired to MSB-Pin. The centre pin gets wired to GND. Depending on the
# switch position (3 possibilities) it will run "start-sd-option-1", "start-sd-option-2", or "start-sd-option-3".
#
# If you instead use a 4-position rotary switch and some diodes, you could also use "start-sd-option-4".
#
# Malcolm Macleod - August 2017.
#

import subprocess
import RPi.GPIO as GPIO

LSB_PIN = 16 # GPIO channel 16. 3rd pin from USB end, on board-edge side.
MSB_PIN = 12 # GPIO channel 12. 5th pin from USB end, on board-edge side.
# FYI: The pin between GPIO12 and GPIO16 is GND.

GPIO.setmode(GPIO.BCM) #Use BCM numbering scheme.
GPIO.setup(LSB_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Setup the pin as an input with a 50K Ohm pull up.
GPIO.setup(MSB_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) #Setup the pin as an input with a 50K Ohm pull up.

if (GPIO.input(LSB_PIN)==True) and (GPIO.input(MSB_PIN)==True):
	# Both Pins are High
	script_name = "start-sd-option-1"
elif (GPIO.input(LSB_PIN)==False) and (GPIO.input(MSB_PIN)==True):
	# Only LSB-PIN is Low
	script_name = "start-sd-option-2"
elif (GPIO.input(LSB_PIN)==True) and (GPIO.input(MSB_PIN)==False):
	# Only MSB-PIN is Low
	script_name = "start-sd-option-3"
else:
	# Both Pins are Low
	script_name = "start-sd-option-4"

print ("Starting SerialDisk with the following script (based on position of front panel switch): " + script_name)

subprocess.call(['/home/pi/' + script_name + '&'], shell=True)

GPIO.cleanup()       #Clean up GPIO on exit
