1414 */
1515#include " hardware.h"
1616
17+ /* *************************************************************************/
18+ /* !
19+ @brief Ctor for PWMHardware
20+ */
21+ /* *************************************************************************/
1722PWMHardware::PWMHardware () {
1823_is_attached = false ;
1924}
2025
26+ /* *************************************************************************/
27+ /* !
28+ @brief Dtor for PWMHardware
29+ */
30+ /* *************************************************************************/
2131PWMHardware::~PWMHardware () {
2232 if (_is_attached)
2333 DetachPin ();
2434}
2535
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+ /* *************************************************************************/
2645bool PWMHardware::AttachPin (uint8_t pin, uint32_t frequency, uint32_t resolution) {
2746#ifdef ARDUINO_ARCH_ESP32
2847 _is_attached = ledcAttach (_pin, _frequency, _resolution);
@@ -40,6 +59,12 @@ if (_is_attached) {
4059return _is_attached;
4160}
4261
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+ /* *************************************************************************/
4368bool PWMHardware::DetachPin () {
4469 if (! _is_attached) {
4570 WS_DEBUG_PRINTLN (" [pwm] Pin not attached!" );
@@ -56,6 +81,14 @@ bool PWMHardware::DetachPin() {
5681 digitalWrite (_pin, LOW);
5782}
5883
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+ /* *************************************************************************/
5992bool PWMHardware::WriteDutyCycle (uint32_t duty) {
6093 if (! _is_attached) {
6194 WS_DEBUG_PRINTLN (" [pwm] Pin not attached!" );
@@ -80,8 +113,60 @@ bool PWMHardware::WriteDutyCycle(uint32_t duty) {
80113 return true ;
81114}
82115
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+
83160// LEDC API Wrappers
84161#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+ /* *************************************************************************/
85170bool PWMHardware::analogWrite (uint32_t value) {
86171 // clamp
87172 if (value > 255 || value < 0 ) {
0 commit comments