[
  {
    "path": "README.adoc",
    "content": "= Servo Library for ESP32 =\n\n*** THIS CODE IS OBSOLETE ***\nIt needs to be updated to be compatible with changes to the ESP32 standard libraries.\nAlso, if fine granularity control is required, it needs to use long int arithmetic\ninstead of floating point.\nI am no longer actively developing ESP32 code, so it will be a while before I get to this.\n\nThis library attempts to faithfully replicate the semantics of the\nArduino Servo library (see http://www.arduino.cc/en/Reference/Servo)\nfor the ESP32, with two (optional) additions. The two new functions\nexpose the ability of the ESP32 PWM timers to vary timer width.\n\n== License ==\n\nCopyright (c) 2017 John K. Bennett.  All right reserved.\n\nThis library is free software; you can redistribute it and/or\nmodify it under the terms of the GNU Lesser General Public\nLicense as published by the Free Software Foundation; either\nversion 2.1 of the License, or (at your option) any later version.\n\nThis library is distributed in the hope that it will be useful,\nbut WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\nLesser General Public License for more details.\n\nYou should have received a copy of the GNU Lesser General Public\nLicense along with this library; if not, write to the Free Software\nFoundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n\nLibrary Description:\n--------------------\n    Servo - Class for manipulating servo motors connected to ESP32 pins.\n    int attach(pin )  - Attaches the given GPIO pin to the next free channel\n        (channels that have previously been detached are used first), \n        returns channel number or 0 if failure. All pin numbers are allowed,\n        but only pins 2,4,12-19,21-23,25-27,32-33 are recommended.\n    int attach(pin, min, max  ) - Attaches to a pin setting min and max \n        values in microseconds; enforced minimum min is 500, enforced max\n        is 2500. Other semantics are the same as attach().\n    void write () - Sets the servo angle in degrees; a value below 500 is\n        treated as a value in degrees (0 to 180). These limit are enforced,\n        i.e., values are constrained as follows:\n            Value                                   Becomes\n            -----                                   -------\n            < 0                                        0\n            0 - 180                                 value (treated as degrees)\n            181 - 499                                 180\n            500 - (min-1)                             min\n            min-max (from attach or default)   value (treated as microseconds)\n            (max+1) - 2500                            max\n    \n    void writeMicroseconds() - Sets the servo pulse width in microseconds.\n        min and max are enforced (see above). \n    int read() - Gets the last written servo pulse width as an angle between 0 and 180. \n    int readMicroseconds()   - Gets the last written servo pulse width in microseconds.\n    bool attached() - Returns true if this servo instance is attached to a pin. \n    void detach() - Stops an the attached servo, frees the attached pin, and frees\n        its channel for reuse. \n    \n    *** New ESP32-specific functions **\n    setTimerWidth(value) - Sets the PWM timer width (must be 16-20) (ESP32 ONLY);\n        as a side effect, the pulse width is recomputed.\n    int readTimerWidth() - Gets the PWM timer width (ESP32 ONLY) \n \nUseful Defaults:\n----------------\ndefault min pulse width for attach(): 1000us\ndefault max pulse width for attach(): 2000us\ndefault timer width 16 (if timer width is not set)\ndefault pulse width 1500us (servos are initialized with this value)\nMINIMUM pulse with: 500us\nMAXIMUM pulse with: 2500us\nMAXIMUM number of servos: 16 (this is the number of PWM channels in the ESP32)  \n"
  },
  {
    "path": "examples/Knob/Knob.ino",
    "content": "/*\n Controlling a servo position using a potentiometer (variable resistor)\n by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>\n\n modified on 8 Nov 2013\n by Scott Fitzgerald\n\n modified for the ESP32 on March 2017\n by John Bennett\n \n see  http://www.arduino.cc/en/Tutorial/Knob for a description of the original code\n\n * Different servos require different pulse widths to vary servo angle, but the range is \n * an approximately 500-2500 microsecond pulse every 20ms (50Hz). In general, hobbyist servos\n * sweep 180 degrees, so the lowest number in the published range for a particular servo\n * represents an angle of 0 degrees, the middle of the range represents 90 degrees, and the top\n * of the range represents 180 degrees. So for example, if the range is 1000us to 2000us,\n * 1000us would equal an angle of 0, 1500us would equal 90 degrees, and 2000us would equal 1800\n * degrees.\n * \n * Circuit: (using an ESP32 Thing from Sparkfun)\n * Servo motors have three wires: power, ground, and signal. The power wire is typically red,\n * the ground wire is typically black or brown, and the signal wire is typically yellow,\n * orange or white. Since the ESP32 can supply limited current at only 3.3V, and servos draw\n * considerable power, we will connect servo power to the VBat pin of the ESP32 (located\n * near the USB connector). THIS IS ONLY APPROPRIATE FOR SMALL SERVOS. \n * \n * We could also connect servo power to a separate external\n * power source (as long as we connect all of the grounds (ESP32, servo, and external power).\n * In this example, we just connect ESP32 ground to servo ground. The servo signal pins\n * connect to any available GPIO pins on the ESP32 (in this example, we use pin 18.\n * \n * In this example, we assume a Tower Pro SG90 small servo connected to VBat.\n * The published min and max for this servo are 500 and 2400, respectively.\n * These values actually drive the servos a little past 0 and 180, so\n * if you are particular, adjust the min and max values to match your needs.\n */\n\n// Include the ESP32 Arduino Servo Library instead of the original Arduino Servo Library\n#include <ESP32_Servo.h> \n\nServo myservo;  // create servo object to control a servo\n\n// Possible PWM GPIO pins on the ESP32: 0(used by on-board button),2,4,5(used by on-board LED),12-19,21-23,25-27,32-33 \nint servoPin = 18;      // GPIO pin used to connect the servo control (digital out)\n// Possible ADC pins on the ESP32: 0,2,4,12-15,32-39; 34-39 are recommended for analog input\nint potPin = 34;        // GPIO pin used to connect the potentiometer (analog in)\nint ADC_Max = 4096;     // This is the default ADC max value on the ESP32 (12 bit ADC width);\n                        // this width can be set (in low-level oode) from 9-12 bits, for a\n                        // a range of max values of 512-4096\n  \nint val;    // variable to read the value from the analog pin\n\nvoid setup()\n{\n  myservo.attach(servoPin, 500, 2400);   // attaches the servo on pin 18 to the servo object\n                                         // using SG90 servo min/max of 500us and 2400us\n                                         // for MG995 large servo, use 1000us and 2000us,\n                                         // which are the defaults, so this line could be\n                                         // \"myservo.attach(servoPin);\"\n}\n\nvoid loop() {\n  val = analogRead(potPin);            // read the value of the potentiometer (value between 0 and 1023)\n  val = map(val, 0, ADC_Max, 0, 180);     // scale it to use it with the servo (value between 0 and 180)\n  myservo.write(val);                  // set the servo position according to the scaled value\n  delay(200);                          // wait for the servo to get there\n}\n\n"
  },
  {
    "path": "examples/Multiple-Servo-Example-Arduino/Multiple-Servo-Example-Arduino.ino",
    "content": "/*\n * ESP32 Servo Example Using Arduino ESP32 Servo Library\n * John K. Bennett\n * March, 2017\n * \n * This sketch uses the Arduino ESP32 Servo Library to sweep 4 servos in sequence.\n * \n * Different servos require different pulse widths to vary servo angle, but the range is \n * an approximately 500-2500 microsecond pulse every 20ms (50Hz). In general, hobbyist servos\n * sweep 180 degrees, so the lowest number in the published range for a particular servo\n * represents an angle of 0 degrees, the middle of the range represents 90 degrees, and the top\n * of the range represents 180 degrees. So for example, if the range is 1000us to 2000us,\n * 1000us would equal an angle of 0, 1500us would equal 90 degrees, and 2000us would equal 1800\n * degrees.\n * \n * Circuit:\n * Servo motors have three wires: power, ground, and signal. The power wire is typically red,\n * the ground wire is typically black or brown, and the signal wire is typically yellow,\n * orange or white. Since the ESP32 can supply limited current at only 3.3V, and servos draw\n * considerable power, we will connect servo power to the VBat pin of the ESP32 (located\n * near the USB connector). THIS IS ONLY APPROPRIATE FOR SMALL SERVOS. \n * \n * We could also connect servo power to a separate external\n * power source (as long as we connect all of the grounds (ESP32, servo, and external power).\n * In this example, we just connect ESP32 ground to servo ground. The servo signal pins\n * connect to any available GPIO pins on the ESP32 (in this example, we use pins\n * 22, 19, 23, & 18).\n * \n * In this example, we assume four Tower Pro SG90 small servos.\n * The published min and max for this servo are 500 and 2400, respectively.\n * These values actually drive the servos a little past 0 and 180, so\n * if you are particular, adjust the min and max values to match your needs.\n * Experimentally, 550 and 2350 are pretty close to 0 and 180.\n */\n\n#include <ESP32_Servo.h>\n\n// create four servo objects \nServo servo1;\nServo servo2; \nServo servo3; \nServo servo4;  \n\n// Published values for SG90 servos; adjust if needed\nint minUs = 500;\nint maxUs = 2400;\n\n// These are all GPIO pins on the ESP32\n// Recommended pins include 2,4,12-19,21-23,25-27,32-33 \nint servo1Pin = 18;\nint servo2Pin = 19;\nint servo3Pin = 22;\nint servo4Pin = 23;\n\nint pos = 0;      // position in degrees\n\nvoid setup()\n{ \n  servo1.attach(servo1Pin, minUs, maxUs);\n  servo2.attach(servo2Pin, minUs, maxUs);\n  servo3.attach(servo3Pin, minUs, maxUs);\n  servo4.attach(servo4Pin, minUs, maxUs);        \n}\n\nvoid loop() {\n  for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees\n    // in steps of 1 degree\n    servo1.write(pos);              \n    delay(20);                       // waits 20ms for the servo to reach the position\n  }\n  for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees\n    servo1.write(pos);\n    delay(20);      \n  }\n\n  for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees\n    // in steps of 1 degree\n    servo2.write(pos);              \n    delay(20);                       // waits 20ms for the servo to reach the position\n  }\n  for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees\n    servo2.write(pos);\n    delay(20);      \n  }\n\n  for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees\n    // in steps of 1 degree\n    servo3.write(pos);              \n    delay(20);                       // waits 20ms for the servo to reach the position\n  }\n  for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees\n    servo3.write(pos);\n    delay(20);      \n  }\n\n  for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees\n    // in steps of 1 degree\n    servo4.write(pos);              \n    delay(20);                       // waits 20ms for the servo to reach the position\n  }\n  for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees\n    servo4.write(pos);\n    delay(20);      \n  }\n}\n\n"
  },
  {
    "path": "examples/Multiple-Servo-Example-ESP32/Multiple-Servo-Example-ESP32.ino",
    "content": "/*\n * ESP32 Servo Example\n * John K. Bennett\n * March, 2017\n * \n * This sketch uses low-level ESP32 PWM functionality to sweep 4 servos in sequence.\n * It does NOT use the ESP32_Servo library for Arduino.\n * \n * The ESP32 supports 16 hardware LED PWM channels that are intended\n * to be used for LED brightness control. The low level ESP32 code allows us to set the \n * PWM frequency and bit-depth, and then control them by setting bits in the relevant control\n * register. The core files esp32-hal-ledc.* provides helper functions to make this set up \n * straightforward.  \n * \n * Different servos require different pulse widths to vary servo angle, but the range is \n * an approximately 500-2500 microsecond pulse every 20ms (50Hz). In general, hobbyist servos\n * sweep 180 degrees, so the lowest number in the published range for a particular servo\n * represents an angle of 0 degrees, the middle of the range represents 90 degrees, and the top\n * of the range represents 180 degrees. So for example, if the range is 1000us to 2000us,\n * 1000us would equal an angle of 0, 1500us would equal 90 degrees, and 2000us would equal 1800\n * degrees.\n * \n * The ESP32 PWM timers allow us to set the timer width (max 20 bits). Thus\n * the timer \"tick\" length is (pulse_period/2**timer_width), and the equation for pulse_high_width\n * (the portion of cycle (20ms in our case) that the signal is high) becomes:\n * \n *                  pulse_high_width  = count * tick_length\n *                                    = count * (pulse_period/2**timer_width)    \n *                                    \n *            and count = (pulse_high_width / (pulse_period/2**timer_width))\n *                                    \n * For example, if we want a 1500us pulse_high_width, we set pulse_period to 20ms (20000us)\n * (this value is set in the ledcSetup call), and count (used in the ledcWrite call) to\n * 1500/(20000/65655), or 4924. This is the value we write to the timer in the ledcWrite call.\n *\n * As a concrete example, suppose we want to repeatedly sweep four Tower Pro SG90 servos\n * from 0 to 180 degrees.  The published pulse width range for the SG90 is 500-2400us. Thus,\n * we should vary the count used in ledcWrite from 1638 to 7864.\n * \n * Circuit:\n * Servo motors have three wires: power, ground, and signal. The power wire is typically red,\n * the ground wire is typically black or brown, and the signal wire is typically yellow,\n * orange or white. Since the ESP32 can supply limited current at only 3.3V, and servos draw\n * considerable power, we will connect servo power to the VBat pin of the ESP32 (located\n * near the USB connector). THIS IS ONLY APPROPRIATE FOR SMALL SERVOS. \n * \n * We could also connect servo power to a separate external\n * power source (as long as we connect all of the grounds (ESP32, servo, and external power).\n * In this example, we just connect ESP32 ground to servo ground. The servo signal pins\n * connect to any available GPIO pins on the ESP32 (in this example, we use pins\n * 22, 19, 23, & 18).\n * \n * In this example, we assume four Tower Pro SG90 small servos.\n * The published min and max for this servo are 500 and 2400, respectively.\n * These values actually drive the servos a little past 0 and 180, so\n * if you are particular, adjust the min and max values to match your needs.\n * Experimentally, 550us and 2350us are pretty close to 0 and 180.\n * \n * This code was inspired by a post on Hackaday by Elliot Williams.\n */\n\n // Values for TowerPro SG90 small servos; adjust if needed\n #define COUNT_LOW 1638\n #define COUNT_HIGH 7864\n\n #define TIMER_WIDTH 16\n\n#include \"esp32-hal-ledc.h\"\n\nvoid setup() {\n   ledcSetup(1, 50, TIMER_WIDTH); // channel 1, 50 Hz, 16-bit width\n   ledcAttachPin(22, 1);   // GPIO 22 assigned to channel 1\n   \n   ledcSetup(2, 50, TIMER_WIDTH); // channel 2, 50 Hz, 16-bit width\n   ledcAttachPin(19, 2);   // GPIO 19 assigned to channel 2\n   \n   ledcSetup(3, 50, TIMER_WIDTH); // channel 3, 50 Hz, 16-bit width\n   ledcAttachPin(23, 3);   // GPIO 23 assigned to channel 3\n   \n   ledcSetup(4, 50, TIMER_WIDTH); // channel 4, 50 Hz, 16-bit width\n   ledcAttachPin(18, 4);   // GPIO 18 assigned to channel 4\n}\n\nvoid loop() {\n   for (int i=COUNT_LOW ; i < COUNT_HIGH ; i=i+100)\n   {\n      ledcWrite(1, i);       // sweep servo 1\n      delay(200);\n   }\n    \n   for (int i=COUNT_LOW ; i < COUNT_HIGH ; i=i+100)\n   {\n      ledcWrite(2, i);       // sweep servo 2\n      delay(200);\n   }\n\n   for (int i=COUNT_LOW ; i < COUNT_HIGH ; i=i+100)\n   {\n      ledcWrite(3, i);       // sweep the servo\n      delay(200);\n   }\n\n   for (int i=COUNT_LOW ; i < COUNT_HIGH ; i=i+100)\n    {\n      ledcWrite(4, i);       // sweep the servo\n      delay(200);\n    }\n}\n\n"
  },
  {
    "path": "examples/Sweep/Sweep.ino",
    "content": "/* Sweep\n by BARRAGAN <http://barraganstudio.com>\n This example code is in the public domain.\n\n modified 8 Nov 2013\n by Scott Fitzgerald\n\n modified for the ESP32 on March 2017\n by John Bennett\n\n see http://www.arduino.cc/en/Tutorial/Sweep for a description of the original code\n\n * Different servos require different pulse widths to vary servo angle, but the range is \n * an approximately 500-2500 microsecond pulse every 20ms (50Hz). In general, hobbyist servos\n * sweep 180 degrees, so the lowest number in the published range for a particular servo\n * represents an angle of 0 degrees, the middle of the range represents 90 degrees, and the top\n * of the range represents 180 degrees. So for example, if the range is 1000us to 2000us,\n * 1000us would equal an angle of 0, 1500us would equal 90 degrees, and 2000us would equal 1800\n * degrees.\n * \n * Circuit: (using an ESP32 Thing from Sparkfun)\n * Servo motors have three wires: power, ground, and signal. The power wire is typically red,\n * the ground wire is typically black or brown, and the signal wire is typically yellow,\n * orange or white. Since the ESP32 can supply limited current at only 3.3V, and servos draw\n * considerable power, we will connect servo power to the VBat pin of the ESP32 (located\n * near the USB connector). THIS IS ONLY APPROPRIATE FOR SMALL SERVOS. \n * \n * We could also connect servo power to a separate external\n * power source (as long as we connect all of the grounds (ESP32, servo, and external power).\n * In this example, we just connect ESP32 ground to servo ground. The servo signal pins\n * connect to any available GPIO pins on the ESP32 (in this example, we use pin 18.\n * \n * In this example, we assume a Tower Pro MG995 large servo connected to an external power source.\n * The published min and max for this servo is 1000 and 2000, respectively, so the defaults are fine.\n * These values actually drive the servos a little past 0 and 180, so\n * if you are particular, adjust the min and max values to match your needs.\n */\n\n#include <ESP32_Servo.h>\n\nServo myservo;  // create servo object to control a servo\n                // 16 servo objects can be created on the ESP32\n\nint pos = 0;    // variable to store the servo position\n// Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27,32-33 \nint servoPin = 18;\n\nvoid setup() {\n  myservo.attach(servoPin);   // attaches the servo on pin 18 to the servo object\n                              // using default min/max of 1000us and 2000us\n                              // different servos may require different min/max settings\n                              // for an accurate 0 to 180 sweep\n}\n\nvoid loop() {\n  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees\n    // in steps of 1 degree\n    myservo.write(pos);              // tell servo to go to position in variable 'pos'\n    delay(15);                       // waits 15ms for the servo to reach the position\n  }\n  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees\n    myservo.write(pos);              // tell servo to go to position in variable 'pos'\n    delay(15);                       // waits 15ms for the servo to reach the position\n  }\n}\n\n"
  },
  {
    "path": "keywords.txt",
    "content": "#######################################\n# Syntax Coloring Map ESP32_Servo\n#######################################\n\n#######################################\n# Datatypes (KEYWORD1)\n#######################################\n\nServo\tKEYWORD1\n\n#######################################\n# Methods and Functions (KEYWORD2)\n#######################################\nattach\tKEYWORD2\ndetach\tKEYWORD2\nwrite\tKEYWORD2\nread\tKEYWORD2\nattached\tKEYWORD2\nwriteMicroseconds\tKEYWORD2\nreadMicroseconds\tKEYWORD2\nsetTimerWidth \t\tKEYWORD2\nreadTimerWidth\t\tKEYWORD2\n\n#######################################\n# Constants (LITERAL1)\n#######################################"
  },
  {
    "path": "library.properties",
    "content": "name=ESP32_Servo\nversion=1.0\nauthor=John K. Bennett\nmaintainer=John K. Bennett <jkb@colorado.edu>\nsentence=Allows ESP32 boards to control servo motors using Arduino semantics. \nparagraph=This library can control a many types of servos.<br />It makes use of the ESP32 PWM timers: the library can control up to 16 servos on individual channels<br />No attempt has been made to support multiple servos per channel.<br />\ncategory=Device Control\nurl=http://www.arduino.cc/en/Reference/Servo\narchitectures=esp32\n"
  },
  {
    "path": "src/ESP32_Servo.cpp",
    "content": "/*\nCopyright (c) 2017 John K. Bennett. All right reserved.\n\nThis library is free software; you can redistribute it and/or\nmodify it under the terms of the GNU Lesser General Public\nLicense as published by the Free Software Foundation; either\nversion 2.1 of the License, or (at your option) any later version.\n\nThis library is distributed in the hope that it will be useful,\nbut WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\nLesser General Public License for more details.\n\nYou should have received a copy of the GNU Lesser General Public\nLicense along with this library; if not, write to the Free Software\nFoundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n\n* Notes on the implementation:\n* The ESP32 supports 16 hardware LED PWM channels that are intended\n* to be used for LED brightness control. The low level ESP32 code\n* (esp32-hal-ledc.*) allows us to set the PWM frequency and bit-depth,\n* and then manipulate them by setting bits in the relevant control\n* registers.\n*\n* Different servos require different pulse widths to vary servo angle, but the range is\n* an approximately 500-2500 microsecond pulse every 20ms (50Hz). In general, hobbyist servos\n* sweep 180 degrees, so the lowest number in the published range for a particular servo\n* represents an angle of 0 degrees, the middle of the range represents 90 degrees, and the top\n* of the range represents 180 degrees. So for example, if the range is 1000us to 2000us,\n* 1000us would equal an angle of 0, 1500us would equal 90 degrees, and 2000us would equal 180\n* degrees. We vary pulse width (recall that the pulse period is already set to 20ms) as follows:\n*\n* The ESP32 PWM timers allow us to set the timer width (max 20 bits). Thus\n* the timer \"tick\" length is (pulse_period/2**timer_width), and the equation for pulse_high_width\n* (the portion of the 20ms cycle that the signal is high) becomes:\n*\n*                  pulse_high_width  = count * tick_length\n*                                    = count * (pulse_period/2**timer_width)\n*\n*            and   count = (pulse_high_width / (pulse_period/2**timer_width))\n*\n* So, for example, if I want a 1500us pulse_high_width, I set pulse_period to 20ms (20000us)\n* (this value is set in the ledcSetup call), and count (used in the ledcWrite call) to\n* 1500/(20000/65536), or 4924. This is the value we write to the timer in the ledcWrite call.\n* If we increase the timer_width, the timer_count values need to be adjusted.\n*\n* The servo signal pins connect to any available GPIO pins on the ESP32, but not all pins are\n* GPIO pins.\n*\n* The ESP32 is a 32 bit processor that includes FP support; this code reflects that fact.\n*/\n\n#include \"ESP32_Servo.h\"\n#include \"esp32-hal-ledc.h\"\n#include \"Arduino.h\"\n\n// initialize the class variable ServoCount\nint Servo::ServoCount = 0;\n\n// The ChannelUsed array elements are 0 if never used, 1 if in use, and -1 if used and disposed\n// (i.e., available for reuse)\nint Servo::ChannelUsed[MAX_SERVOS+1] = {0}; // we ignore the zeroth element\n\nServo::Servo()\n{\n    this->servoChannel = 0;\n    // see if there is a servo channel available for reuse\n    bool foundChannelForReuse = false;\n    for (int i = 1; i < MAX_SERVOS+1; i++)\n    {\n        if (ChannelUsed[i] == -1)\n        {\n            // reclaim this channel\n            ChannelUsed[i] = 1;\n            this->servoChannel = i;\n            foundChannelForReuse = true;\n            break;\n        }\n    }\n    if (!foundChannelForReuse)\n    {\n        // no channels available for reuse; get a new one if we can\n        if (ServoCount < MAX_SERVOS)\n        {\n            this->servoChannel = ++ServoCount;   // assign a servo channel number to this instance\n            ChannelUsed[this->servoChannel] = 1;\n        }\n        else \n        {\n            this->servoChannel = 0;  // too many servos in use\n        }\n    }\n    // if we got a channel either way, finish initializing it\n    if (this->servoChannel > 0)\n    {            \n        // initialize this channel with plausible values, except pin # (we set pin # when attached)\n        this->ticks = DEFAULT_PULSE_WIDTH_TICKS;   \n        this->timer_width = DEFAULT_TIMER_WIDTH;\n        this->pinNumber = -1;     // make it clear that we haven't attached a pin to this channel \n        this->min = DEFAULT_uS_LOW;\n        this->max = DEFAULT_uS_HIGH;\n        this->timer_width_ticks = pow(2,this->timer_width);\n    }\n}\n\nint Servo::attach(int pin)\n{\n    return (this->attach(pin, DEFAULT_uS_LOW, DEFAULT_uS_HIGH));\n}\n\nint Servo::attach(int pin, int min, int max)\n{    \n    if ((this->servoChannel <= MAX_SERVOS) && (this->servoChannel > 0))\n    { \n        // Recommend only the following pins 2,4,12-19,21-23,25-27,32-33 (enforcement commented out)\n        //if ((pin == 2) || (pin ==4) || ((pin >= 12) && (pin <= 19)) || ((pin >= 21) && (pin <= 23)) ||\n        //        ((pin >= 25) && (pin <= 27)) || (pin == 32) || (pin == 33))\n        //{\n            // OK to proceed; first check for new/reuse\n            if (this->pinNumber < 0) // we are attaching to a new or previously detached pin; we need to initialize/reinitialize\n            {\n                // claim/reclaim this channel\n                ChannelUsed[this->servoChannel] = 1;\n                this->ticks = DEFAULT_PULSE_WIDTH_TICKS;\n                this->timer_width = DEFAULT_TIMER_WIDTH;\n                this->timer_width_ticks = pow(2,this->timer_width);\n            }\n            this->pinNumber = pin;\n        //}\n        //else\n        //{\n        //    return 0;\n        //}\n\n        // min/max checks \n        if (min < MIN_PULSE_WIDTH)          // ensure pulse width is valid\n            min = MIN_PULSE_WIDTH;\n        if (max > MAX_PULSE_WIDTH)\n            max = MAX_PULSE_WIDTH;\n        this->min = min;     //store this value in uS\n        this->max = max;    //store this value in uS\n        // Set up this channel\n        // if you want anything other than default timer width, you must call setTimerWidth() before attach\n        ledcSetup(this->servoChannel, REFRESH_CPS, this->timer_width); // channel #, 50 Hz, timer width\n        ledcAttachPin(this->pinNumber, this->servoChannel);   // GPIO pin assigned to channel        \n    }\n    else return 0;  \n}\n\nvoid Servo::detach()\n{\n    if (this->attached())\n    {\n        ledcDetachPin(this->pinNumber);\n        //keep track of detached servos channels so we can reuse them if needed\n        ChannelUsed[this->servoChannel] = -1;\n        this->pinNumber = -1;\n    }\n}\n\nvoid Servo::write(int value)\n{\n    // treat values less than MIN_PULSE_WIDTH (500) as angles in degrees (valid values in microseconds are handled as microseconds)\n    if (value < MIN_PULSE_WIDTH)\n    {\n        if (value < 0)\n            value = 0;\n        else if (value > 180)\n            value = 180;\n\n        value = map(value, 0, 180, this->min, this->max);\n    }\n    this->writeMicroseconds(value);\n}\n\nvoid Servo::writeMicroseconds(int value)\n{\n    // calculate and store the values for the given channel\n    if ((this->servoChannel <= MAX_SERVOS) && (this->attached()))   // ensure channel is valid\n    {\n        if (value < this->min)          // ensure pulse width is valid\n            value = this->min;\n        else if (value > this->max)\n            value = this->max;\n\n        value = usToTicks(value);  // convert to ticks\n        this->ticks = value;\n        // do the actual write\n        ledcWrite(this->servoChannel, this->ticks);\n    }\n}\n\nint Servo::read() // return the value as degrees\n{\n    return (map(readMicroseconds()+1, this->min, this->max, 0, 180));\n}\n\nint Servo::readMicroseconds()\n{\n    int pulsewidthUsec;\n    if ((this->servoChannel <= MAX_SERVOS) && (this->attached()))\n    { \n        pulsewidthUsec = ticksToUs(this->ticks);\n    }\n    else\n    {\n        pulsewidthUsec = 0;\n    }\n\n    return (pulsewidthUsec);\n}\n\nbool Servo::attached()\n{\n    return (ChannelUsed[this->servoChannel]);\n}\n\nvoid Servo::setTimerWidth(int value)\n{\n    // only allow values between 16 and 20\n    if (value < 16)\n        value = 16;\n    else if (value > 20)\n        value = 20;\n        \n    // Fix the current ticks value after timer width change\n    // The user can reset the tick value with a write() or writeUs()\n    int widthDifference = this->timer_width - value;\n    // if positive multiply by diff; if neg, divide\n    if (widthDifference > 0)\n    {\n        this->ticks << widthDifference;\n    }\n    else\n    {\n        this->ticks >> widthDifference;\n    }\n    \n    this->timer_width = value;\n    this->timer_width_ticks = pow(2,this->timer_width);\n    \n    // If this is an attached servo, clean up\n    if ((this->servoChannel <= MAX_SERVOS) && (this->attached()))\n    {\n        // detach, setup and attach again to reflect new timer width\n        ledcDetachPin(this->pinNumber);\n        ledcSetup(this->servoChannel, REFRESH_CPS, this->timer_width);\n        ledcAttachPin(this->pinNumber, this->servoChannel);\n    }        \n}\n\nint Servo::readTimerWidth()\n{\n    return (this->timer_width);\n}\n\nint Servo::usToTicks(int usec)\n{\n    return (int)((float)usec / ((float)REFRESH_USEC / (float)this->timer_width_ticks));   \n}\n\nint Servo::ticksToUs(int ticks)\n{\n    return (int)((float)ticks * ((float)REFRESH_USEC / (float)this->timer_width_ticks)); \n}\n\n "
  },
  {
    "path": "src/ESP32_Servo.h",
    "content": "/*\n  Copyright (c) 2017 John K. Bennett. All right reserved.\n  \n  ESP32_Servo.h - Servo library for ESP32 - Version 1\n  \n  Original Servo.h written by Michael Margolis in 2009\n  \n  This library is free software; you can redistribute it and/or\n  modify it under the terms of the GNU Lesser General Public\n  License as published by the Free Software Foundation; either\n  version 2.1 of the License, or (at your option) any later version.\n\n  This library is distributed in the hope that it will be useful,\n  but WITHOUT ANY WARRANTY; without even the implied warranty of\n  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n  Lesser General Public License for more details.\n\n  You should have received a copy of the GNU Lesser General Public\n  License along with this library; if not, write to the Free Software\n  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\n*/\n\n/* \n  A servo is activated by creating an instance of the Servo class, and passing \n  the desired GPIO pin to the attach() method.\n  The servos are pulsed in the background using the value most recently \n  written using the write() method.\n\n  The class methods are:\n\n    Servo - Class for manipulating servo motors connected to ESP32 pins.\n    int attach(pin )  - Attaches the given GPIO pin to the next free channel\n        (channels that have previously been detached are used first), \n        returns channel number or 0 if failure. All pin numbers are allowed,\n        but only pins 2,4,12-19,21-23,25-27,32-33 are recommended.\n    int attach(pin, min, max  ) - Attaches to a pin setting min and max \n        values in microseconds; enforced minimum min is 500, enforced max\n        is 2500. Other semantics same as attach().\n    void write () - Sets the servo angle in degrees; a value below 500 is\n        treated as a value in degrees (0 to 180). These limit are enforced,\n        i.e., values are treated as follows:\n            Value                                   Becomes\n            -----                                   -------\n            < 0                                        0\n            0 - 180                             value (treated as degrees)\n            181 - 499                                 180\n            500 - (min-1)                             min\n            min-max (from attach or default)    value (treated as microseconds)\n            (max+1) - 2500                            max\n    \n    void writeMicroseconds() - Sets the servo pulse width in microseconds.\n        min and max are enforced (see above). \n    int read() - Gets the last written servo pulse width as an angle between 0 and 180. \n    int readMicroseconds()   - Gets the last written servo pulse width in microseconds.\n    bool attached() - Returns true if this servo instance is attached. \n    void detach() - Stops an the attached servo, frees its attached pin, and frees\n        its channel for reuse). \n    \n    *** ESP32-specific functions **\n    setTimerWidth(value) - Sets the PWM timer width (must be 16-20) (ESP32 ONLY);\n        as a side effect, the pulse width is recomputed.\n    int readTimerWidth() - Gets the PWM timer width (ESP32 ONLY) \n */\n \n#ifndef ESP32_Servo_h\n#define ESP32_Servo_h\n\n// Values for TowerPro MG995 large servos (and many other hobbyist servos)\n#define DEFAULT_uS_LOW 1000        // 1000us\n#define DEFAULT_uS_HIGH 2000      // 2000us\n\n// Values for TowerPro SG90 small servos\n//#define DEFAULT_uS_LOW 400\n//#define DEFAULT_uS_HIGH 2400\n\n#define DEFAULT_TIMER_WIDTH 16\n#define DEFAULT_TIMER_WIDTH_TICKS 65536\n\n#define ESP32_Servo_VERSION           1     // software version of this library\n\n#define MIN_PULSE_WIDTH       500     // the shortest pulse sent to a servo  \n#define MAX_PULSE_WIDTH      2500     // the longest pulse sent to a servo \n#define DEFAULT_PULSE_WIDTH  1500     // default pulse width when servo is attached\n#define DEFAULT_PULSE_WIDTH_TICKS 4825\n#define REFRESH_CPS            50\n#define REFRESH_USEC         20000\n\n#define MAX_SERVOS              16     // no. of PWM channels in ESP32\n\n/*\n* This group/channel/timmer mapping is for information only;\n* the details are handled by lower-level code\n*\n* LEDC Chan to Group/Channel/Timer Mapping\n** ledc: 0  => Group: 0, Channel: 0, Timer: 0\n** ledc: 1  => Group: 0, Channel: 1, Timer: 0\n** ledc: 2  => Group: 0, Channel: 2, Timer: 1\n** ledc: 3  => Group: 0, Channel: 3, Timer: 1\n** ledc: 4  => Group: 0, Channel: 4, Timer: 2\n** ledc: 5  => Group: 0, Channel: 5, Timer: 2\n** ledc: 6  => Group: 0, Channel: 6, Timer: 3\n** ledc: 7  => Group: 0, Channel: 7, Timer: 3\n** ledc: 8  => Group: 1, Channel: 0, Timer: 0\n** ledc: 9  => Group: 1, Channel: 1, Timer: 0\n** ledc: 10 => Group: 1, Channel: 2, Timer: 1\n** ledc: 11 => Group: 1, Channel: 3, Timer: 1\n** ledc: 12 => Group: 1, Channel: 4, Timer: 2\n** ledc: 13 => Group: 1, Channel: 5, Timer: 2\n** ledc: 14 => Group: 1, Channel: 6, Timer: 3\n** ledc: 15 => Group: 1, Channel: 7, Timer: 3\n*/\n\nclass Servo\n{\npublic:\n  Servo();\n  // Arduino Servo Library calls\n  int attach(int pin);                   // attach the given pin to the next free channel, returns channel number or 0 if failure\n  int attach(int pin, int min, int max); // as above but also sets min and max values for writes. \n  void detach();\n  void write(int value);                 // if value is < MIN_PULSE_WIDTH its treated as an angle, otherwise as pulse width in microseconds \n  void writeMicroseconds(int value);     // Write pulse width in microseconds \n  int read();                            // returns current pulse width as an angle between 0 and 180 degrees\n  int readMicroseconds();                // returns current pulse width in microseconds for this servo\n  bool attached();                       // return true if this servo is attached, otherwise false  \n  \n  // ESP32 only functions\n  void setTimerWidth(int value);     // set the PWM timer width (ESP32 ONLY)\n  int readTimerWidth();              // get the PWM timer width (ESP32 ONLY)  \n\n  private: \n   int usToTicks(int usec);\n   int ticksToUs(int ticks);\n   static int ServoCount;                             // the total number of attached servos\n   static int ChannelUsed[];                          // used to track whether a channel is in service\n   int servoChannel = 0;                              // channel number for this servo\n   int min = DEFAULT_uS_LOW;                          // minimum pulse width for this servo   \n   int max = DEFAULT_uS_HIGH;                         // maximum pulse width for this servo \n   int pinNumber = 0;                                 // GPIO pin assigned to this channel\n   int timer_width = DEFAULT_TIMER_WIDTH;             // ESP32 allows variable width PWM timers\n   int ticks = DEFAULT_PULSE_WIDTH_TICKS;             // current pulse width on this channel\n   int timer_width_ticks = DEFAULT_TIMER_WIDTH_TICKS; // no. of ticks at rollover; varies with width\n};\n#endif\n"
  }
]