mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-19 23:22:16 +03:00
* Allow specifying waveform generator in source code Allows code to explicitly specify which waveform generator it wants, without needing to use one of the 100 IDE menus or adding a `-D` compile-time define. Uses weakrefs to allow for apps to call `enablePhaseLockedWaveform();` within their `setup()` (or anywhere, really) and have the phase locked versions override the default waveform generators automatically. For example: ```` void setup() { // Uncomment following line to use phase-locked waveform generator // enablePhaseLockedWaveform(); Serial.begin(115200); pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output analogWriteRange(1000); } void loop() { analogWrite(LED_BUILTIN, 100); delay(1000); // Wait for a second analogWrite(LED_BUILTIN, 900); delay(2000); // Wait for two seconds (to demonstrate the active low LED) } ```` Also adds an example showing it's use. Address @dok-net's comments and also remove the _weak/_bound version of startWaveform() since it's invariant of the actual waveform generator.
90 lines
2.8 KiB
C++
90 lines
2.8 KiB
C++
/*
|
|
Tone.cpp
|
|
|
|
A Tone Generator Library for the ESP8266
|
|
|
|
Original Copyright (c) 2016 Ben Pirt. All rights reserved.
|
|
This file is part of the esp8266 core for Arduino environment.
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU Lesser General Public
|
|
License as published by the Free Software Foundation; either
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
License along with this library; if not, write to the Free Software
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
|
|
#include "Arduino.h"
|
|
#include "core_esp8266_waveform.h"
|
|
#include "user_interface.h"
|
|
|
|
static void _startTone(uint8_t _pin, uint32_t high, uint32_t low, uint32_t duration) {
|
|
if (_pin > 16) {
|
|
return;
|
|
}
|
|
|
|
// Stop any analogWrites (PWM) because they are a different generator
|
|
_stopPWM(_pin);
|
|
// If there's another Tone or startWaveform on this pin
|
|
// it will be changed on-the-fly (no need to stop it)
|
|
|
|
pinMode(_pin, OUTPUT);
|
|
|
|
high = std::max(high, (uint32_t)microsecondsToClockCycles(25)); // new 20KHz maximum tone frequency,
|
|
low = std::max(low, (uint32_t)microsecondsToClockCycles(25)); // (25us high + 25us low period = 20KHz)
|
|
|
|
duration = microsecondsToClockCycles(duration * 1000UL);
|
|
duration += high + low - 1;
|
|
duration -= duration % (high + low);
|
|
startWaveformClockCycles(_pin, high, low, duration);
|
|
}
|
|
|
|
|
|
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) {
|
|
if (frequency == 0) {
|
|
noTone(_pin);
|
|
} else {
|
|
uint32_t period = microsecondsToClockCycles(1000000UL) / frequency;
|
|
uint32_t high = period / 2;
|
|
uint32_t low = period - high;
|
|
_startTone(_pin, high, low, duration);
|
|
}
|
|
}
|
|
|
|
|
|
// Separate tone(float) to hopefully not pull in floating point libs unless
|
|
// it's called with a float.
|
|
void tone(uint8_t _pin, double frequency, unsigned long duration) {
|
|
if (frequency < 1.0) { // FP means no exact comparisons
|
|
noTone(_pin);
|
|
} else {
|
|
double period = (double)microsecondsToClockCycles(1000000UL) / frequency;
|
|
uint32_t high = (uint32_t)((period / 2.0) + 0.5);
|
|
uint32_t low = (uint32_t)(period + 0.5) - high;
|
|
_startTone(_pin, high, low, duration);
|
|
}
|
|
}
|
|
|
|
|
|
// Fix ambiguous tone() binding when adding in a duration
|
|
void tone(uint8_t _pin, int frequency, unsigned long duration) {
|
|
// Call the unsigned int version of the function explicitly
|
|
tone(_pin, (unsigned int)frequency, duration);
|
|
}
|
|
|
|
|
|
void noTone(uint8_t _pin) {
|
|
if (_pin > 16) {
|
|
return;
|
|
}
|
|
stopWaveform(_pin);
|
|
digitalWrite(_pin, 0);
|
|
}
|