@@ -44,6 +44,28 @@ ServoController::~ServoController() {
44
44
}
45
45
}
46
46
47
+ /* *************************************************************************/
48
+ /* !
49
+ @brief Publishes a ServoAdded message to the broker
50
+ @param servo_pin
51
+ Pin number of the servo
52
+ @param did_attach
53
+ True if the servo was attached successfully, False otherwise
54
+ @returns True if successful, False otherwise
55
+ */
56
+ /* *************************************************************************/
57
+ bool ServoController::PublishServoAddedMsg (
58
+ const char *servo_pin, bool did_attach,
59
+ wippersnapper_servo_ServoAdd *msg_add) {
60
+ _servo_model->EncodeServoAdded (msg_add->servo_pin , did_attach);
61
+ if (!WsV2.PublishSignal (wippersnapper_signal_DeviceToBroker_servo_added_tag,
62
+ _servo_model->GetServoAddedMsg ())) {
63
+ WS_DEBUG_PRINTLN (" [servo] Error: Failed publishing a ServoAdded message!" );
64
+ return false ;
65
+ }
66
+ return true ;
67
+ }
68
+
47
69
/* *************************************************************************/
48
70
/* !
49
71
@brief Handles a ServoAdd message
@@ -53,13 +75,16 @@ ServoController::~ServoController() {
53
75
*/
54
76
/* *************************************************************************/
55
77
bool ServoController::Handle_Servo_Add (pb_istream_t *stream) {
78
+ bool did_attach;
56
79
if (_active_servo_pins >= MAX_SERVOS) {
57
80
WS_DEBUG_PRINTLN (" [servo] Error: Maximum number of servos reached!" );
81
+ PublishServoAddedMsg (" PIN_UNKNOWN" , false , _servo_model->GetServoAddMsg ());
58
82
return false ;
59
83
}
60
84
61
85
if (!_servo_model->DecodeServoAdd (stream)) {
62
86
WS_DEBUG_PRINTLN (" [servo] Error: Failed to decode ServoAdd message!" );
87
+ PublishServoAddedMsg (" PIN_UNKNOWN" , false , _servo_model->GetServoAddMsg ());
63
88
return false ;
64
89
}
65
90
@@ -69,7 +94,6 @@ bool ServoController::Handle_Servo_Add(pb_istream_t *stream) {
69
94
pin, (int )msg_add->min_pulse_width , (int )msg_add->max_pulse_width ,
70
95
(int )msg_add->servo_freq );
71
96
// Attempt to attach the servo to the pin
72
- bool did_attach = false ;
73
97
did_attach = _servo_hardware[_active_servo_pins]->ServoAttach ();
74
98
75
99
// Write the pulse width to the servo
@@ -87,12 +111,8 @@ bool ServoController::Handle_Servo_Add(pb_istream_t *stream) {
87
111
}
88
112
89
113
// Publish ServoAdded message to IO
90
- _servo_model->EncodeServoAdded (msg_add->servo_pin , did_attach);
91
- if (!WsV2.PublishSignal (wippersnapper_signal_DeviceToBroker_servo_added_tag,
92
- _servo_model->GetServoAddedMsg ())) {
93
- WS_DEBUG_PRINTLN (" [servo] Error: Failed publishing a ServoAdded message!" );
114
+ if (!PublishServoAddedMsg (" " , false , _servo_model->GetServoAddMsg ()))
94
115
return false ;
95
- }
96
116
return true ;
97
117
}
98
118
0 commit comments