Skip to content

Commit 8c3dbdd

Browse files
committed
Address brent PR
1 parent 7589b78 commit 8c3dbdd

File tree

3 files changed

+38
-25
lines changed

3 files changed

+38
-25
lines changed

src/components/servo/controller.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ ServoController::~ServoController() {
5353
*/
5454
/**************************************************************************/
5555
bool ServoController::Handle_Servo_Add(pb_istream_t *stream) {
56-
if (!_servo_model->DecodeServoAdd(stream)) {
57-
WS_DEBUG_PRINTLN("[servo] Error: Failed to decode ServoAdd message!");
56+
if (_active_servo_pins >= MAX_SERVOS) {
57+
WS_DEBUG_PRINTLN("[servo] Error: Maximum number of servos reached!");
5858
return false;
5959
}
6060

61-
if (_active_servo_pins >= MAX_SERVOS) {
62-
WS_DEBUG_PRINTLN("[servo] Error: Maximum number of servos reached!");
61+
if (!_servo_model->DecodeServoAdd(stream)) {
62+
WS_DEBUG_PRINTLN("[servo] Error: Failed to decode ServoAdd message!");
6363
return false;
6464
}
6565

@@ -72,15 +72,16 @@ bool ServoController::Handle_Servo_Add(pb_istream_t *stream) {
7272
bool did_attach = false;
7373
did_attach = _servo_hardware[_active_servo_pins]->ServoAttach();
7474

75-
// Write the default minimum to a servo
75+
// Write the pulse width to the servo
7676
if (did_attach) {
7777
_servo_hardware[_active_servo_pins]->ServoWrite(
7878
(int)msg_add->min_pulse_width);
7979
WS_DEBUG_PRINT("[servo] Servo attached to pin: ");
8080
WS_DEBUG_PRINTLN(msg_add->servo_pin);
8181
_active_servo_pins++;
8282
} else {
83-
WS_DEBUG_PRINTLN("[servo] Error: Failed to attach servo to pin!");
83+
WS_DEBUG_PRINT("[servo] Error: Failed to attach servo to pin !");
84+
WS_DEBUG_PRINT(msg_add->servo_pin);
8485
delete _servo_hardware[_active_servo_pins];
8586
_servo_hardware[_active_servo_pins] = nullptr;
8687
}
@@ -129,6 +130,11 @@ bool ServoController::Handle_Servo_Write(pb_istream_t *stream) {
129130
*/
130131
/**************************************************************************/
131132
bool ServoController::Handle_Servo_Remove(pb_istream_t *stream) {
133+
if (_active_servo_pins <= 0) {
134+
WS_DEBUG_PRINTLN("[servo] Error: No active servos!");
135+
return false;
136+
}
137+
132138
if (!_servo_model->DecodeServoRemove(stream)) {
133139
WS_DEBUG_PRINTLN("[servo] Error: Failed to decode ServoRemove message!");
134140
return false;
@@ -142,11 +148,6 @@ bool ServoController::Handle_Servo_Remove(pb_istream_t *stream) {
142148
return false;
143149
}
144150

145-
if (_active_servo_pins <= 0) {
146-
WS_DEBUG_PRINTLN("[servo] Error: No active servos!");
147-
return false;
148-
}
149-
150151
// The destructor of ServoHardware will handle proper detachment
151152
delete _servo_hardware[servo_idx];
152153
_servo_hardware[servo_idx] = nullptr;

src/components/servo/hardware.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ bool ServoHardware::ServoDetach() {
8686
*/
8787
/**************************************************************************/
8888
bool ServoHardware::ServoAttach() {
89-
uint16_t rc = 255;
89+
uint16_t rc;
9090

9191
// Attach the servo to the pin
9292
#ifdef ARDUINO_ARCH_ESP32
9393
if (!ledcAttach(_pin, _frequency, LEDC_TIMER_WIDTH)) {
94-
rc = 255;
94+
rc = ERROR_SERVO_ATTACH;
9595
} else {
9696
WS_DEBUG_PRINTLN("[servo:hw:L99] Servo attached to pin");
9797
rc = 1;
@@ -105,7 +105,7 @@ bool ServoHardware::ServoAttach() {
105105
rc = _servo->attach(_pin, _min_pulse_width, _max_pulse_width);
106106
#endif
107107

108-
if (rc == 255) {
108+
if (rc == ERROR_SERVO_ATTACH) {
109109
WS_DEBUG_PRINT("[servo] Error: Failed to attach servo to pin: ");
110110
WS_DEBUG_PRINTLN(_pin);
111111
return false;
@@ -122,6 +122,24 @@ bool ServoHardware::ServoAttach() {
122122
/**************************************************************************/
123123
uint8_t ServoHardware::GetPin() { return _pin; }
124124

125+
/**************************************************************************/
126+
/*!
127+
@brief Clamps the pulse width to the min/max range
128+
@param value
129+
The value to clamp
130+
@returns The clamped value
131+
*/
132+
/**************************************************************************/
133+
int ServoHardware::ClampPulseWidth(int value) {
134+
if (value < _min_pulse_width) {
135+
value = _min_pulse_width;
136+
}
137+
if (value > _max_pulse_width) {
138+
value = _max_pulse_width;
139+
}
140+
return value;
141+
}
142+
125143
/**************************************************************************/
126144
/*!
127145
@brief Writes a value to the servo pin
@@ -141,11 +159,7 @@ void ServoHardware::ServoWrite(int value) {
141159
WS_DEBUG_PRINTLN("[servo] Error: Servo not attached!");
142160
return;
143161
}
144-
// Clamp value to a valid pulse_width range
145-
if (value < _min_pulse_width)
146-
value = _min_pulse_width;
147-
if (value > _max_pulse_width)
148-
value = _max_pulse_width;
162+
value = ClampPulseWidth(value);
149163
_servo->writeMicroseconds(value);
150164
WS_DEBUG_PRINT("[servo] Set Pulse Width: ");
151165
WS_DEBUG_PRINT(value);
@@ -164,11 +178,8 @@ void ServoHardware::ServoWrite(int value) {
164178
*/
165179
/**************************************************************************/
166180
void ServoHardware::writeMicroseconds(int value) {
167-
// Clamp value to a valid pulse_width range
168-
if (value < _min_pulse_width)
169-
value = _min_pulse_width;
170-
if (value > _max_pulse_width)
171-
value = _max_pulse_width;
181+
// Clamp the value to the min/max range
182+
value = ClampPulseWidth(value);
172183

173184
// Formula from ESP32Servo library
174185
// https://github.com/madhephaestus/ESP32Servo/blob/master/src/ESP32Servo.cpp

src/components/servo/hardware.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include <Servo.h>
2727
#endif
2828

29-
#define MIN_SERVO_PULSE_WIDTH 500 ///< Default min. servo pulse width of 500uS
29+
#define ERROR_SERVO_ATTACH 255 ///< Error code for servo attach failure
3030

3131
/**************************************************************************/
3232
/*!
@@ -44,6 +44,7 @@ class ServoHardware {
4444

4545
private:
4646
bool ServoDetach();
47+
int ClampPulseWidth(int value);
4748
#ifdef ARDUINO_ARCH_ESP32
4849
// Mocks Servo library API for ESP32x's LEDC manager
4950
// https://github.com/arduino-libraries/Servo/blob/master/src/Servo.h

0 commit comments

Comments
 (0)