@@ -21,6 +21,13 @@ PWMController::PWMController() {
21
21
22
22
PWMController::~PWMController () { delete _pwm_model; }
23
23
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
+ /* *************************************************************************/
24
31
bool PWMController::Handle_PWM_Add (pb_istream_t *stream) {
25
32
bool did_attach;
26
33
if (!_pwm_model->DecodePWMAdd (stream)) {
@@ -109,7 +116,31 @@ bool PWMController::Handle_PWM_Write_DutyCycle_Multi(pb_istream_t *stream) {
109
116
}
110
117
111
118
bool 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 ;
113
144
}
114
145
115
146
bool PWMController::Handle_PWM_Remove (pb_istream_t *stream) { return false ; }
0 commit comments