SAMD51 Servo class (#14781)

This commit is contained in:
Giuliano Zaro
2019-08-02 14:37:41 +02:00
committed by Scott Lahteine
parent 8efa3455c2
commit 21993b75f4
15 changed files with 318 additions and 117 deletions

View File

@ -116,9 +116,8 @@ void Servo::detach() {
}
void Servo::write(int value) {
if (value < MIN_PULSE_WIDTH) { // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
if (value < MIN_PULSE_WIDTH) // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
value = map(constrain(value, 0, 180), 0, 180, SERVO_MIN(), SERVO_MAX());
}
this->writeMicroseconds(value);
}
@ -140,7 +139,7 @@ void Servo::writeMicroseconds(int value) {
int Servo::read() { return map(this->readMicroseconds() + 1, SERVO_MIN(), SERVO_MAX(), 0, 180); }
int Servo::readMicroseconds() {
return (this->servoIndex == INVALID_SERVO) ? 0 : ticksToUs(servo_info[this->servoIndex].ticks) + TRIM_DURATION;
return (this->servoIndex == INVALID_SERVO) ? 0 : ticksToUs(servo_info[this->servoIndex].ticks) + (TRIM_DURATION);
}
bool Servo::attached() { return servo_info[this->servoIndex].Pin.isActive; }

View File

@ -84,10 +84,10 @@
#else
#include <stdint.h>
#if defined(__AVR__) || defined(ARDUINO_ARCH_SAM)
#if defined(__AVR__) || defined(ARDUINO_ARCH_SAM) || defined (__SAMD51__)
// we're good to go
#else
#error "This library only supports boards with an AVR or SAM3X processor."
#error "This library only supports boards with an AVR, SAM3X or SAMD51 processor."
#endif
#define Servo_VERSION 2 // software version of this library

View File

@ -47,8 +47,10 @@
#include "../HAL_AVR/ServoTimers.h"
#elif defined(ARDUINO_ARCH_SAM)
#include "../HAL_DUE/ServoTimers.h"
#elif defined(__SAMD51__)
#include "../HAL_SAMD51/ServoTimers.h"
#else
#error "This library only supports boards with an AVR or SAM3X processor."
#error "This library only supports boards with an AVR, SAM3X or SAMD51 processor."
#endif
// Macros
@ -64,10 +66,8 @@
#define INVALID_SERVO 255 // flag indicating an invalid servo index
// Convert microseconds to ticks and back (PRESCALER depends on architecture)
#define usToTicks(_us) (clockCyclesPerMicrosecond() * (_us) / (PRESCALER))
#define ticksToUs(_ticks) (unsigned(_ticks) * (PRESCALER) / clockCyclesPerMicrosecond())
//#define NBR_TIMERS ((MAX_SERVOS) / (SERVOS_PER_TIMER))
#define usToTicks(_us) (clockCyclesPerMicrosecond() * (_us) / (SERVO_TIMER_PRESCALER))
#define ticksToUs(_ticks) (unsigned(_ticks) * (SERVO_TIMER_PRESCALER) / clockCyclesPerMicrosecond())
// convenience macros
#define SERVO_INDEX_TO_TIMER(_servo_nbr) ((timer16_Sequence_t)(_servo_nbr / (SERVOS_PER_TIMER))) // returns the timer controlling this servo
@ -78,7 +78,7 @@
// Types
typedef struct {
uint8_t nbr : 6 ; // a pin number from 0 to 63
uint8_t nbr : 7 ; // a pin number from 0 to 127
uint8_t isActive : 1 ; // true if this channel is enabled, pin not pulsed if false
} ServoPin_t;