From c26c27917451b62251b9508d8ebd8b0eda5520d6 Mon Sep 17 00:00:00 2001 From: Ben Nuttall Date: Mon, 5 Oct 2015 12:37:43 +0100 Subject: [PATCH] Change PWM interface from 0-100 to 0-1, close #45 --- gpiozero/output_devices.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/gpiozero/output_devices.py b/gpiozero/output_devices.py index 1a3213c..8a6c2f8 100644 --- a/gpiozero/output_devices.py +++ b/gpiozero/output_devices.py @@ -161,19 +161,21 @@ class PWMOutputDevice(DigitalOutputDevice): self._frequency = 100 self._pwm = GPIO.PWM(self._pin, self._frequency) self._pwm.start(0) + self._min_pwm = 0 + self._max_pwm = 1 self.value = 0 def on(self): """ Turn the device on """ - self.value = 100 + self.value = self._max_pwm def off(self): """ Turn the device off """ - self.value = 0 + self.value = self._min_pwm def toggle(self): """ @@ -181,7 +183,9 @@ class PWMOutputDevice(DigitalOutputDevice): If it's on (a value greater than 0), turn it off; if it's off, turn it on. """ - self.value = 100 if self.value == 0 else 0 + _min = self._min_pwm + _max = self._max_pwm + self.value = _max if self.value == _min else _min @property def value(self): @@ -189,6 +193,14 @@ class PWMOutputDevice(DigitalOutputDevice): @value.setter def value(self, n): + _min = self._min_pwm + _max = self._max_pwm + if _min >= n >= _max: + n *= 100 + else: + raise GPIODeviceError( + "Value must be between %s and %s" % (_min, _max) + ) self._pwm.ChangeDutyCycle(n) self._value = n