Skip to content

Commit 1709cdd

Browse files
committed
WIP split ESP32SPI into more, smaller modules
1 parent 6fff5a3 commit 1709cdd

File tree

14 files changed

+1391
-1311
lines changed

14 files changed

+1391
-1311
lines changed

adafruit_esp32spi/PWMOut.py

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,29 @@
1010
* Author(s): Brent Rubell
1111
"""
1212

13+
from . import pin
1314

1415
class PWMOut:
1516
"""
1617
Implementation of CircuitPython PWMOut for ESP32SPI.
1718
18-
:param int esp_pin: Valid ESP32 GPIO Pin, predefined in ESP32_GPIO_PINS.
1919
:param ESP_SPIcontrol esp: The ESP object we are using.
20+
:param int esp_pin: Valid ESP32 GPIO Pin.
2021
:param int duty_cycle: The fraction of each pulse which is high, 16-bit.
21-
:param int frequency: The target frequency in Hertz (32-bit).
22-
:param bool variable_frequency: True if the frequency will change over time.
22+
:param int frequency: Not supported.
23+
:param bool variable_frequency: Not supported.
2324
"""
2425

25-
ESP32_PWM_PINS = set(
26-
[0, 1, 2, 4, 5, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 25, 26, 27, 32, 33]
27-
)
28-
2926
def __init__(
30-
self, esp, pwm_pin, *, frequency=500, duty_cycle=0, variable_frequency=False
27+
self, esp, pin, *, frequency=None, duty_cycle=0, variable_frequency=None
3128
):
32-
if pwm_pin in self.ESP32_PWM_PINS:
33-
self._pwm_pin = pwm_pin
34-
else:
35-
raise AttributeError("Pin %d is not a valid ESP32 GPIO Pin." % pwm_pin)
29+
if not isinstance(pin, pin.Pin)
30+
raise AttributeError("Pin is not a valid ESP32 GPIO Pin.")
31+
self._pin = pin
3632
self._esp = esp
3733
self._duty_cycle = duty_cycle
38-
self._freq = frequency
39-
self._var_freq = variable_frequency
34+
if frequency or variable_frequency is not None:
35+
raise NotImplementedError("PWMOut frequency not implemented in ESP32SPI")
4036

4137
def __enter__(self):
4238
return self
@@ -47,50 +43,46 @@ def __exit__(self, exc_type, exc_value, exc_traceback):
4743
def deinit(self):
4844
"""De-initalize the PWMOut object."""
4945
self._duty_cycle = 0
50-
self._freq = 0
51-
self._pwm_pin = None
46+
self._pin = None
5247

5348
def _is_deinited(self):
5449
"""Checks if PWMOut object has been previously de-initalized"""
55-
if self._pwm_pin is None:
50+
if self._pin is None:
5651
raise ValueError(
5752
"PWMOut Object has been deinitialized and can no longer "
5853
"be used. Create a new PWMOut object."
5954
)
6055

6156
@property
6257
def duty_cycle(self):
63-
"""Returns the PWMOut object's duty cycle as a
64-
ratio from 0.0 to 1.0."""
58+
"""The PWMOut duty cycle as a 16-bit value from 0 to 65535."""
6559
self._is_deinited()
6660
return self._duty_cycle
6761

6862
@duty_cycle.setter
6963
def duty_cycle(self, duty_cycle):
70-
"""Sets the PWMOut duty cycle.
71-
:param float duty_cycle: Between 0.0 (low) and 1.0 (high).
72-
:param int duty_cycle: Between 0 (low) and 1 (high).
73-
"""
7464
self._is_deinited()
75-
if not isinstance(duty_cycle, (int, float)):
76-
raise TypeError("Invalid duty_cycle, should be int or float.")
77-
duty_cycle /= 65535.0
78-
if not 0.0 <= duty_cycle <= 1.0:
79-
raise ValueError("Invalid duty_cycle, should be between 0.0 and 1.0")
80-
self._esp.set_analog_write(self._pwm_pin, duty_cycle)
65+
if not isinstance(duty_cycle, int):
66+
raise TypeError("Invalid duty_cycle, should be an int.")
67+
if not 0 <= duty_cycle <= 65535:
68+
raise ValueError("Invalid duty_cycle, should be between 0 and 65535")
69+
resp = self._send_command_get_response(
70+
_SET_ANALOG_WRITE_CMD, ((self._pin.number,), (duty_cycle // 256,))
71+
)
72+
if resp[0][0] != 1:
73+
raise RuntimeError("Failed to write to pin")
74+
self._duty_cycle = duty_cycle
8175

8276
@property
8377
def frequency(self):
8478
"""Returns the PWMOut object's frequency value."""
8579
self._is_deinited()
86-
return self._freq
80+
return None
8781

8882
@frequency.setter
8983
def frequency(self, freq):
9084
"""Sets the PWMOut object's frequency value.
9185
:param int freq: 32-bit value that dictates the PWM frequency in Hertz.
9286
NOTE: Only writeable when constructed with variable_Frequency=True.
9387
"""
94-
self._is_deinited()
95-
self._freq = freq
9688
raise NotImplementedError("PWMOut Frequency not implemented in ESP32SPI")

0 commit comments

Comments
 (0)