Преглед изворни кода

Added PWM Sound. Not so good

Thomas Chef пре 4 месеци
родитељ
комит
c6fb02938c
4 измењених фајлова са 87 додато и 1 уклоњено
  1. 3 1
      main/CMakeLists.txt
  2. 2 0
      main/main.c
  3. 80 0
      main/sound.c
  4. 2 0
      main/sound.h

+ 3 - 1
main/CMakeLists.txt

@@ -1,2 +1,4 @@
-idf_component_register(SRCS "main.c"
+idf_component_register(SRCS "sound.c" "main.c"
+
                     INCLUDE_DIRS ".")
+

+ 2 - 0
main/main.c

@@ -4,6 +4,7 @@
 #include "driver/gpio.h"
 #include "esp_log.h"
 #include "sdkconfig.h"
+#include "sound.h"
 
 /* Use project configuration menu (idf.py menuconfig) to choose the GPIO to blink,
    or you can edit the following line and set a number here.
@@ -57,6 +58,7 @@ void app_main(void)
     /* Configure the peripheral according to the LED type */
     configure_led();
     setup_photo_sensor();
+    initSound();
 
     xTaskCreate(read_photo_sensor_task, "read_sensor_task", 2048, NULL, 10, NULL);
 

+ 80 - 0
main/sound.c

@@ -0,0 +1,80 @@
+#include "sound.h"
+#include <stdio.h>
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
+#include "driver/gpio.h"
+#include "esp_log.h"
+#include "sdkconfig.h"
+#include "driver/ledc.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)
+
+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);
+
+    ESP_LOGI("SOUND", "Sound setup");
+}
+
+static void sound_task(void *pvParameters) {
+
+    setupSound();
+
+    while (1) {
+
+        ledc_set_duty(LEDC_LOW_SPEED_MODE, PWM_CHANNEL, 30);
+        ledc_update_duty(LEDC_LOW_SPEED_MODE, PWM_CHANNEL);
+
+        /*// 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));
+        }*/
+
+        vTaskDelay(pdMS_TO_TICKS(1000));  // Keep sound for 500ms
+
+        ledc_set_duty(LEDC_LOW_SPEED_MODE, PWM_CHANNEL, 10);
+        ledc_update_duty(LEDC_LOW_SPEED_MODE, PWM_CHANNEL);
+
+        /*// 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));
+        }*/
+
+
+        vTaskDelay(pdMS_TO_TICKS(1000)); // Delay 1 second
+    }
+
+    ESP_LOGI("SOUND", "Sound task starts....");
+}
+
+void initSound() {
+
+    xTaskCreate(sound_task, "sound_task", 2048, NULL, 10, NULL);
+    ESP_LOGI("SOUND", "Sound initialized");
+}

+ 2 - 0
main/sound.h

@@ -0,0 +1,2 @@
+
+void initSound();