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))
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))