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