|
| 1 | +/* |
| 2 | + Servo.h - Interrupt driven Servo library for Esp8266 using timers |
| 3 | + Copyright (c) 2015 Michael C. Miller. All right reserved. |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | + */ |
| 19 | + |
| 20 | + |
| 21 | +// A servo is activated by creating an instance of the Servo class passing |
| 22 | +// the desired pin to the attach() method. |
| 23 | +// The servos are pulsed in the background using the value most recently |
| 24 | +// written using the write() method. |
| 25 | +// |
| 26 | +// This library uses time0 and timer1. |
| 27 | +// Note that timer0 may be repurposed when the first servo is attached. |
| 28 | +// |
| 29 | +// Timers are seized as needed in groups of 12 servos - 24 servos use two |
| 30 | +// timers, there are only two timers for the esp8266 so the support stops here |
| 31 | +// The sequence used to sieze timers is defined in timers.h |
| 32 | +// |
| 33 | +// The methods are: |
| 34 | +// |
| 35 | +// Servo - Class for manipulating servo motors connected to Arduino pins. |
| 36 | +// |
| 37 | +// attach(pin ) - Attaches a servo motor to an i/o pin. |
| 38 | +// attach(pin, min, max ) - Attaches to a pin setting min and max values in microseconds |
| 39 | +// default min is 544, max is 2400 |
| 40 | +// |
| 41 | +// write() - Sets the servo angle in degrees. (invalid angle that is valid as pulse in microseconds is treated as microseconds) |
| 42 | +// writeMicroseconds() - Sets the servo pulse width in microseconds |
| 43 | +// read() - Gets the last written servo pulse width as an angle between 0 and 180. |
| 44 | +// readMicroseconds() - Gets the last written servo pulse width in microseconds. (was read_us() in first release) |
| 45 | +// attached() - Returns true if there is a servo attached. |
| 46 | +// detach() - Stops an attached servos from pulsing its i/o pin. |
| 47 | + |
| 48 | + |
| 49 | +#ifndef Servo_h |
| 50 | +#define Servo_h |
| 51 | + |
| 52 | +#include <Arduino.h> |
| 53 | + |
| 54 | +// the following are in us (microseconds) |
| 55 | +// |
| 56 | +#define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo |
| 57 | +#define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo |
| 58 | +#define DEFAULT_PULSE_WIDTH 1500 // default pulse width when servo is attached |
| 59 | +#define REFRESH_INTERVAL 20000 // minumim time to refresh servos in microseconds |
| 60 | + |
| 61 | +// NOTE: to maintain a strict refresh interval the user needs to not exceede 8 servos |
| 62 | +#define SERVOS_PER_TIMER 12 // the maximum number of servos controlled by one timer |
| 63 | +#define MAX_SERVOS (ServoTimerSequence_COUNT * SERVOS_PER_TIMER) |
| 64 | + |
| 65 | +#if defined(ESP8266) |
| 66 | + |
| 67 | +#include "esp8266/ServoTimers.h" |
| 68 | + |
| 69 | +#else |
| 70 | + |
| 71 | +#error "This library only supports esp8266 boards." |
| 72 | + |
| 73 | +#endif |
| 74 | + |
| 75 | +class Servo |
| 76 | +{ |
| 77 | +public: |
| 78 | + Servo(); |
| 79 | + uint8_t attach(int pin); // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure |
| 80 | + uint8_t attach(int pin, int min, int max); // as above but also sets min and max values for writes. |
| 81 | + void detach(); |
| 82 | + void write(int value); // if value is < 200 its treated as an angle, otherwise as pulse width in microseconds |
| 83 | + void writeMicroseconds(int value); // Write pulse width in microseconds |
| 84 | + int read(); // returns current pulse width as an angle between 0 and 180 degrees |
| 85 | + int readMicroseconds(); // returns current pulse width in microseconds for this servo (was read_us() in first release) |
| 86 | + bool attached(); // return true if this servo is attached, otherwise false |
| 87 | +private: |
| 88 | + uint8_t _servoIndex; // index into the channel data for this servo |
| 89 | + uint16_t _minUs; |
| 90 | + uint16_t _maxUs; |
| 91 | +}; |
| 92 | + |
| 93 | +#endif |
0 commit comments