Skip to content

Commit 3304fb4

Browse files
committed
Full PWM implementation, sans writemulti..
1 parent 31e5758 commit 3304fb4

File tree

4 files changed

+112
-14
lines changed

4 files changed

+112
-14
lines changed

src/Wippersnapper_V2.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -439,21 +439,14 @@ bool cbDecodeBrokerToDevice(pb_istream_t *stream, const pb_field_t *field,
439439
return false;
440440
}
441441
break;
442-
case wippersnapper_signal_BrokerToDevice_pwm_write_duty_cycle_tag:
442+
case wippersnapper_signal_BrokerToDevice_pwm_write_duty_tag:
443443
WS_DEBUG_PRINTLN("-> PWM Write Duty Cycle Message Type");
444444
if (!WsV2._pwm_controller->Handle_PWM_Write_DutyCycle(stream)) {
445445
WS_DEBUG_PRINTLN("ERROR: Unable to write duty cycle to PWM pin!");
446446
return false;
447447
}
448448
break;
449-
case wippersnapper_signal_BrokerToDevice_pwm_write_duty_cycle_multi_tag:
450-
WS_DEBUG_PRINTLN("-> PWM Write Duty Cycle Multi Message Type");
451-
if (!WsV2._pwm_controller->Handle_PWM_Write_DutyCycle_Multi(stream)) {
452-
WS_DEBUG_PRINTLN("ERROR: Unable to write multi duty cycle to PWM pins!");
453-
return false;
454-
}
455-
break;
456-
case wippersnapper_signal_BrokerToDevice_pwm_write_frequency_tag:
449+
case wippersnapper_signal_BrokerToDevice_pwm_write_freq_tag:
457450
WS_DEBUG_PRINTLN("-> PWM Write Frequency Message Type");
458451
if (!WsV2._pwm_controller->Handle_PWM_Write_Frequency(stream)) {
459452
WS_DEBUG_PRINTLN("ERROR: Unable to write frequency to PWM pin!");

src/components/pwm/controller.cpp

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,21 @@
1414
*/
1515
#include "controller.h"
1616

17+
/**************************************************************************/
18+
/*!
19+
@brief Ctor for PWMController.
20+
*/
21+
/**************************************************************************/
1722
PWMController::PWMController() {
1823
_pwm_model = new PWMModel();
1924
_active_pwm_pins = 0;
2025
}
2126

27+
/**************************************************************************/
28+
/*!
29+
@brief Dtor for PWMController.
30+
*/
31+
/**************************************************************************/
2232
PWMController::~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+
/**************************************************************************/
83134
bool 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+
/**************************************************************************/
114172
bool 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+
/**************************************************************************/
118183
bool 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+
}

src/components/pwm/hardware.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ bool PWMHardware::DetachPin() {
7979

8080
// "Disable" the pin's output
8181
digitalWrite(_pin, LOW);
82+
return did_detach;
8283
}
8384

8485
/**************************************************************************/

src/components/pwm/model.cpp

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ PWMModel::~PWMModel() {
5151
/**************************************************************************/
5252
bool PWMModel::DecodePWMAdd(pb_istream_t *stream) {
5353
memset(&_msg_pwm_add, 0, sizeof(_msg_pwm_add));
54-
// Attempt to decode the stream into a pwm_PWMAdd message
5554
return pb_decode(stream, wippersnapper_pwm_PWMAdd_fields, &_msg_pwm_add);
5655
}
5756

@@ -98,11 +97,26 @@ wippersnapper_pwm_PWMAdded *PWMModel::GetPWMAddedMsg() {
9897
return &_msg_pwm_added;
9998
}
10099

100+
/**************************************************************************/
101+
/*!
102+
@brief Decodes a PWMRemove message from an input stream.
103+
@param stream The stream to decode from.
104+
@return true if successful, false otherwise.
105+
*/
106+
/**************************************************************************/
101107
bool PWMModel::DecodePWMRemove(pb_istream_t *stream) {
102-
return false;
108+
memset(&_msg_pwm_remove, 0, sizeof(_msg_pwm_remove));
109+
return pb_decode(stream, wippersnapper_pwm_PWMRemove_fields, &_msg_pwm_remove);
103110
}
104111

112+
/**************************************************************************/
113+
/*!
114+
@brief Returns a pointer to the PWMRemove message.
115+
@return Pointer to the PWMRemove message.
116+
*/
117+
/**************************************************************************/
105118
wippersnapper_pwm_PWMRemove *PWMModel::GetPWMRemoveMsg() {
119+
return &_msg_pwm_remove;
106120
}
107121

108122
/**************************************************************************/
@@ -127,18 +141,45 @@ wippersnapper_pwm_PWMWriteDutyCycle *PWMModel::GetPWMWriteDutyCycleMsg() {
127141
return &_msg_pwm_write_duty_cycle;
128142
}
129143

144+
/**************************************************************************/
145+
/*!
146+
@brief Decodes a PWMWriteDutyCycleMulti message from an input stream.
147+
@param stream The stream to decode from.
148+
@return true if successful, false otherwise.
149+
*/
150+
/**************************************************************************/
130151
bool PWMModel::DecodePWMWriteDutyCycleMulti(pb_istream_t *stream) {
131152
return false;
132153
}
133154

155+
/**************************************************************************/
156+
/*!
157+
@brief Returns a pointer to the PWMWriteDutyCycleMulti message.
158+
@return Pointer to the PWMWriteDutyCycleMulti message.
159+
*/
160+
/**************************************************************************/
134161
wippersnapper_pwm_PWMWriteDutyCycleMulti *PWMModel::GetPWMWriteDutyCycleMultiMsg() {
162+
return &_msg_pwm_write_duty_cycle_multi;
135163
}
136164

165+
/**************************************************************************/
166+
/*!
167+
@brief Decodes a PWMWriteFrequency message from an input stream.
168+
@param stream The stream to decode from.
169+
@return true if successful, false otherwise.
170+
*/
171+
/**************************************************************************/
137172
bool PWMModel::DecodePWMWriteFrequency(pb_istream_t *stream) {
138173
memset(&_msg_pwm_write_frequency, 0, sizeof(_msg_pwm_write_frequency));
139174
return pb_decode(stream, wippersnapper_pwm_PWMWriteFrequency_fields, &_msg_pwm_write_frequency);
140175
}
141176

177+
/**************************************************************************/
178+
/*!
179+
@brief Returns a pointer to the PWMWriteFrequency message.
180+
@return Pointer to the PWMWriteFrequency message.
181+
*/
182+
/**************************************************************************/
142183
wippersnapper_pwm_PWMWriteFrequency *PWMModel::GetPWMWriteFrequencyMsg() {
143184
return &_msg_pwm_write_frequency;
144185
}

0 commit comments

Comments
 (0)