1 # SPDX-FileCopyrightText: 2024 Hajime Fujimoto
 
   3 # SPDX-License-Identifier: MIT
 
   4 """Custom PWMOut Wrapper for Hobot.GPIO PWM Class"""
 
   7 GPIO.setmode(GPIO.BCM)  # Use BCM pins D4 = GPIO #4
 
   8 GPIO.setwarnings(False)  # shh!
 
  12 # pylint: disable=unnecessary-pass
 
  13 class PWMError(IOError):
 
  14     """Base class for PWM errors."""
 
  19 # pylint: enable=unnecessary-pass
 
  23     """Pulse Width Modulation Output Class"""
 
  25     def __init__(self, pin, *, frequency=48000, duty_cycle=0, variable_frequency=False):
 
  28         self._open(pin, duty_cycle, frequency, variable_frequency)
 
  36     def __exit__(self, t, value, traceback):
 
  39     def _open(self, pin, duty=0, freq=500, variable_frequency=False):
 
  45             raise ValueError("PWM is only available on D12 or D13.")
 
  47         GPIO.setmode(GPIO.BCM)
 
  48         #        GPIO.setup(self._pin, GPIO.OUT)
 
  49         self._pwmpin = GPIO.PWM(self._pin, freq)
 
  51         if variable_frequency:
 
  52             print("Variable Frequency is not supported, continuing without it...")
 
  57         duty = 656 if duty <= 656 else duty
 
  58         self.duty_cycle = duty
 
  64         if self._pwmpin is not None:
 
  66             GPIO.cleanup(self._pin)
 
  69     def _is_deinited(self):
 
  70         if self._pwmpin is None:
 
  72                 "Object has been deinitialize and can no longer "
 
  73                 "be used. Create a new object."
 
  78         """Get or set the PWM's output period in seconds.
 
  81             PWMError: if an I/O or OS error occurs.
 
  82             TypeError: if value type is not int or float.
 
  86         return 1.0 / self.frequency
 
  89     def period(self, period):
 
  90         if not isinstance(period, (int, float)):
 
  91             raise TypeError("Invalid period type, should be int or float.")
 
  93         self.frequency = 1.0 / period
 
  97         """Get or set the PWM's output duty cycle which is the fraction of
 
  98         each pulse which is high. 16-bit
 
 101             PWMError: if an I/O or OS error occurs.
 
 102             TypeError: if value type is not int or float.
 
 103             ValueError: if value is out of bounds of 0.0 to 1.0.
 
 107         return int(self._duty_cycle * 65535)
 
 110     def duty_cycle(self, duty_cycle):
 
 111         if not isinstance(duty_cycle, (int, float)):
 
 112             raise TypeError("Invalid duty cycle type, should be int or float.")
 
 114         if not 0 <= duty_cycle <= 65535:
 
 115             raise ValueError("Invalid duty cycle value, should be between 0 and 65535")
 
 117         # convert from 16-bit
 
 118         duty_cycle /= 65535.0
 
 120         self._duty_cycle = duty_cycle
 
 121         self._pwmpin.ChangeDutyCycle(round(self._duty_cycle * 100))
 
 125         """Get or set the PWM's output frequency in Hertz.
 
 128             PWMError: if an I/O or OS error occurs.
 
 129             TypeError: if value type is not int or float.
 
 134         return self._frequency
 
 137     def frequency(self, frequency):
 
 138         if not isinstance(frequency, (int, float)):
 
 139             raise TypeError("Invalid frequency type, should be int or float.")
 
 141         self._pwmpin.ChangeFrequency(round(frequency))
 
 142         self._frequency = frequency
 
 146         """Get or set the PWM's output enabled state.
 
 149             PWMError: if an I/O or OS error occurs.
 
 150             TypeError: if value type is not bool.
 
 157     def enabled(self, value):
 
 158         if not isinstance(value, bool):
 
 159             raise TypeError("Invalid enabled type, should be string.")
 
 162             self._pwmpin.start(round(self._duty_cycle * 100))
 
 166         self._enabled = value
 
 168     # String representation
 
 170         return "pin %s (freq=%f Hz, duty_cycle=%f%%)" % (