+# 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
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):
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.
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:
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.
# 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)