1414 */
1515#include " controller.h"
1616
17+ /* *************************************************************************/
18+ /* !
19+ @brief Ctor for PWMController.
20+ */
21+ /* *************************************************************************/
1722PWMController::PWMController () {
1823 _pwm_model = new PWMModel ();
1924 _active_pwm_pins = 0 ;
2025}
2126
27+ /* *************************************************************************/
28+ /* !
29+ @brief Dtor for PWMController.
30+ */
31+ /* *************************************************************************/
2232PWMController::~PWMController () { delete _pwm_model; }
2333
2434/* *************************************************************************/
@@ -63,6 +73,40 @@ bool PWMController::Handle_PWM_Add(pb_istream_t *stream) {
6373 return true ;
6474}
6575
76+ /* *************************************************************************/
77+ /* !
78+ @brief Handles the PWM_Remove message.
79+ @param stream The stream containing the message data.
80+ @return True if the message was handled successfully, false otherwise.
81+ */
82+ /* *************************************************************************/
83+ bool PWMController::Handle_PWM_Remove (pb_istream_t *stream) {
84+ if (!_pwm_model->DecodePWMRemove (stream)) {
85+ WS_DEBUG_PRINTLN (" [pwm] Error: Failed to decode PWMRemove message!" );
86+ return false ;
87+ }
88+ wippersnapper_pwm_PWMRemove msg_remove = *_pwm_model->GetPWMRemoveMsg ();
89+ uint8_t pin = atoi (msg_remove.pin + 1 );
90+ // Check if the pin is already attached
91+ int pin_idx = GetPWMHardwareIdx (pin);
92+ if (pin_idx == -1 ) {
93+ WS_DEBUG_PRINTLN (" [pwm] Error: pin not found!" );
94+ return false ;
95+ }
96+
97+ // Detach and free the pin
98+ _pwm_hardware[pin_idx]->DetachPin ();
99+ delete _pwm_hardware[pin_idx];
100+ _pwm_hardware[pin_idx] = nullptr ;
101+
102+ // Update _active_pwm_pins[]
103+ _active_pwm_pins--;
104+ for (int i = pin_idx; i < _active_pwm_pins; i++) {
105+ _pwm_hardware[i] = _pwm_hardware[i + 1 ];
106+ }
107+ return true ;
108+ }
109+
66110/* *************************************************************************/
67111/* !
68112 @brief Returns the index of the PWM hardware object that corresponds
@@ -80,6 +124,13 @@ int PWMController::GetPWMHardwareIdx(uint8_t pin) {
80124 return -1 ;
81125}
82126
127+ /* *************************************************************************/
128+ /* !
129+ @brief Handles the PWM_Write_DutyCycle message.
130+ @param stream The stream containing the message data.
131+ @return True if the message was handled successfully, false otherwise.
132+ */
133+ /* *************************************************************************/
83134bool PWMController::Handle_PWM_Write_DutyCycle (pb_istream_t *stream) {
84135 if (!_pwm_model->DecodePWMWriteDutyCycle (stream)) {
85136 WS_DEBUG_PRINTLN (
@@ -111,10 +162,24 @@ bool PWMController::Handle_PWM_Write_DutyCycle(pb_istream_t *stream) {
111162 return true ;
112163}
113164
165+ /* *************************************************************************/
166+ /* !
167+ @brief Handles the PWM_Write_DutyCycle_Multi message.
168+ @param stream The stream containing the message data.
169+ @return True if the message was handled successfully, false otherwise.
170+ */
171+ /* *************************************************************************/
114172bool PWMController::Handle_PWM_Write_DutyCycle_Multi (pb_istream_t *stream) {
115173 return false ;
116174}
117175
176+ /* *************************************************************************/
177+ /* !
178+ @brief Handles the PWM_Write_Frequency message.
179+ @param stream The stream containing the message data.
180+ @return True if the message was handled successfully, false otherwise.
181+ */
182+ /* *************************************************************************/
118183bool PWMController::Handle_PWM_Write_Frequency (pb_istream_t *stream) {
119184 if (!_pwm_model->DecodePWMWriteFrequency (stream)) {
120185 WS_DEBUG_PRINTLN (
@@ -141,6 +206,4 @@ bool PWMController::Handle_PWM_Write_Frequency(pb_istream_t *stream) {
141206 WS_DEBUG_PRINTLN (" to pin: " );
142207 WS_DEBUG_PRINT (msg_write_frequency.pin );
143208 return true ;
144- }
145-
146- bool PWMController::Handle_PWM_Remove (pb_istream_t *stream) { return false ; }
209+ }
0 commit comments