|
@@ -2,79 +2,83 @@
|
|
|
#include <stdio.h>
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
#include "freertos/task.h"
|
|
|
-#include "driver/gpio.h"
|
|
|
+//#include "driver/gpio.h"
|
|
|
#include "esp_log.h"
|
|
|
#include "sdkconfig.h"
|
|
|
-#include "driver/ledc.h"
|
|
|
+#include "driver/dac.h"
|
|
|
+#include "math.h"
|
|
|
|
|
|
-#define PWM_PIN GPIO_NUM_19 // GPIO pin for PWM output
|
|
|
-#define PWM_FREQ 262 // Frequency for C4 (Middle C)
|
|
|
-#define PWM_CHANNEL LEDC_CHANNEL_0
|
|
|
-#define PWM_TIMER LEDC_TIMER_0
|
|
|
-#define PWM_RES LEDC_TIMER_8_BIT // 8-bit resolution (0-255)
|
|
|
+#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
|
|
|
|
|
|
-static void setupSound() {
|
|
|
-
|
|
|
- // Configure LEDC PWM Timer
|
|
|
- ledc_timer_config_t timer_conf = {
|
|
|
- .speed_mode = LEDC_LOW_SPEED_MODE,
|
|
|
- .duty_resolution = PWM_RES,
|
|
|
- .timer_num = PWM_TIMER,
|
|
|
- .freq_hz = PWM_FREQ,
|
|
|
- .clk_cfg = LEDC_AUTO_CLK,
|
|
|
- };
|
|
|
- ledc_timer_config(&timer_conf);
|
|
|
-
|
|
|
- // Configure LEDC PWM Channel
|
|
|
- ledc_channel_config_t channel_conf = {
|
|
|
- .gpio_num = PWM_PIN,
|
|
|
- .speed_mode = LEDC_LOW_SPEED_MODE,
|
|
|
- .channel = PWM_CHANNEL,
|
|
|
- .timer_sel = PWM_TIMER,
|
|
|
- .duty = 0, // Start at 0% volume
|
|
|
- };
|
|
|
- ledc_channel_config(&channel_conf);
|
|
|
+bool soundEnabled = false;
|
|
|
|
|
|
- ESP_LOGI("SOUND", "Sound setup");
|
|
|
-}
|
|
|
+static void play_tone(int frequency, float duration, int quietDuration) {
|
|
|
|
|
|
-static void sound_task(void *pvParameters) {
|
|
|
+ int semitones = 0;
|
|
|
|
|
|
- setupSound();
|
|
|
+ int t_freq = frequency * pow(2.0, semitones / 12.0);
|
|
|
|
|
|
- while (1) {
|
|
|
+ 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};
|
|
|
|
|
|
- ledc_set_duty(LEDC_LOW_SPEED_MODE, PWM_CHANNEL, 30);
|
|
|
- ledc_update_duty(LEDC_LOW_SPEED_MODE, PWM_CHANNEL);
|
|
|
+ if (frequency == 0) {
|
|
|
+ dac_output_disable(DAC_CHANNEL_1);
|
|
|
+ } else {
|
|
|
+ dac_cw_generator_config(&cosineConf);
|
|
|
+ dac_output_enable(DAC_CHANNEL_1);
|
|
|
+ }
|
|
|
|
|
|
- /*// Gradually increase volume (fade-in)
|
|
|
- for (int duty = 10; duty <= 127; duty += 5) {
|
|
|
- ledc_set_duty(LEDC_LOW_SPEED_MODE, PWM_CHANNEL, duty);
|
|
|
- ledc_update_duty(LEDC_LOW_SPEED_MODE, PWM_CHANNEL);
|
|
|
- vTaskDelay(pdMS_TO_TICKS(50));
|
|
|
- }*/
|
|
|
+ // 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);
|
|
|
+}
|
|
|
|
|
|
- vTaskDelay(pdMS_TO_TICKS(1000)); // Keep sound for 500ms
|
|
|
+static void setupSound() {
|
|
|
|
|
|
- ledc_set_duty(LEDC_LOW_SPEED_MODE, PWM_CHANNEL, 10);
|
|
|
- ledc_update_duty(LEDC_LOW_SPEED_MODE, PWM_CHANNEL);
|
|
|
+ int8_t cw_offset = 0;
|
|
|
+ dac_cw_config_t cosineConf = {DAC_CHANNEL_1, DAC_CW_SCALE_8, DAC_CW_PHASE_0, 440, cw_offset};
|
|
|
|
|
|
- /*// Gradually decrease volume (fade-out)
|
|
|
- for (int duty = 127; duty >= 0; duty -= 5) {
|
|
|
- ledc_set_duty(LEDC_LOW_SPEED_MODE, PWM_CHANNEL, duty);
|
|
|
- ledc_update_duty(LEDC_LOW_SPEED_MODE, PWM_CHANNEL);
|
|
|
- vTaskDelay(pdMS_TO_TICKS(50));
|
|
|
- }*/
|
|
|
+ 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");
|
|
|
+}
|
|
|
|
|
|
- vTaskDelay(pdMS_TO_TICKS(1000)); // Delay 1 second
|
|
|
- }
|
|
|
+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;
|
|
|
+}
|