1 # SPDX-FileCopyrightText: 2024 Hajime Fujimoto
3 # SPDX-License-Identifier: MIT
4 """Custom PWMOut Wrapper for Hobot.GPIO PWM Class"""
5 import Hobot.GPIO as GPIO
7 GPIO.setmode(GPIO.BCM) # Use BCM pins D4 = GPIO #4
8 GPIO.setwarnings(False) # shh!
11 # pylint: disable=unnecessary-pass
12 class PWMError(IOError):
13 """Base class for PWM errors."""
18 # pylint: enable=unnecessary-pass
22 """Pulse Width Modulation Output Class"""
24 def __init__(self, pin, *, frequency=48000, duty_cycle=0, variable_frequency=False):
27 self._open(pin, duty_cycle, frequency, variable_frequency)
35 def __exit__(self, t, value, traceback):
38 def _open(self, pin, duty=0, freq=500, variable_frequency=False):
45 "PWM is only available on D12 or D13."
48 GPIO.setmode(GPIO.BCM)
49 # GPIO.setup(self._pin, GPIO.OUT)
50 self._pwmpin = GPIO.PWM(self._pin, freq)
52 if variable_frequency:
53 print("Variable Frequency is not supported, continuing without it...")
58 duty = 656 if duty <= 656 else duty
59 self.duty_cycle = duty
65 if self._pwmpin is not None:
67 GPIO.cleanup(self._pin)
70 def _is_deinited(self):
71 if self._pwmpin is None:
73 "Object has been deinitialize and can no longer "
74 "be used. Create a new object."
79 """Get or set the PWM's output period in seconds.
82 PWMError: if an I/O or OS error occurs.
83 TypeError: if value type is not int or float.
87 return 1.0 / self.frequency
90 def period(self, period):
91 if not isinstance(period, (int, float)):
92 raise TypeError("Invalid period type, should be int or float.")
94 self.frequency = 1.0 / period
98 """Get or set the PWM's output duty cycle which is the fraction of
99 each pulse which is high. 16-bit
102 PWMError: if an I/O or OS error occurs.
103 TypeError: if value type is not int or float.
104 ValueError: if value is out of bounds of 0.0 to 1.0.
108 return int(self._duty_cycle * 65535)
111 def duty_cycle(self, duty_cycle):
112 if not isinstance(duty_cycle, (int, float)):
113 raise TypeError("Invalid duty cycle type, should be int or float.")
115 if not 0 <= duty_cycle <= 65535:
116 raise ValueError("Invalid duty cycle value, should be between 0 and 65535")
118 # convert from 16-bit
119 duty_cycle /= 65535.0
121 self._duty_cycle = duty_cycle
122 self._pwmpin.ChangeDutyCycle(round(self._duty_cycle * 100))
126 """Get or set the PWM's output frequency in Hertz.
129 PWMError: if an I/O or OS error occurs.
130 TypeError: if value type is not int or float.
135 return self._frequency
138 def frequency(self, frequency):
139 if not isinstance(frequency, (int, float)):
140 raise TypeError("Invalid frequency type, should be int or float.")
142 self._pwmpin.ChangeFrequency(round(frequency))
143 self._frequency = frequency
147 """Get or set the PWM's output enabled state.
150 PWMError: if an I/O or OS error occurs.
151 TypeError: if value type is not bool.
158 def enabled(self, value):
159 if not isinstance(value, bool):
160 raise TypeError("Invalid enabled type, should be string.")
163 self._pwmpin.start(round(self._duty_cycle * 100))
167 self._enabled = value
169 # String representation
171 return "pin %s (freq=%f Hz, duty_cycle=%f%%)" % (