Skip to content

Commit ffd7a71

Browse files
committed
Tidy handler for PWMWriteDutycycle
1 parent 141753b commit ffd7a71

File tree

5 files changed

+70
-2
lines changed

5 files changed

+70
-2
lines changed

src/components/pwm/controller.cpp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
5976
bool 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

63104
bool PWMController::Handle_PWM_Write_DutyCycle_Multi(pb_istream_t *stream) {

src/components/pwm/controller.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class PWMController {
3939
bool Handle_PWM_Write_DutyCycle_Multi(pb_istream_t *stream);
4040
bool Handle_PWM_Write_Frequency(pb_istream_t *stream);
4141
bool Handle_PWM_Remove(pb_istream_t *stream);
42+
int GetPWMHardwareIdx(uint8_t pin);
4243

4344
private:
4445
PWMModel *_pwm_model;

src/components/pwm/hardware.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ void PWMHardware::WriteNoTone() {
157157
#endif
158158
}
159159

160+
/**************************************************************************/
161+
/*!
162+
@brief Returns the pin number of the PWM pin
163+
@return The logical pin number of the PWM pin
164+
*/
165+
/**************************************************************************/
166+
uint8_t PWMHardware::GetPin() {
167+
return _pin;
168+
}
169+
160170
// LEDC API Wrappers
161171
#ifdef ARDUINO_ARCH_ESP32
162172
/**************************************************************************/

src/components/pwm/hardware.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class PWMHardware {
3434
bool WriteDutyCycle(uint32_t duty);
3535
uint32_t WriteTone(uint32_t freq);
3636
void WriteNoTone();
37+
uint8_t GetPin();
3738

3839
// Abstractions for LEDC API
3940
#ifdef ARDUINO_ARCH_ESP32

src/components/pwm/model.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,26 @@ bool PWMModel::DecodePWMRemove(pb_istream_t *stream) {
105105
wippersnapper_pwm_PWMRemove *PWMModel::GetPWMRemoveMsg() {
106106
}
107107

108+
/**************************************************************************/
109+
/*!
110+
@brief Decodes a PWMWriteDutyCycle message from an input stream.
111+
@param stream The stream to decode from.
112+
@return true if successful, false otherwise.
113+
*/
114+
/**************************************************************************/
108115
bool PWMModel::DecodePWMWriteDutyCycle(pb_istream_t *stream) {
109-
return false;
116+
memset(&_msg_pwm_write_duty_cycle, 0, sizeof(_msg_pwm_write_duty_cycle));
117+
return pb_decode(stream, wippersnapper_pwm_PWMWriteDutyCycle_fields, &_msg_pwm_write_duty_cycle);
110118
}
111119

120+
/**************************************************************************/
121+
/*!
122+
@brief Returns a pointer to the PWMWriteDutyCycle message.
123+
@return Pointer to the PWMWriteDutyCycle message.
124+
*/
125+
/**************************************************************************/
112126
wippersnapper_pwm_PWMWriteDutyCycle *PWMModel::GetPWMWriteDutyCycleMsg() {
127+
return &_msg_pwm_write_duty_cycle;
113128
}
114129

115130
bool PWMModel::DecodePWMWriteDutyCycleMulti(pb_istream_t *stream) {

0 commit comments

Comments
 (0)