X-Git-Url: https://git.ayoreis.com/Adafruit_Blinka-hackapet.git/blobdiff_plain/98f4a4378478d11e514acd7ff22a9c674f3cbca3..HEAD:/src/adafruit_blinka/microcontroller/nxp_lpc4330/pwmout.py diff --git a/src/adafruit_blinka/microcontroller/nxp_lpc4330/pwmout.py b/src/adafruit_blinka/microcontroller/nxp_lpc4330/pwmout.py index 08a31b7..cc67184 100644 --- a/src/adafruit_blinka/microcontroller/nxp_lpc4330/pwmout.py +++ b/src/adafruit_blinka/microcontroller/nxp_lpc4330/pwmout.py @@ -1,19 +1,20 @@ +# 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 +46,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 +128,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 +148,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 +164,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 +180,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)