]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/rockchip/PWMOut.py
e152377d76c542aa11487c260634ceca995e9c56
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / rockchip / PWMOut.py
1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4 """
5 Much code from https://github.com/vsergeev/python-periphery/blob/master/periphery/pwm.py
6 Copyright (c) 2015-2016 vsergeev / Ivan (Vanya) A. Sergeev
7 License: MIT
8 """
9
10 import os
11 from time import sleep
12 from errno import EACCES
13
14 try:
15     from microcontroller.pin import pwmOuts
16 except ImportError:
17     raise RuntimeError("No PWM outputs defined for this board.") from ImportError
18
19
20 # pylint: disable=unnecessary-pass, too-many-instance-attributes
21
22
23 class PWMError(IOError):
24     """Base class for PWM errors."""
25
26     pass
27
28
29 # pylint: enable=unnecessary-pass
30
31
32 class PWMOut:
33     """Pulse Width Modulation Output Class"""
34
35     # Number of retries to check for successful PWM export on open
36     PWM_STAT_RETRIES = 10
37     # Delay between check for successful PWM export on open (100ms)
38     PWM_STAT_DELAY = 0.1
39
40     # Sysfs paths
41     _chip_path = "pwmchip{}"
42     _channel_path = "pwm{}"
43
44     def __init__(self, pwm, *, frequency=500, duty_cycle=0, variable_frequency=False):
45         """Instantiate a PWM object and open the sysfs PWM corresponding to the
46         specified chip and channel.
47         Args:
48             pwm (str): PWM pin.
49             frequency (int, float): target frequency in Hertz (32-bit).
50             duty_cycle (int, float): The fraction of each pulse which is high (16-bit).
51             variable_frequency (bool): True if the frequency will change over time.
52         Returns:
53             PWM: PWM object.
54         Raises:
55             PWMError: if an I/O or OS error occurs.
56             TypeError: if `chip` or `channel` types are invalid.
57             LookupError: if PWM chip does not exist.
58             TimeoutError: if waiting for PWM export times out.
59         """
60
61         self._chip = None
62         self._channel = None
63         self._period_ns = 0
64         self._open(pwm, frequency, duty_cycle, variable_frequency)
65
66     def __del__(self):
67         self.close()
68
69     def __enter__(self):
70         return self
71
72     def __exit__(self, exc_type, exc_val, exc_tb):
73         self.close()
74
75     def _open(self, pwm, frequency, duty_cycle, variable_frequency):
76         for pwmout in pwmOuts:
77             if pwmout[1] == pwm:
78                 self._chip = pwmout[0][0]
79                 self._channel = pwmout[0][1]
80
81
82         self._chip_path = os.path.join(
83             "/sys/class/pwm", self._chip_path.format(self._chip)
84         )
85         self._channel_path = os.path.join(
86             self._chip_path, self._channel_path.format(self._channel)
87         )
88
89         if variable_frequency:
90             print("Variable Frequency is not supported, continuing without it...")
91
92         if not os.path.isdir(self._chip_path):
93             raise LookupError("Opening PWM: PWM chip {} not found.".format(self._chip))
94
95         if not os.path.isdir(self._channel_path):
96             # Exporting the PWM.
97             try:
98                 with open(
99                     os.path.join(self._chip_path, "export"), "w", encoding="utf-8"
100                 ) as f_export:
101                     f_export.write("{:d}\n".format(self._channel))
102             except IOError as e:
103                 raise PWMError(
104                     e.errno, "Exporting PWM channel: " + e.strerror
105                 ) from IOError
106
107             # Loop until PWM is exported
108             exported = False
109             for i in range(PWMOut.PWM_STAT_RETRIES):
110                 if os.path.isdir(self._channel_path):
111                     exported = True
112                     break
113
114                 sleep(PWMOut.PWM_STAT_DELAY)
115
116             if not exported:
117                 raise TimeoutError(
118                     'Exporting PWM: waiting for "{:s}" timed out.'.format(
119                         self._channel_path
120                     )
121                 )
122
123             # Loop until 'period' is writable, This could take some time after
124             # export as application of the udev rules after export is asynchronous.
125             # Without this loop, the following properties may not be writable yet.
126             for i in range(PWMOut.PWM_STAT_RETRIES):
127                 try:
128                     with open(
129                         os.path.join(self._channel_path, "period"),
130                         "w",
131                         encoding="utf-8",
132                     ):
133                         break
134                 except IOError as e:
135                     if e.errno != EACCES or (
136                         e.errno == EACCES and i == PWMOut.PWM_STAT_RETRIES - 1
137                     ):
138                         raise PWMError(
139                             e.errno, "Opening PWM period: " + e.strerror
140                         ) from IOError
141
142                 sleep(PWMOut.PWM_STAT_DELAY)
143
144             self.frequency = frequency
145             self.duty_cycle = duty_cycle
146             self.polarity = "normal"
147             self.enable()
148
149             # Cache the period for fast duty cycle updates
150             self._period_ns = self._get_period_ns()
151
152     def close(self):
153         """Close the PWM."""
154         if self._channel is not None:
155             # Unexporting the PWM channel
156             try:
157                 unexport_fd = os.open(
158                     os.path.join(self._chip_path, "unexport"), os.O_WRONLY
159                 )
160                 os.write(unexport_fd, "{:d}\n".format(self._channel).encode())
161                 os.close(unexport_fd)
162             except OSError as e:
163                 raise PWMError(e.errno, "Unexporting PWM: " + e.strerror) from OSError
164
165         self._chip = None
166         self._channel = None
167
168     def _write_channel_attr(self, attr, value):
169         with open(
170             os.path.join(self._channel_path, attr), "w", encoding="utf-8"
171         ) as f_attr:
172             f_attr.write(value + "\n")
173
174     def _read_channel_attr(self, attr):
175         with open(
176             os.path.join(self._channel_path, attr), "r", encoding="utf-8"
177         ) as f_attr:
178             return f_attr.read().strip()
179
180     # Methods
181
182     def enable(self):
183         """Enable the PWM output."""
184         self.enabled = True
185
186     def disable(self):
187         """Disable the PWM output."""
188         self.enabled = False
189
190     # Mutable properties
191
192     def _get_period(self):
193         return float(self.period_ms) / 1000
194
195     def _set_period(self, period):
196         if not isinstance(period, (int, float)):
197             raise TypeError("Invalid period type, should be int.")
198
199         self.period_ms = int(period * 1000)
200
201     period = property(_get_period, _set_period)
202     """Get or set the PWM's output period in seconds.
203
204     Raises:
205         PWMError: if an I/O or OS error occurs.
206         TypeError: if value type is not int.
207
208     :type: int, float
209     """
210
211     def _get_period_ms(self):
212         return self.period_us / 1000
213
214     def _set_period_ms(self, period_ms):
215         if not isinstance(period_ms, (int, float)):
216             raise TypeError("Invalid period type, should be int or float.")
217         self.period_us = int(period_ms * 1000)
218
219     period_ms = property(_get_period_ms, _set_period_ms)
220     """Get or set the PWM's output period in milliseconds.
221
222     Raises:
223         PWMError: if an I/O or OS error occurs.
224         TypeError: if value type is not int.
225
226     :type: int, float
227     """
228
229     def _get_period_us(self):
230         return self.period_ns / 1000
231
232     def _set_period_us(self, period_us):
233         if not isinstance(period_us, int):
234             raise TypeError("Invalid period type, should be int.")
235
236         self.period_ns = int(period_us * 1000)
237
238     period_us = property(_get_period_us, _set_period_us)
239     """Get or set the PWM's output period in microseconds.
240
241     Raises:
242         PWMError: if an I/O or OS error occurs.
243         TypeError: if value type is not int.
244
245     :type: int
246     """
247
248     def _get_period_ns(self):
249         period_ns = self._read_channel_attr("period")
250         try:
251             period_ns = int(period_ns)
252         except ValueError:
253             raise PWMError(
254                 None, 'Unknown period value: "%s".' % period_ns
255             ) from ValueError
256
257         self._period_ns = period_ns
258
259         return period_ns
260
261     def _set_period_ns(self, period_ns):
262         if not isinstance(period_ns, int):
263             raise TypeError("Invalid period type, should be int.")
264
265         self._write_channel_attr("period", str(period_ns))
266
267         # Update our cached period
268         self._period_ns = period_ns
269
270     period_ns = property(_get_period_ns, _set_period_ns)
271     """Get or set the PWM's output period in nanoseconds.
272
273     Raises:
274         PWMError: if an I/O or OS error occurs.
275         TypeError: if value type is not int.
276
277     :type: int
278     """
279
280     def _get_duty_cycle_ns(self):
281         duty_cycle_ns_str = self._read_channel_attr("duty_cycle")
282
283         try:
284             duty_cycle_ns = int(duty_cycle_ns_str)
285         except ValueError:
286             raise PWMError(
287                 None, 'Unknown duty cycle value: "{:s}"'.format(duty_cycle_ns_str)
288             ) from ValueError
289
290         return duty_cycle_ns
291
292     def _set_duty_cycle_ns(self, duty_cycle_ns):
293         if not isinstance(duty_cycle_ns, int):
294             raise TypeError("Invalid duty cycle type, should be int.")
295
296         self._write_channel_attr("duty_cycle", str(duty_cycle_ns))
297
298     duty_cycle_ns = property(_get_duty_cycle_ns, _set_duty_cycle_ns)
299     """Get or set the PWM's output duty cycle in nanoseconds.
300
301     Raises:
302         PWMError: if an I/O or OS error occurs.
303         TypeError: if value type is not int.
304
305     :type: int
306     """
307
308     def _get_duty_cycle(self):
309         return float(self.duty_cycle_ns) / self._period_ns
310
311     def _set_duty_cycle(self, duty_cycle):
312         if not isinstance(duty_cycle, (int, float)):
313             raise TypeError("Invalid duty cycle type, should be int or float.")
314
315         if not 0.0 <= duty_cycle <= 1.0:
316             raise ValueError("Invalid duty cycle value, should be between 0.0 and 1.0.")
317
318         # Convert duty cycle from ratio to nanoseconds
319         self.duty_cycle_ns = int(duty_cycle * self._period_ns)
320
321     duty_cycle = property(_get_duty_cycle, _set_duty_cycle)
322     """Get or set the PWM's output duty cycle as a ratio from 0.0 to 1.0.
323     Raises:
324         PWMError: if an I/O or OS error occurs.
325         TypeError: if value type is not int or float.
326         ValueError: if value is out of bounds of 0.0 to 1.0.
327     :type: int, float
328     """
329
330     def _get_frequency(self):
331         return 1.0 / self.period
332
333     def _set_frequency(self, frequency):
334         if not isinstance(frequency, (int, float)):
335             raise TypeError("Invalid frequency type, should be int or float.")
336
337         self.period = 1.0 / frequency
338
339     frequency = property(_get_frequency, _set_frequency)
340     """Get or set the PWM's output frequency in Hertz.
341     Raises:
342         PWMError: if an I/O or OS error occurs.
343         TypeError: if value type is not int or float.
344     :type: int, float
345     """
346
347     def _get_polarity(self):
348         return self._read_channel_attr("polarity")
349
350     def _set_polarity(self, polarity):
351         if not isinstance(polarity, str):
352             raise TypeError("Invalid polarity type, should be str.")
353
354         if polarity.lower() not in ["normal", "inversed"]:
355             raise ValueError('Invalid polarity, can be: "normal" or "inversed".')
356
357         self._write_channel_attr("polarity", polarity.lower())
358
359     polarity = property(_get_polarity, _set_polarity)
360     """Get or set the PWM's output polarity. Can be "normal" or "inversed".
361     Raises:
362         PWMError: if an I/O or OS error occurs.
363         TypeError: if value type is not str.
364         ValueError: if value is invalid.
365     :type: str
366     """
367
368     def _get_enabled(self):
369         enabled = self._read_channel_attr("enable")
370
371         if enabled == "1":
372             return True
373         if enabled == "0":
374             return False
375
376         raise PWMError(None, 'Unknown enabled value: "{:s}"'.format(enabled))
377
378     def _set_enabled(self, value):
379         if not isinstance(value, bool):
380             raise TypeError("Invalid enabled type, should be bool.")
381
382         self._write_channel_attr("enable", "1" if value else "0")
383
384     enabled = property(_get_enabled, _set_enabled)
385     """Get or set the PWM's output enabled state.
386     Raises:
387         PWMError: if an I/O or OS error occurs.
388         TypeError: if value type is not bool.
389     :type: bool
390     """
391
392     # String representation
393
394     def __str__(self):
395         return (
396             "PWM {:d}, chip {:d} (period={:f} sec, duty_cycle={:f}%,"
397             " polarity={:s}, enabled={:s})".format(
398                 self._channel,
399                 self._chip,
400                 self.period,
401                 self.duty_cycle * 100,
402                 self.polarity,
403                 str(self.enabled),
404             )
405         )