Skip to content

Commit 63bc30c

Browse files
authored
LEDC Refactoring - Peripheral manager implemented (#8126)
* LEDC periman implementation * Fix examples * Rework tone * Update ledc docs * fix missing bracket * Update analog funtions esp32-hal.h * Update CameraWebServer example * Fix HiFreq_ADC example * minor fixes - typos * Avoid calling tone/notone when tone already runs on dif. pin * Remove unused channels_resolution
1 parent 59f7975 commit 63bc30c

File tree

10 files changed

+309
-308
lines changed

10 files changed

+309
-308
lines changed

Diff for: cores/esp32/Tone.cpp

+47-53
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,18 @@
66

77
static TaskHandle_t _tone_task = NULL;
88
static QueueHandle_t _tone_queue = NULL;
9-
static uint8_t _channel = 0;
9+
static int8_t _pin = -1;
1010

1111
typedef enum{
1212
TONE_START,
13-
TONE_END,
14-
TONE_SET_CHANNEL
13+
TONE_END
1514
} tone_cmd_t;
1615

1716
typedef struct{
1817
tone_cmd_t tone_cmd;
1918
uint8_t pin;
2019
unsigned int frequency;
2120
unsigned long duration;
22-
uint8_t channel;
2321
} tone_msg_t;
2422

2523
static void tone_task(void*){
@@ -28,28 +26,28 @@ static void tone_task(void*){
2826
xQueueReceive(_tone_queue, &tone_msg, portMAX_DELAY);
2927
switch(tone_msg.tone_cmd){
3028
case TONE_START:
31-
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);
29+
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);
3230

33-
log_d("Setup LED controll on channel %d", _channel);
34-
ledcAttachPin(tone_msg.pin, _channel);
35-
ledcWriteTone(_channel, tone_msg.frequency);
31+
if (_pin == -1) {
32+
if (ledcAttach(tone_msg.pin, tone_msg.frequency, 10) == 0) {
33+
log_e("Tone start failed");
34+
break;
35+
}
36+
_pin = tone_msg.pin;
37+
}
38+
ledcWriteTone(tone_msg.pin, tone_msg.frequency);
3639

3740
if(tone_msg.duration){
3841
delay(tone_msg.duration);
39-
ledcDetachPin(tone_msg.pin);
40-
ledcWriteTone(_channel, 0);
42+
ledcWriteTone(tone_msg.pin, 0);
4143
}
4244
break;
4345

4446
case TONE_END:
4547
log_d("Task received from queue TONE_END: pin=%d", tone_msg.pin);
46-
ledcDetachPin(tone_msg.pin);
47-
ledcWriteTone(_channel, 0);
48-
break;
49-
50-
case TONE_SET_CHANNEL:
51-
log_d("Task received from queue TONE_SET_CHANNEL: channel=%d", tone_msg.channel);
52-
_channel = tone_msg.channel;
48+
ledcWriteTone(tone_msg.pin, 0);
49+
ledcDetach(tone_msg.pin);
50+
_pin = -1;
5351
break;
5452

5553
default: ; // do nothing
@@ -87,49 +85,45 @@ static int tone_init(){
8785
return 1; // OK
8886
}
8987

90-
void setToneChannel(uint8_t channel){
91-
log_d("channel=%d", channel);
92-
if(tone_init()){
93-
tone_msg_t tone_msg = {
94-
.tone_cmd = TONE_SET_CHANNEL,
95-
.pin = 0, // Ignored
96-
.frequency = 0, // Ignored
97-
.duration = 0, // Ignored
98-
.channel = channel
99-
};
100-
xQueueSend(_tone_queue, &tone_msg, portMAX_DELAY);
101-
}
102-
}
103-
104-
void noTone(uint8_t _pin){
88+
void noTone(uint8_t pin){
10589
log_d("noTone was called");
106-
if(tone_init()){
107-
tone_msg_t tone_msg = {
108-
.tone_cmd = TONE_END,
109-
.pin = _pin,
110-
.frequency = 0, // Ignored
111-
.duration = 0, // Ignored
112-
.channel = 0 // Ignored
113-
};
114-
xQueueSend(_tone_queue, &tone_msg, portMAX_DELAY);
90+
if(_pin == pin) {
91+
if(tone_init()){
92+
tone_msg_t tone_msg = {
93+
.tone_cmd = TONE_END,
94+
.pin = pin,
95+
.frequency = 0, // Ignored
96+
.duration = 0, // Ignored
97+
};
98+
xQueueSend(_tone_queue, &tone_msg, portMAX_DELAY);
99+
}
100+
}
101+
else {
102+
log_e("Tone is not running on given pin %d", pin);
115103
}
116104
}
117105

118106
// parameters:
119-
// _pin - pin number which will output the signal
107+
// pin - pin number which will output the signal
120108
// frequency - PWM frequency in Hz
121109
// duration - time in ms - how long will the signal be outputted.
122110
// If not provided, or 0 you must manually call noTone to end output
123-
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration){
124-
log_d("_pin=%d, frequency=%u Hz, duration=%lu ms", _pin, frequency, duration);
125-
if(tone_init()){
126-
tone_msg_t tone_msg = {
127-
.tone_cmd = TONE_START,
128-
.pin = _pin,
129-
.frequency = frequency,
130-
.duration = duration,
131-
.channel = 0 // Ignored
132-
};
133-
xQueueSend(_tone_queue, &tone_msg, portMAX_DELAY);
111+
void tone(uint8_t pin, unsigned int frequency, unsigned long duration){
112+
log_d("pin=%d, frequency=%u Hz, duration=%lu ms", pin, frequency, duration);
113+
if(_pin == -1 || _pin == pin) {
114+
if(tone_init()){
115+
tone_msg_t tone_msg = {
116+
.tone_cmd = TONE_START,
117+
.pin = pin,
118+
.frequency = frequency,
119+
.duration = duration,
120+
};
121+
xQueueSend(_tone_queue, &tone_msg, portMAX_DELAY);
122+
return;
123+
}
124+
}
125+
else {
126+
log_e("Tone is still running on pin %d, call noTone(%d) first!", _pin, _pin);
127+
return;
134128
}
135129
}

0 commit comments

Comments
 (0)