|
| 1 | +#if defined(ARDUINO_ARCH_ESP32) |
| 2 | + |
| 3 | +#include <Arduino.h> |
| 4 | +#include <Servo.h> |
| 5 | + |
| 6 | +#if defined __has_include |
| 7 | +# if __has_include ("pinDefinitions.h") |
| 8 | +# include "pinDefinitions.h" |
| 9 | +# endif |
| 10 | +#endif |
| 11 | + |
| 12 | +/* |
| 13 | + * This group/channel/timmer mapping is for information only; |
| 14 | + * the details are handled by lower-level code |
| 15 | + * |
| 16 | + * LEDC Chan to Group/Channel/Timer Mapping |
| 17 | + ** ledc: 0 => Group: 0, Channel: 0, Timer: 0 |
| 18 | + ** ledc: 1 => Group: 0, Channel: 1, Timer: 0 |
| 19 | + ** ledc: 2 => Group: 0, Channel: 2, Timer: 1 |
| 20 | + ** ledc: 3 => Group: 0, Channel: 3, Timer: 1 |
| 21 | + ** ledc: 4 => Group: 0, Channel: 4, Timer: 2 |
| 22 | + ** ledc: 5 => Group: 0, Channel: 5, Timer: 2 |
| 23 | + ** ledc: 6 => Group: 0, Channel: 6, Timer: 3 |
| 24 | + ** ledc: 7 => Group: 0, Channel: 7, Timer: 3 |
| 25 | + ** ledc: 8 => Group: 1, Channel: 0, Timer: 0 |
| 26 | + ** ledc: 9 => Group: 1, Channel: 1, Timer: 0 |
| 27 | + ** ledc: 10 => Group: 1, Channel: 2, Timer: 1 |
| 28 | + ** ledc: 11 => Group: 1, Channel: 3, Timer: 1 |
| 29 | + ** ledc: 12 => Group: 1, Channel: 4, Timer: 2 |
| 30 | + ** ledc: 13 => Group: 1, Channel: 5, Timer: 2 |
| 31 | + ** ledc: 14 => Group: 1, Channel: 6, Timer: 3 |
| 32 | + ** ledc: 15 => Group: 1, Channel: 7, Timer: 3 |
| 33 | + */ |
| 34 | + |
| 35 | +class ServoImpl { |
| 36 | + uint8_t pin; |
| 37 | + |
| 38 | +public: |
| 39 | + ServoImpl(const uint8_t _pin, const uint8_t _channel) : pin(_pin) { |
| 40 | + // Setup timer |
| 41 | + ledcSetup(_channel, (1000000 / REFRESH_INTERVAL), LEDC_MAX_BIT_WIDTH); |
| 42 | + |
| 43 | + // Attach timer to a LED pin |
| 44 | + ledcAttachPin(pin, _channel); |
| 45 | + } |
| 46 | + |
| 47 | + ~ServoImpl() { |
| 48 | + ledcDetachPin(pin); |
| 49 | + } |
| 50 | + |
| 51 | + void set(const uint8_t _channel, const uint32_t duration_us) { |
| 52 | + ledcWrite(_channel, LEDC_US_TO_TICKS(duration_us)); |
| 53 | + } |
| 54 | + |
| 55 | + uint32_t get(const uint8_t _channel) const { |
| 56 | + return LEDC_TICKS_TO_US(ledcRead(_channel)); |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +static ServoImpl* servos[MAX_PWM_SERVOS] = {nullptr}; // static array of servo structures |
| 61 | +uint8_t ServoCount = 0; // the total number of attached servos |
| 62 | + |
| 63 | +#define SERVO_MIN() (MIN_PULSE_WIDTH - this->min) // minimum value in us for this servo |
| 64 | +#define SERVO_MAX() (MAX_PULSE_WIDTH - this->max) // maximum value in us for this servo |
| 65 | + |
| 66 | +Servo::Servo() |
| 67 | +{ |
| 68 | + if (ServoCount < MAX_PWM_SERVOS) { |
| 69 | + this->servoIndex = ServoCount++; |
| 70 | + } else { |
| 71 | + this->servoIndex = INVALID_SERVO; // too many servos |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +uint8_t Servo::attach(int pin) |
| 76 | +{ |
| 77 | + return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH); |
| 78 | +} |
| 79 | + |
| 80 | +uint8_t Servo::attach(int pin, int min, int max) |
| 81 | +{ |
| 82 | + servos[this->servoIndex] = new ServoImpl(pin, this->servoIndex); |
| 83 | + |
| 84 | + this->min = (MIN_PULSE_WIDTH - min); |
| 85 | + this->max = (MAX_PULSE_WIDTH - max); |
| 86 | + return this->servoIndex; |
| 87 | +} |
| 88 | + |
| 89 | +void Servo::detach() |
| 90 | +{ |
| 91 | + delete servos[this->servoIndex]; |
| 92 | + servos[this->servoIndex] = NULL; |
| 93 | +} |
| 94 | + |
| 95 | +void Servo::write(int value) |
| 96 | +{ |
| 97 | + // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds) |
| 98 | + if (value < MIN_PULSE_WIDTH) |
| 99 | + { |
| 100 | + if (value < 0) |
| 101 | + value = 0; |
| 102 | + else if (value > 180) |
| 103 | + value = 180; |
| 104 | + |
| 105 | + value = map(value, 0, 180, SERVO_MIN(), SERVO_MAX()); |
| 106 | + } |
| 107 | + writeMicroseconds(value); |
| 108 | +} |
| 109 | + |
| 110 | +void Servo::writeMicroseconds(int value) |
| 111 | +{ |
| 112 | + if (!servos[this->servoIndex]) { |
| 113 | + return; |
| 114 | + } |
| 115 | + // calculate and store the values for the given channel |
| 116 | + byte channel = this->servoIndex; |
| 117 | + if( (channel < MAX_PWM_SERVOS) ) // ensure channel is valid |
| 118 | + { |
| 119 | + if (value < SERVO_MIN()) // ensure pulse width is valid |
| 120 | + value = SERVO_MIN(); |
| 121 | + else if (value > SERVO_MAX()) |
| 122 | + value = SERVO_MAX(); |
| 123 | + |
| 124 | + servos[this->servoIndex]->set(this->servoIndex, value); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +int Servo::read() // return the value as degrees |
| 129 | +{ |
| 130 | + return map(readMicroseconds(), SERVO_MIN(), SERVO_MAX(), 0, 180); |
| 131 | +} |
| 132 | + |
| 133 | +int Servo::readMicroseconds() |
| 134 | +{ |
| 135 | + if (!servos[this->servoIndex]) { |
| 136 | + return 0; |
| 137 | + } |
| 138 | + return servos[this->servoIndex]->get(this->servoIndex); |
| 139 | +} |
| 140 | + |
| 141 | +bool Servo::attached() |
| 142 | +{ |
| 143 | + return servos[this->servoIndex] != NULL; |
| 144 | +} |
| 145 | + |
| 146 | +#endif |
0 commit comments