Skip to content

LEDC Refactoring - Peripheral manager implemented #8126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 11, 2023
100 changes: 47 additions & 53 deletions cores/esp32/Tone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@

static TaskHandle_t _tone_task = NULL;
static QueueHandle_t _tone_queue = NULL;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, but I think that @me-no-dev want us to try to do not use Tasks within the peripherals.
I don't see a clear motivation for using a task in the Tone implementation...

Copy link
Member Author

@P-R-O-C-H-Y P-R-O-C-H-Y May 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe @PilnyTomas can take a look on this in separate PR, as he wrote this implementation :) (If I am right)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Motivation here is to be able to "setup" a melody and let it play, without having to wait for it to be over. Same goes for when you want to just set it to beep for 100ms and not having to wait. We could look into doing it blocking or maybe coming up with another idea.

static uint8_t _channel = 0;
static int8_t _pin = -1;

typedef enum{
TONE_START,
TONE_END,
TONE_SET_CHANNEL
TONE_END
} 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*){
Expand All @@ -28,28 +26,28 @@ static void tone_task(void*){
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("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 (_pin == -1) {
if (ledcAttach(tone_msg.pin, tone_msg.frequency, 10) == 0) {
log_e("Tone start failed");
break;
}
_pin = tone_msg.pin;
}
ledcWriteTone(tone_msg.pin, tone_msg.frequency);

if(tone_msg.duration){
delay(tone_msg.duration);
ledcDetachPin(tone_msg.pin);
ledcWriteTone(_channel, 0);
ledcWriteTone(tone_msg.pin, 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;
ledcWriteTone(tone_msg.pin, 0);
ledcDetach(tone_msg.pin);
_pin = -1;
break;

default: ; // do nothing
Expand Down Expand Up @@ -87,49 +85,45 @@ static int tone_init(){
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){
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);
if(_pin == pin) {
if(tone_init()){
tone_msg_t tone_msg = {
.tone_cmd = TONE_END,
.pin = pin,
.frequency = 0, // Ignored
.duration = 0, // Ignored
};
xQueueSend(_tone_queue, &tone_msg, portMAX_DELAY);
}
}
else {
log_e("Tone is not running on given pin %d", pin);
}
}

// parameters:
// _pin - pin number which will output the signal
// 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);
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(_pin == -1 || _pin == pin) {
if(tone_init()){
tone_msg_t tone_msg = {
.tone_cmd = TONE_START,
.pin = pin,
.frequency = frequency,
.duration = duration,
};
xQueueSend(_tone_queue, &tone_msg, portMAX_DELAY);
return;
}
}
else {
log_e("Tone is still running on pin %d, call noTone(%d) first!", _pin, _pin);
return;
}
}
Loading