-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsensor_showcase.py
executable file
·78 lines (63 loc) · 2.41 KB
/
sensor_showcase.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
#!/usr/bin/env python
import argparse
from motion_detector import *
from led import *
from distance_detector import *
parser = argparse.ArgumentParser(description='Sensor showcase')
parser.add_argument('-a','--all', action="store_true", help='Show all demos')
parser.add_argument('-m','--motion', action="store_true", help='Motion sensor demo')
parser.add_argument('-l','--led', action="store_true", help=u'Led demo')
parser.add_argument('-d','--distance', action="store_true", help=u'Distance measurement demo')
args = parser.parse_args()
def header(title='Some demo'):
char = '*'
frame = char * (len(title) + 4)
print frame
print "%s %s %s" % (char,title,char)
print frame
print "Press CTRL+C to quit at any time"
DEMO_RUNTIME_SEC = 30
if args.all or args.motion:
header("Motion sensor demo")
print "Will be moving to next demo in %d seconds" % DEMO_RUNTIME_SEC
print "Wave or walk in front of connected PIR sensor. You should see message once motion is detected."
start = time.time()
md = MotionDetector()
i=0
while time.time() - start < DEMO_RUNTIME_SEC:
if md.any_movement():
i+=1
print "Motion Detected - %d" % i
print '...zzz'
time.sleep(0.5)
if args.all or args.led:
header("LED light demo")
print "Will be moving to next demo in %d seconds" % DEMO_RUNTIME_SEC
start = time.time()
led = Light()
LIGHT_TIME_SEC = 5
SLEEP_TIME_SEC = 3
while time.time() - start < DEMO_RUNTIME_SEC:
print "Shining for %d seconds..." % LIGHT_TIME_SEC
led.shine_for(LIGHT_TIME_SEC)
print '...zzz'
time.sleep(SLEEP_TIME_SEC)
if args.all or args.distance:
header("Distance detector demo")
print "Will be moving to next demo in %d seconds" % DEMO_RUNTIME_SEC
detector = DistanceDetector()
start = time.time()
INTERVAL_SEC = 3
while time.time() - start < DEMO_RUNTIME_SEC:
pos = 0
false_count = 0
collective = 0
current_measure_start=time.time()
while time.time() - current_measure_start < INTERVAL_SEC:
reading = detector.measure()
if reading > 0:
pos += 1
collective += reading
else:
false_count += 1
print "Distance measured over interval of %d sec: %f cm (pos: %d, false: %d)" % (INTERVAL_SEC, collective/pos if pos > 0 else -1, pos, false_count)