1 # SPDX-FileCopyrightText: 2024 Vladimir Shtarev
3 # SPDX-License-Identifier: MIT
4 """Custom PWMOut Wrapper for VisionFive.GPIO PWM Class"""
6 import VisionFive.gpio as GPIO
8 GPIO.setmode(GPIO.BOARD)
9 GPIO.setwarnings(False)
12 # pylint: disable=unnecessary-pass
13 class PWMError(IOError):
14 """Base class for PWM errors."""
19 # pylint: enable=unnecessary-pass
22 def create(pin, *, frequency=500, duty_cycle=0, variable_frequency=False):
27 duty_cycle=duty_cycle,
28 variable_frequency=variable_frequency,
33 """Pulse Width Modulation Output Class"""
35 def __init__(self, pin, *, frequency=500, duty_cycle=0, variable_frequency=False):
38 self._open(pin, duty_cycle, frequency, variable_frequency)
46 def __exit__(self, t, value, traceback):
49 def _open(self, pin, duty=0, freq=500, variable_frequency=True):
51 GPIO.setup(pin.id, GPIO.OUT)
52 self._pwmpin = GPIO.PWM(pin.id, freq)
54 if variable_frequency:
55 print("Variable Frequency is not supported, continuing without it...")
60 self.duty_cycle = duty
66 if self._pwmpin is not None:
68 GPIO.cleanup(self._pin.id)
71 def _is_deinited(self):
72 if self._pwmpin is None:
74 "Object has been deinitialize and can no longer "
75 "be used. Create a new object."
80 """Get or set the PWM's output period in seconds.
83 PWMError: if an I/O or OS error occurs.
84 TypeError: if value type is not int or float.
88 return 1.0 / self.frequency
91 def period(self, period):
92 if not isinstance(period, (int, float)):
93 raise TypeError("Invalid period type, should be int or float.")
95 self.frequency = 1.0 / period
99 """Get or set the PWM's output duty cycle which is the fraction of
100 each pulse which is high. 16-bit
103 PWMError: if an I/O or OS error occurs.
104 TypeError: if value type is not int or float.
105 ValueError: if value is out of bounds of 0.0 to 1.0.
109 return int(self._duty_cycle * 65535)
112 def duty_cycle(self, duty_cycle):
113 if not isinstance(duty_cycle, (int, float)):
114 raise TypeError("Invalid duty cycle type, should be int or float.")
116 if not 0 <= duty_cycle <= 65535:
117 raise ValueError("Invalid duty cycle value, should be between 0 and 65535")
119 duty_cycle = duty_cycle / 655.35
120 self._duty_cycle = duty_cycle
121 self._pwmpin.ChangeDutyCycle(round(self._duty_cycle))
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%%)" % (