|
| 1 | +/* |
| 2 | + Copyright (c) 2015 Arduino LLC. All right reserved. |
| 3 | + Copyright (c) 2016 Thomas Roell. 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 | +#include <Arduino.h> |
| 21 | +#include <Servo.h> |
| 22 | + |
| 23 | +#include "wiring_private.h" |
| 24 | + |
| 25 | +#define INVALID_SERVO 255 |
| 26 | + |
| 27 | +static stm32l4_servo_t stm32l4_servo; |
| 28 | + |
| 29 | +static uint8_t ServoAttached = 0; |
| 30 | +static volatile bool ServoSync = false; |
| 31 | +static stm32l4_servo_table_t ServoTable; |
| 32 | + |
| 33 | +static void servo_event_callback(void *context, uint32_t events) |
| 34 | +{ |
| 35 | + if (ServoSync) |
| 36 | + { |
| 37 | + ServoSync = 0; |
| 38 | + |
| 39 | + stm32l4_servo_configure(&stm32l4_servo, &ServoTable); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +Servo::Servo() |
| 44 | +{ |
| 45 | + uint8_t servoIndex; |
| 46 | + |
| 47 | + if (stm32l4_servo.state == SERVO_STATE_NONE) { |
| 48 | + stm32l4_servo_create(&stm32l4_servo, TIMER_INSTANCE_TIM15, STM32L4_SERVO_IRQ_PRIORITY); |
| 49 | + } |
| 50 | + |
| 51 | + if (ServoTable.entries < MAX_SERVOS) { |
| 52 | + this->servoIndex = ServoTable.entries; |
| 53 | + |
| 54 | + ServoTable.slot[this->servoIndex].pin = GPIO_PIN_NONE; |
| 55 | + ServoTable.slot[this->servoIndex].width = DEFAULT_PULSE_WIDTH; |
| 56 | + |
| 57 | + ServoTable.entries++; |
| 58 | + } else { |
| 59 | + this->servoIndex = INVALID_SERVO; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +uint8_t Servo::attach(int pin) |
| 64 | +{ |
| 65 | + return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH); |
| 66 | +} |
| 67 | + |
| 68 | +uint8_t Servo::attach(int pin, int min, int max) |
| 69 | +{ |
| 70 | + if (g_APinDescription[pin].GPIO == NULL) { |
| 71 | + return INVALID_SERVO; |
| 72 | + } |
| 73 | + |
| 74 | + if (this->servoIndex >= MAX_SERVOS) { |
| 75 | + return INVALID_SERVO; |
| 76 | + } |
| 77 | + |
| 78 | + if (ServoTable.slot[this->servoIndex].pin == GPIO_PIN_NONE) { |
| 79 | + ServoAttached++; |
| 80 | + } |
| 81 | + |
| 82 | + pinMode(pin, OUTPUT); |
| 83 | + |
| 84 | + ServoTable.slot[this->servoIndex].pin = g_APinDescription[pin].pin; |
| 85 | + |
| 86 | + this->min = min; |
| 87 | + this->max = max; |
| 88 | + |
| 89 | + if (stm32l4_servo.state < SERVO_STATE_READY) { |
| 90 | + stm32l4_servo_enable(&stm32l4_servo, &ServoTable, servo_event_callback, NULL, SERVO_EVENT_SYNC); |
| 91 | + } else { |
| 92 | + if (ServoAttached == 1) { |
| 93 | + stm32l4_servo_configure(&stm32l4_servo, &ServoTable); |
| 94 | + } else { |
| 95 | + ServoSync = true; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return this->servoIndex; |
| 100 | +} |
| 101 | + |
| 102 | +void Servo::detach() |
| 103 | +{ |
| 104 | + if (this->servoIndex >= MAX_SERVOS) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + if (ServoTable.slot[this->servoIndex].pin != GPIO_PIN_NONE) { |
| 109 | + ServoAttached--; |
| 110 | + } |
| 111 | + |
| 112 | + ServoTable.slot[this->servoIndex].pin = GPIO_PIN_NONE; // store default values |
| 113 | + |
| 114 | + ServoSync = true; |
| 115 | +} |
| 116 | + |
| 117 | +void Servo::write(int angle) |
| 118 | +{ |
| 119 | + int width; |
| 120 | + |
| 121 | + if (angle < 200) { |
| 122 | + if (angle < 0) { |
| 123 | + angle = 0; |
| 124 | + } |
| 125 | + |
| 126 | + if (angle > 180) { |
| 127 | + angle = 180; |
| 128 | + } |
| 129 | + |
| 130 | + width = map(angle, 0, 180, this->min, this->max); |
| 131 | + } else { |
| 132 | + width = angle; |
| 133 | + } |
| 134 | + |
| 135 | + writeMicroseconds(width); |
| 136 | +} |
| 137 | + |
| 138 | +void Servo::writeMicroseconds(int width) |
| 139 | +{ |
| 140 | + if (this->servoIndex >= MAX_SERVOS) { |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + if (width < SERVO_PULSE_MIN) { |
| 145 | + width = SERVO_PULSE_MIN; |
| 146 | + } |
| 147 | + |
| 148 | + if (width > SERVO_PULSE_MAX) { |
| 149 | + width = SERVO_PULSE_MAX; |
| 150 | + } |
| 151 | + |
| 152 | + ServoTable.slot[this->servoIndex].width = width; |
| 153 | + |
| 154 | + ServoSync = true; |
| 155 | +} |
| 156 | + |
| 157 | +int Servo::read() // return the value as degrees |
| 158 | +{ |
| 159 | + return map(readMicroseconds()+1, this->min, this->max, 0, 180); |
| 160 | +} |
| 161 | + |
| 162 | +int Servo::readMicroseconds() |
| 163 | +{ |
| 164 | + if (this->servoIndex >= MAX_SERVOS) { |
| 165 | + return 0; |
| 166 | + } |
| 167 | + |
| 168 | + return ServoTable.slot[this->servoIndex].width; |
| 169 | +} |
| 170 | + |
| 171 | +bool Servo::attached() |
| 172 | +{ |
| 173 | + return (ServoTable.slot[this->servoIndex].pin != GPIO_PIN_NONE); |
| 174 | +} |
0 commit comments