diff --git a/gpiozero/devices.py b/gpiozero/devices.py index 97c4040..ba2ecc4 100644 --- a/gpiozero/devices.py +++ b/gpiozero/devices.py @@ -376,9 +376,12 @@ class GPIODevice(Device): self._active_state = True self._inactive_state = False + def _state_to_value(self, state): + return bool(state == self._active_state) + def _read(self): try: - return self.pin.state == self._active_state + return self._state_to_value(self.pin.state) except (AttributeError, TypeError): self._check_open() raise diff --git a/gpiozero/output_devices.py b/gpiozero/output_devices.py index ecb2a5b..c0dd6e2 100644 --- a/gpiozero/output_devices.py +++ b/gpiozero/output_devices.py @@ -43,16 +43,15 @@ class OutputDevice(SourceMixin, GPIODevice): self.active_high = active_high if initial_value is None: self.pin.function = 'output' - elif initial_value: - self.pin.output_with_state(self._active_state) else: - self.pin.output_with_state(self._inactive_state) + self.pin.output_with_state(self._value_to_state(initial_value)) + + def _value_to_state(self, value): + return bool(self._active_state if value else self._inactive_state) def _write(self, value): - if not self.active_high: - value = not value try: - self.pin.state = bool(value) + self.pin.state = self._value_to_state(value) except AttributeError: self._check_open() raise @@ -318,23 +317,16 @@ class PWMOutputDevice(OutputDevice): pass super(PWMOutputDevice, self).close() - def _read(self): - self._check_open() - if self.active_high: - return self.pin.state - else: - return 1 - self.pin.state + def _state_to_value(self, state): + return float(state if self.active_high else 1 - state) + + def _value_to_state(self, value): + return float(value if self.active_high else 1 - value) def _write(self, value): - if not self.active_high: - value = 1 - value if not 0 <= value <= 1: raise OutputDeviceBadValue("PWM value must be between 0 and 1") - try: - self.pin.state = value - except AttributeError: - self._check_open() - raise + super(PWMOutputDevice, self)._write(value) @property def value(self):