Skip to content

Commit 532d5f2

Browse files
authored
feat(tone): missing set tone channel implementation (#10305)
Adds the implementation to setToneChannel() declared in Arduino.h, but removed when movin from Arduino 2.x to 3.x.
1 parent 2853b7c commit 532d5f2

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

cores/esp32/Tone.cpp

+26-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
#include "freertos/queue.h"
55
#include "freertos/semphr.h"
66

7+
#if SOC_LEDC_SUPPORTED
78
static TaskHandle_t _tone_task = NULL;
89
static QueueHandle_t _tone_queue = NULL;
910
static int8_t _pin = -1;
11+
static uint8_t _channel = 255;
1012

1113
typedef enum {
1214
TONE_START,
@@ -20,6 +22,12 @@ typedef struct {
2022
unsigned long duration;
2123
} tone_msg_t;
2224

25+
#ifdef SOC_LEDC_SUPPORT_HS_MODE
26+
#define LEDC_CHANNELS (SOC_LEDC_CHANNEL_NUM << 1)
27+
#else
28+
#define LEDC_CHANNELS (SOC_LEDC_CHANNEL_NUM)
29+
#endif
30+
2331
static void tone_task(void *) {
2432
tone_msg_t tone_msg;
2533
while (1) {
@@ -29,7 +37,13 @@ static void tone_task(void *) {
2937
log_d("Task received from queue TONE_START: pin=%d, frequency=%u Hz, duration=%lu ms", tone_msg.pin, tone_msg.frequency, tone_msg.duration);
3038

3139
if (_pin == -1) {
32-
if (ledcAttach(tone_msg.pin, tone_msg.frequency, 10) == 0) {
40+
bool ret = true;
41+
if (_channel == 255) {
42+
ret = ledcAttach(tone_msg.pin, tone_msg.frequency, 10);
43+
} else {
44+
ret = ledcAttachChannel(tone_msg.pin, tone_msg.frequency, 10, _channel);
45+
}
46+
if (!ret) {
3347
log_e("Tone start failed");
3448
break;
3549
}
@@ -73,7 +87,7 @@ static int tone_init() {
7387
"toneTask", // Name of the task
7488
3500, // Stack size in words
7589
NULL, // Task input parameter
76-
1, // Priority of the task
90+
10, // Priority of the task must be higher than Arduino task
7791
&_tone_task // Task handle.
7892
);
7993
if (_tone_task == NULL) {
@@ -126,3 +140,13 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration) {
126140
return;
127141
}
128142
}
143+
144+
void setToneChannel(uint8_t channel) {
145+
if (channel >= LEDC_CHANNELS) {
146+
log_e("Channel %u is not available (maximum %u)!", channel, LEDC_CHANNELS);
147+
return;
148+
}
149+
_channel = channel;
150+
}
151+
152+
#endif /* SOC_LEDC_SUPPORTED */

0 commit comments

Comments
 (0)