Merge pull request #562 from adafruit/followup-496
clean up pwn and analog
This commit is contained in:
8 files changed
+373
-319
No files matched your search
@@ -1,68 +0,0 @@
|
||||
sudo: required
|
||||
language: c
|
||||
dist: xenial
|
||||
git:
|
||||
depth: false
|
||||
quiet: true
|
||||
|
||||
env:
|
||||
global:
|
||||
- BSP_PATH="$HOME/.arduino15/packages/adafruit/hardware/nrf52"
|
||||
jobs:
|
||||
# empty env required, else allow_failures will SILENTY IGNORE matching against env...
|
||||
-
|
||||
|
||||
jobs:
|
||||
fast_finish: true
|
||||
include:
|
||||
- name: "Feather nRF52840"
|
||||
env: VARIANT="feather52840"
|
||||
- name: "Feather nRF52840 (All warnings)"
|
||||
env: ALL_WARNINGS="true" VARIANT="feather52840"
|
||||
- name: "Circuit Playground nRF52840"
|
||||
env: VARIANT="cplaynrf52840"
|
||||
- name: "Circuit Playground nRF52840 (All warnings)"
|
||||
env: ALL_WARNINGS="true" VARIANT="cplaynrf52840"
|
||||
- name: "Feather nRF52832"
|
||||
env: VARIANT="feather52832"
|
||||
- name: "Feather nRF52832 (All warnings)"
|
||||
env: ALL_WARNINGS="true" VARIANT="feather52832"
|
||||
- name: "ItsyBitsy nRF52840 Express"
|
||||
env: VARIANT="itsybitsy52840"
|
||||
- name: "Clue nRF52840"
|
||||
env: VARIANT="cluenrf52840"
|
||||
allow_failures:
|
||||
- env: ALL_WARNINGS="true" VARIANT="feather52840"
|
||||
- env: ALL_WARNINGS="true" VARIANT="cplaynrf52840"
|
||||
- env: ALL_WARNINGS="true" VARIANT="feather52832"
|
||||
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- python3
|
||||
- python3-pip
|
||||
- python3-setuptools
|
||||
snaps:
|
||||
- name: ubuntu-make
|
||||
confinement: classic
|
||||
|
||||
install:
|
||||
# Filter only mDNS / Bonjour traffic
|
||||
- sudo iptables --insert INPUT --jump DROP --protocol udp --dport 5353 -m comment --comment "silently drop all 5353/udp input"
|
||||
- sudo iptables --insert INPUT --jump DROP --destination 224.0.0.251 -m comment --comment "silently drop all mDNS ipv4 broadcast"
|
||||
# Install the nRF52 support files for arduino
|
||||
- pip3 install --user adafruit-nrfutil
|
||||
- umake electronics arduino $HOME/arduino_ide
|
||||
- export PATH=$HOME/arduino_ide:$PATH
|
||||
- arduino --pref "boardsmanager.additional.urls=https://adafruit.github.io/arduino-board-index/package_adafruit_index.json" --save-prefs
|
||||
# Install BSP for tools, then remove and create symlink the git code
|
||||
- arduino --install-boards adafruit:nrf52
|
||||
- BSP_VERSION=`eval ls $BSP_PATH`
|
||||
- rm -r $BSP_PATH/*
|
||||
- ln -s $TRAVIS_BUILD_DIR $BSP_PATH/$BSP_VERSION
|
||||
- arduino --install-library "Adafruit NeoPixel","Adafruit NeoMatrix","Adafruit GFX Library","Adafruit SSD1306","MIDI Library","Adafruit ILI9341","Adafruit HX8357 Library","Adafruit Circuit Playground"
|
||||
|
||||
before_script:
|
||||
|
||||
script:
|
||||
- python3 tools/build_all_travis.py
|
||||
+112
-140
@@ -54,10 +54,10 @@ HardwarePWM* HwPWMx[] =
|
||||
};
|
||||
|
||||
#if CFG_DEBUG
|
||||
bool can_stringify_token(uintptr_t token)
|
||||
bool can_stringify_token(uint32_t token)
|
||||
{
|
||||
uint8_t * t = (uint8_t *)&token;
|
||||
for (size_t i = 0; i < sizeof(uintptr_t); ++i, ++t)
|
||||
for (size_t i = 0; i < sizeof(uint32_t); ++i, ++t)
|
||||
{
|
||||
uint8_t x = *t;
|
||||
if ((x < 0x20) || (x > 0x7E)) return false;
|
||||
@@ -71,14 +71,12 @@ void HardwarePWM::DebugOutput(Stream& logger)
|
||||
logger.printf("HwPWM Debug:");
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
HardwarePWM const * pwm = HwPWMx[i];
|
||||
uintptr_t token = pwm->_owner_token;
|
||||
uint32_t token = pwm->_owner_token;
|
||||
logger.printf(" || %d:", i);
|
||||
if (can_stringify_token(token)) {
|
||||
uint8_t * t = (uint8_t*)(&token);
|
||||
static_assert(sizeof(uintptr_t) == 4);
|
||||
logger.printf(" \"%c%c%c%c\"", t[0], t[1], t[2], t[3] );
|
||||
} else {
|
||||
static_assert(sizeof(uintptr_t) == 4);
|
||||
logger.printf(" %08x", token);
|
||||
}
|
||||
for (size_t j = 0; j < MAX_CHANNELS; j++) {
|
||||
@@ -96,76 +94,6 @@ void HardwarePWM::DebugOutput(Stream& logger)
|
||||
void HardwarePWM::DebugOutput(Stream& logger) {}
|
||||
#endif // CFG_DEBUG
|
||||
|
||||
// returns true ONLY when (1) no PWM channel has a pin, and (2) the owner token is nullptr
|
||||
bool HardwarePWM::takeOwnership(uintptr_t token)
|
||||
{
|
||||
bool notInIsr = !isInISR();
|
||||
if (token == 0) {
|
||||
if (notInIsr) {
|
||||
LOG_LV1("HwPWM", "zero / nullptr is not a valid ownership token (attempted use in takeOwnership)");
|
||||
}
|
||||
return false; // cannot take ownership with nullptr
|
||||
}
|
||||
if (token == this->_owner_token) {
|
||||
if (notInIsr) {
|
||||
LOG_LV1("HwPWM", "failing to acquire ownership because already owned by requesting token (cannot take ownership twice)");
|
||||
}
|
||||
}
|
||||
if (this->_owner_token != 0) {
|
||||
return false;
|
||||
}
|
||||
if (this->usedChannelCount() != 0) {
|
||||
return false;
|
||||
}
|
||||
if (this->enabled()) {
|
||||
return false;
|
||||
}
|
||||
// TODO: warn, but do not fail, if taking ownership with IRQs already enabled
|
||||
// NVIC_GetActive
|
||||
|
||||
// Use C++11 atomic CAS operation
|
||||
uintptr_t newValue = 0U;
|
||||
return this->_owner_token.compare_exchange_strong(newValue, token);
|
||||
}
|
||||
// returns true ONLY when (1) no PWM channel has a pin attached, and (2) the owner token matches
|
||||
bool HardwarePWM::releaseOwnership(uintptr_t token)
|
||||
{
|
||||
bool notInIsr = !isInISR();
|
||||
if (token == 0) {
|
||||
if (notInIsr) {
|
||||
LOG_LV1("HwPWM", "zero / nullptr is not a valid ownership token (attempted use in releaseOwnership)");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (!this->isOwner(token)) {
|
||||
if (notInIsr) {
|
||||
LOG_LV1("HwPWM", "attempt to release ownership when not the current owner");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (this->usedChannelCount() != 0) {
|
||||
if (notInIsr) {
|
||||
LOG_LV1("HwPWM", "attempt to release ownership when at least on channel is still connected");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (this->enabled()) {
|
||||
if (notInIsr) {
|
||||
LOG_LV1("HwPWM", "attempt to release ownership when PWM peripheral is still enabled");
|
||||
}
|
||||
return false; // if it's enabled, do not allow ownership to be released, even with no pins in use
|
||||
}
|
||||
// TODO: warn, but do not fail, if releasing ownership with IRQs enabled
|
||||
// NVIC_GetActive
|
||||
|
||||
// Use C++11 atomic CAS operation
|
||||
bool result = this->_owner_token.compare_exchange_strong(token, 0U);
|
||||
if (!result) {
|
||||
LOG_LV1("HwPWM", "race condition resulted in failure to acquire ownership");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
HardwarePWM::HardwarePWM(NRF_PWM_Type* pwm) :
|
||||
_pwm(pwm)
|
||||
{
|
||||
@@ -179,6 +107,38 @@ HardwarePWM::HardwarePWM(NRF_PWM_Type* pwm) :
|
||||
_pwm->PSEL.OUT[1] = 0xFFFFFFFFUL;
|
||||
}
|
||||
|
||||
void HardwarePWM::begin(void)
|
||||
{
|
||||
// Initialize Registers
|
||||
_pwm->MODE = PWM_MODE_UPDOWN_Up;
|
||||
_pwm->COUNTERTOP = _max_value; // default is 255 (8 bit), can be configured before begin()
|
||||
_pwm->PRESCALER = _clock_div;
|
||||
_pwm->DECODER = PWM_DECODER_LOAD_Individual;
|
||||
_pwm->LOOP = 0;
|
||||
|
||||
_pwm->SEQ[0].PTR = (uint32_t) _seq0;
|
||||
_pwm->SEQ[0].CNT = MAX_CHANNELS; // default mode is Individual --> count must be 4
|
||||
_pwm->SEQ[0].REFRESH = 0;
|
||||
_pwm->SEQ[0].ENDDELAY = 0;
|
||||
|
||||
_pwm->SEQ[1].PTR = 0;
|
||||
_pwm->SEQ[1].CNT = 0;
|
||||
_pwm->SEQ[1].REFRESH = 0;
|
||||
_pwm->SEQ[1].ENDDELAY = 0;
|
||||
|
||||
_pwm->ENABLE = 1;
|
||||
}
|
||||
|
||||
void HardwarePWM::stop(void)
|
||||
{
|
||||
_pwm->ENABLE = 0;
|
||||
}
|
||||
|
||||
bool HardwarePWM::enabled (void)
|
||||
{
|
||||
return _pwm->ENABLE;
|
||||
}
|
||||
|
||||
void HardwarePWM::setResolution(uint8_t bitnum)
|
||||
{
|
||||
setMaxValue( bit(min8(bitnum, 15)) -1 );
|
||||
@@ -196,6 +156,25 @@ void HardwarePWM::setClockDiv(uint8_t div)
|
||||
_pwm->PRESCALER = _clock_div;
|
||||
}
|
||||
|
||||
void HardwarePWM::_set_psel(int ch, uint32_t value)
|
||||
{
|
||||
// Must disable before changing PSEL
|
||||
if ( enabled() )
|
||||
{
|
||||
_pwm->ENABLE = 0;
|
||||
_pwm->PSEL.OUT[ch] = value;
|
||||
_seq0[ch] = 0;
|
||||
_pwm->ENABLE = 1;
|
||||
|
||||
// re-start sequence
|
||||
if ( usedChannelCount() ) _pwm->TASKS_SEQSTART[0] = 1;
|
||||
}else
|
||||
{
|
||||
_pwm->PSEL.OUT[ch] = value;
|
||||
_seq0[ch] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add pin to this group.
|
||||
* @param pin Pin to add
|
||||
@@ -222,17 +201,7 @@ bool HardwarePWM::addPin(uint8_t pin)
|
||||
pinMode(pin, OUTPUT);
|
||||
digitalWrite(pin, LOW);
|
||||
|
||||
// Must disable before changing PSEL
|
||||
if ( enabled() )
|
||||
{
|
||||
_pwm->ENABLE = 0;
|
||||
_pwm->PSEL.OUT[ch] = g_ADigitalPinMap[pin];
|
||||
_pwm->ENABLE = 1;
|
||||
_start();
|
||||
}else
|
||||
{
|
||||
_pwm->PSEL.OUT[ch] = g_ADigitalPinMap[pin];
|
||||
}
|
||||
_set_psel(ch, g_ADigitalPinMap[pin]);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -242,70 +211,21 @@ bool HardwarePWM::removePin(uint8_t pin)
|
||||
int ch = pin2channel(pin);
|
||||
VERIFY( ch >= 0 );
|
||||
|
||||
bool const en = enabled();
|
||||
|
||||
// Must disable before changing PSEL
|
||||
if ( en ) _pwm->ENABLE = 0;
|
||||
|
||||
_pwm->PSEL.OUT[ch] = 0xFFFFFFFFUL;
|
||||
_seq0[ch] = 0;
|
||||
|
||||
if ( en ) _pwm->ENABLE = 1;
|
||||
|
||||
_set_psel(ch, 0xFFFFFFFFUL);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool HardwarePWM::enabled (void)
|
||||
{
|
||||
return _pwm->ENABLE;
|
||||
}
|
||||
|
||||
void HardwarePWM::begin(void)
|
||||
{
|
||||
// Initialize Registers
|
||||
_pwm->MODE = PWM_MODE_UPDOWN_Up;
|
||||
_pwm->COUNTERTOP = _max_value; // default is 255 (8 bit), can be configured before begin()
|
||||
_pwm->PRESCALER = _clock_div;
|
||||
_pwm->DECODER = PWM_DECODER_LOAD_Individual;
|
||||
_pwm->LOOP = 0;
|
||||
|
||||
_pwm->SEQ[0].PTR = (uint32_t) _seq0;
|
||||
_pwm->SEQ[0].CNT = MAX_CHANNELS; // default mode is Individual --> count must be 4
|
||||
_pwm->SEQ[0].REFRESH = 0;
|
||||
_pwm->SEQ[0].ENDDELAY = 0;
|
||||
|
||||
_pwm->SEQ[1].PTR = 0;
|
||||
_pwm->SEQ[1].CNT = 0;
|
||||
_pwm->SEQ[1].REFRESH = 0;
|
||||
_pwm->SEQ[1].ENDDELAY = 0;
|
||||
|
||||
_pwm->ENABLE = 1;
|
||||
}
|
||||
|
||||
void HardwarePWM::_start(void)
|
||||
{
|
||||
// update sequence count (depending on mode)
|
||||
// _pwm->SEQ[0].CNT = MAX_CHANNELS;
|
||||
|
||||
// start sequence
|
||||
_pwm->TASKS_SEQSTART[0] = 1;
|
||||
}
|
||||
|
||||
void HardwarePWM::stop(void)
|
||||
{
|
||||
_pwm->ENABLE = 0;
|
||||
}
|
||||
|
||||
bool HardwarePWM::writeChannel(uint8_t ch, uint16_t value, bool inverted )
|
||||
bool HardwarePWM::writeChannel(uint8_t ch, uint16_t value, bool inverted)
|
||||
{
|
||||
VERIFY( ch < MAX_CHANNELS );
|
||||
|
||||
_seq0[ch] = value | (inverted ? 0 : bit(15));
|
||||
|
||||
// Start PWM if not already
|
||||
// Initialize PWM if not already
|
||||
if ( !enabled() ) begin();
|
||||
|
||||
_start();
|
||||
// start sequence
|
||||
_pwm->TASKS_SEQSTART[0] = 1;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -350,3 +270,55 @@ uint8_t HardwarePWM::freeChannelCount(void) const
|
||||
return MAX_CHANNELS - usedChannelCount();
|
||||
}
|
||||
|
||||
// returns true ONLY when (1) no PWM channel has a pin, and (2) the owner token is nullptr
|
||||
bool HardwarePWM::takeOwnership(uint32_t token)
|
||||
{
|
||||
bool const thread_mode = !isInISR();
|
||||
|
||||
if (token == 0) {
|
||||
if (thread_mode) LOG_LV1("HwPWM", "zero is not a valid ownership token (attempted use in takeOwnership)");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (token == this->_owner_token) {
|
||||
if (thread_mode) LOG_LV1("HwPWM", "failing to acquire ownership because already owned by requesting token (cannot take ownership twice)");
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( this->_owner_token != 0 ) return false;
|
||||
if ( this->usedChannelCount() != 0 ) return false;
|
||||
if ( this->enabled() ) return false;
|
||||
|
||||
// Use C++11 atomic CAS operation
|
||||
uint32_t expectedValue = 0U;
|
||||
return this->_owner_token.compare_exchange_strong(expectedValue, token);
|
||||
}
|
||||
|
||||
// returns true ONLY when (1) no PWM channel has a pin attached, and (2) the owner token matches
|
||||
bool HardwarePWM::releaseOwnership(uint32_t token)
|
||||
{
|
||||
bool const thread_mode = !isInISR();
|
||||
|
||||
if (token == 0) {
|
||||
if (thread_mode) LOG_LV1("HwPWM", "zero is not a valid ownership token (attempted use in releaseOwnership)");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!this->isOwner(token)) {
|
||||
if (thread_mode) LOG_LV1("HwPWM", "attempt to release ownership when not the current owner");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this->usedChannelCount() != 0) {
|
||||
if (thread_mode) LOG_LV1("HwPWM", "attempt to release ownership when at least on channel is still connected");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (this->enabled()) {
|
||||
if (thread_mode) LOG_LV1("HwPWM", "attempt to release ownership when PWM peripheral is still enabled");
|
||||
return false; // if it's enabled, do not allow ownership to be released, even with no pins in use
|
||||
}
|
||||
|
||||
// Use C++11 atomic CAS operation
|
||||
return this->_owner_token.compare_exchange_strong(token, 0U);
|
||||
}
|
||||
@@ -51,15 +51,13 @@ class HardwarePWM
|
||||
private:
|
||||
enum { MAX_CHANNELS = 4 }; // Max channel per group
|
||||
NRF_PWM_Type * const _pwm;
|
||||
std::atomic_uintptr_t _owner_token;
|
||||
std::atomic_uint32_t _owner_token;
|
||||
|
||||
uint16_t _seq0[MAX_CHANNELS];
|
||||
|
||||
uint16_t _max_value;
|
||||
uint8_t _clock_div;
|
||||
|
||||
void _start(void);
|
||||
|
||||
public:
|
||||
HardwarePWM(NRF_PWM_Type* pwm);
|
||||
|
||||
@@ -72,19 +70,24 @@ class HardwarePWM
|
||||
// Cooperative ownership sharing
|
||||
|
||||
// returns true ONLY when (1) no PWM channel has a pin, and (2) the owner token is nullptr
|
||||
bool takeOwnership (uintptr_t token);
|
||||
bool takeOwnership (uint32_t token);
|
||||
|
||||
// returns true ONLY when (1) no PWM channel has a pin attached, and (2) the owner token matches
|
||||
bool releaseOwnership(uintptr_t token);
|
||||
bool releaseOwnership(uint32_t token);
|
||||
|
||||
// allows caller to verify that they own the peripheral
|
||||
__INLINE bool isOwner(uintptr_t token) const
|
||||
bool isOwner(uint32_t token) const
|
||||
{
|
||||
return this->_owner_token == token;
|
||||
}
|
||||
|
||||
// Add a pin to PWM module
|
||||
bool addPin (uint8_t pin);
|
||||
|
||||
// Remove a pin from PWM module
|
||||
bool removePin (uint8_t pin);
|
||||
|
||||
// Get the mapped channel of a pin
|
||||
int pin2channel(uint8_t pin) const
|
||||
{
|
||||
pin = g_ADigitalPinMap[pin];
|
||||
@@ -95,6 +98,7 @@ class HardwarePWM
|
||||
return (-1);
|
||||
}
|
||||
|
||||
// Check if pin is controlled by PWM
|
||||
bool checkPin(uint8_t pin) const
|
||||
{
|
||||
return pin2channel(pin) >= 0;
|
||||
@@ -117,6 +121,9 @@ class HardwarePWM
|
||||
uint8_t freeChannelCount(void) const;
|
||||
|
||||
static void DebugOutput(Stream& logger);
|
||||
|
||||
private:
|
||||
void _set_psel(int ch, uint32_t value);
|
||||
};
|
||||
|
||||
extern HardwarePWM HwPWM0;
|
||||
|
||||
+31
-42
@@ -48,22 +48,16 @@ static HardwarePWM * const _HwPWM = HwPWMx[2];
|
||||
// Defined a struct, to simplify validation testing ... also provides context when debugging
|
||||
class TonePwmConfig {
|
||||
private:
|
||||
const nrf_pwm_clk_t clock_frequency = NRF_PWM_CLK_125kHz;
|
||||
const nrf_pwm_mode_t pwm_mode = NRF_PWM_MODE_UP;
|
||||
const nrf_pwm_dec_step_t step_mode = NRF_PWM_STEP_AUTO;
|
||||
const nrf_pwm_dec_load_t load_mode = NRF_PWM_LOAD_COMMON;
|
||||
const uintptr_t toneToken = 0x656e6f54; //< 'T' 'o' 'n' 'e'
|
||||
private:
|
||||
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
|
||||
uint16_t loop_count; //< how many times to restart SEQ0/SEQ1?
|
||||
uint16_t time_period; //< how many clock cycles allocated to each PWM pulse?
|
||||
uint16_t duty_with_polarity; //< SEQ[N].PTR will point here, length == 1
|
||||
uint8_t arduino_pin; //< the arduino pin for playback
|
||||
uint8_t nrf_pin; //< the nrf pin for playback
|
||||
nrf_pwm_task_t task_to_start; //< Whether to start playback at SEQ0 or SEQ1
|
||||
nrf_pwm_short_mask_t shorts; //< shortcuts to enable
|
||||
|
||||
public:
|
||||
bool ensurePwmPeripheralOwnership(void);
|
||||
bool initializeFromPulseCountAndTimePeriod(uint64_t pulse_count, uint16_t time_period);
|
||||
@@ -98,6 +92,7 @@ constexpr static uint16_t _calculate_time_period(uint32_t frequency) {
|
||||
// which fits in 16 bits.
|
||||
return 125000 / frequency;
|
||||
};
|
||||
|
||||
constexpr static uint64_t _calculate_pulse_count(uint32_t frequency, uint32_t duration) {
|
||||
// range for frequency == [20..25000],
|
||||
// range for duration == [ 1..0xFFFF_FFFF]
|
||||
@@ -111,6 +106,7 @@ constexpr static uint64_t _calculate_pulse_count(uint32_t frequency, uint32_t du
|
||||
(duration / 1000ULL) * frequency :
|
||||
(((uint64_t)duration) * frequency / 1000ULL);
|
||||
};
|
||||
|
||||
static int _bits_used(unsigned long x) {
|
||||
if (0 == x) return 0;
|
||||
unsigned int result = 0;
|
||||
@@ -119,6 +115,7 @@ static int _bits_used(unsigned long x) {
|
||||
} while (x >>= 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
static int _bits_used(unsigned long long x) {
|
||||
if (0 == x) return 0;
|
||||
unsigned int result = 0;
|
||||
@@ -173,26 +170,27 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration)
|
||||
{
|
||||
// limit frequency to reasonable audible range
|
||||
if((frequency < 20) | (frequency > 25000)) {
|
||||
LOG_LV1("TON", "frequency outside range [20..25000] -- ignoring");
|
||||
LOG_LV1("Tone", "frequency outside range [20..25000] -- ignoring");
|
||||
return;
|
||||
}
|
||||
uint64_t pulse_count = _calculate_pulse_count(frequency, duration);
|
||||
uint16_t time_period = _calculate_time_period(frequency);
|
||||
if (!_pwm_config.ensurePwmPeripheralOwnership()) {
|
||||
LOG_LV1("TON", "Unable to acquire PWM peripheral");
|
||||
} else if (!_pwm_config.stopPlayback()) {
|
||||
LOG_LV1("TON", "Unable to stop playback");
|
||||
LOG_LV1("Tone", "Unable to acquire PWM peripheral");
|
||||
} else if (!_pwm_config.stopPlayback(false)) {
|
||||
LOG_LV1("Tone", "Unable to stop playback");
|
||||
} else if (!_pwm_config.initializeFromPulseCountAndTimePeriod(pulse_count, time_period)) {
|
||||
LOG_LV1("TON", "Failed calculating configuration");
|
||||
LOG_LV1("Tone", "Failed calculating configuration");
|
||||
} else if (!_pwm_config.applyConfiguration(pin)) {
|
||||
LOG_LV1("TON", "Failed applying configuration");
|
||||
LOG_LV1("Tone", "Failed applying configuration");
|
||||
} else if (!_pwm_config.startPlayback()) {
|
||||
LOG_LV1("TON", "Failed attempting to start PWM peripheral");
|
||||
LOG_LV1("Tone", "Failed attempting to start PWM peripheral");
|
||||
} else {
|
||||
//LOG_LV2("TON", "Started playback of tone at frequency %d duration %ld", frequency, duration);
|
||||
//LOG_LV2("Tone", "Started playback of tone at frequency %d duration %ld", frequency, duration);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void noTone(uint8_t pin)
|
||||
{
|
||||
( void )pin; // avoid unreferenced parameter compiler warning
|
||||
@@ -200,8 +198,8 @@ void noTone(uint8_t pin)
|
||||
}
|
||||
|
||||
bool TonePwmConfig::ensurePwmPeripheralOwnership(void) {
|
||||
if (!_HwPWM->isOwner(TonePwmConfig::toneToken) && !_HwPWM->takeOwnership(TonePwmConfig::toneToken)) {
|
||||
LOG_LV1("TON", "unable to allocate PWM2 to Tone");
|
||||
if (!_HwPWM->isOwner(TonePwmConfig::TONE_TOKEN) && !_HwPWM->takeOwnership(TonePwmConfig::TONE_TOKEN)) {
|
||||
LOG_LV1("Tone", "unable to allocate PWM2 to Tone");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -240,7 +238,7 @@ bool TonePwmConfig::ensurePwmPeripheralOwnership(void) {
|
||||
bool TonePwmConfig::initializeFromPulseCountAndTimePeriod(uint64_t pulse_count_x, uint16_t time_period) {
|
||||
|
||||
if (_bits_used(pulse_count_x) > 37) {
|
||||
LOG_LV1("TON", "pulse count is limited to 37 bit long value");
|
||||
LOG_LV1("Tone", "pulse count is limited to 37 bit long value");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -310,6 +308,7 @@ bool TonePwmConfig::initializeFromPulseCountAndTimePeriod(uint64_t pulse_count_x
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TonePwmConfig::applyConfiguration(uint32_t pin) {
|
||||
if (pin >= PINS_COUNT) {
|
||||
return false;
|
||||
@@ -319,11 +318,8 @@ bool TonePwmConfig::applyConfiguration(uint32_t pin) {
|
||||
}
|
||||
this->stopPlayback(false);
|
||||
|
||||
this->arduino_pin = pin;
|
||||
this->nrf_pin = g_ADigitalPinMap[pin];
|
||||
|
||||
uint32_t pins[NRF_PWM_CHANNEL_COUNT] = {
|
||||
this->nrf_pin,
|
||||
g_ADigitalPinMap[pin],
|
||||
NRF_PWM_PIN_NOT_CONNECTED,
|
||||
NRF_PWM_PIN_NOT_CONNECTED,
|
||||
NRF_PWM_PIN_NOT_CONNECTED
|
||||
@@ -331,19 +327,13 @@ bool TonePwmConfig::applyConfiguration(uint32_t pin) {
|
||||
|
||||
nrf_pwm_pins_set(_PWMInstance, pins); // must set pins before enabling
|
||||
nrf_pwm_enable(_PWMInstance);
|
||||
nrf_pwm_configure(_PWMInstance, TonePwmConfig::clock_frequency, TonePwmConfig::pwm_mode, this->time_period);
|
||||
nrf_pwm_decoder_set(_PWMInstance, TonePwmConfig::load_mode, TonePwmConfig::step_mode);
|
||||
nrf_pwm_configure(_PWMInstance, NRF_PWM_CLK_125kHz, NRF_PWM_MODE_UP, this->time_period);
|
||||
nrf_pwm_decoder_set(_PWMInstance, NRF_PWM_LOAD_COMMON, NRF_PWM_STEP_AUTO);
|
||||
nrf_pwm_shorts_set(_PWMInstance, this->shorts);
|
||||
nrf_pwm_int_set(_PWMInstance, 0);
|
||||
|
||||
// static sequence value ... This is the value actually used
|
||||
// during playback ... do NOT modify without semaphore *and*
|
||||
// having ensured playback has stopped!
|
||||
static uint16_t seq_values; // static value
|
||||
seq_values = this->duty_with_polarity;
|
||||
|
||||
nrf_pwm_seq_ptr_set(_PWMInstance, 0, &seq_values);
|
||||
nrf_pwm_seq_ptr_set(_PWMInstance, 1, &seq_values);
|
||||
nrf_pwm_seq_ptr_set(_PWMInstance, 0, &this->duty_with_polarity);
|
||||
nrf_pwm_seq_ptr_set(_PWMInstance, 1, &this->duty_with_polarity);
|
||||
nrf_pwm_seq_cnt_set(_PWMInstance, 0, 1);
|
||||
nrf_pwm_seq_cnt_set(_PWMInstance, 1, 1);
|
||||
|
||||
@@ -362,23 +352,21 @@ bool TonePwmConfig::applyConfiguration(uint32_t pin) {
|
||||
nrf_pwm_event_clear(_PWMInstance, NRF_PWM_EVENT_LOOPSDONE);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TonePwmConfig::startPlayback(void) {
|
||||
if (!this->ensurePwmPeripheralOwnership()) {
|
||||
LOG_LV1("TON", "PWM peripheral not available for playback");
|
||||
LOG_LV1("Tone", "PWM peripheral not available for playback");
|
||||
return false;
|
||||
}
|
||||
nrf_pwm_task_trigger(_PWMInstance, this->task_to_start);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TonePwmConfig::stopPlayback(bool releaseOwnership) {
|
||||
|
||||
bool notInIsr = !isInISR();
|
||||
|
||||
if (!_HwPWM->isOwner(TonePwmConfig::toneToken)) {
|
||||
if (notInIsr) {
|
||||
LOG_LV2("TON", "Attempt to set noTone when not the owner of the PWM peripheral. Ignoring call....");
|
||||
}
|
||||
return false;
|
||||
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;
|
||||
}
|
||||
// ensure stopped and then disable
|
||||
if (_is_pwm_enabled(_PWMInstance)) {
|
||||
@@ -388,7 +376,8 @@ bool TonePwmConfig::stopPlayback(bool releaseOwnership) {
|
||||
}
|
||||
|
||||
if (releaseOwnership) {
|
||||
_HwPWM->releaseOwnership(TonePwmConfig::toneToken);
|
||||
_HwPWM->releaseOwnership(TonePwmConfig::TONE_TOKEN);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -46,8 +46,12 @@
|
||||
extern "C"
|
||||
{
|
||||
|
||||
enum
|
||||
{
|
||||
ANALOG_TOKEN = 0x676f6c41 // 'A' 'l' 'o' 'g'
|
||||
};
|
||||
|
||||
static uint8_t _analogResolution = 8; // default is 256 levels
|
||||
static uintptr_t _analogToken = 0x676f6c41; // 'A' 'l' 'o' 'g'
|
||||
|
||||
/**
|
||||
* This will apply to all PWM Hardware currently used by analogWrite(),
|
||||
@@ -59,8 +63,10 @@ void analogWriteResolution( uint8_t res )
|
||||
_analogResolution = res;
|
||||
for (int i = 0; i<HWPWM_MODULE_NUM; i++)
|
||||
{
|
||||
if (!HwPWMx[i]->isOwner(_analogToken)) continue;
|
||||
HwPWMx[i]->setResolution(res);
|
||||
if (HwPWMx[i]->isOwner(ANALOG_TOKEN))
|
||||
{
|
||||
HwPWMx[i]->setResolution(res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,39 +79,30 @@ void analogWriteResolution( uint8_t res )
|
||||
*/
|
||||
void analogWrite( uint32_t pin, uint32_t value )
|
||||
{
|
||||
/* If the pin is already in use for analogWrite, this should be fast
|
||||
If the pin is not already in use, then it's OK to take slightly more time to setup
|
||||
*/
|
||||
|
||||
// first, handle the case where the pin is already in use by analogWrite()
|
||||
for(int i=0; i<HWPWM_MODULE_NUM; i++)
|
||||
{
|
||||
if (!HwPWMx[i]->isOwner(_analogToken)) {
|
||||
continue; // skip if not owner of this PWM instance
|
||||
if (HwPWMx[i]->isOwner(ANALOG_TOKEN))
|
||||
{
|
||||
int const ch = HwPWMx[i]->pin2channel(pin);
|
||||
if (ch >= 0)
|
||||
{
|
||||
HwPWMx[i]->writeChannel(ch, value);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
int const ch = HwPWMx[i]->pin2channel(pin);
|
||||
if (ch < 0) {
|
||||
continue; // pin not in use by this PWM instance
|
||||
}
|
||||
HwPWMx[i]->writeChannel(ch, value);
|
||||
return;
|
||||
}
|
||||
|
||||
// Next, handle the case where can add the pin to a PWM instance already owned by analogWrite()
|
||||
for(int i=0; i<HWPWM_MODULE_NUM; i++)
|
||||
{
|
||||
if (!HwPWMx[i]->isOwner(_analogToken)) {
|
||||
continue;
|
||||
if ( HwPWMx[i]->isOwner(ANALOG_TOKEN) && HwPWMx[i]->addPin(pin) )
|
||||
{
|
||||
// successfully added the pin, so write the value also
|
||||
HwPWMx[i]->writePin(pin, value);
|
||||
LOG_LV2("Analog", "Added pin %" PRIu32 " to already-owned PWM %d", pin, i);
|
||||
return;
|
||||
}
|
||||
if (!HwPWMx[i]->addPin(pin)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// successfully added the pin, so write the value also
|
||||
LOG_LV2("ANA", "Added pin %" PRIu32 " to already-owned PWM %d", pin, i);
|
||||
HwPWMx[i]->writePin(pin, value);
|
||||
return;
|
||||
}
|
||||
|
||||
// Attempt to acquire a new HwPWMx instance ... but only where
|
||||
@@ -113,25 +110,19 @@ void analogWrite( uint32_t pin, uint32_t value )
|
||||
// 2. it currently has no pins in use.
|
||||
for(int i=0; i<HWPWM_MODULE_NUM; i++)
|
||||
{
|
||||
if (!HwPWMx[i]->takeOwnership(_analogToken)) {
|
||||
continue;
|
||||
if (HwPWMx[i]->takeOwnership(ANALOG_TOKEN))
|
||||
{
|
||||
// apply the cached analog resolution to newly owned instances
|
||||
HwPWMx[i]->setResolution(_analogResolution);
|
||||
HwPWMx[i]->addPin(pin);
|
||||
HwPWMx[i]->writePin(pin, value);
|
||||
LOG_LV2("Analog", "took ownership of, and added pin %" PRIu32 " to, PWM %d", pin, i);
|
||||
return;
|
||||
}
|
||||
|
||||
// apply the cached analog resolution to newly owned instances
|
||||
HwPWMx[i]->setResolution(_analogResolution);
|
||||
HwPWMx[i]->addPin(pin);
|
||||
HwPWMx[i]->writePin(pin, value);
|
||||
LOG_LV2("ANA", "took ownership of, and added pin %" PRIu32 " to, PWM %d", pin, i);
|
||||
return;
|
||||
}
|
||||
|
||||
// failed to allocate a HwPWM instance.
|
||||
// output appropriate debug message.
|
||||
LOG_LV1("ANA", "Unable to find a free PWM peripheral");
|
||||
// TODO: Add additional diagnostics to function at higher log levels
|
||||
LOG_LV1("Analog", "Unable to find a free PWM peripheral");
|
||||
return;
|
||||
}
|
||||
|
||||
} // end extern "C"
|
||||
|
||||
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
// Circuit Playground Birthday Candles
|
||||
//
|
||||
// All the NeoPixels will flicker like candles and the speaker will
|
||||
// play the tune to Happy Birthday continuously. Blow on the board
|
||||
// to slowly blow out all the candles and hear a victory song!
|
||||
// Controls:
|
||||
// - Slide switch: Move this to +/on to hear music or -/off to disable sound.
|
||||
// - Left button: Hold this down and press reset to use a rainbow flicker color.
|
||||
// - Right button: Hold this down and press reset to use a simple solid color (no flicker).
|
||||
// - If neither left or right button are pressed at startup then a flame-like
|
||||
// flickering animation will be used.
|
||||
// After all the candles are blown out press reset to reset and start over!
|
||||
//
|
||||
// Author: Tony DiCola
|
||||
// License: MIT (https://opensource.org/licenses/MIT)
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
// PIN_BUZZER should be defined by the supported variant e.g CPlay Bluefruit or CLUE.
|
||||
// Otherwise please define the pin you would like to use for tone output
|
||||
#ifndef PIN_BUZZER
|
||||
#define PIN_BUZZER 5
|
||||
#endif
|
||||
|
||||
|
||||
// A few music note frequencies as defined in this tone example:
|
||||
// https://www.arduino.cc/en/Tutorial/toneMelody
|
||||
#define NOTE_C4 262
|
||||
#define NOTE_CS4 277
|
||||
#define NOTE_D4 294
|
||||
#define NOTE_DS4 311
|
||||
#define NOTE_E4 330
|
||||
#define NOTE_F4 349
|
||||
#define NOTE_FS4 370
|
||||
#define NOTE_G4 392
|
||||
#define NOTE_GS4 415
|
||||
#define NOTE_A4 440
|
||||
#define NOTE_AS4 466
|
||||
#define NOTE_B4 494
|
||||
#define NOTE_C5 523
|
||||
#define NOTE_CS5 554
|
||||
#define NOTE_D5 587
|
||||
#define NOTE_DS5 622
|
||||
#define NOTE_E5 659
|
||||
#define NOTE_F5 698
|
||||
#define NOTE_FS5 740
|
||||
#define NOTE_G5 784
|
||||
#define NOTE_GS5 831
|
||||
#define NOTE_A5 880
|
||||
#define NOTE_AS5 932
|
||||
#define NOTE_B5 988
|
||||
|
||||
// Define note durations. You only need to adjust the whole note
|
||||
// time and other notes will be subdivided from it directly.
|
||||
#define WHOLE 2200 // Length of time in milliseconds of a whole note (i.e. a full bar).
|
||||
#define HALF WHOLE/2
|
||||
#define QUARTER HALF/2
|
||||
#define EIGHTH QUARTER/2
|
||||
#define EIGHTH_TRIPLE QUARTER/3
|
||||
#define SIXTEENTH EIGHTH/2
|
||||
|
||||
// Play a note of the specified frequency and for the specified duration.
|
||||
// Hold is an optional bool that specifies if this note should be held a
|
||||
// little longer, i.e. for eigth notes that are tied together.
|
||||
// While waiting for a note to play the waitBreath delay function is used
|
||||
// so breath detection and pixel animation continues to run. No tones
|
||||
// will play if the slide switch is in the -/off position or all the
|
||||
// candles have been blown out.
|
||||
void playNote(int frequency, int duration, bool hold=false, bool measure=true) {
|
||||
(void) measure;
|
||||
|
||||
if (hold) {
|
||||
// For a note that's held play it a little longer than the specified duration
|
||||
// so it blends into the next tone (but there's still a small delay to
|
||||
// hear the next note).
|
||||
tone(PIN_BUZZER, frequency, duration + duration/32);
|
||||
}
|
||||
else {
|
||||
// For a note that isn't held just play it for the specified duration.
|
||||
tone(PIN_BUZZER, frequency, duration);
|
||||
}
|
||||
|
||||
delay(duration + duration/16);
|
||||
}
|
||||
|
||||
// Song to play when the candles are blown out.
|
||||
void celebrateSong() {
|
||||
// Play a little charge melody, from:
|
||||
// https://en.wikipedia.org/wiki/Charge_(fanfare)
|
||||
// Note the explicit boolean parameters in particular the measure=false
|
||||
// at the end. This means the notes will play without any breath measurement
|
||||
// logic. Without this false value playNote will try to keep waiting for candles
|
||||
// to blow out during the celebration song!
|
||||
playNote(NOTE_G4, EIGHTH_TRIPLE, true, false);
|
||||
playNote(NOTE_C5, EIGHTH_TRIPLE, true, false);
|
||||
playNote(NOTE_E5, EIGHTH_TRIPLE, false, false);
|
||||
playNote(NOTE_G5, EIGHTH, true, false);
|
||||
playNote(NOTE_E5, SIXTEENTH, false);
|
||||
playNote(NOTE_G5, HALF, false);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
// Initialize serial output and Circuit Playground library.
|
||||
Serial.begin(115200);
|
||||
|
||||
pinMode(PIN_BUZZER, OUTPUT);
|
||||
digitalWrite(PIN_BUZZER, LOW);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Play happy birthday tune, from:
|
||||
// http://www.irish-folk-songs.com/happy-birthday-tin-whistle-sheet-music.html#.WXFJMtPytBw
|
||||
// Inside each playNote call it will play a note and drive the NeoPixel animation
|
||||
// and check for a breath against the sound sensor. Once all the candles are blown out
|
||||
// the playNote calls will stop playing music.
|
||||
playNote(NOTE_D4, EIGHTH, true);
|
||||
playNote(NOTE_D4, EIGHTH);
|
||||
playNote(NOTE_E4, QUARTER); // Bar 1
|
||||
playNote(NOTE_D4, QUARTER);
|
||||
playNote(NOTE_G4, QUARTER);
|
||||
playNote(NOTE_FS4, HALF); // Bar 2
|
||||
playNote(NOTE_D4, EIGHTH, true);
|
||||
playNote(NOTE_D4, EIGHTH);
|
||||
playNote(NOTE_E4, QUARTER); // Bar 3
|
||||
playNote(NOTE_D4, QUARTER);
|
||||
playNote(NOTE_A4, QUARTER);
|
||||
playNote(NOTE_G4, HALF); // Bar 4
|
||||
playNote(NOTE_D4, EIGHTH, true);
|
||||
playNote(NOTE_D4, EIGHTH);
|
||||
playNote(NOTE_D5, QUARTER); // Bar 5
|
||||
playNote(NOTE_B4, QUARTER);
|
||||
playNote(NOTE_G4, QUARTER);
|
||||
playNote(NOTE_FS4, QUARTER); // Bar 6
|
||||
playNote(NOTE_E4, QUARTER);
|
||||
playNote(NOTE_C5, EIGHTH, true);
|
||||
playNote(NOTE_C5, EIGHTH);
|
||||
playNote(NOTE_B4, QUARTER); // Bar 7
|
||||
playNote(NOTE_G4, QUARTER);
|
||||
playNote(NOTE_A4, QUARTER);
|
||||
playNote(NOTE_G4, HALF); // Bar 8
|
||||
|
||||
celebrateSong();
|
||||
|
||||
// One second pause before repeating the loop and playing
|
||||
delay(1000);
|
||||
}
|
||||
@@ -111,8 +111,8 @@ public:
|
||||
bool attached(); // return true if this servo is attached, otherwise false
|
||||
private:
|
||||
uint8_t servoIndex; // index into the channel data for this servo
|
||||
int16_t min; // minimum pulse in µs
|
||||
int16_t max; // maximum pulse in µs
|
||||
int16_t min; // minimum pulse in µs
|
||||
int16_t max; // maximum pulse in µs
|
||||
|
||||
HardwarePWM* pwm;
|
||||
};
|
||||
|
||||
@@ -21,7 +21,10 @@
|
||||
#include <Arduino.h>
|
||||
#include <Servo.h>
|
||||
|
||||
static uintptr_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
|
||||
@@ -47,41 +50,45 @@ uint8_t Servo::attach(int pin, int min, int max)
|
||||
return INVALID_SERVO;
|
||||
}
|
||||
bool succeeded = false;
|
||||
pinMode(pin, OUTPUT); // set servo pin to output
|
||||
servos[this->servoIndex].Pin.nbr = pin;
|
||||
|
||||
if (min < MIN_PULSE_WIDTH) {
|
||||
if (min < MIN_PULSE_WIDTH)
|
||||
{
|
||||
min = MIN_PULSE_WIDTH;
|
||||
}
|
||||
if (max > MAX_PULSE_WIDTH) {
|
||||
|
||||
if (max > MAX_PULSE_WIDTH)
|
||||
{
|
||||
max = MAX_PULSE_WIDTH;
|
||||
}
|
||||
|
||||
//fix min if conversion to pulse cycle value is too low
|
||||
if ( (min/DUTY_CYCLE_RESOLUTION) * DUTY_CYCLE_RESOLUTION < min) {
|
||||
if ( (min / DUTY_CYCLE_RESOLUTION) * DUTY_CYCLE_RESOLUTION < min )
|
||||
{
|
||||
min += DUTY_CYCLE_RESOLUTION;
|
||||
}
|
||||
|
||||
this->min = min;
|
||||
this->max = max;
|
||||
|
||||
servos[this->servoIndex].Pin.isActive = true;
|
||||
this->min = min;
|
||||
this->max = max;
|
||||
|
||||
// Adafruit, add pin to 1 of available Hw PWM
|
||||
// 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)) {
|
||||
for ( int i = 0; i < HWPWM_MODULE_NUM; i++ )
|
||||
{
|
||||
if ( HwPWMx[i]->isOwner(SERVO_TOKEN) && HwPWMx[i]->addPin(pin) )
|
||||
{
|
||||
this->pwm = HwPWMx[i];
|
||||
succeeded = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// if could not add to existing owned PWM modules, try to add to a new PWM module
|
||||
if (!succeeded) {
|
||||
for(int i=0; i<HWPWM_MODULE_NUM; i++) {
|
||||
if (HwPWMx[i]->takeOwnership(_servoToken) &&
|
||||
HwPWMx[i]->addPin(pin)) {
|
||||
if ( !succeeded )
|
||||
{
|
||||
for ( int i = 0; i < HWPWM_MODULE_NUM; i++ )
|
||||
{
|
||||
if ( HwPWMx[i]->takeOwnership(SERVO_TOKEN) && HwPWMx[i]->addPin(pin) )
|
||||
{
|
||||
this->pwm = HwPWMx[i];
|
||||
succeeded = true;
|
||||
break;
|
||||
@@ -89,11 +96,20 @@ uint8_t Servo::attach(int pin, int min, int max)
|
||||
}
|
||||
}
|
||||
|
||||
if (succeeded) { // do not use this->pwm unless success above!
|
||||
if ( succeeded )
|
||||
{
|
||||
pinMode(pin, OUTPUT);
|
||||
servos[this->servoIndex].Pin.nbr = pin;
|
||||
servos[this->servoIndex].Pin.isActive = true;
|
||||
|
||||
this->pwm->setMaxValue(MAXVALUE);
|
||||
this->pwm->setClockDiv(CLOCKDIV);
|
||||
|
||||
return this->servoIndex;
|
||||
}else
|
||||
{
|
||||
return INVALID_SERVO;
|
||||
}
|
||||
return succeeded ? this->servoIndex : INVALID_SERVO; // return INVALID_SERVO on failure (zero is a valid servo index)
|
||||
}
|
||||
|
||||
void Servo::detach()
|
||||
@@ -104,13 +120,14 @@ void Servo::detach()
|
||||
|
||||
uint8_t const pin = servos[this->servoIndex].Pin.nbr;
|
||||
servos[this->servoIndex].Pin.isActive = false;
|
||||
|
||||
// remove pin from HW PWM
|
||||
HardwarePWM * pwm = this->pwm;
|
||||
this->pwm = nullptr;
|
||||
pwm->removePin(pin);
|
||||
if (pwm->usedChannelCount() == 0) {
|
||||
pwm->stop(); // disables peripheral so can release ownership
|
||||
pwm->releaseOwnership(_servoToken);
|
||||
pwm->releaseOwnership(SERVO_TOKEN);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user