14
14
*/
15
15
#include " hardware.h"
16
16
17
+ /* *************************************************************************/
18
+ /* !
19
+ @brief Ctor for PWMHardware
20
+ */
21
+ /* *************************************************************************/
17
22
PWMHardware::PWMHardware () {
18
23
_is_attached = false ;
19
24
}
20
25
26
+ /* *************************************************************************/
27
+ /* !
28
+ @brief Dtor for PWMHardware
29
+ */
30
+ /* *************************************************************************/
21
31
PWMHardware::~PWMHardware () {
22
32
if (_is_attached)
23
33
DetachPin ();
24
34
}
25
35
36
+ /* *************************************************************************/
37
+ /* !
38
+ @brief Attach a pin to the PWM hardware
39
+ @param pin The pin to attach
40
+ @param frequency The frequency of the PWM signal
41
+ @param resolution The resolution of the PWM signal
42
+ @return true if the pin was successfully attached, false otherwise
43
+ */
44
+ /* *************************************************************************/
26
45
bool PWMHardware::AttachPin (uint8_t pin, uint32_t frequency, uint32_t resolution) {
27
46
#ifdef ARDUINO_ARCH_ESP32
28
47
_is_attached = ledcAttach (_pin, _frequency, _resolution);
@@ -40,6 +59,12 @@ if (_is_attached) {
40
59
return _is_attached;
41
60
}
42
61
62
+ /* *************************************************************************/
63
+ /* !
64
+ @brief Detaches a PWM pin and frees it for use.
65
+ @return true if the PWM pin was successfully detached, false otherwise.
66
+ */
67
+ /* *************************************************************************/
43
68
bool PWMHardware::DetachPin () {
44
69
if (! _is_attached) {
45
70
WS_DEBUG_PRINTLN (" [pwm] Pin not attached!" );
@@ -56,6 +81,14 @@ bool PWMHardware::DetachPin() {
56
81
digitalWrite (_pin, LOW);
57
82
}
58
83
84
+ /* *************************************************************************/
85
+ /* !
86
+ @brief Writes a duty cycle to a PWM pin with a fixed frequency
87
+ of 5kHz and 8-bit resolution.
88
+ @param duty The desired duty cycle to write to the pin.
89
+ @return true if the duty cycle was successfully written, false otherwise
90
+ */
91
+ /* *************************************************************************/
59
92
bool PWMHardware::WriteDutyCycle (uint32_t duty) {
60
93
if (! _is_attached) {
61
94
WS_DEBUG_PRINTLN (" [pwm] Pin not attached!" );
@@ -80,8 +113,60 @@ bool PWMHardware::WriteDutyCycle(uint32_t duty) {
80
113
return true ;
81
114
}
82
115
116
+ /* *************************************************************************/
117
+ /* !
118
+ @brief Writes a frequency to a PWM pin with a fixed duty cycle.
119
+ @param freq The desired frequency to write to the pin.
120
+ @return true if the tone was successfully written, false otherwise
121
+ */
122
+ /* *************************************************************************/
123
+ uint32_t PWMHardware::WriteTone (uint32_t freq) {
124
+ if (! _is_attached) {
125
+ WS_DEBUG_PRINTLN (" [pwm] Pin not attached!" );
126
+ return false ;
127
+ }
128
+
129
+ uint32_t rc = 0 ;
130
+ #ifdef ARDUINO_ARCH_ESP32
131
+ rc = ledcWriteTone (_pin, freq);
132
+ #else
133
+ tone (_pin, freq);
134
+ rc = freq;
135
+ #endif
136
+
137
+ return rc;
138
+ }
139
+
140
+ /* *************************************************************************/
141
+ /* !
142
+ @brief Stops a "tone" output on a PWM pin from WriteTone(). Must be
143
+ called to stop the tone before detaching the pin.
144
+ @return true if the tone was successfully stopped, false otherwise
145
+ */
146
+ /* *************************************************************************/
147
+ void PWMHardware::WriteNoTone () {
148
+ if (! _is_attached) {
149
+ WS_DEBUG_PRINTLN (" [pwm] Pin not attached!" );
150
+ return ; // back out
151
+ }
152
+
153
+ #ifdef ARDUINO_ARCH_ESP32
154
+ ledcWriteTone (_pin, 0 );
155
+ #else
156
+ noTone (_pin);
157
+ #endif
158
+ }
159
+
83
160
// LEDC API Wrappers
84
161
#ifdef ARDUINO_ARCH_ESP32
162
+ /* *************************************************************************/
163
+ /* !
164
+ @brief Mocks the Arduino analogWrite() function for the Arduino-ESP32
165
+ LEDC API
166
+ @param value The value to write (0-255)
167
+ @return true if the value was successfully written, false otherwise
168
+ */
169
+ /* *************************************************************************/
85
170
bool PWMHardware::analogWrite (uint32_t value) {
86
171
// clamp
87
172
if (value > 255 || value < 0 ) {
0 commit comments