Skip to content

Commit 68daea4

Browse files
authored
Implemented tone and noTone; fixes espressif#980 (espressif#6402)
* Implemented tone * Tone uses queue; implemented setToneChannel
1 parent 52e0181 commit 68daea4

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ set(CORE_SRCS
5757
cores/esp32/stdlib_noniso.c
5858
cores/esp32/Stream.cpp
5959
cores/esp32/StreamString.cpp
60+
cores/esp32/Tone.cpp
6061
cores/esp32/HWCDC.cpp
6162
cores/esp32/USB.cpp
6263
cores/esp32/USBCDC.cpp

cores/esp32/Arduino.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ extern "C" void configTime(long gmtOffset_sec, int daylightOffset_sec,
195195
extern "C" void configTzTime(const char* tz,
196196
const char* server1, const char* server2 = nullptr, const char* server3 = nullptr);
197197

198+
void setToneChannel(uint8_t channel = 0);
199+
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
200+
void noTone(uint8_t _pin);
201+
198202
// WMath prototypes
199203
long random(long);
200204
#endif /* __cplusplus */

cores/esp32/Tone.cpp

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#include <Arduino.h>
2+
#include "esp32-hal-ledc.h"
3+
#include "freertos/task.h"
4+
#include "freertos/queue.h"
5+
#include "freertos/semphr.h"
6+
7+
static TaskHandle_t _tone_task = NULL;
8+
static QueueHandle_t _tone_queue = NULL;
9+
static uint8_t _channel = 0;
10+
11+
typedef enum{
12+
TONE_START,
13+
TONE_END,
14+
TONE_SET_CHANNEL
15+
} tone_cmd_t;
16+
17+
typedef struct{
18+
tone_cmd_t tone_cmd;
19+
uint8_t pin;
20+
unsigned int frequency;
21+
unsigned long duration;
22+
uint8_t channel;
23+
} tone_msg_t;
24+
25+
static void tone_task(void*){
26+
tone_msg_t tone_msg;
27+
while(1){
28+
xQueueReceive(_tone_queue, &tone_msg, portMAX_DELAY);
29+
switch(tone_msg.tone_cmd){
30+
case TONE_START:
31+
log_d("Task received from queue TONE_START: _pin=%d, frequency=%u Hz, duration=%u ms", tone_msg.pin, tone_msg.frequency, tone_msg.duration);
32+
33+
log_d("Setup LED controll on channel %d", _channel);
34+
// ledcSetup(_channel, tone_msg.frequency, 11);
35+
// ledcAttachPin(tone_msg.pin, _channel);
36+
// ledcWrite(_channel, 1024);
37+
ledcWriteTone(_channel, tone_msg.frequency);
38+
ledcAttachPin(tone_msg.pin, _channel);
39+
40+
if(tone_msg.duration){
41+
delay(tone_msg.duration);
42+
ledcDetachPin(tone_msg.pin);
43+
ledcWriteTone(_channel, 0);
44+
}
45+
break;
46+
47+
case TONE_END:
48+
log_d("Task received from queue TONE_END: pin=%d", tone_msg.pin);
49+
ledcDetachPin(tone_msg.pin);
50+
ledcWriteTone(_channel, 0);
51+
break;
52+
53+
case TONE_SET_CHANNEL:
54+
log_d("Task received from queue TONE_SET_CHANNEL: channel=%d", tone_msg.channel);
55+
_channel = tone_msg.channel;
56+
break;
57+
58+
default: ; // do nothing
59+
} // switch
60+
} // infinite loop
61+
}
62+
63+
static int tone_init(){
64+
if(_tone_queue == NULL){
65+
log_v("Creating tone queue");
66+
_tone_queue = xQueueCreate(128, sizeof(tone_msg_t));
67+
if(_tone_queue == NULL){
68+
log_e("Could not create tone queue");
69+
return 0; // ERR
70+
}
71+
log_v("Tone queue created");
72+
}
73+
74+
if(_tone_task == NULL){
75+
log_v("Creating tone task");
76+
xTaskCreate(
77+
tone_task, // Function to implement the task
78+
"toneTask", // Name of the task
79+
3500, // Stack size in words
80+
NULL, // Task input parameter
81+
1, // Priority of the task
82+
&_tone_task // Task handle.
83+
);
84+
if(_tone_task == NULL){
85+
log_e("Could not create tone task");
86+
return 0; // ERR
87+
}
88+
log_v("Tone task created");
89+
}
90+
return 1; // OK
91+
}
92+
93+
void setToneChannel(uint8_t channel){
94+
log_d("channel=%d", channel);
95+
if(tone_init()){
96+
tone_msg_t tone_msg = {
97+
.tone_cmd = TONE_SET_CHANNEL,
98+
.channel = channel
99+
};
100+
xQueueSend(_tone_queue, &tone_msg, portMAX_DELAY);
101+
}
102+
}
103+
104+
void noTone(uint8_t _pin){
105+
log_d("noTone was called");
106+
if(tone_init()){
107+
tone_msg_t tone_msg = {
108+
.tone_cmd = TONE_END,
109+
.pin = _pin
110+
};
111+
xQueueSend(_tone_queue, &tone_msg, portMAX_DELAY);
112+
}
113+
}
114+
115+
// parameters:
116+
// _pin - pin number which will output the signal
117+
// frequency - PWM frequency in Hz
118+
// duration - time in ms - how long will the signal be outputted.
119+
// If not provided, or 0 you must manually call noTone to end output
120+
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration){
121+
log_d("_pin=%d, frequency=%u Hz, duration=%u ms", _pin, frequency, duration);
122+
if(tone_init()){
123+
tone_msg_t tone_msg = {
124+
.tone_cmd = TONE_START,
125+
.pin = _pin,
126+
.frequency = frequency,
127+
.duration = duration
128+
};
129+
xQueueSend(_tone_queue, &tone_msg, portMAX_DELAY);
130+
}
131+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy