55 *
66 * Built by Khoi Hoang https://github.com/khoih-prog/ESP8266_ISR_Servo
77 * Licensed under MIT license
8- * Version: 1.0.0
8+ * Version: 1.0.1
99 *
1010 * The ESP8266 timers are badly designed, using only 23-bit counter along with maximum 256 prescaler. They're only better than UNO / Mega.
1111 * The ESP8266 has two hardware timers, but timer0 has been used for WiFi and it's not advisable to use. Only timer1 is available.
2727 * Version Modified By Date Comments
2828 * ------- ----------- ---------- -----------
2929 * 1.0.0 K Hoang 04/12/2019 Initial coding
30- *****************************************************************************************************************************/
30+ * 1.0.1 K Hoang 05/12/2019 Add more features getPosition and getPulseWidth. Optimize.
31+ *****************************************************************************************************************************/
3132
3233#include " ESP8266_ISR_Servo.h"
3334#include < string.h>
3435
3536#ifndef ISR_SERVO_DEBUG
36- #define ISR_SERVO_DEBUG 1
37+ #define ISR_SERVO_DEBUG 0
3738#endif
3839
3940// extern void ICACHE_RAM_ATTR ESP8266_ISR_Servo_Handler(void);
@@ -175,7 +176,7 @@ int ESP8266_ISR_Servo::setupServo(uint8_t pin, int min, int max)
175176 return servoIndex;
176177}
177178
178- bool ESP8266_ISR_Servo::setPosition (unsigned servoIndex, unsigned long position)
179+ bool ESP8266_ISR_Servo::setPosition (unsigned servoIndex, int position)
179180{
180181 if (servoIndex >= MAX_SERVOS)
181182 return false ;
@@ -197,6 +198,79 @@ bool ESP8266_ISR_Servo::setPosition(unsigned servoIndex, unsigned long position)
197198 return false ;
198199}
199200
201+ // returns last position in degrees if success, or -1 on wrong servoIndex
202+ int ESP8266_ISR_Servo::getPosition (unsigned servoIndex)
203+ {
204+ if (servoIndex >= MAX_SERVOS)
205+ return -1 ;
206+
207+ // Updates interval of existing specified servo
208+ if ( servo[servoIndex].enabled && (servo[servoIndex].pin <= ESP8266_MAX_PIN) )
209+ {
210+ #if (ISR_SERVO_DEBUG > 0)
211+ Serial.println (" Idx = " + String (servoIndex) + " , cnt = " + String (servo[servoIndex].count ) + " , pos = " + String (servo[servoIndex].position ));
212+ #endif
213+
214+ return (servo[servoIndex].position );
215+ }
216+
217+ // return 0 for non-used numServo or bad pin
218+ return -1 ;
219+ }
220+
221+
222+ // setPulseWidth will set servo PWM Pulse Width in microseconds, correcponding to certain position in degrees
223+ // by using PWM, turn HIGH 'pulseWidth' microseconds within REFRESH_INTERVAL (20000us)
224+ // min and max for each individual servo are enforced
225+ // returns true on success or -1 on wrong servoIndex
226+ bool ESP8266_ISR_Servo::setPulseWidth (unsigned servoIndex, unsigned int pulseWidth)
227+ {
228+ if (servoIndex >= MAX_SERVOS)
229+ return false ;
230+
231+ // Updates interval of existing specified servo
232+ if ( servo[servoIndex].enabled && (servo[servoIndex].pin <= ESP8266_MAX_PIN) )
233+ {
234+ if (pulseWidth < servo[servoIndex].min )
235+ pulseWidth = servo[servoIndex].min ;
236+ else if (pulseWidth > servo[servoIndex].max )
237+ pulseWidth = servo[servoIndex].max ;
238+
239+ servo[servoIndex].count = pulseWidth / TIMER_INTERVAL_MICRO;
240+ servo[servoIndex].position = map (pulseWidth, servo[servoIndex].min , servo[servoIndex].max , 0 , 180 );
241+
242+ #if (ISR_SERVO_DEBUG > 0)
243+ Serial.println (" Idx = " + String (servoIndex) + " , cnt = " + String (servo[servoIndex].count ) + " , pos = " + String (servo[servoIndex].position ));
244+ #endif
245+
246+ return true ;
247+ }
248+
249+ // false return for non-used numServo or bad pin
250+ return false ;
251+ }
252+
253+ // returns pulseWidth in microsecs (within min/max range) if success, or 0 on wrong servoIndex
254+ unsigned int ESP8266_ISR_Servo::getPulseWidth (unsigned servoIndex)
255+ {
256+ if (servoIndex >= MAX_SERVOS)
257+ return 0 ;
258+
259+ // Updates interval of existing specified servo
260+ if ( servo[servoIndex].enabled && (servo[servoIndex].pin <= ESP8266_MAX_PIN) )
261+ {
262+ #if (ISR_SERVO_DEBUG > 0)
263+ Serial.println (" Idx = " + String (servoIndex) + " , cnt = " + String (servo[servoIndex].count ) + " , pos = " + String (servo[servoIndex].position ));
264+ #endif
265+
266+ return (servo[servoIndex].count * TIMER_INTERVAL_MICRO );
267+ }
268+
269+ // return 0 for non-used numServo or bad pin
270+ return 0 ;
271+ }
272+
273+
200274void ESP8266_ISR_Servo::deleteServo (unsigned servoIndex)
201275{
202276 if ( (numServos == 0 ) || (servoIndex >= MAX_SERVOS) )
0 commit comments