]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/generic_agnostic_board/PWMOut.py
9f6e1624a79c10844a832c033f6a1cc82e8f8852
[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         self._pwmpin = GPIO.PWM(pin.id, freq)
30
31         if variable_frequency:
32             print("Variable Frequency is not supported, continuing without it...")
33
34         # set frequency
35         self.frequency = freq
36         # set duty
37         self.duty_cycle = duty
38
39         self.enabled = True
40
41     def deinit(self):
42         """Deinit the PWM."""
43         if self._pwmpin is not None:
44             self._pwmpin = None
45
46     def _is_deinited(self):
47         if self._pwmpin is None:
48             raise ValueError(
49                 "Object has been deinitialize and can no longer "
50                 "be used. Create a new object."
51             )
52
53     @property
54     def period(self):
55         """Get or set the PWM's output period in seconds.
56
57         Raises:
58             PWMError: if an I/O or OS error occurs.
59             TypeError: if value type is not int or float.
60
61         :type: int, float
62         """
63         return 1.0 / self.frequency
64
65     @period.setter
66     def period(self, period):
67         if not isinstance(period, (int, float)):
68             raise TypeError("Invalid period type, should be int or float.")
69
70         self.frequency = 1.0 / period
71
72     @property
73     def duty_cycle(self):
74         """Get or set the PWM's output duty cycle which is the fraction of
75         each pulse which is high. 16-bit
76
77         Raises:
78             PWMError: if an I/O or OS error occurs.
79             TypeError: if value type is not int or float.
80             ValueError: if value is out of bounds of 0.0 to 1.0.
81
82         :type: int, float
83         """
84         return int(self._duty_cycle * 65535)
85
86     @duty_cycle.setter
87     def duty_cycle(self, duty_cycle):
88         if not isinstance(duty_cycle, (int, float)):
89             raise TypeError("Invalid duty cycle type, should be int or float.")
90
91         if not 0 <= duty_cycle <= 65535:
92             raise ValueError("Invalid duty cycle value, should be between 0 and 65535")
93
94         # convert from 16-bit
95         duty_cycle /= 65535.0
96
97         self._duty_cycle = duty_cycle
98
99     @property
100     def frequency(self):
101         """Get or set the PWM's output frequency in Hertz.
102
103         Raises:
104             PWMError: if an I/O or OS error occurs.
105             TypeError: if value type is not int or float.
106
107         :type: int, float
108         """
109
110         return self._frequency
111
112     @frequency.setter
113     def frequency(self, frequency):
114         if not isinstance(frequency, (int, float)):
115             raise TypeError("Invalid frequency type, should be int or float.")
116
117         self._frequency = frequency
118
119     @property
120     def enabled(self):
121         """Get or set the PWM's output enabled state.
122
123         Raises:
124             PWMError: if an I/O or OS error occurs.
125             TypeError: if value type is not bool.
126
127         :type: bool
128         """
129         return self._enabled
130
131     @enabled.setter
132     def enabled(self, value):
133         if not isinstance(value, bool):
134             raise TypeError("Invalid enabled type, should be string.")
135
136         self._enabled = value
137
138     # String representation
139     def __str__(self):
140         return "pin %s (freq=%f Hz, duty_cycle=%f%%)" % (
141             self._pin,
142             self.frequency,
143             self.duty_cycle,
144         )