Exemplo n.º 1
0
    def run_inner(self):
        """Perform one on/off/blink pulse."""
        try:
            item = self.blink_command_queue.get(block=True, timeout=self.blink_duration)
            self.blinking = item[0]
            if self.blinking:
                # Always begin opposite of current state for immediate visual feedback
                GPIO.output(self.output_pin, not GPIO.input(self.output_pin))
                # Calculate number of remaining on/off blink toggles after this one
                # A zero count means to keep blinking indefinitely
                self.blink_count = item[1] * 2 - 1 if item[1] else 0
            else:
                # Remember last non-blinking state of the light which will be restored
                # after finite blink count has completed.
                self.steady_state = item[1]
                GPIO.output(self.output_pin, item[1])
        except queue.Empty:
            if self.blinking:
                # If blinking a finite number of times, count each on/off transition
                if self.blink_count > 0:
                    self.blink_count -= 1
                    if self.blink_count == 0:
                        # Ensure at the end of finite blink count we always return to
                        # the last on/off steady state, even if a new blink was started
                        # before a previous blink has finished.
                        GPIO.output(self.output_pin, self.steady_state)
                        self.blinking = False

                # Toggle output if blinking indefinitely or finite count not exhausted
                if self.blinking:
                    # When blinking, invert every timeout expiration (we might have woken
                    # up for some other reason, but this appears to work in practice).
                    GPIO.output(self.output_pin, not GPIO.input(self.output_pin))
Exemplo n.º 2
0
  def run_inner(self):
    next_mode = self.set_queue.get(block=True)
    if next_mode == OFF:
      GPIO.output(self.output_pin, False)
    elif next_mode == ON:
      GPIO.output(self.output_pin, True)
    elif next_mode in (BEEP, BEEPING):
      # TODO Support both types of piezo buzzer/transducer.
      # As-is, this provides a logic HIGH to make a 4KHz sound on PK-12N40PQ;
      # newer prototypes include one that requires the Pi to make a 4KHz
      # square wave, which should use pigpio instead of RPi.GPIO.
      print "Beeping"
      while True:
        if not self.set_queue.empty():
          print "Done beeping"
          break
        GPIO.output(self.output_pin, True)
        time.sleep(0.3)
        GPIO.output(self.output_pin, False)
        time.sleep(0.3)
        if next_mode == BEEP:
	  break
        print "...more beep"
    elif next_mode == HAPPY:
      print "Happy"
      # TODO use pigpio
      p = GPIO.PWM(self.output_pin, 440)
      if p is None:
        # FakeRPi.GPIO does this
        return
      p.start(50)
      time.sleep(0.5)
      p.ChangeFrequency(440 * 1.25)
      time.sleep(0.5)
      p.ChangeFrequency(440 * 1.5)
      time.sleep(0.5)
      p.stop()  # TODO make sure it ends up off
    elif next_mode == SAD:
      print "Sad"
      # TODO use pigpio
      p = GPIO.PWM(self.output_pin, 440 * 1.2)
      if p is None:
        # FakeRPi.GPIO does this
        return
      p.start(50)
      time.sleep(0.5)
      p.ChangeFrequency(440 * 0.75)
      time.sleep(0.5)
      p.stop()  # TODO make sure it ends up off
    else:
      print "Error", next_mode
Exemplo n.º 3
0
 def run_inner(self):
   """Perform one on/off/blink pulse."""
   try:
     item = self.blink_command_queue.get(block=True, timeout=self.blink_duration)
     self.blinking = item[0]
     if self.blinking:
       # Always begin on; always turn off when disabling
       GPIO.output(self.output_pin, True)
     else:
       GPIO.output(self.output_pin, item[1])
   except Queue.Empty:
     if self.blinking:
       # When blinking, invert every timeout expiration (we might have woken
       # up for some other reason, but this appears to work in practice).
       GPIO.output(self.output_pin, not GPIO.input(self.output_pin))
Exemplo n.º 4
0
 def run_inner(self, block=True):
   item = self.set_queue.get(block=block)
   next_mode = item[0]
   if next_mode == OFF:
     GPIO.output(self.output_pin, False)
   elif next_mode == ON:
     GPIO.output(self.output_pin, True)
   elif next_mode in (BEEP, BEEPING):
     # As-is, this provides a logic HIGH to make a 4KHz sound on PK-12N40PQ;
     print("Beeping")
     while True:
       if not self.set_queue.empty():
         print("Done beeping")
         break
       GPIO.output(self.output_pin, True)
       time.sleep(item[1])
       GPIO.output(self.output_pin, False)
       time.sleep(item[2])
       if next_mode == BEEP:
         break
       print("...more beep")
   else:
     print("Error", next_mode)
  def run_inner(self, block=True):
    item = self.set_queue.get(block=block)
    next_mode = item[0]
    if next_mode == OFF:
      GPIO.output(self.output_pin, False)
    elif next_mode == ON:
      GPIO.output(self.output_pin, True)
    elif next_mode in (BEEP, BEEPING):
      # As-is, this provides a logic HIGH to make a 4KHz sound on PK-12N40PQ;
      print "Beeping"
      while True:
        if not self.set_queue.empty():
          print "Done beeping"
          break
        GPIO.output(self.output_pin, True)
        time.sleep(item[1])
        GPIO.output(self.output_pin, False)
        time.sleep(item[2])
        if next_mode == BEEP:
	  break
        print "...more beep"
    else:
      print "Error", next_mode
Exemplo n.º 6
0
  def __init__(self, event_queue, config_name, output_pin):
    super(Buzzer, self).__init__(event_queue, config_name, None, int(output_pin))
    self.set_queue = queue.Queue()

    GPIO.output(self.output_pin, False)
Exemplo n.º 7
0
 def off(self):
   GPIO.output(self.output_pin, not self.output_on_val)
Exemplo n.º 8
0
 def on(self):
   GPIO.output(self.output_pin, self.output_on_val)
  def __init__(self, event_queue, config_name, output_pin):
    super(Buzzer, self).__init__(event_queue, config_name, None, int(output_pin))
    self.set_queue = Queue.Queue(None)

    GPIO.output(self.output_pin, False)