1
2
3 '''
4 Class that represents an active buzzer on some GPIO port.
5
6
7 This software is part of the raspibrick module.
8 It is Open Source Free Software, so you may
9 - run the code for any purpose
10 - study how the code works and adapt it to your needs
11 - integrate all or parts of the code in your own programs
12 - redistribute copies of the code777
13 - improve the code and release your improvements to the public
14 However the use of the code is entirely your responsibility.
15 '''
16
17 import SharedConstants
18 from RobotInstance import RobotInstance
19 from Tools import Tools
20 from threading import Thread
21 import RPi.GPIO as GPIO
22 import time
23
24
26 '''
27 Abstraction of the beeper attached to given port (and ground).
28 @param port: the GPIO port number (default: 40)
29 '''
38
40 '''
41 Turns the beeper on.
42 '''
43 Tools.debug("Beeper turned on")
44 GPIO.output(self._pin, GPIO.HIGH)
45
47 '''
48 Turns the beeper off.
49 '''
50 Tools.debug("Beeper turned off")
51 GPIO.output(self._pin, GPIO.LOW)
52
53 - def beep(self, count = 1):
54 '''
55 Emits a short beep the given number of times. Blocking until the beeps are played.
56 @param count: the number of beeps
57 '''
58 self.start(60, 120, count, True)
59
60 - def start(self, onTime, offTime, count = 0, blocking = False):
61 '''
62 Starts beeping. The beeping period is offTime + onTime.
63 May be stopped by calling stop(). If blocking is False, the
64 function returns immediately while the blinking goes on. The blinking is stopped by setColor().
65 @param onTime: the time in ms in on state
66 @param offTime: the time in ms in off state
67 @param count: total number of on states; 0 for endlessly (default)
68 @param blocking: if True, the method blocks until the beeper has finished; otherwise
69 it returns immediately (default: False)
70 '''
71 Tools.debug("Starting beeper with params onTime = " + str(onTime) +
72 " offTime = " + str(offTime) +
73 " count = " + str(count) +
74 " blocking = " + str(blocking))
75 if self._beeperThread != None:
76 self.stop()
77 self._beeperThread = BeeperThread(self, onTime, offTime, count)
78 if blocking:
79 while self.isBeeping():
80 continue
81
83 '''
84 Sets the time the speaker is off.
85 @param offTime: the offTime in ms
86 '''
87 if self._beeperThread != None:
88 self._beeperThread._offTime = offTime
89
91 '''
92 Sets the time the speaker is on.
93 @param onTime: the onTime in ms
94 '''
95 if self._beeperThread != None:
96 self._beeperThread._onTime = onTime
97
99 '''
100 Sets the time the speaker is on and off.
101 @param onTime: the onTime in ms
102 @param offTime: the offTime in ms
103 '''
104 if self._beeperThread != None:
105 self._beeperThread._onTime = onTime
106 self._beeperThread._offTime = offTime
107
109 '''
110 Stops beeping.
111 '''
112 if self._beeperThread != None:
113 self._beeperThread.stop()
114
116 '''
117 @return: True, if the beeper is active; otherwise False
118 '''
119 time.sleep(0.001)
120 return self._beeperThread != None
121
122
126
127
128
129
130
132 - def __init__(self, beeper, onTime, offTime, count):
133 Thread.__init__(self)
134 self._beeper = beeper
135 self._onTime = onTime
136 self._offTime = offTime
137 self._count = count
138 self._isAlive = True
139 self.start()
140
142 Tools.debug("Beeper thread started")
143 nb = 0
144 self._isRunning = True
145 while self._isRunning:
146 if self._onTime <= 0:
147 self._beeper.turnOff()
148 time.sleep(0.01)
149 else:
150 self._beeper.turnOn()
151 startTime = time.time()
152 while time.time() - startTime < self._onTime / 1000 and self._isRunning:
153 time.sleep(0.001)
154 if not self._isRunning:
155 break
156
157 self._beeper.turnOff()
158 startTime = time.time()
159 while time.time() - startTime < self._offTime / 1000 and self._isRunning:
160 time.sleep(0.001)
161 if not self._isRunning:
162 break
163
164 nb += 1
165 if nb == self._count:
166 self._isRunning = False
167 self._beeper.turnOff()
168 self._beeper._beeperThread = None
169 self._isAlive = False
170 Tools.debug("Beeper thread finished")
171
173 self._isRunning = False
174 while self._isAlive:
175 continue
176