forked from DexterInd/BrickPi_Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_sensor_test.py
More file actions
114 lines (105 loc) · 3.19 KB
/
Copy pathexample_sensor_test.py
File metadata and controls
114 lines (105 loc) · 3.19 KB
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
113
114
#!/usr/bin/env python
# example_sensor_test.py
#
# Frans Duijnhouwer
# frans.duijnhouwer<at>gmail.com
#
# Initial Date: January 28, 2014
# Last Updated: February 11, 2014
#
# This file has been made available online through a Creative Commons
# Attribution-ShareAlike 3.0 license.
# (http://creativecommons.org/licenses/by-sa/3.0/)
#
# Test program for ThreadSafeBrickPi implementation of LEGO sensors
#
import ThreadSafeBrickPi
import threading
import time
BPi = ThreadSafeBrickPi
running = True
class myThreadOne( threading.Thread ):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
def run( self ):
time.sleep(1)
bpc = BPi.BrickPiCom(0.01)
us = bpc.register_sensor(
"BrickPiLegoUltraSonicSensorI2C",
"LEGO-Sensors",
BPi.PORT_1)
ls = bpc.register_sensor(
"BrickPiLegoLightSensor",
"LEGO-Sensors",
BPi.PORT_2,
1)
ss = bpc.register_sensor(
"BrickPiLegoSoundSensor",
"LEGO-Sensors",
BPi.PORT_3)
tstart = time.time()
while running:
bpc.update()
print("%s: Ultra sonic sensor value : %d"
% (self.name, us.get_value()))
print("%s: Light sensor value : %d"
% (self.name, ls.get_value()))
print("%s: Sound sensor value : %d"
% (self.name, ss.get_value()))
time.sleep(0.6)
telapsed = time.time() - tstart
if telapsed > 5:
tstart = time.time()
ls.toggle_light()
class myThreadTwo( threading.Thread ):
def __init__(self, threadID, name):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
def run(self):
time.sleep(1)
bpc = BPi.BrickPiCom(0.01)
us = bpc.register_sensor(
"BrickPiLegoUltraSonicSensorI2C",
"LEGO-Sensors",
BPi.PORT_1)
cs = bpc.register_sensor(
"BrickPiLegoColorSensor",
"LEGO-Sensors",
BPi.PORT_4)
tstart = time.time()
while running:
bpc.update()
print("%s: Ultra sonic sensor value : %d"
% ( self.name, us.get_value()))
print("%s: Color sensor color : %s"
% ( self.name, cs.get_color()))
print("%s: Color sensor value : %d"
% ( self.name, cs.get_value()))
time.sleep(0.5)
telapsed = time.time() - tstart
if telapsed > 5:
tstart = time.time()
cs.set_mode((cs.get_mode() + 1) % 5)
thread1 = myThreadOne( 1, "Thread-1" )
thread2 = myThreadTwo( 2, "Thread-2" )
thread1.setDaemon( True )
thread2.setDaemon( True )
thread1.start()
thread2.start()
while True:
try:
print "Press enter to quit."
c = raw_input( "" )
running = False
time.sleep(0.2)
print "Bye"
break;
except KeyboardInterrupt: #Triggered by pressing Ctrl+C
running = False
time.sleep(0.2)
print "Bye"
break;
time.sleep( 1 )