X-Git-Url: https://git.ayoreis.com/Adafruit_Blinka-hackapet.git/blobdiff_plain/71bf7896ed9137c7d36ba5dd306760c488c3a012..373c160b4a588ff883285ba10dba94e918a2abbb:/src/adafruit_blinka/microcontroller/bcm283x/pulseio/PWMOut.py diff --git a/src/adafruit_blinka/microcontroller/bcm283x/pulseio/PWMOut.py b/src/adafruit_blinka/microcontroller/bcm283x/pulseio/PWMOut.py index 77824b0..65f269f 100644 --- a/src/adafruit_blinka/microcontroller/bcm283x/pulseio/PWMOut.py +++ b/src/adafruit_blinka/microcontroller/bcm283x/pulseio/PWMOut.py @@ -49,9 +49,10 @@ class PWMOut: def deinit(self): """Deinit the PWM.""" - self._pwmpin.stop() - GPIO.cleanup(self._pin.id) - self._pwmpin = None + if self._pwmpin is not None: + self._pwmpin.stop() + GPIO.cleanup(self._pin.id) + self._pwmpin = None def _is_deinited(self): if self._pwmpin is None: @@ -98,13 +99,14 @@ class PWMOut: if not isinstance(duty_cycle, (int, float)): raise TypeError("Invalid duty cycle type, should be int or float.") + if not 0 <= duty_cycle <= 65535: + raise ValueError("Invalid duty cycle value, should be between 0 and 65535") + # convert from 16-bit duty_cycle /= 65535.0 - if not 0.0 <= duty_cycle <= 1.0: - raise ValueError("Invalid duty cycle value, should be between 0.0 and 1.0.") - self._pwmpin.ChangeDutyCycle(round(duty_cycle * 100)) self._duty_cycle = duty_cycle + self._pwmpin.ChangeDutyCycle(round(self._duty_cycle * 100)) @property def frequency(self): @@ -145,7 +147,7 @@ class PWMOut: raise TypeError("Invalid enabled type, should be string.") if value: - self._pwmpin.start(self.duty_cycle) + self._pwmpin.start(round(self._duty_cycle * 100)) else: self._pwmpin.stop() @@ -156,5 +158,5 @@ class PWMOut: return "pin %s (freq=%f Hz, duty_cycle=%f%%)" % ( self._pin, self.frequency, - self.duty_cycle * 100, + self.duty_cycle, )