]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/generic_agnostic_board/PWMOut.py
fix freq
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / generic_agnostic_board / PWMOut.py
1 # SPDX-FileCopyrightText: 2024 Brent Rubell for Adafruit Industries
2 #
3 # SPDX-License-Identifier: MIT
4 """Mock PWMOut Wrapper for Generic Agnostic Board"""
5
6 class PWMError(IOError):
7     """Base class for PWM errors."""
8
9
10 class PWMOut:
11     """Pulse Width Modulation Output Class"""
12
13     def __init__(self, pin, *, frequency=500, duty_cycle=0, variable_frequency=False):
14         self._pwmpin = None
15         self._period = 0
16         self._open(pin, duty_cycle, frequency, variable_frequency)
17
18     def __del__(self):
19         self.deinit()
20
21     def __enter__(self):
22         return self
23
24     def __exit__(self, t, value, traceback):
25         self.deinit()
26
27     def _open(self, pin, duty=0, freq=500, variable_frequency=False):
28         self._pin = pin
29
30         # set frequency
31         self.frequency = freq
32         self._variable_frequency = variable_frequency
33         # set duty
34         self.duty_cycle = duty
35
36         self.enabled = True
37
38     def deinit(self):
39         """Deinit the PWM."""
40         if self._pwmpin is not None:
41             self._pwmpin = None
42
43     def _is_deinited(self):
44         if self._pwmpin is None:
45             raise ValueError(
46                 "Object has been deinitialize and can no longer "
47                 "be used. Create a new object."
48             )
49
50     @property
51     def period(self):
52         """Get or set the PWM's output period in seconds.
53
54         Raises:
55             PWMError: if an I/O or OS error occurs.
56             TypeError: if value type is not int or float.
57
58         :type: int, float
59         """
60         return 1.0 / self.frequency
61
62     @period.setter
63     def period(self, period):
64         if not isinstance(period, (int, float)):
65             raise TypeError("Invalid period type, should be int or float.")
66
67         self.frequency = 1.0 / period
68
69     @property
70     def duty_cycle(self):
71         """Get or set the PWM's output duty cycle which is the fraction of
72         each pulse which is high. 16-bit
73
74         Raises:
75             PWMError: if an I/O or OS error occurs.
76             TypeError: if value type is not int or float.
77             ValueError: if value is out of bounds of 0.0 to 1.0.
78
79         :type: int, float
80         """
81         return int(self._duty_cycle * 65535)
82
83     @duty_cycle.setter
84     def duty_cycle(self, duty_cycle):
85         if not isinstance(duty_cycle, (int, float)):
86             raise TypeError("Invalid duty cycle type, should be int or float.")
87
88         if not 0 <= duty_cycle <= 65535:
89             raise ValueError("Invalid duty cycle value, should be between 0 and 65535")
90
91         # convert from 16-bit
92         duty_cycle /= 65535.0
93
94         self._duty_cycle = duty_cycle
95
96     @property
97     def frequency(self):
98         """Get or set the PWM's output frequency in Hertz.
99
100         Raises:
101             PWMError: if an I/O or OS error occurs.
102             TypeError: if value type is not int or float.
103
104         :type: int, float
105         """
106
107         return self._frequency
108
109     @frequency.setter
110     def frequency(self, frequency):
111         if not isinstance(frequency, (int, float)):
112             raise TypeError("Invalid frequency type, should be int or float.")
113         self._frequency = frequency
114
115     @property
116     def enabled(self):
117         """Get or set the PWM's output enabled state.
118
119         Raises:
120             PWMError: if an I/O or OS error occurs.
121             TypeError: if value type is not bool.
122
123         :type: bool
124         """
125         return self._enabled
126
127     @enabled.setter
128     def enabled(self, value):
129         if not isinstance(value, bool):
130             raise TypeError("Invalid enabled type, should be string.")
131
132         self._enabled = value
133
134     # String representation
135     def __str__(self):
136         return "pin %s (freq=%f Hz, duty_cycle=%f%%)" % (
137             self._pin,
138             self.frequency,
139             self.duty_cycle,
140         )