|
| 1 | +# SPDX-FileCopyrightText: 2024 Vladimir Shtarev, Jetbrains Research |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +"""Custom PWMOut Wrapper for VisionFive.GPIO PWM Class""" |
| 5 | + |
| 6 | +import VisionFive.gpio as GPIO |
| 7 | + |
| 8 | +GPIO.setmode(GPIO.BOARD) |
| 9 | +GPIO.setwarnings(False) |
| 10 | + |
| 11 | + |
| 12 | +# pylint: disable=unnecessary-pass |
| 13 | +class PWMError(IOError): |
| 14 | + """Base class for PWM errors.""" |
| 15 | + |
| 16 | + pass |
| 17 | + |
| 18 | + |
| 19 | +# pylint: enable=unnecessary-pass |
| 20 | + |
| 21 | + |
| 22 | +class PWMOut: |
| 23 | + """Pulse Width Modulation Output Class""" |
| 24 | + |
| 25 | + def __init__(self, pin, *, frequency=500, duty_cycle=0, variable_frequency=False): |
| 26 | + self._pwmpin = None |
| 27 | + self._period = 0 |
| 28 | + self._open(pin, duty_cycle, frequency, variable_frequency) |
| 29 | + |
| 30 | + def __del__(self): |
| 31 | + self.deinit() |
| 32 | + |
| 33 | + def __enter__(self): |
| 34 | + return self |
| 35 | + |
| 36 | + def __exit__(self, t, value, traceback): |
| 37 | + self.deinit() |
| 38 | + |
| 39 | + def _open(self, pin, duty=0, freq=500, variable_frequency=True): |
| 40 | + self._pin = pin |
| 41 | + GPIO.setup(pin.id, GPIO.OUT) |
| 42 | + self._pwmpin = GPIO.PWM(pin.id, freq) |
| 43 | + |
| 44 | + if variable_frequency: |
| 45 | + print("Variable Frequency is not supported, continuing without it...") |
| 46 | + |
| 47 | + # set frequency |
| 48 | + self.frequency = freq |
| 49 | + # set duty |
| 50 | + self.duty_cycle = duty |
| 51 | + |
| 52 | + self.enabled = True |
| 53 | + |
| 54 | + def deinit(self): |
| 55 | + """Deinit the PWM.""" |
| 56 | + if self._pwmpin is not None: |
| 57 | + self._pwmpin.stop() |
| 58 | + GPIO.cleanup(self._pin.id) |
| 59 | + self._pwmpin = None |
| 60 | + |
| 61 | + def _is_deinited(self): |
| 62 | + if self._pwmpin is None: |
| 63 | + raise ValueError( |
| 64 | + "Object has been deinitialize and can no longer " |
| 65 | + "be used. Create a new object." |
| 66 | + ) |
| 67 | + |
| 68 | + @property |
| 69 | + def period(self): |
| 70 | + """Get or set the PWM's output period in seconds. |
| 71 | +
|
| 72 | + Raises: |
| 73 | + PWMError: if an I/O or OS error occurs. |
| 74 | + TypeError: if value type is not int or float. |
| 75 | +
|
| 76 | + :type: int, float |
| 77 | + """ |
| 78 | + return 1.0 / self.frequency |
| 79 | + |
| 80 | + @period.setter |
| 81 | + def period(self, period): |
| 82 | + if not isinstance(period, (int, float)): |
| 83 | + raise TypeError("Invalid period type, should be int or float.") |
| 84 | + |
| 85 | + self.frequency = 1.0 / period |
| 86 | + |
| 87 | + @property |
| 88 | + def duty_cycle(self): |
| 89 | + """Get or set the PWM's output duty cycle which is the fraction of |
| 90 | + each pulse which is high. 16-bit |
| 91 | +
|
| 92 | + Raises: |
| 93 | + PWMError: if an I/O or OS error occurs. |
| 94 | + TypeError: if value type is not int or float. |
| 95 | + ValueError: if value is out of bounds of 0.0 to 1.0. |
| 96 | +
|
| 97 | + :type: int, float |
| 98 | + """ |
| 99 | + return int(self._duty_cycle * 65535) |
| 100 | + |
| 101 | + @duty_cycle.setter |
| 102 | + def duty_cycle(self, duty_cycle): |
| 103 | + if not isinstance(duty_cycle, (int, float)): |
| 104 | + raise TypeError("Invalid duty cycle type, should be int or float.") |
| 105 | + |
| 106 | + if not 0 <= duty_cycle <= 65535: |
| 107 | + raise ValueError("Invalid duty cycle value, should be between 0 and 65535") |
| 108 | + |
| 109 | + duty_cycle = duty_cycle / 655.35 |
| 110 | + self._duty_cycle = duty_cycle |
| 111 | + self._pwmpin.ChangeDutyCycle(round(self._duty_cycle)) |
| 112 | + |
| 113 | + @property |
| 114 | + def frequency(self): |
| 115 | + """Get or set the PWM's output frequency in Hertz. |
| 116 | +
|
| 117 | + Raises: |
| 118 | + PWMError: if an I/O or OS error occurs. |
| 119 | + TypeError: if value type is not int or float. |
| 120 | +
|
| 121 | + :type: int, float |
| 122 | + """ |
| 123 | + |
| 124 | + return self._frequency |
| 125 | + |
| 126 | + @frequency.setter |
| 127 | + def frequency(self, frequency): |
| 128 | + if not isinstance(frequency, (int, float)): |
| 129 | + raise TypeError("Invalid frequency type, should be int or float.") |
| 130 | + |
| 131 | + self._pwmpin.ChangeFrequency(round(frequency)) |
| 132 | + self._frequency = frequency |
| 133 | + |
| 134 | + @property |
| 135 | + def enabled(self): |
| 136 | + """Get or set the PWM's output enabled state. |
| 137 | +
|
| 138 | + Raises: |
| 139 | + PWMError: if an I/O or OS error occurs. |
| 140 | + TypeError: if value type is not bool. |
| 141 | +
|
| 142 | + :type: bool |
| 143 | + """ |
| 144 | + return self._enabled |
| 145 | + |
| 146 | + @enabled.setter |
| 147 | + def enabled(self, value): |
| 148 | + if not isinstance(value, bool): |
| 149 | + raise TypeError("Invalid enabled type, should be string.") |
| 150 | + |
| 151 | + if value: |
| 152 | + self._pwmpin.start(round(self._duty_cycle * 100)) |
| 153 | + else: |
| 154 | + self._pwmpin.stop() |
| 155 | + |
| 156 | + self._enabled = value |
| 157 | + |
| 158 | + # String representation |
| 159 | + def __str__(self): |
| 160 | + return "pin %s (freq=%f Hz, duty_cycle=%f%%)" % ( |
| 161 | + self._pin, |
| 162 | + self.frequency, |
| 163 | + self.duty_cycle, |
| 164 | + ) |
0 commit comments