X-Git-Url: https://git.ayoreis.com/Adafruit_Blinka-hackapet.git/blobdiff_plain/65ce2c9ee4aa05604fc48ff0a86618df720a28a4..0ab99d44db2e62b79e57ebb751459ac4b371daab:/src/adafruit_blinka/microcontroller/nova/pwmout.py diff --git a/src/adafruit_blinka/microcontroller/nova/pwmout.py b/src/adafruit_blinka/microcontroller/nova/pwmout.py index 57aa0e5..00beba7 100644 --- a/src/adafruit_blinka/microcontroller/nova/pwmout.py +++ b/src/adafruit_blinka/microcontroller/nova/pwmout.py @@ -1,5 +1,4 @@ -import os -import digitalio +"""PWMOut Class for Binho Nova""" try: from microcontroller.pin import pwmOuts @@ -8,12 +7,20 @@ except ImportError: from microcontroller.pin import Pin + +# pylint: disable=unnecessary-pass class PWMError(IOError): """Base class for PWM errors.""" + pass -class PWMOut(object): +# pylint: enable=unnecessary-pass + + +class PWMOut: + """Pulse Width Modulation Output Class""" + # Nova instance _nova = None MAX_CYCLE_LEVEL = 1024 @@ -38,22 +45,17 @@ class PWMOut(object): """ if PWMOut._nova is None: - if Pin._nova is not None: - # check if Pin already connected to Binho Nova - PWMOut._nova = Pin._nova - else: - from binhoHostAdapter import binhoHostAdapter - from binhoHostAdapter import binhoUtilities + # pylint: disable=import-outside-toplevel + from adafruit_blinka.microcontroller.nova import Connection - utilities = binhoUtilities.binhoUtilities() - devices = utilities.listAvailableDevices() + # pylint: enable=import-outside-toplevel - if len(devices) > 0: - PWMOut._nova = binhoHostAdapter.binhoHostAdapter(devices[0]) - else: - raise RuntimeError('No Binho Host Adapter found!') + PWMOut._nova = Connection.getInstance() + PWMOut._nova.setOperationMode(0, "IO") self._pwmpin = None + self._channel = None + self._enable = False self._open(pin, duty_cycle, frequency, variable_frequency) def __del__(self): @@ -76,11 +78,13 @@ class PWMOut(object): if self._channel is None: raise RuntimeError("No PWM channel found for this Pin") + if variable_frequency: + print("Variable Frequency is not supported, continuing without it...") + PWMOut._nova.setIOpinMode(self._pwmpin, Pin.PWM) # set frequency self.frequency = freq - PWMOut._nova.setIOpinPWMFreq(self._pwmpin, self.frequency) # set period self._period = self._get_period() @@ -90,32 +94,33 @@ class PWMOut(object): self._set_enabled(True) def deinit(self): - try: """Deinit the Nova PWM.""" - if self._channel is not None: - #self.duty_cycle = 0 - self._set_enabled(False) # make to disable before unexport - """ - try: - #unexport_path = os.path.join(channel_path, self._unexport_path) - with open(os.path.join(channel_path, self._unexport_path), "w") as f_unexport: - f_unexport.write("%d\n" % self._pwmpin) - except IOError as e: - raise PWMError(e.errno, "Unexporting PWM pin: " + e.strerror) - """ - except Exception as e: - # due to a race condition for which I have not yet been - # able to find the root cause, deinit() often fails - # but it does not effect future usage of the pwm pin - print("warning: failed to deinitialize pwm pin {0}:{1} due to: {2}\n".format(self._channel, self._pwmpin, type(e).__name__)) - finally: - self._channel = None - self._pwmpin = None + # pylint: disable=broad-except + try: + if self._channel is not None: + # self.duty_cycle = 0 + self._set_enabled(False) # make to disable before unexport + + except Exception as e: + # due to a race condition for which I have not yet been + # able to find the root cause, deinit() often fails + # but it does not effect future usage of the pwm pin + print( + "warning: failed to deinitialize pwm pin {0}:{1} due to: {2}\n".format( + self._channel, self._pwmpin, type(e).__name__ + ) + ) + finally: + self._channel = None + self._pwmpin = None + # pylint: enable=broad-except def _is_deinited(self): if self._pwmpin is None: - raise ValueError("Object has been deinitialize and can no longer " - "be used. Create a new object.") + raise ValueError( + "Object has been deinitialize and can no longer " + "be used. Create a new object." + ) # Mutable properties @@ -162,7 +167,9 @@ class PWMOut(object): duty_cycle = duty_cycle * PWMOut.MAX_CYCLE_LEVEL # Set duty cycle + # pylint: disable=protected-access Pin._nova.setIOpinValue(self._pwmpin, duty_cycle) + # pylint: enable=protected-access duty_cycle = property(_get_duty_cycle, _set_duty_cycle) """Get or set the PWM's output duty cycle as a ratio from 0.0 to 1.0. @@ -176,7 +183,7 @@ class PWMOut(object): """ def _get_frequency(self): - return int(PWMOut._nova.getIOpinPWMFreq(self._pwmpin).split('PWMFREQ ')[1]) + return int(PWMOut._nova.getIOpinPWMFreq(self._pwmpin).split("PWMFREQ ")[1]) def _set_frequency(self, frequency): if not isinstance(frequency, (int, float)): @@ -199,28 +206,32 @@ class PWMOut(object): if enabled == "1": return True - elif enabled == "0": + if enabled == "0": return False - raise PWMError(None, "Unknown enabled value: \"%s\"" % enabled) + raise PWMError(None, 'Unknown enabled value: "%s"' % enabled) def _set_enabled(self, value): + """Get or set the PWM's output enabled state. + + Raises: + PWMError: if an I/O or OS error occurs. + TypeError: if value type is not bool. + + :type: bool + """ if not isinstance(value, bool): raise TypeError("Invalid enabled type, should be string.") self._enable = value if not self._enable: self._set_duty_cycle(0.0) - """Get or set the PWM's output enabled state. - - Raises: - PWMError: if an I/O or OS error occurs. - TypeError: if value type is not bool. - - :type: bool - """ # String representation def __str__(self): - return "PWM%d, pin %s (freq=%f Hz, duty_cycle=%f%%)" % \ - (self._pin, self._pin, self.frequency, self.duty_cycle * 100,) + return "PWM%d, pin %s (freq=%f Hz, duty_cycle=%f%%)" % ( + self._pin, + self._pin, + self.frequency, + self.duty_cycle * 100, + )