|
@@ -35,7 +35,7 @@
|
35
|
35
|
|
36
|
36
|
write() - Sets the servo angle in degrees. (invalid angle that is valid as pulse in microseconds is treated as microseconds)
|
37
|
37
|
writeMicroseconds() - Sets the servo pulse width in microseconds
|
38
|
|
- move(pin, angel) - Sequence of attach(pin), write(angel).
|
|
38
|
+ move(pin, angle) - Sequence of attach(pin), write(angle).
|
39
|
39
|
With DEACTIVATE_SERVOS_AFTER_MOVE it waits SERVO_DEACTIVATION_DELAY and detaches.
|
40
|
40
|
read() - Gets the last written servo pulse width as an angle between 0 and 180.
|
41
|
41
|
readMicroseconds() - Gets the last written servo pulse width in microseconds. (was read_us() in first release)
|
|
@@ -238,23 +238,26 @@ Servo::Servo() {
|
238
|
238
|
this->servoIndex = INVALID_SERVO; // too many servos
|
239
|
239
|
}
|
240
|
240
|
|
241
|
|
-uint8_t Servo::attach(int pin) {
|
|
241
|
+int8_t Servo::attach(int pin) {
|
242
|
242
|
return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
|
243
|
243
|
}
|
244
|
244
|
|
245
|
|
-uint8_t Servo::attach(int pin, int min, int max) {
|
246
|
|
- if (this->servoIndex < MAX_SERVOS ) {
|
247
|
|
- if(pin > 0)
|
248
|
|
- servos[this->servoIndex].Pin.nbr = pin;
|
249
|
|
- pinMode(servos[this->servoIndex].Pin.nbr, OUTPUT); // set servo pin to output
|
250
|
|
- // todo min/max check: abs(min - MIN_PULSE_WIDTH) /4 < 128
|
251
|
|
- this->min = (MIN_PULSE_WIDTH - min) / 4; //resolution of min/max is 4 uS
|
252
|
|
- this->max = (MAX_PULSE_WIDTH - max) / 4;
|
253
|
|
- // initialize the timer if it has not already been initialized
|
254
|
|
- timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex);
|
255
|
|
- if (!isTimerActive(timer)) initISR(timer);
|
256
|
|
- servos[this->servoIndex].Pin.isActive = true; // this must be set after the check for isTimerActive
|
257
|
|
- }
|
|
245
|
+int8_t Servo::attach(int pin, int min, int max) {
|
|
246
|
+
|
|
247
|
+ if (this->servoIndex >= MAX_SERVOS) return -1;
|
|
248
|
+
|
|
249
|
+ if (pin > 0) servos[this->servoIndex].Pin.nbr = pin;
|
|
250
|
+ pinMode(servos[this->servoIndex].Pin.nbr, OUTPUT); // set servo pin to output
|
|
251
|
+
|
|
252
|
+ // todo min/max check: abs(min - MIN_PULSE_WIDTH) /4 < 128
|
|
253
|
+ this->min = (MIN_PULSE_WIDTH - min) / 4; //resolution of min/max is 4 uS
|
|
254
|
+ this->max = (MAX_PULSE_WIDTH - max) / 4;
|
|
255
|
+
|
|
256
|
+ // initialize the timer if it has not already been initialized
|
|
257
|
+ timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex);
|
|
258
|
+ if (!isTimerActive(timer)) initISR(timer);
|
|
259
|
+ servos[this->servoIndex].Pin.isActive = true; // this must be set after the check for isTimerActive
|
|
260
|
+
|
258
|
261
|
return this->servoIndex;
|
259
|
262
|
}
|
260
|
263
|
|
|
@@ -301,12 +304,16 @@ int Servo::readMicroseconds() {
|
301
|
304
|
|
302
|
305
|
bool Servo::attached() { return servos[this->servoIndex].Pin.isActive; }
|
303
|
306
|
|
304
|
|
-uint8_t Servo::move(int pin, int value) {
|
305
|
|
- uint8_t ret;
|
306
|
|
- ret = this->attach(pin);
|
307
|
|
- if (ret) {
|
|
307
|
+int8_t Servo::move(int pin, int value) {
|
|
308
|
+ int8_t ret;
|
|
309
|
+ #if SERVO_LEVELING
|
|
310
|
+ ret = this->attach(pin);
|
|
311
|
+ #else
|
|
312
|
+ ret = this->servoIndex;
|
|
313
|
+ #endif
|
|
314
|
+ if (ret >= 0) {
|
308
|
315
|
this->write(value);
|
309
|
|
- #ifdef DEACTIVATE_SERVOS_AFTER_MOVE && (SERVO_DEACTIVATION_DELAY > 0)
|
|
316
|
+ #if SERVO_LEVELING
|
310
|
317
|
delay(SERVO_DEACTIVATION_DELAY);
|
311
|
318
|
this->detach();
|
312
|
319
|
#endif
|