From 5d92240f8964f59e3b6a6e5e265b2bed7b21d325 Mon Sep 17 00:00:00 2001 From: jgromes Date: Wed, 28 Oct 2020 11:24:05 +0100 Subject: [PATCH] Added support for tone on ESP32 (#190) --- src/Module.cpp | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/Module.cpp b/src/Module.cpp index 8d3e2e19..fe419e92 100644 --- a/src/Module.cpp +++ b/src/Module.cpp @@ -336,20 +336,36 @@ RADIOLIB_PIN_STATUS Module::digitalRead(RADIOLIB_PIN_TYPE pin) { } void Module::tone(RADIOLIB_PIN_TYPE pin, uint16_t value) { - // TODO add tone support for platforms without tone() - #ifndef RADIOLIB_TONE_UNSUPPORTED - if(pin != RADIOLIB_NC) { - ::tone(pin, value); + if(pin == RADIOLIB_NC) { + return; } + + #if !defined(RADIOLIB_TONE_UNSUPPORTED) + ::tone(pin, value); + #else + #if defined(ESP32) + // ESP32 - emulate tone() via LED driver on channel 15 + if(ledcRead(15)) { + return; + } + ledcAttachPin(pin, 15); + ledcWriteTone(15, value); + #endif #endif } void Module::noTone(RADIOLIB_PIN_TYPE pin) { - // TODO add tone support for platforms without noTone() - #ifndef RADIOLIB_TONE_UNSUPPORTED - if(pin != RADIOLIB_NC) { - ::noTone(pin); + if(pin == RADIOLIB_NC) { + return; } + + #if !defined(RADIOLIB_TONE_UNSUPPORTED) + ::noTone(pin); + #else + #if defined(ESP32) + ledcDetachPin(pin); + ledcWrite(15, 0); + #endif #endif }