Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ set(CORE_SRCS
cores/esp32/stdlib_noniso.c
cores/esp32/Stream.cpp
cores/esp32/StreamString.cpp
cores/esp32/Tone.cpp
cores/esp32/HWCDC.cpp
cores/esp32/USB.cpp
cores/esp32/USBCDC.cpp
Expand Down
3 changes: 3 additions & 0 deletions cores/esp32/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ extern "C" void configTime(long gmtOffset_sec, int daylightOffset_sec,
extern "C" void configTzTime(const char* tz,
const char* server1, const char* server2 = nullptr, const char* server3 = nullptr);

void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0, unsigned long duty = 50);
void noTone(uint8_t _pin);

// WMath prototypes
long random(long);
#endif /* __cplusplus */
Expand Down
23 changes: 23 additions & 0 deletions cores/esp32/Tone.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <Arduino.h>
#include "esp32-hal-ledc.h"

void noTone(uint8_t _pin){
ledcDetachPin(_pin);
}

// parameters:
// _pin - pin number which will output the signal
// frequency - PWM frequency in Hz
// duration - time in ms - how long will the signal be outputted.
// If not provided, or 0 you must manually call noTone to end output
// duty - PWM duty in % (default 50%)
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration, unsigned long duty){
ledcSetup(0, frequency, 11);
ledcAttachPin(_pin, 0);

ledcWrite(0, duty * 20);
if(duration){
vTaskDelay(pdMS_TO_TICKS(duration));
noTone(_pin);
}
}