def add_event(self, port_number, on_rising_callback=None, on_falling_callback=None): """Adds listening event on given port. In this case 2nd argument passed to a callback is a value read during callback invocation, which in theory might not be the one that actually cause triggering the event. """ if Settings.IS_NO_HARDWARE_MODE: logging.warning('No hardware mode, adding read event failed.') else: port_listener = self._port_listeners.get(port_number) if not port_listener: port_listener = _PiPortListener(self.get_port(port_number)) gpio = get_gpio() gpio.add_event_detect(port_number, gpio.BOTH, callback=port_listener.trigger_callbacks, bouncetime=Settings.READ_SWITCH_DEBOUNCE) self._port_listeners[port_number] = port_listener if on_rising_callback: logging.debug( 'Adding rising callback for interface (%s) on port %d', self, port_number) port_listener.add_rising_callback(on_rising_callback) if on_falling_callback: logging.debug( 'Adding falling callback for interface (%s) on port %d', self, port_number) port_listener.add_falling_callback(on_falling_callback)
def _gpio_output(self, port_number, value): self._validate_port_number(port_number) if Settings.IS_NO_HARDWARE_MODE: logging.warning('No hardware mode, no value written') else: gpio = get_gpio() gpio.output(port_number, gpio.HIGH if value == self.HIGH else gpio.LOW)
def get_value(self, port_number): self._validate_port_number(port_number) value = self._check_no_hardware_port_value(port_number) if value is not None: return value else: gpio = get_gpio() value = gpio.input(port_number) # logging.debug( # 'Read gpio port value (%s): %s', # self.get_port(port_number), # value) return self.HIGH if value == gpio.HIGH else self.LOW
def _gpio_setup(self, port_number, gpio_attr_name): self._validate_port_number(port_number) if Settings.IS_NO_HARDWARE_MODE: logging.warning('No hardware mode, no value written') else: gpio = get_gpio() if gpio_attr_name == 'IN': # Special case for settings port as input. # Pullup or pulldown rezistor should be set here. kwargs = {} if self.pull_up_down_rezistor == self.PULL_UP: kwargs['pull_up_down'] = gpio.PUD_UP elif self.pull_up_down_rezistor == self.PULL_DOWN: kwargs['pull_up_down'] = gpio.PUD_DOWN gpio.setup(port_number, getattr(gpio, gpio_attr_name), **kwargs) else: gpio.setup(port_number, getattr(gpio, gpio_attr_name))
def clear_read_events(self, port_number): if not Settings.IS_NO_HARDWARE_MODE: get_gpio().remove_event_detect(port_number) if port_number in self._port_listeners: del self._port_listeners[port_number]