Skip to content

Commit 141753b

Browse files
committed
Implement Handler for PWMAdd Message
1 parent 184c5df commit 141753b

File tree

4 files changed

+109
-13
lines changed

4 files changed

+109
-13
lines changed

src/components/ds18x20/model.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ DS18X20Model::DS18X20Model() {
2424
memset(&_msg_DS18x20Added, 0, sizeof(_msg_DS18x20Added));
2525
memset(&_msg_DS18x20Remove, 0, sizeof(_msg_DS18x20Remove));
2626
memset(&_msg_DS18x20Event, 0, sizeof(_msg_DS18x20Event));
27-
// no-op
2827
}
2928

3029
/***********************************************************************/

src/components/pwm/controller.cpp

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,45 @@
1616

1717
PWMController::PWMController() {
1818
_pwm_model = new PWMModel();
19-
_pwm_hardware = new PWMHardware();
19+
_active_pwm_pins = 0;
2020
}
2121

22-
PWMController::~PWMController() {
23-
delete _pwm_model;
24-
delete _pwm_hardware;
25-
}
22+
PWMController::~PWMController() { delete _pwm_model; }
23+
24+
bool PWMController::Handle_PWM_Add(pb_istream_t *stream) {
25+
bool did_attach;
26+
if (!_pwm_model->DecodePWMAdd(stream)) {
27+
WS_DEBUG_PRINTLN("[pwm] Failed to decode PWMAdd message!");
28+
return false;
29+
}
30+
wippersnapper_pwm_PWMAdd msg_add = *_pwm_model->GetPWMAddMsg();
31+
uint8_t pin = atoi(msg_add.pin + 1);
32+
_pwm_hardware[_active_pwm_pins] = new PWMHardware();
2633

27-
bool PWMController::Handle_PWM_Add(pb_istream_t *stream) { return false; }
34+
did_attach = _pwm_hardware[_active_pwm_pins]->AttachPin(
35+
pin, (uint32_t)msg_add.frequency, (uint32_t)msg_add.resolution);
36+
if (!did_attach) {
37+
WS_DEBUG_PRINTLN("[pwm] Failed to attach pin!");
38+
// TODO: Test init. failure to see if the below line crashes?
39+
// TODO: if it doesn't, we should probably implement this
40+
// delete in the pixel controller class to avoid memory leaks
41+
delete _pwm_hardware[_active_pwm_pins];
42+
} else {
43+
_active_pwm_pins++;
44+
}
45+
46+
// Publish PixelsAdded message to the broker
47+
if (!_pwm_model->EncodePWMAdded(msg_add.pin, did_attach)) {
48+
WS_DEBUG_PRINTLN("[pwm]: Failed to encode PWMAdded message!");
49+
return false;
50+
}
51+
if (!WsV2.PublishSignal(wippersnapper_signal_DeviceToBroker_pwm_added_tag,
52+
_pwm_model->GetPWMAddedMsg())) {
53+
WS_DEBUG_PRINTLN("[PWM]: Unable to publish PWMAdded message!");
54+
return false;
55+
}
56+
return true;
57+
}
2858

2959
bool PWMController::Handle_PWM_Write_DutyCycle(pb_istream_t *stream) {
3060
return false;

src/components/pwm/controller.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "Wippersnapper_V2.h"
1818
#include "hardware.h"
1919
#include "model.h"
20+
#define MAX_PWM_PINS 25 ///< Maximum number of PWM pins supported
2021

2122
class Wippersnapper_V2; // Forward declaration
2223
class PWMModel; // Forward declaration
@@ -33,7 +34,6 @@ class PWMController {
3334
public:
3435
PWMController();
3536
~PWMController();
36-
// Called by the cbDecodeBrokerToDevice router function
3737
bool Handle_PWM_Add(pb_istream_t *stream);
3838
bool Handle_PWM_Write_DutyCycle(pb_istream_t *stream);
3939
bool Handle_PWM_Write_DutyCycle_Multi(pb_istream_t *stream);
@@ -42,7 +42,8 @@ class PWMController {
4242

4343
private:
4444
PWMModel *_pwm_model;
45-
PWMHardware *_pwm_hardware;
45+
PWMHardware *_pwm_hardware[MAX_PWM_PINS] = {nullptr};
46+
int _active_pwm_pins;
4647
};
4748
extern Wippersnapper_V2 WsV2; ///< Wippersnapper V2 instance
4849
#endif // WS_PWM_CONTROLLER_H

src/components/pwm/model.cpp

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,88 @@
1414
*/
1515
#include "model.h"
1616

17-
PWMModel::PWMModel() {}
17+
/**************************************************************************/
18+
/*!
19+
@brief Ctor for PWMModel.
20+
*/
21+
/**************************************************************************/
22+
PWMModel::PWMModel() {
23+
memset(&_msg_pwm_add, 0, sizeof(_msg_pwm_add));
24+
memset(&_msg_pwm_added, 0, sizeof(_msg_pwm_added));
25+
memset(&_msg_pwm_remove, 0, sizeof(_msg_pwm_remove));
26+
memset(&_msg_pwm_write_duty_cycle, 0, sizeof(_msg_pwm_write_duty_cycle));
27+
memset(&_msg_pwm_write_duty_cycle_multi, 0, sizeof(_msg_pwm_write_duty_cycle_multi));
28+
memset(&_msg_pwm_write_frequency, 0, sizeof(_msg_pwm_write_frequency));
29+
}
1830

19-
PWMModel::~PWMModel() {}
31+
/**************************************************************************/
32+
/*!
33+
@brief Dtor for PWMModel.
34+
*/
35+
/**************************************************************************/
36+
PWMModel::~PWMModel() {
37+
memset(&_msg_pwm_add, 0, sizeof(_msg_pwm_add));
38+
memset(&_msg_pwm_added, 0, sizeof(_msg_pwm_added));
39+
memset(&_msg_pwm_remove, 0, sizeof(_msg_pwm_remove));
40+
memset(&_msg_pwm_write_duty_cycle, 0, sizeof(_msg_pwm_write_duty_cycle));
41+
memset(&_msg_pwm_write_duty_cycle_multi, 0, sizeof(_msg_pwm_write_duty_cycle_multi));
42+
memset(&_msg_pwm_write_frequency, 0, sizeof(_msg_pwm_write_frequency));
43+
}
2044

45+
/**************************************************************************/
46+
/*!
47+
@brief Decodes a PWMAdd message from an input stream.
48+
@param stream The stream to decode from.
49+
@return true if successful, false otherwise.
50+
*/
51+
/**************************************************************************/
2152
bool PWMModel::DecodePWMAdd(pb_istream_t *stream) {
22-
return false;
53+
memset(&_msg_pwm_add, 0, sizeof(_msg_pwm_add));
54+
// Attempt to decode the stream into a pwm_PWMAdd message
55+
return pb_decode(stream, wippersnapper_pwm_PWMAdd_fields, &_msg_pwm_add);
2356
}
2457

58+
/**************************************************************************/
59+
/*!
60+
@brief Returns a pointer to the PWMAdd message.
61+
@return Pointer to the PWMAdd message.
62+
*/
63+
/**************************************************************************/
2564
wippersnapper_pwm_PWMAdd *PWMModel::GetPWMAddMsg() {
65+
return &_msg_pwm_add;
2666
}
2767

68+
/**************************************************************************/
69+
/*!
70+
@brief Encodes a PWMAdded message with the given pin name and attach status.
71+
@param pin_name The name of the pin.
72+
@param did_attach True if the pin was successfully attached, false otherwise.
73+
@return true if successful, false otherwise.
74+
*/
75+
/**************************************************************************/
2876
bool PWMModel::EncodePWMAdded(char *pin_name, bool did_attach) {
29-
return false;
77+
// Fill the message
78+
memset(&_msg_pwm_added, 0, sizeof(_msg_pwm_added));
79+
_msg_pwm_added.did_attach = did_attach;
80+
strncpy(_msg_pwm_added.pin, pin_name, sizeof(_msg_pwm_added.pin));
81+
// Encode it!
82+
size_t sz_msg;
83+
if (!pb_get_encoded_size(&sz_msg, wippersnapper_pwm_PWMAdded_fields,
84+
&_msg_pwm_added))
85+
return false;
86+
uint8_t buf[sz_msg];
87+
pb_ostream_t msg_stream = pb_ostream_from_buffer(buf, sizeof(buf));
88+
return pb_encode(&msg_stream, wippersnapper_pwm_PWMAdded_fields, &_msg_pwm_added);
3089
}
3190

91+
/**************************************************************************/
92+
/*!
93+
@brief Returns a pointer to the PWMAdded message.
94+
@return Pointer to the PWMAdded message.
95+
*/
96+
/**************************************************************************/
3297
wippersnapper_pwm_PWMAdded *PWMModel::GetPWMAddedMsg() {
98+
return &_msg_pwm_added;
3399
}
34100

35101
bool PWMModel::DecodePWMRemove(pb_istream_t *stream) {

0 commit comments

Comments
 (0)