Skip to content

Commit 261df33

Browse files
authored
Merge pull request #850 from brentru/add-pwmout-generic-board
Add PWMOut Support for Generic Agnostic Board
2 parents c88ce53 + 1e5b11b commit 261df33

File tree

2 files changed

+143
-0
lines changed

2 files changed

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

src/pwmio.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
from adafruit_blinka.microcontroller.tegra.PWMOut import PWMOut
4747
elif detector.board.any_luckfox_pico_board:
4848
from adafruit_blinka.microcontroller.generic_linux.sysfs_pwmout import PWMOut
49+
elif detector.board.OS_AGNOSTIC_BOARD:
50+
from adafruit_blinka.microcontroller.generic_agnostic_board.PWMOut import PWMOut
4951
elif (
5052
detector.board.feather_u2if
5153
or detector.board.feather_can_u2if

0 commit comments

Comments
 (0)