|
| 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