1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
 
   3 # SPDX-License-Identifier: MIT
 
   4 # SPDX-FileCopyrightText: 2022 Martin Schnur for Siemens AG
 
   6 # SPDX-License-Identifier: MIT
 
   8 # pylint: disable=pointless-string-statement
 
   9 # pylint: disable=ungrouped-imports,wrong-import-position,unused-import
 
  10 # pylint: disable=import-outside-toplevel
 
  12 """Custom PWMOut Wrapper for am65xx"""
 
  14 Much code from https://github.com/vsergeev/python-periphery/blob/master/periphery/pwm.py
 
  15 Copyright (c) 2015-2016 vsergeev / Ivan (Vanya) A. Sergeev
 
  21 from adafruit_blinka.microcontroller.am65xx.pin import Pin
 
  23 # pylint: disable=unnecessary-pass
 
  26 class PWMError(IOError):
 
  27     """Base class for PWM errors."""
 
  32 # pylint: enable=unnecessary-pass
 
  36     """Pulse Width Modulation Output Class"""
 
  38     def __init__(self, pin, *, frequency=500, duty_cycle=0, variable_frequency=False):
 
  39         self._frequency = None
 
  40         self._duty_cycle = None
 
  44         self._varfreq = variable_frequency
 
  45         # check pin for PWM support
 
  46         self._pin = Pin(pin.id)
 
  47         self._pin.init(mode=Pin.PWM)
 
  49         self._open(pin, duty_cycle, frequency, variable_frequency)
 
  57     def __exit__(self, t, value, traceback):
 
  60     def _open(self, pin, duty=0, freq=500, variable_frequency=False):
 
  61         self._pwmpin = mraa.Pwm(pin.id)
 
  64         self._varfreq = variable_frequency
 
  65         self.duty_cycle = duty
 
  69         if self._pwmpin is not None:
 
  70             self._pwmpin.enable(False)
 
  73     def _is_deinited(self):
 
  74         if self._pwmpin is None:
 
  76                 "Object has been deinitialize and can no longer "
 
  77                 "be used. Create a new object."
 
  82         """Get or set the PWM's output period in seconds.
 
  85             PWMError: if an I/O or OS error occurs.
 
  86             TypeError: if value type is not int or float.
 
  90         return 1.0 / self.frequency
 
  93     def period(self, period):
 
  94         if not isinstance(period, (int, float)):
 
  95             raise TypeError("Invalid period type, should be int or float.")
 
  97         self.frequency = 1.0 / period
 
 100     def duty_cycle(self):
 
 101         """Get or set the PWM's output duty cycle which is the fraction of
 
 102         each pulse which is high. 16-bit
 
 105             PWMError: if an I/O or OS error occurs.
 
 106             TypeError: if value type is not int or float.
 
 107             ValueError: if value is out of bounds of 0.0 to 1.0.
 
 111         return int(self._duty_cycle * 65535)
 
 114     def duty_cycle(self, duty_cycle):
 
 115         if not isinstance(duty_cycle, (int, float)):
 
 116             raise TypeError("Invalid duty cycle type, should be int or float.")
 
 118         if not 0 <= duty_cycle <= 65535:
 
 119             raise ValueError("Invalid duty cycle value, should be between 0 and 65535")
 
 121         # convert from 16-bit
 
 122         duty_cycle /= 65535.0
 
 124         self._duty_cycle = duty_cycle
 
 125         # self._pwmpin.ChangeDutyCycle(round(self._duty_cycle * 100))
 
 126         self._pwmpin.write(self._duty_cycle)  # mraa duty_cycle 0.0f - 1.0f
 
 130         """Get or set the PWM's output frequency in Hertz.
 
 133             PWMError: if an I/O or OS error occurs.
 
 134             TypeError: if value type is not int or float.
 
 139         return self._frequency
 
 142     def frequency(self, frequency):
 
 143         if not isinstance(frequency, (int, float)):
 
 144             raise TypeError("Invalid frequency type, should be int or float.")
 
 146         if self._enabled and not self._varfreq:
 
 148                 " Set variable_frequency = True to allow changing frequency "
 
 150         # mraa has different variants in seconds,milli(_ms),micro(_us)
 
 151         self._pwmpin.period((1 / frequency))
 
 152         self._frequency = frequency
 
 156         """Get or set the PWM's output enabled state.
 
 159             PWMError: if an I/O or OS error occurs.
 
 160             TypeError: if value type is not bool.
 
 167     def enabled(self, value):
 
 168         if not isinstance(value, bool):
 
 169             raise TypeError("Invalid enabled type, should be string.")
 
 172             self._pwmpin.enable(True)
 
 173             self._enabled = value
 
 175             self._pwmpin.enable(False)
 
 178     # String representation
 
 180         return "pin %s (freq=%f Hz, duty_cycle=%f%%)" % (