-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_inputs.py
56 lines (46 loc) · 1.99 KB
/
read_inputs.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
# External module imports
import RPi.GPIO as GPIO
sampleRounds=1000
in1 = 6 # master_bhye
in2 = 13 # harvest_byhve
in3 = 19 # tanks_not_full
in4 = 26 # charge
# Pin Setup:
GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
GPIO.setup(in1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(in2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(in3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(in4, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def read_inputs_st():
master = harvest = tank = charge = 0
controllerStateLow=0
controllerStateHigh=1
while controllerStateLow != controllerStateHigh:
for i in range(sampleRounds):
master += GPIO.input(in1)
harvest += GPIO.input(in2)
tank += GPIO.input(in3)
charge += GPIO.input(in4)
controllerStateLow = (master>sampleRounds/3)<<3 | (harvest>sampleRounds/3)<<2 | (tank>sampleRounds/3) <<1 | (charge>sampleRounds/3)
controllerStateHigh = (master>2*sampleRounds/3)<<3 | (harvest>2*sampleRounds/3)<<2 | (tank>2*sampleRounds/3) <<1 | (charge>2*sampleRounds/3)
print("%04x, %04x" % (controllerStateLow, controllerStateHigh))
return controllerStateLow, master, harvest, tank, charge
def read_inputs():
master = harvest = tank = charge = 0
for i in range(sampleRounds):
master += GPIO.input(in1)
harvest += GPIO.input(in2)
tank += GPIO.input(in3)
charge += GPIO.input(in4)
controllerState = (master>sampleRounds/2)<<3 | (harvest>sampleRounds/2)<<2 | (tank>sampleRounds/2) <<1 | (charge>sampleRounds/2)
return controllerState, master, harvest, tank, charge
# main
def main():
try:
while 1:
controllerState, master, harvest, tank, charge = read_inputs()
print("Current Controller State: %04x (%04x %04x %04x %04x)" % (controllerState, master, harvest, tank, charge))
except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly:
GPIO.cleanup() # cleanup all GPIO
if __name__ == "__main__":
main()