123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- #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/dac.h"
- #include "math.h"
- #include "config.h"
- #include "mqtt.h"
- #ifdef SOUND_ENABLED
- #define BUZZ_PIN GPIO_NUM_25 // GPIO pin for DAC output GPIO25=Channel 1
- #define BUZZ_SHORT_DURATION 200 // Duration of the buzz in ms
- #define QUIET_DURATION 1000 // Duration of the quiet period in ms
- static bool alarmEnabled = false; // Tracks the state of the normal alarm
- static void activate_buzzer(int on_time, int off_time, int repetitions) {
-
- for (int i = 0; i < repetitions; i++) {
- gpio_set_level(BUZZ_PIN, 1); // Set GPIO high
- vTaskDelay(pdMS_TO_TICKS(on_time)); // Wait for the specified on time
- gpio_set_level(BUZZ_PIN, 0); // Set GPIO low
- vTaskDelay(pdMS_TO_TICKS(off_time)); // Wait for the specified off time
- }
- }
- static void setupSound() {
- // Configure GPIO25 as output and set it to inactive (low) by default
- gpio_config_t io_conf = {
- .pin_bit_mask = (1ULL << BUZZ_PIN),
- .mode = GPIO_MODE_OUTPUT,
- .pull_up_en = GPIO_PULLUP_DISABLE,
- .pull_down_en = GPIO_PULLDOWN_DISABLE,
- .intr_type = GPIO_INTR_DISABLE
- };
- gpio_config(&io_conf);
- // Set GPIO25 to low (inactive)
- gpio_set_level(BUZZ_PIN, 0);
- ESP_LOGI("SOUND", "Sound setup");
- }
- static void sound_task(void *pvParameters) {
- ESP_LOGI("SOUND", "Sound task starts....");
- setupSound();
- activate_buzzer(50, 0, 1); // Initial short beep to indicate sound system is ready
- while (1) {
- while( alarmEnabled ) {
- activate_buzzer(BUZZ_SHORT_DURATION, QUIET_DURATION, 1);
- }
- vTaskDelay(pdMS_TO_TICKS(100)); // Check alarm state every second
- }
- }
- void setAlarmState(bool enabled) {
- alarmEnabled = enabled;
- //ESP_LOGI("SOUND", "Alarm state set to: %s", enabled ? "ENABLED" : "DISABLED");
- }
- void triggerNotificationSound() {
- ESP_LOGI("SOUND", "Triggering short notification sound");
- activate_buzzer(100, 0, 1); // 100ms beep, no quiet time, single iteration
- }
- void initSound() {
- xTaskCreate(sound_task, "sound_task", 2048, NULL, 10, NULL);
- ESP_LOGI("SOUND", "Sound initialized");
- }
- #else
- void initSound() {};
- void triggerNotificationSound() {};
- void setAlarmState(bool enabled) {};
- #endif
|