]> Repositories - Adafruit_Blinka-hackapet.git/blobdiff - src/adafruit_blinka/microcontroller/bcm283x/pulseio/PWMOut.py
Merge pull request #464 from twa127/master
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / bcm283x / pulseio / PWMOut.py
index 77824b0748048194814a7dc7816bdc908197aca7..65f269f5b0463755e937e031e4771d4e8f50e9d0 100644 (file)
@@ -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,
         )