@@ -44,6 +44,28 @@ ServoController::~ServoController() {
4444 }
4545}
4646
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+
4769/* *************************************************************************/
4870/* !
4971 @brief Handles a ServoAdd message
@@ -53,13 +75,16 @@ ServoController::~ServoController() {
5375*/
5476/* *************************************************************************/
5577bool ServoController::Handle_Servo_Add (pb_istream_t *stream) {
78+ bool did_attach;
5679 if (_active_servo_pins >= MAX_SERVOS) {
5780 WS_DEBUG_PRINTLN (" [servo] Error: Maximum number of servos reached!" );
81+ PublishServoAddedMsg (" PIN_UNKNOWN" , false , _servo_model->GetServoAddMsg ());
5882 return false ;
5983 }
6084
6185 if (!_servo_model->DecodeServoAdd (stream)) {
6286 WS_DEBUG_PRINTLN (" [servo] Error: Failed to decode ServoAdd message!" );
87+ PublishServoAddedMsg (" PIN_UNKNOWN" , false , _servo_model->GetServoAddMsg ());
6388 return false ;
6489 }
6590
@@ -69,7 +94,6 @@ bool ServoController::Handle_Servo_Add(pb_istream_t *stream) {
6994 pin, (int )msg_add->min_pulse_width , (int )msg_add->max_pulse_width ,
7095 (int )msg_add->servo_freq );
7196 // Attempt to attach the servo to the pin
72- bool did_attach = false ;
7397 did_attach = _servo_hardware[_active_servo_pins]->ServoAttach ();
7498
7599 // Write the pulse width to the servo
@@ -87,12 +111,8 @@ bool ServoController::Handle_Servo_Add(pb_istream_t *stream) {
87111 }
88112
89113 // 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 ()))
94115 return false ;
95- }
96116 return true ;
97117}
98118
0 commit comments