@@ -56,8 +56,49 @@ bool PWMController::Handle_PWM_Add(pb_istream_t *stream) {
5656 return true ;
5757}
5858
59+ /* *************************************************************************/
60+ /* !
61+ @brief Returns the index of the PWM hardware object that corresponds
62+ to the given pin.
63+ @param pin The pin number to search for.
64+ @return The index of the PWM hardware object, or -1 if not found.
65+ */
66+ /* *************************************************************************/
67+ int PWMController::GetPWMHardwareIdx (uint8_t pin) {
68+ for (int i = 0 ; i < _active_pwm_pins; i++) {
69+ if (_pwm_hardware[i]->GetPin () == pin) {
70+ return i;
71+ }
72+ }
73+ return -1 ;
74+ }
75+
5976bool PWMController::Handle_PWM_Write_DutyCycle (pb_istream_t *stream) {
60- return false ;
77+ if (!_pwm_model->DecodePWMWriteDutyCycle (stream)) {
78+ WS_DEBUG_PRINTLN (" [pwm] Error: Failed to decode PWMWriteDutyCycle message!" );
79+ return false ;
80+ }
81+
82+ wippersnapper_pwm_PWMWriteDutyCycle msg_write_duty_cycle = *_pwm_model->GetPWMWriteDutyCycleMsg ();
83+ uint8_t pin = atoi (msg_write_duty_cycle.pin + 1 );
84+
85+ // Check if the pin is already attached
86+ int pin_idx = GetPWMHardwareIdx (pin);
87+ if (pin_idx == -1 ) {
88+ WS_DEBUG_PRINTLN (" [pwm] Error: pin not found!" );
89+ return false ;
90+ }
91+
92+ // Write the duty cycle to the pin
93+ if (! _pwm_hardware[pin_idx]->WriteDutyCycle (msg_write_duty_cycle.duty_cycle )) {
94+ WS_DEBUG_PRINTLN (" [pwm] Error: Failed to write duty cycle!" );
95+ return false ;
96+ }
97+ WS_DEBUG_PRINTLN (" [pwm] Wrote duty cycle: " );
98+ WS_DEBUG_PRINT (msg_write_duty_cycle.duty_cycle );
99+ WS_DEBUG_PRINTLN (" to pin: " );
100+ WS_DEBUG_PRINT (msg_write_duty_cycle.pin );
101+ return true ;
61102}
62103
63104bool PWMController::Handle_PWM_Write_DutyCycle_Multi (pb_istream_t *stream) {
0 commit comments