#include "sound.h" #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" //#include "driver/gpio.h" #include "esp_log.h" #include "sdkconfig.h" #include "driver/dac.h" #include "math.h" #define DAC_CHANNEL 1 #define DAC_PIN GPIO_NUM_25 // GPIO pin for DAC output GPIO25=Channel 1 #define TONE_FREQ 440 // Frequency of the tone in Hz #define TONE_DURATION 1000 // Duration of the tone in ms #define QUIET_DURATION 1000 // Duration of the quiet period in ms bool soundEnabled = false; static void play_tone(int frequency, float duration, int quietDuration) { int semitones = 0; int t_freq = frequency * pow(2.0, semitones / 12.0); int8_t cw_offset = 0; dac_cw_config_t cosineConf = {DAC_CHANNEL_1, DAC_CW_SCALE_8, DAC_CW_PHASE_0, t_freq, cw_offset}; if (frequency == 0) { dac_output_disable(DAC_CHANNEL_1); } else { dac_cw_generator_config(&cosineConf); dac_output_enable(DAC_CHANNEL_1); } // Loop with 50mS loops until the duration is complete for(int i = 0; i < duration; i+=50) { vTaskDelay(pdMS_TO_TICKS(50)); if( soundEnabled == false ) { break; } } dac_output_disable(DAC_CHANNEL_1); } static void setupSound() { int8_t cw_offset = 0; dac_cw_config_t cosineConf = {DAC_CHANNEL_1, DAC_CW_SCALE_8, DAC_CW_PHASE_0, 440, cw_offset}; dac_output_disable(DAC_CHANNEL_1); dac_cw_generator_config(&cosineConf); dac_cw_generator_enable(); dac_output_disable(DAC_CHANNEL_1); ESP_LOGI("SOUND", "Sound setup"); } static void sound_task(void *pvParameters) { ESP_LOGI("SOUND", "Sound task starts...."); setupSound(); while (1) { vTaskDelay(pdMS_TO_TICKS(1000)); if( soundEnabled ) { play_tone(TONE_FREQ, TONE_DURATION, QUIET_DURATION); } } } void initSound() { xTaskCreate(sound_task, "sound_task", 2048, NULL, 10, NULL); ESP_LOGI("SOUND", "Sound initialized"); } void enableNotificationSound(bool enable) { if (soundEnabled != enable) { ESP_LOGI("SOUND", "Notification sound %s", enable ? "enabled" : "disabled"); } soundEnabled = enable; }