mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
Always make PWMOutputDevice operate on floats.
And better 'encapsulation' of the active_high properties.
This commit is contained in:
@@ -376,9 +376,12 @@ class GPIODevice(Device):
|
|||||||
self._active_state = True
|
self._active_state = True
|
||||||
self._inactive_state = False
|
self._inactive_state = False
|
||||||
|
|
||||||
|
def _state_to_value(self, state):
|
||||||
|
return bool(state == self._active_state)
|
||||||
|
|
||||||
def _read(self):
|
def _read(self):
|
||||||
try:
|
try:
|
||||||
return self.pin.state == self._active_state
|
return self._state_to_value(self.pin.state)
|
||||||
except (AttributeError, TypeError):
|
except (AttributeError, TypeError):
|
||||||
self._check_open()
|
self._check_open()
|
||||||
raise
|
raise
|
||||||
|
|||||||
@@ -43,16 +43,15 @@ class OutputDevice(SourceMixin, GPIODevice):
|
|||||||
self.active_high = active_high
|
self.active_high = active_high
|
||||||
if initial_value is None:
|
if initial_value is None:
|
||||||
self.pin.function = 'output'
|
self.pin.function = 'output'
|
||||||
elif initial_value:
|
|
||||||
self.pin.output_with_state(self._active_state)
|
|
||||||
else:
|
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):
|
def _write(self, value):
|
||||||
if not self.active_high:
|
|
||||||
value = not value
|
|
||||||
try:
|
try:
|
||||||
self.pin.state = bool(value)
|
self.pin.state = self._value_to_state(value)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
self._check_open()
|
self._check_open()
|
||||||
raise
|
raise
|
||||||
@@ -318,23 +317,16 @@ class PWMOutputDevice(OutputDevice):
|
|||||||
pass
|
pass
|
||||||
super(PWMOutputDevice, self).close()
|
super(PWMOutputDevice, self).close()
|
||||||
|
|
||||||
def _read(self):
|
def _state_to_value(self, state):
|
||||||
self._check_open()
|
return float(state if self.active_high else 1 - state)
|
||||||
if self.active_high:
|
|
||||||
return self.pin.state
|
def _value_to_state(self, value):
|
||||||
else:
|
return float(value if self.active_high else 1 - value)
|
||||||
return 1 - self.pin.state
|
|
||||||
|
|
||||||
def _write(self, value):
|
def _write(self, value):
|
||||||
if not self.active_high:
|
|
||||||
value = 1 - value
|
|
||||||
if not 0 <= value <= 1:
|
if not 0 <= value <= 1:
|
||||||
raise OutputDeviceBadValue("PWM value must be between 0 and 1")
|
raise OutputDeviceBadValue("PWM value must be between 0 and 1")
|
||||||
try:
|
super(PWMOutputDevice, self)._write(value)
|
||||||
self.pin.state = value
|
|
||||||
except AttributeError:
|
|
||||||
self._check_open()
|
|
||||||
raise
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def value(self):
|
def value(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user