-
Notifications
You must be signed in to change notification settings - Fork 0
/
hardwareAdapter.py
112 lines (94 loc) · 3.77 KB
/
hardwareAdapter.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import RPi.GPIO as GPIO
import sys
import time
# -----------------------------------------
# ----------------------------------------- DEFINE PINS
# -----------------------------------------
firePins = [
7, # GPIO 4 || channel 1
29, # GPIO 5 || channel 2
31, # GPIO 6 || channel 3
26 # GPIO 7 || channel 4
]
btnPin = 12 # GPIO 18
# prepare args, remove first arg (script name)
args = sys.argv
args.pop(0)
# -----------------------------------------
# ----------------------------------------- FUNCTIONS
# -----------------------------------------
# cleanup GPIO pins && exit script with defined error code
def exitSave(exitCode = 0):
GPIO.cleanup()
sys.exit(exitCode)
# setup GPIO pins
def configureGPIO():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
# configure fire pins
for pin in firePins:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.HIGH)
# configure button
GPIO.setup(btnPin, GPIO.IN)
# fire a single GPIO pin
def firePin(pinNr):
GPIO.output(pinNr, GPIO.LOW)
time.sleep(0.5)
GPIO.output(pinNr, GPIO.HIGH)
# fire all GPIO pins at once
def fireAllPins():
# enable pins
for pin in firePins:
GPIO.output(pin, GPIO.LOW)
time.sleep(0.5)
# disable pins
for pin in firePins:
GPIO.output(pin, GPIO.HIGH)
def isHardwareDisabled():
# this is a security messure! there is a switch on the device. If this switch is on disable, the whole divice can't be armed!
return(GPIO.input(btnPin) != GPIO.HIGH)
# -----------------------------------------
# ----------------------------------------- SCRIPT
# -----------------------------------------
configureGPIO()
# check the arguments provided
if len(sys.argv) == 1:
if args[0] == 'fireall':
# \/ START ----------------------------------------- FIRE ALL \/
if isHardwareDisabled():
print("The device is hardware disabled")
exitSave(1)
fireAllPins()
exitSave(0)
# /\ END ----------------------------------------- FIRE ALL /\
elif args[0] == 'ishardwaredisabled':
# \/ START ----------------------------------------- IS HARDWARE DISABLED \/
print(isHardwareDisabled())
exitSave(0)
# /\ END ----------------------------------------- IS HARDWARE DISABLED /\
else:
print("Please provide a valid option 'fireall' or 'fire <num>' or 'ishardwaredisabled'")
exitSave(1)
elif len(sys.argv) == 2:
if args[0] == 'fire':
# \/ START ----------------------------------------- FIRE \/
if isHardwareDisabled():
print("The device is hardware disabled")
exitSave(1)
if not args[1].isdigit():
print("Please provide a number between 1 and {}".format(len(firePins)))
exitSave(1)
if int(args[1]) > len(firePins) or int(args[1]) <= 0:
print("Please provide a number between 1 and {}".format(len(firePins)))
exitSave(1)
pin = firePins[int(args[1]) - 1]
firePin(pin)
exitSave(0)
# /\ END ----------------------------------------- FIRE /\
else:
print("Please provide a valid option 'fireall' or 'fire <num>' or 'ishardwaredisabled'")
exitSave(1)
else:
print("Please provide a valid option 'fireall' or 'fire <num>' or 'ishardwaredisabled'")
exitSave(1)