1
2
3
4 '''
5 This software is part of the raspibrick module.
6 It is Open Source Free Software, so you may
7 - run the code for any purpose
8 - study how the code works and adapt it to your needs
9 - integrate all or parts of the code in your own programs
10 - redistribute copies of the code777
11 - improve the code and release your improvements to the public
12 However the use of the code is entirely your responsibility.
13 '''
14
15 from RobotInstance import RobotInstance
16 from Tools import Tools
17 import time
18
20 '''
21 Class that represents a beeper.
22 '''
24 '''
25 Creates a beeper instance attached to the given GPIO port.
26 @param pin: the GPIO port number (default: 40)
27 '''
28 self._checkRobot()
29 self.device = "beeper"
30 self.robot = RobotInstance.getRobot()
31 self.robot.sendCommand(self.device + ".create." + str(pin))
32
34 '''
35 Turns the beeper on.
36 '''
37 self._checkRobot()
38 self.robot.sendCommand(self.device + ".turnOn")
39
41 '''
42 Turns the beeper off.
43 '''
44 self._checkRobot()
45 self.robot.sendCommand(self.device + ".turnOff")
46
47 - def start(self, onTime, offTime, count = 0, blocking = False):
48 '''
49 Starts beeping. The beeping period is offTime + onTime.
50 May be stopped by calling stop(). If blocking is False, the
51 function returns immediately while the blinking goes on. The blinking is stopped by setColor().
52 @param onTime: the time in ms in on state
53 @param offTime: the time in ms in off state
54 @param count: total number of on states; 0 for endlessly (default)
55 @param blocking: if True, the method blocks until the beeper has finished; otherwise
56 it returns immediately (default: False)
57 '''
58 self._checkRobot()
59 blockingStr = "1" if blocking else "0"
60 self.robot.sendCommand(self.device + ".start." +
61 str(onTime) + "." +
62 str(offTime) + "." +
63 str(count) + "." +
64 str(blockingStr))
65
72
74 '''
75 Sets the time the speaker is off.
76 @param offTime: the offTime in ms
77 '''
78 self._checkRobot()
79 self.robot.sendCommand(self.device + ".setOffTime." + str(offTime))
80
82 '''
83 Sets the time the speaker is on.
84 @param onTime: the onTime in ms
85 '''
86 self._checkRobot()
87 self.robot.sendCommand(self.device + ".setOnTime." + str(onTime))
88
89 - def beep(self, count = 1):
90 '''
91 Emits a short beep the given number of times. Blocking until the beeps are played.
92 @param count: the number of beeps
93 '''
94 self.start(60, 120, count, True)
95
97 '''
98 @return: True, if the beeper is active; otherwise False
99 '''
100 time.sleep(0.001)
101 rc = self.robot.sendCommand(self.device + ".isBeeping")
102 v = True if rc == "True" else False
103 return v
104
105
106
107
111