28
28
"""
29
29
30
30
class PWMOut ():
31
- ESP32_PWM_PINS = set ([0 , 1 , 2 , 4 , 5 ,
32
- 12 , 13 , 14 , 15 ,
33
- 16 , 17 , 18 , 19 ,
34
- 21 , 22 , 23 , 25 ,
35
- 26 , 27 , 32 , 33 ])
36
31
"""
37
32
Implementation of CircuitPython PWMOut for ESP32SPI.
38
33
@@ -42,19 +37,24 @@ class PWMOut():
42
37
:param int frequency: The target frequency in Hertz (32-bit).
43
38
:param bool variable_frequency: True if the frequency will change over time.
44
39
"""
40
+ ESP32_PWM_PINS = set ([0 , 1 , 2 , 4 , 5 ,
41
+ 12 , 13 , 14 , 15 ,
42
+ 16 , 17 , 18 , 19 ,
43
+ 21 , 22 , 23 , 25 ,
44
+ 26 , 27 , 32 , 33 ])
45
45
def __init__ (self , esp , pwm_pin , * , frequency = 500 , duty_cycle = 0 , variable_frequency = False ):
46
46
if pwm_pin in self .ESP32_PWM_PINS :
47
47
self ._pwm_pin = pwm_pin
48
48
else :
49
- raise AttributeError ("Pin %d is not a valid ESP32 GPIO Pin." % esp_pin )
49
+ raise AttributeError ("Pin %d is not a valid ESP32 GPIO Pin." % pwm_pin )
50
50
self ._esp = esp
51
51
self ._duty_cycle = duty_cycle
52
52
self ._freq = frequency
53
53
self ._var_freq = variable_frequency
54
54
55
55
def __enter__ (self ):
56
56
return self
57
-
57
+
58
58
def __exit__ (self , exc_type , exc_value , exc_traceback ):
59
59
self .deinit ()
60
60
@@ -63,7 +63,7 @@ def deinit(self):
63
63
self ._duty_cycle = 0
64
64
self ._freq = 0
65
65
self ._pwm_pin = None
66
-
66
+
67
67
def _is_deinited (self ):
68
68
if self ._pwm_pin is None :
69
69
raise ValueError ("PWMOut Object has been deinitialized and can no longer "
@@ -75,7 +75,7 @@ def duty_cycle(self):
75
75
ratio from 0.0 to 1.0."""
76
76
self ._is_deinited ()
77
77
return self ._duty_cycle
78
-
78
+
79
79
@duty_cycle .setter
80
80
def duty_cycle (self , duty_cycle ):
81
81
"""Sets the PWMOut duty cycle.
@@ -85,7 +85,7 @@ def duty_cycle(self, duty_cycle):
85
85
self ._is_deinited ()
86
86
if not isinstance (duty_cycle , (int , float )):
87
87
raise TypeError ("Invalid duty_cycle, should be int or float." )
88
-
88
+
89
89
duty_cycle /= 65535.0
90
90
if not 0.0 <= duty_cycle <= 1.0 :
91
91
raise ValueError ("Invalid duty_cycle, should be between 0.0 and 1.0" )
@@ -96,7 +96,7 @@ def frequency(self):
96
96
"""Returns the PWMOut object's frequency value."""
97
97
self ._is_deinited ()
98
98
raise NotImplementedError ("PWMOut Frequency not implemented in ESP32SPI" )
99
-
99
+
100
100
@frequency .setter
101
101
def frequency (self , freq ):
102
102
"""Sets the PWMOut object's frequency value.
@@ -105,5 +105,3 @@ def frequency(self, freq):
105
105
"""
106
106
self ._is_deinited ()
107
107
raise NotImplementedError ("PWMOut Frequency not implemented in ESP32SPI" )
108
-
109
-
0 commit comments