@@ -48,32 +48,38 @@ ServoHardware::ServoHardware(int pin, int min_pulse_width, int max_pulse_width,
48
48
*/
49
49
/* *************************************************************************/
50
50
ServoHardware::~ServoHardware () {
51
- #ifdef ARDUINO_ARCH_ESP32
52
- if (!_is_attached) {
53
- WS_DEBUG_PRINTLN (" [servo] Error: Failed to detach, servo not attached!" );
54
- return ;
51
+ if (!ServoDetach ()) {
52
+ WS_DEBUG_PRINTLN (" [servo] Error: Failed to detach servo!" );
53
+ } else {
54
+ WS_DEBUG_PRINT (" [servo] Servo detached from pin: " );
55
+ WS_DEBUG_PRINTLN (_pin);
55
56
}
56
- if (!ledcDetach (_pin)) {
57
- WS_DEBUG_PRINTLN (" [servo] Error: Failed to detach servo from pin!" );
58
- return ;
57
+ }
58
+
59
+ /* *************************************************************************/
60
+ /* !
61
+ @brief Detaches the servo from the pin and frees the pin for
62
+ other uses.
63
+ @returns true if successful, false otherwise
64
+ */
65
+ /* *************************************************************************/
66
+ bool ServoHardware::ServoDetach () {
67
+ #ifdef ARDUINO_ARCH_ESP32
68
+ detach ();
69
+ if (attached ()) {
70
+ WS_DEBUG_PRINTLN (" [servo]Error: Servo detach failure!" );
71
+ return false ;
59
72
}
60
- _is_attached = false ;
61
73
#else
62
- if (_servo == nullptr ) {
63
- WS_DEBUG_PRINTLN (" [servo] Error: Failed to detach, servo not created!" );
64
- return ;
65
- }
66
- if (!_servo->attached ()) {
67
- WS_DEBUG_PRINTLN (" [servo] Error: Failed to detach, servo not attached!" );
68
- return ;
74
+ if (_servo == nullptr || !_servo->attached ()) {
75
+ WS_DEBUG_PRINTLN (" [servo] Detach Error: Servo not attached!" );
76
+ return false ;
69
77
}
70
78
_servo->detach ();
71
79
delete _servo;
72
80
_servo = nullptr ;
73
81
#endif
74
-
75
- WS_DEBUG_PRINT (" [servo] Servo detached from pin " );
76
- WS_DEBUG_PRINTLN (_pin);
82
+ return true ;
77
83
}
78
84
79
85
/* *************************************************************************/
@@ -95,7 +101,7 @@ bool ServoHardware::ServoAttach() {
95
101
}
96
102
#else
97
103
if (_servo == nullptr ) {
98
- WS_DEBUG_PRINTLN (" [servo] Error: Failed to detach, servo not created !" );
104
+ WS_DEBUG_PRINTLN (" [servo] Attach Error: Servo not initialized !" );
99
105
return false ;
100
106
}
101
107
rc = _servo->attach (_pin, _min_pulse_width, _max_pulse_width);
@@ -127,7 +133,7 @@ uint8_t ServoHardware::GetPin() { return _pin; }
127
133
/* *************************************************************************/
128
134
void ServoHardware::ServoWrite (int value) {
129
135
#ifdef ARDUINO_ARCH_ESP32
130
- if (!_is_attached ) {
136
+ if (!attached () ) {
131
137
WS_DEBUG_PRINTLN (" [servo] Error: Servo not attached!" );
132
138
return ;
133
139
}
@@ -137,7 +143,12 @@ void ServoHardware::ServoWrite(int value) {
137
143
WS_DEBUG_PRINTLN (" [servo] Error: Servo not attached!" );
138
144
return ;
139
145
}
140
- _servo.writeMicroseconds (value);
146
+ // Clamp value to a valid pulse_width range
147
+ if (value < _min_pulse_width)
148
+ value = _min_pulse_width;
149
+ if (value > _max_pulse_width)
150
+ value = _max_pulse_width;
151
+ _servo->writeMicroseconds (value);
141
152
#endif
142
153
}
143
154
@@ -166,4 +177,24 @@ void ServoHardware::writeMicroseconds(int value) {
166
177
if (!ledcWrite (_pin, count))
167
178
WS_DEBUG_PRINTLN (" [servo] Error: Failed to write to servo pin!" );
168
179
}
180
+
181
+ /* *************************************************************************/
182
+ /* !
183
+ @brief Detaches the servo from the LEDC manager and frees the pin for
184
+ other uses.
185
+ */
186
+ /* *************************************************************************/
187
+ void ServoHardware::detach () {
188
+ if (!attached ())
189
+ return ;
190
+ _is_attached = ledcDetach (_pin);
191
+ }
192
+
193
+ /* *************************************************************************/
194
+ /* !
195
+ @brief Returns true if the servo is attached to the pin
196
+ @returns true if the servo is attached to the pin, false otherwise
197
+ */
198
+ /* *************************************************************************/
199
+ bool ServoHardware::attached () { return _is_attached; }
169
200
#endif
0 commit comments