Less use of "this"

This commit is contained in:
Scott Lahteine
2019-09-25 08:29:59 -05:00
parent 661c3cfc99
commit 0b4aedf13e
14 changed files with 245 additions and 248 deletions

View File

@ -213,31 +213,31 @@ void SPIClass::setDataSize(uint32_t datasize) {
}
void SPIClass::setDataMode(uint8_t dataMode) {
/*
Notes:
As far as we know the AVR numbers for dataMode match the numbers required by the STM32.
From the AVR doc http://www.atmel.com/images/doc2585.pdf section 2.4
SPI Mode CPOL CPHA Shift SCK-edge Capture SCK-edge
0 0 0 Falling Rising
1 0 1 Rising Falling
2 1 0 Rising Falling
3 1 1 Falling Rising
On the STM32 it appears to be
bit 1 - CPOL : Clock polarity
(This bit should not be changed when communication is ongoing)
0 : CLK to 0 when idle
1 : CLK to 1 when idle
bit 0 - CPHA : Clock phase
(This bit should not be changed when communication is ongoing)
0 : The first clock transition is the first data capture edge
1 : The second clock transition is the first data capture edge
If someone finds this is not the case or sees a logic error with this let me know ;-)
*/
/**
* Notes:
* As far as we know the AVR numbers for dataMode match the numbers required by the STM32.
* From the AVR doc http://www.atmel.com/images/doc2585.pdf section 2.4
*
* SPI Mode CPOL CPHA Shift SCK-edge Capture SCK-edge
* 0 0 0 Falling Rising
* 1 0 1 Rising Falling
* 2 1 0 Rising Falling
* 3 1 1 Falling Rising
*
* On the STM32 it appears to be
*
* bit 1 - CPOL : Clock polarity
* (This bit should not be changed when communication is ongoing)
* 0 : CLK to 0 when idle
* 1 : CLK to 1 when idle
*
* bit 0 - CPHA : Clock phase
* (This bit should not be changed when communication is ongoing)
* 0 : The first clock transition is the first data capture edge
* 1 : The second clock transition is the first data capture edge
*
* If someone finds this is not the case or sees a logic error with this let me know ;-)
*/
_currentSetting->dataMode = dataMode;
uint32_t cr1 = _currentSetting->spi_d->regs->CR1 & ~(SPI_CR1_CPOL|SPI_CR1_CPHA);
_currentSetting->spi_d->regs->CR1 = cr1 | (dataMode & (SPI_CR1_CPOL|SPI_CR1_CPHA));
@ -593,7 +593,7 @@ void SPIClass::detachInterrupt() {
// Should be disableInterrupt()
}
/*
/**
* Pin accessors
*/
@ -613,25 +613,14 @@ uint8_t SPIClass::nssPin() {
return dev_to_spi_pins(_currentSetting->spi_d)->nss;
}
/*
/**
* Deprecated functions
*/
uint8_t SPIClass::send(uint8_t data) { write(data); return 1; }
uint8_t SPIClass::send(uint8_t *buf, uint32_t len) { write(buf, len); return len; }
uint8_t SPIClass::recv() { return read(); }
uint8_t SPIClass::send(uint8_t data) {
this->write(data);
return 1;
}
uint8_t SPIClass::send(uint8_t *buf, uint32_t len) {
this->write(buf, len);
return len;
}
uint8_t SPIClass::recv() {
return this->read();
}
/*
/**
* DMA call back functions, one per port.
*/
#if BOARD_NR_SPI >= 1
@ -650,7 +639,7 @@ uint8_t SPIClass::recv() {
}
#endif
/*
/**
* Auxiliary functions
*/
static const spi_pins* dev_to_spi_pins(spi_dev *dev) {

View File

@ -96,36 +96,36 @@ typedef enum {
class SPISettings {
public:
SPISettings(uint32_t clock, BitOrder bitOrder, uint8_t dataMode) {
if (__builtin_constant_p(clock))
init_AlwaysInline(clock, bitOrder, dataMode, DATA_SIZE_8BIT);
SPISettings(uint32_t inClock, BitOrder inBitOrder, uint8_t inDataMode) {
if (__builtin_constant_p(inClock))
init_AlwaysInline(inClock, inBitOrder, inDataMode, DATA_SIZE_8BIT);
else
init_MightInline(clock, bitOrder, dataMode, DATA_SIZE_8BIT);
init_MightInline(inClock, inBitOrder, inDataMode, DATA_SIZE_8BIT);
}
SPISettings(uint32_t clock, BitOrder bitOrder, uint8_t dataMode, uint32_t dataSize) {
if (__builtin_constant_p(clock))
init_AlwaysInline(clock, bitOrder, dataMode, dataSize);
SPISettings(uint32_t inClock, BitOrder inBitOrder, uint8_t inDataMode, uint32_t inDataSize) {
if (__builtin_constant_p(inClock))
init_AlwaysInline(inClock, inBitOrder, inDataMode, inDataSize);
else
init_MightInline(clock, bitOrder, dataMode, dataSize);
init_MightInline(inClock, inBitOrder, inDataMode, inDataSize);
}
SPISettings(uint32_t clock) {
if (__builtin_constant_p(clock))
init_AlwaysInline(clock, MSBFIRST, SPI_MODE0, DATA_SIZE_8BIT);
SPISettings(uint32_t inClock) {
if (__builtin_constant_p(inClock))
init_AlwaysInline(inClock, MSBFIRST, SPI_MODE0, DATA_SIZE_8BIT);
else
init_MightInline(clock, MSBFIRST, SPI_MODE0, DATA_SIZE_8BIT);
init_MightInline(inClock, MSBFIRST, SPI_MODE0, DATA_SIZE_8BIT);
}
SPISettings() {
init_AlwaysInline(4000000, MSBFIRST, SPI_MODE0, DATA_SIZE_8BIT);
}
private:
void init_MightInline(uint32_t clock, BitOrder bitOrder, uint8_t dataMode, uint32_t dataSize) {
init_AlwaysInline(clock, bitOrder, dataMode, dataSize);
void init_MightInline(uint32_t inClock, BitOrder inBitOrder, uint8_t inDataMode, uint32_t inDataSize) {
init_AlwaysInline(inClock, inBitOrder, inDataMode, inDataSize);
}
void init_AlwaysInline(uint32_t clock, BitOrder bitOrder, uint8_t dataMode, uint32_t dataSize) __attribute__((__always_inline__)) {
this->clock = clock;
this->bitOrder = bitOrder;
this->dataMode = dataMode;
this->dataSize = dataSize;
void init_AlwaysInline(uint32_t inClock, BitOrder inBitOrder, uint8_t inDataMode, uint32_t inDataSize) __attribute__((__always_inline__)) {
clock = inClock;
bitOrder = inBitOrder;
dataMode = inDataMode;
dataSize = inDataSize;
}
uint32_t clock;
uint32_t dataSize;
@ -339,7 +339,7 @@ public:
* or 1-3 in high density devices.
*/
void setModule(int spi_num) {
_currentSetting=&_settings[spi_num-1];// SPI channels are called 1 2 and 3 but the array is zero indexed
_currentSetting = &_settings[spi_num - 1];// SPI channels are called 1 2 and 3 but the array is zero indexed
}
/* -- The following methods are deprecated --------------------------- */

View File

@ -56,52 +56,50 @@ uint8_t ServoCount = 0;
#define SERVO_OVERFLOW ((uint16_t)round((double)TAU_CYC / SERVO_PRESCALER))
// Unit conversions
#define US_TO_COMPARE(us) ((uint16_t)map((us), 0, TAU_USEC, 0, SERVO_OVERFLOW))
#define COMPARE_TO_US(c) ((uint32_t)map((c), 0, SERVO_OVERFLOW, 0, TAU_USEC))
#define ANGLE_TO_US(a) ((uint16_t)(map((a), this->minAngle, this->maxAngle, \
SERVO_DEFAULT_MIN_PW, SERVO_DEFAULT_MAX_PW)))
#define US_TO_ANGLE(us) ((int16_t)(map((us), SERVO_DEFAULT_MIN_PW, SERVO_DEFAULT_MAX_PW, \
this->minAngle, this->maxAngle)))
#define US_TO_COMPARE(us) uint16_t(map((us), 0, TAU_USEC, 0, SERVO_OVERFLOW))
#define COMPARE_TO_US(c) uint32_t(map((c), 0, SERVO_OVERFLOW, 0, TAU_USEC))
#define ANGLE_TO_US(a) uint16_t(map((a), minAngle, maxAngle, SERVO_DEFAULT_MIN_PW, SERVO_DEFAULT_MAX_PW))
#define US_TO_ANGLE(us) int16_t(map((us), SERVO_DEFAULT_MIN_PW, SERVO_DEFAULT_MAX_PW, minAngle, maxAngle))
void libServo::servoWrite(uint8_t pin, uint16_t duty_cycle) {
void libServo::servoWrite(uint8_t inPin, uint16_t duty_cycle) {
#ifdef SERVO0_TIMER_NUM
if (this->servoIndex == 0) {
this->pwmSetDuty(duty_cycle);
if (servoIndex == 0) {
pwmSetDuty(duty_cycle);
return;
}
#endif
timer_dev *tdev = PIN_MAP[pin].timer_device;
uint8_t tchan = PIN_MAP[pin].timer_channel;
timer_dev *tdev = PIN_MAP[inPin].timer_device;
uint8_t tchan = PIN_MAP[inPin].timer_channel;
if (tdev) timer_set_compare(tdev, tchan, duty_cycle);
}
libServo::libServo() {
this->servoIndex = ServoCount < MAX_SERVOS ? ServoCount++ : INVALID_SERVO;
servoIndex = ServoCount < MAX_SERVOS ? ServoCount++ : INVALID_SERVO;
}
bool libServo::attach(const int32_t pin, const int32_t minAngle, const int32_t maxAngle) {
if (this->servoIndex >= MAX_SERVOS) return false;
if (pin >= BOARD_NR_GPIO_PINS) return false;
bool libServo::attach(const int32_t inPin, const int32_t inMinAngle, const int32_t inMaxAngle) {
if (servoIndex >= MAX_SERVOS) return false;
if (inPin >= BOARD_NR_GPIO_PINS) return false;
this->minAngle = minAngle;
this->maxAngle = maxAngle;
this->angle = -1;
minAngle = inMinAngle;
maxAngle = inMaxAngle;
angle = -1;
#ifdef SERVO0_TIMER_NUM
if (this->servoIndex == 0 && this->setupSoftPWM(pin)) {
this->pin = pin; // set attached()
if (servoIndex == 0 && setupSoftPWM(inPin)) {
pin = inPin; // set attached()
return true;
}
#endif
if (!PWM_PIN(pin)) return false;
if (!PWM_PIN(inPin)) return false;
timer_dev *tdev = PIN_MAP[pin].timer_device;
//uint8_t tchan = PIN_MAP[pin].timer_channel;
timer_dev *tdev = PIN_MAP[inPin].timer_device;
//uint8_t tchan = PIN_MAP[inPin].timer_channel;
SET_PWM(pin);
servoWrite(pin, 0);
SET_PWM(inPin);
servoWrite(inPin, 0);
timer_pause(tdev);
timer_set_prescaler(tdev, SERVO_PRESCALER - 1); // prescaler is 1-based
@ -109,25 +107,24 @@ bool libServo::attach(const int32_t pin, const int32_t minAngle, const int32_t m
timer_generate_update(tdev);
timer_resume(tdev);
this->pin = pin; // set attached()
pin = inPin; // set attached()
return true;
}
bool libServo::detach() {
if (!this->attached()) return false;
this->angle = -1;
servoWrite(this->pin, 0);
if (!attached()) return false;
angle = -1;
servoWrite(pin, 0);
return true;
}
int32_t libServo::read() const {
if (this->attached()) {
if (attached()) {
#ifdef SERVO0_TIMER_NUM
if (this->servoIndex == 0) return this->angle;
if (servoIndex == 0) return angle;
#endif
timer_dev *tdev = PIN_MAP[this->pin].timer_device;
uint8_t tchan = PIN_MAP[this->pin].timer_channel;
timer_dev *tdev = PIN_MAP[pin].timer_device;
uint8_t tchan = PIN_MAP[pin].timer_channel;
return US_TO_ANGLE(COMPARE_TO_US(timer_get_compare(tdev, tchan)));
}
return 0;
@ -137,12 +134,12 @@ void libServo::move(const int32_t value) {
constexpr uint16_t servo_delay[] = SERVO_DELAY;
static_assert(COUNT(servo_delay) == NUM_SERVOS, "SERVO_DELAY must be an array NUM_SERVOS long.");
if (this->attached()) {
this->angle = constrain(value, this->minAngle, this->maxAngle);
servoWrite(this->pin, US_TO_COMPARE(ANGLE_TO_US(this->angle)));
safe_delay(servo_delay[this->servoIndex]);
if (attached()) {
angle = constrain(value, minAngle, maxAngle);
servoWrite(pin, US_TO_COMPARE(ANGLE_TO_US(angle)));
safe_delay(servo_delay[servoIndex]);
#if ENABLED(DEACTIVATE_SERVOS_AFTER_MOVE)
this->detach();
detach();
#endif
}
}
@ -169,13 +166,13 @@ void libServo::move(const int32_t value) {
}
}
bool libServo::setupSoftPWM(const int32_t pin) {
bool libServo::setupSoftPWM(const int32_t inPin) {
timer_dev *tdev = get_timer_dev(SERVO0_TIMER_NUM);
if (!tdev) return false;
#ifdef SERVO0_PWM_OD
OUT_WRITE_OD(pin, 1);
OUT_WRITE_OD(inPin, 1);
#else
OUT_WRITE(pin, 0);
OUT_WRITE(inPin, 0);
#endif
timer_pause(tdev);
@ -206,9 +203,9 @@ void libServo::move(const int32_t value) {
timer_disable_irq(tdev, 1);
timer_disable_irq(tdev, 2);
#ifdef SERVO0_PWM_OD
OUT_WRITE_OD(this->pin, 1); // off
OUT_WRITE_OD(pin, 1); // off
#else
OUT_WRITE(this->pin, 0);
OUT_WRITE(pin, 0);
#endif
}
}
@ -221,7 +218,7 @@ void libServo::move(const int32_t value) {
#else
bool libServo::setupSoftPWM(const int32_t pin) { return false; }
bool libServo::setupSoftPWM(const int32_t inPin) { return false; }
void libServo::pwmSetDuty(const uint16_t duty_cycle) {}
void libServo::pauseSoftPWM() {}

View File

@ -41,7 +41,7 @@ class libServo {
public:
libServo();
bool attach(const int32_t pin, const int32_t minAngle=SERVO_DEFAULT_MIN_ANGLE, const int32_t maxAngle=SERVO_DEFAULT_MAX_ANGLE);
bool attached() const { return this->pin != NOT_ATTACHED; }
bool attached() const { return pin != NOT_ATTACHED; }
bool detach();
void move(const int32_t value);
int32_t read() const;