Skip to content

Commit edadb31

Browse files
committed
Implement WriteFrequency message
1 parent aadac2b commit edadb31

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/components/pwm/controller.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ PWMController::PWMController() {
2121

2222
PWMController::~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+
/**************************************************************************/
2431
bool 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

111118
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;
113144
}
114145

115146
bool PWMController::Handle_PWM_Remove(pb_istream_t *stream) { return false; }

src/components/pwm/hardware.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ bool PWMHardware::WriteDutyCycle(uint32_t duty) {
117117
/*!
118118
@brief Writes a frequency to a PWM pin with a fixed duty cycle.
119119
@param freq The desired frequency to write to the pin.
120-
@return true if the tone was successfully written, false otherwise
120+
@return The frequency that was written to the pin.
121121
*/
122122
/**************************************************************************/
123123
uint32_t PWMHardware::WriteTone(uint32_t freq) {

src/components/pwm/model.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,10 @@ wippersnapper_pwm_PWMWriteDutyCycleMulti *PWMModel::GetPWMWriteDutyCycleMultiMsg
135135
}
136136

137137
bool PWMModel::DecodePWMWriteFrequency(pb_istream_t *stream) {
138-
return false;
138+
memset(&_msg_pwm_write_frequency, 0, sizeof(_msg_pwm_write_frequency));
139+
return pb_decode(stream, wippersnapper_pwm_PWMWriteFrequency_fields, &_msg_pwm_write_frequency);
139140
}
140141

141142
wippersnapper_pwm_PWMWriteFrequency *PWMModel::GetPWMWriteFrequencyMsg() {
143+
return &_msg_pwm_write_frequency;
142144
}

0 commit comments

Comments
 (0)