From 786ceae1f1d48ed61758766064bec3b1c253c646 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 27 Aug 2020 15:45:16 +0700 Subject: [PATCH] change token from const var to enum --- cores/nRF5/Tone.cpp | 8 ++++---- cores/nRF5/wiring_analog.cpp | 14 +++++++++----- libraries/Servo/src/nrf52/Servo.cpp | 11 +++++++---- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/cores/nRF5/Tone.cpp b/cores/nRF5/Tone.cpp index b29a6ddd..e1569a7a 100644 --- a/cores/nRF5/Tone.cpp +++ b/cores/nRF5/Tone.cpp @@ -48,7 +48,7 @@ static HardwarePWM * const _HwPWM = HwPWMx[2]; // Defined a struct, to simplify validation testing ... also provides context when debugging class TonePwmConfig { private: - const uint32_t toneToken = 0x656e6f54; //< 'T' 'o' 'n' 'e' + enum { TONE_TOKEN = 0x656e6f54 }; //< 'T' 'o' 'n' 'e' uint64_t pulse_count; //< total number of PWM pulses uint32_t seq0_refresh; //< count of pulses for each SEQ0 iteration uint32_t seq1_refresh; //< count of pulses for each SEQ1 iteration @@ -198,7 +198,7 @@ void noTone(uint8_t pin) } bool TonePwmConfig::ensurePwmPeripheralOwnership(void) { - if (!_HwPWM->isOwner(TonePwmConfig::toneToken) && !_HwPWM->takeOwnership(TonePwmConfig::toneToken)) { + if (!_HwPWM->isOwner(TonePwmConfig::TONE_TOKEN) && !_HwPWM->takeOwnership(TonePwmConfig::TONE_TOKEN)) { LOG_LV1("Tone", "unable to allocate PWM2 to Tone"); return false; } @@ -364,7 +364,7 @@ bool TonePwmConfig::startPlayback(void) { bool TonePwmConfig::stopPlayback(bool releaseOwnership) { - if (!_HwPWM->isOwner(TonePwmConfig::toneToken)) { + if (!_HwPWM->isOwner(TonePwmConfig::TONE_TOKEN)) { LOG_LV2("Tone", "Attempt to set noTone when not the owner of the PWM peripheral. Ignoring call...."); return false; } @@ -376,7 +376,7 @@ bool TonePwmConfig::stopPlayback(bool releaseOwnership) { } if (releaseOwnership) { - _HwPWM->releaseOwnership(TonePwmConfig::toneToken); + _HwPWM->releaseOwnership(TonePwmConfig::TONE_TOKEN); } return true; diff --git a/cores/nRF5/wiring_analog.cpp b/cores/nRF5/wiring_analog.cpp index 7065b89a..38fefdf1 100644 --- a/cores/nRF5/wiring_analog.cpp +++ b/cores/nRF5/wiring_analog.cpp @@ -46,8 +46,12 @@ extern "C" { +enum +{ + ANALOG_TOKEN = 0x676f6c41 // 'A' 'l' 'o' 'g' +}; + static uint8_t _analogResolution = 8; // default is 256 levels -static uint32_t _analogToken = 0x676f6c41; // 'A' 'l' 'o' 'g' /** * This will apply to all PWM Hardware currently used by analogWrite(), @@ -59,7 +63,7 @@ void analogWriteResolution( uint8_t res ) _analogResolution = res; for (int i = 0; iisOwner(_analogToken)) + if (HwPWMx[i]->isOwner(ANALOG_TOKEN)) { HwPWMx[i]->setResolution(res); } @@ -78,7 +82,7 @@ void analogWrite( uint32_t pin, uint32_t value ) // first, handle the case where the pin is already in use by analogWrite() for(int i=0; iisOwner(_analogToken)) + if (HwPWMx[i]->isOwner(ANALOG_TOKEN)) { int const ch = HwPWMx[i]->pin2channel(pin); if (ch >= 0) @@ -92,7 +96,7 @@ void analogWrite( uint32_t pin, uint32_t value ) // Next, handle the case where can add the pin to a PWM instance already owned by analogWrite() for(int i=0; iisOwner(_analogToken) && HwPWMx[i]->addPin(pin) ) + if ( HwPWMx[i]->isOwner(ANALOG_TOKEN) && HwPWMx[i]->addPin(pin) ) { // successfully added the pin, so write the value also HwPWMx[i]->writePin(pin, value); @@ -106,7 +110,7 @@ void analogWrite( uint32_t pin, uint32_t value ) // 2. it currently has no pins in use. for(int i=0; itakeOwnership(_analogToken)) + if (HwPWMx[i]->takeOwnership(ANALOG_TOKEN)) { // apply the cached analog resolution to newly owned instances HwPWMx[i]->setResolution(_analogResolution); diff --git a/libraries/Servo/src/nrf52/Servo.cpp b/libraries/Servo/src/nrf52/Servo.cpp index 015bddf5..e83b97cb 100644 --- a/libraries/Servo/src/nrf52/Servo.cpp +++ b/libraries/Servo/src/nrf52/Servo.cpp @@ -21,7 +21,10 @@ #include #include -static uint32_t _servoToken = 0x76726553; // 'S' 'e' 'r' 'v' +enum +{ + SERVO_TOKEN = 0x76726553 // 'S' 'e' 'r' 'v' +}; static servo_t servos[MAX_SERVOS]; // static array of servo structures uint8_t ServoCount = 0; // the total number of attached servos @@ -71,7 +74,7 @@ uint8_t Servo::attach(int pin, int min, int max) // first, use existing HWPWM modules (already owned by Servo) for ( int i = 0; i < HWPWM_MODULE_NUM; i++ ) { - if ( HwPWMx[i]->isOwner(_servoToken) && HwPWMx[i]->addPin(pin) ) + if ( HwPWMx[i]->isOwner(SERVO_TOKEN) && HwPWMx[i]->addPin(pin) ) { this->pwm = HwPWMx[i]; succeeded = true; @@ -84,7 +87,7 @@ uint8_t Servo::attach(int pin, int min, int max) { for ( int i = 0; i < HWPWM_MODULE_NUM; i++ ) { - if ( HwPWMx[i]->takeOwnership(_servoToken) && HwPWMx[i]->addPin(pin) ) + if ( HwPWMx[i]->takeOwnership(SERVO_TOKEN) && HwPWMx[i]->addPin(pin) ) { this->pwm = HwPWMx[i]; succeeded = true; @@ -124,7 +127,7 @@ void Servo::detach() pwm->removePin(pin); if (pwm->usedChannelCount() == 0) { pwm->stop(); // disables peripheral so can release ownership - pwm->releaseOwnership(_servoToken); + pwm->releaseOwnership(SERVO_TOKEN); } }