@@ -21,6 +21,13 @@ PWMController::PWMController() {
2121
2222PWMController::~PWMController () { delete _pwm_model; }
2323
24+ /* *************************************************************************/
25+ /* !
26+ @brief Handles the PWM_Add message.
27+ @param stream The stream containing the message data.
28+ @return True if the message was handled successfully, false otherwise.
29+ */
30+ /* *************************************************************************/
2431bool PWMController::Handle_PWM_Add (pb_istream_t *stream) {
2532 bool did_attach;
2633 if (!_pwm_model->DecodePWMAdd (stream)) {
@@ -109,7 +116,31 @@ bool PWMController::Handle_PWM_Write_DutyCycle_Multi(pb_istream_t *stream) {
109116}
110117
111118bool PWMController::Handle_PWM_Write_Frequency (pb_istream_t *stream) {
112- return false ;
119+ if (!_pwm_model->DecodePWMWriteFrequency (stream)) {
120+ WS_DEBUG_PRINTLN (
121+ " [pwm] Error: Failed to decode PWMWriteFrequency message!" );
122+ return false ;
123+ }
124+ wippersnapper_pwm_PWMWriteFrequency msg_write_frequency =
125+ *_pwm_model->GetPWMWriteFrequencyMsg ();
126+ uint8_t pin = atoi (msg_write_frequency.pin + 1 );
127+ // Check if the pin is already attached
128+ int pin_idx = GetPWMHardwareIdx (pin);
129+ if (pin_idx == -1 ) {
130+ WS_DEBUG_PRINTLN (" [pwm] Error: pin not found!" );
131+ return false ;
132+ }
133+
134+ if (!_pwm_hardware[pin_idx]->WriteTone (msg_write_frequency.frequency ) ==
135+ msg_write_frequency.frequency ) {
136+ WS_DEBUG_PRINTLN (" [pwm] Error: Failed to write frequency!" );
137+ return false ;
138+ }
139+ WS_DEBUG_PRINTLN (" [pwm] Wrote frequency: " );
140+ WS_DEBUG_PRINT (msg_write_frequency.frequency );
141+ WS_DEBUG_PRINTLN (" to pin: " );
142+ WS_DEBUG_PRINT (msg_write_frequency.pin );
143+ return true ;
113144}
114145
115146bool PWMController::Handle_PWM_Remove (pb_istream_t *stream) { return false ; }
0 commit comments