@@ -48,32 +48,38 @@ ServoHardware::ServoHardware(int pin, int min_pulse_width, int max_pulse_width,
4848*/
4949/* *************************************************************************/
5050ServoHardware::~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);
5556 }
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 ;
5972 }
60- _is_attached = false ;
6173#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 ;
6977 }
7078 _servo->detach ();
7179 delete _servo;
7280 _servo = nullptr ;
7381#endif
74-
75- WS_DEBUG_PRINT (" [servo] Servo detached from pin " );
76- WS_DEBUG_PRINTLN (_pin);
82+ return true ;
7783}
7884
7985/* *************************************************************************/
@@ -95,7 +101,7 @@ bool ServoHardware::ServoAttach() {
95101 }
96102#else
97103 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 !" );
99105 return false ;
100106 }
101107 rc = _servo->attach (_pin, _min_pulse_width, _max_pulse_width);
@@ -127,7 +133,7 @@ uint8_t ServoHardware::GetPin() { return _pin; }
127133/* *************************************************************************/
128134void ServoHardware::ServoWrite (int value) {
129135#ifdef ARDUINO_ARCH_ESP32
130- if (!_is_attached ) {
136+ if (!attached () ) {
131137 WS_DEBUG_PRINTLN (" [servo] Error: Servo not attached!" );
132138 return ;
133139 }
@@ -137,7 +143,12 @@ void ServoHardware::ServoWrite(int value) {
137143 WS_DEBUG_PRINTLN (" [servo] Error: Servo not attached!" );
138144 return ;
139145 }
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);
141152#endif
142153}
143154
@@ -166,4 +177,24 @@ void ServoHardware::writeMicroseconds(int value) {
166177 if (!ledcWrite (_pin, count))
167178 WS_DEBUG_PRINTLN (" [servo] Error: Failed to write to servo pin!" );
168179}
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; }
169200#endif
0 commit comments