]> Repositories - Adafruit_Blinka-hackapet.git/blobdiff - src/adafruit_blinka/microcontroller/nxp_lpc4330/pwmout.py
Merge pull request #688 from caternuson/add_kb2040
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / nxp_lpc4330 / pwmout.py
index 08a31b772f99af2e5322e812ee05526fa4a64e9a..cab12589c92d420f26e35b7fb76da51016886840 100644 (file)
@@ -1,19 +1,19 @@
+# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
+#
+# SPDX-License-Identifier: MIT
 """PWMOut Class for NXP LPC4330"""
 
 from greatfet import GreatFET
-from greatfet.interfaces.pattern_generator import PatternGenerator
 
 try:
     from microcontroller.pin import pwmOuts
 except ImportError:
-    raise RuntimeError("No PWM outputs defined for this board")
-
-from microcontroller.pin import Pin
-
+    raise RuntimeError("No PWM outputs defined for this board") from ImportError
 
 # pylint: disable=unnecessary-pass
 class PWMError(IOError):
     """Base class for PWM errors."""
+
     pass
 
 
@@ -45,11 +45,13 @@ class PWMOut:
         self._gf = GreatFET()
 
         if variable_frequency:
-            raise NotImplemented("Variable Frequency is not currently supported.")
+            raise NotImplementedError("Variable Frequency is not currently supported.")
 
         self._pattern = None
         self._channel = None
         self._enable = False
+        self._frequency = 500
+        self._duty_cycle = 0
         self._open(pin, duty_cycle, frequency)
 
     def __enter__(self):
@@ -125,7 +127,6 @@ class PWMOut:
 
     period = property(_get_period, _set_period)
 
-
     def _get_duty_cycle(self):
         """Get or set the PWM's output duty cycle as a ratio from 0.0 to 1.0.
 
@@ -146,12 +147,14 @@ class PWMOut:
         if isinstance(duty_cycle, int):
             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.")
-       
+            raise ValueError("Invalid duty cycle value, should be between 0.0 and 1.0.")
+
         # Generate a pattern for 1024 samples of the duty cycle
         pattern = [(1 << self._channel)] * round(PWMOut.MAX_CYCLE_LEVEL * duty_cycle)
-        pattern += [(0 << self._channel)] * round(PWMOut.MAX_CYCLE_LEVEL * (1.0 - duty_cycle))
-        
+        pattern += [(0 << self._channel)] * round(
+            PWMOut.MAX_CYCLE_LEVEL * (1.0 - duty_cycle)
+        )
+
         self._pattern = pattern
         self._duty_cycle = duty_cycle
         if self._enable:
@@ -160,7 +163,7 @@ class PWMOut:
     duty_cycle = property(_get_duty_cycle, _set_duty_cycle)
 
     def _get_frequency(self):
-        return int(PWMOut._nova.getIOpinPWMFreq(self._pwmpin).split("PWMFREQ ")[1])
+        return self._frequency
 
     def _set_frequency(self, frequency):
         """Get or set the PWM's output frequency in Hertz.
@@ -176,6 +179,7 @@ class PWMOut:
 
         # We are sending 1024 samples per second already
         self._gf.pattern_generator.set_sample_rate(frequency * len(self._pattern))
+        self._frequency = frequency
 
     frequency = property(_get_frequency, _set_frequency)