-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_sensor_relays.py
108 lines (76 loc) · 2.39 KB
/
test_sensor_relays.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
# coding: utf-8
"""Test."""
import argparse
import json
import RPi.GPIO as GPIO
from inout.relay import Relay
from inout.ultrasonic import UltrasonicSensor
def parse_args():
"""Command line parser"""
parser = argparse.ArgumentParser()
parser.add_argument("--pins", type=str, default="./config/pins.json",
help="Path to the pin config file (default: './config/pins.json')")
return parser.parse_args()
def gpio(function):
"""Decorator.
- Set mode to GPIO.BCM
- Call GPIO.cleanup if an exception is raised (or if ctrl+c)
"""
def gpio_function():
GPIO.setmode(GPIO.BCM)
try:
function()
except KeyboardInterrupt:
print("\nInterruption from user.")
except Exception as e:
print(e)
finally:
print("Cleaning GPIO.")
GPIO.cleanup()
return gpio_function
@gpio
def main():
# Parse command line
args = parse_args()
# set pin mode
# GPIO.setmode(GPIO.BCM)
# Load pin info
with open(args.pins, "r") as f:
pins = json.load(f)
# Create ultrasonic sensor
sensor = UltrasonicSensor(trig=pins["trigger"],
echo=pins["echo"],
temperature=20)
# Create relays
relays = {}
for id, pin in pins["relay"].items():
if pin is None:
print("Relay {} is not available (according to 'pins.json')".format(id))
relays[int(id)] = None
continue
relays[int(id)] = Relay(pin)
while True:
# Read input from user
i = input("Press 'm' to measure, or any number between 1 and 8 to change a relay state (ctrl + c to stop): ").lower()
# Measurement
if i == 'm':
dist = sensor.median_measure() * 100
print("\nMeasured Distance = %.1f cm" % dist)
continue
# Relay
try:
i = int(i)
if i < 1 or i > 8:
print("Number out of range: {}".format(i))
continue
except:
print("Invalid input: {}".format(i))
continue
r = relays[i]
if r is None:
print("Relay {} is unavailable".format(i))
continue
# Reverse relay state
r.reverse()
if __name__ == "__main__":
main()