1   
  2   
  3  ''' 
  4  Class that represents an active buzzer on some GPIO port. 
  5   
  6    
  7   This software is part of the raspibrickpi 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  from Tools import Tools 
 18  from threading import Thread 
 19  import RPi.GPIO as GPIO 
 20  import time 
 21   
 22   
 24      ''' 
 25      Abstraction of the beeper attached to given port (and ground). 
 26      @param port: the GPIO port number (default: 22) 
 27      ''' 
 29          GPIO.setmode(GPIO.BOARD) 
 30          GPIO.setwarnings(False) 
 31          self._pin = pin 
 32          self._beeperThread = None 
 33   
 34          GPIO.setup(pin, GPIO.OUT) 
 35          self.turnOff() 
  36       
 38          ''' 
 39          Turns the beeper on. 
 40          ''' 
 41   
 42          GPIO.output(self._pin, GPIO.HIGH) 
  43   
 45          ''' 
 46          Turns the beeper off. 
 47          ''' 
 48   
 49          GPIO.output(self._pin, GPIO.LOW) 
  50   
 51 -    def beep(self, count = 1): 
  52          ''' 
 53          Emits a short beep the given number of times. Blocking until the beeps are played. 
 54          @param count: the number of beeps 
 55          ''' 
 56          self.start(60, 120, count, True) 
  57       
 58 -    def start(self, onTime, offTime, count = 0, blocking = False): 
  59          ''' 
 60          Starts beeping. The beeping period is offTime + onTime.  
 61          May be stopped by calling stop(). If blocking is False, the 
 62          function returns immediately while the blinking goes on. The blinking is stopped by setColor(). 
 63          @param onTime: the time in ms in on state 
 64          @param offTime: the time in ms in off state 
 65          @param count: total number of on states; 0 for endlessly (default) 
 66          @param blocking: if True, the method blocks until the beeper has finished; otherwise 
 67           it returns immediately (default: False) 
 68          ''' 
 69   
 70   
 71   
 72   
 73          if self._beeperThread != None: 
 74              self.stop() 
 75          self._beeperThread = BeeperThread(self, onTime, offTime, count) 
 76          if blocking: 
 77              while self.isBeeping(): 
 78                  continue 
  79   
 81          ''' 
 82          Sets the time the speaker is off. 
 83          @param offTime: the offTime in ms 
 84          ''' 
 85          if self._beeperThread != None: 
 86              self._beeperThread._offTime = offTime 
  87   
 89          ''' 
 90          Sets the time the speaker is on. 
 91          @param onTime: the onTime in ms 
 92          ''' 
 93          if self._beeperThread != None: 
 94              self._beeperThread._onTime = onTime 
  95   
 97          ''' 
 98          Sets the time the speaker is on and off. 
 99          @param onTime: the onTime in ms 
100          @param offTime: the offTime in ms 
101          ''' 
102          if self._beeperThread != None: 
103              self._beeperThread._onTime = onTime 
104              self._beeperThread._offTime = offTime 
 105          
107          ''' 
108          Stops beeping. 
109          ''' 
110          if self._beeperThread != None: 
111              self._beeperThread.stop() 
 112   
114          ''' 
115          @return: True, if the beeper is active; otherwise False 
116          ''' 
117          time.sleep(0.001) 
118          return self._beeperThread != None 
  119       
120   
121   
122   
124 -    def __init__(self, beeper, onTime, offTime, count): 
 125          Thread.__init__(self) 
126          self._beeper = beeper 
127          self._onTime = onTime 
128          self._offTime = offTime 
129          self._count = count 
130          self._isAlive = True 
131          self.start() 
 132   
134   
135          nb = 0 
136          self._isRunning = True 
137          while self._isRunning: 
138              if self._onTime <= 0: 
139                  self._beeper.turnOff() 
140                  time.sleep(0.01) 
141              else: 
142                  self._beeper.turnOn() 
143                  startTime = time.time() 
144                  while time.time() - startTime < self._onTime / 1000 and self._isRunning: 
145                      time.sleep(0.001) 
146                  if not self._isRunning: 
147                      break 
148       
149                  self._beeper.turnOff() 
150                  startTime = time.time() 
151                  while time.time() - startTime < self._offTime / 1000 and self._isRunning: 
152                      time.sleep(0.001) 
153              if not self._isRunning: 
154                  break 
155   
156              nb += 1 
157              if nb == self._count: 
158                  self._isRunning = False 
159          self._beeper.turnOff() 
160          self._beeper._beeperThread = None 
161          self._isAlive = False 
 162   
163   
165          self._isRunning = False 
166          while self._isAlive:  
167              continue 
  168