Browse Source

It works with sensor timing and sound

Thomas Chef 4 months ago
parent
commit
4f033ab38e
3 changed files with 69 additions and 126 deletions
  1. 36 6
      main/main.c
  2. 28 119
      main/sound.c
  3. 5 1
      main/sound.h

+ 36 - 6
main/main.c

@@ -3,6 +3,7 @@
 #include "freertos/task.h"
 #include "driver/gpio.h"
 #include "esp_log.h"
+#include <stdbool.h>
 #include "sdkconfig.h"
 #include "sound.h"
 
@@ -12,6 +13,8 @@
 #define BLINK_GPIO CONFIG_BLINK_GPIO
 #define PHOTO_SENSOR_PIN GPIO_NUM_18
 
+#define ACTIVE_THRESHOLD_MS 5000  // 60 seconds in milliseconds
+
 static void configure_led(void)
 {
     ESP_LOGI("LED", "Example configured to blink GPIO LED!");
@@ -34,21 +37,48 @@ void setup_photo_sensor() {
     ESP_LOGI("PHOTO", "Photomicrosensor configured on GPIO %d", PHOTO_SENSOR_PIN);
 }
 
+void check_sensor_active_time(bool fridgeDoorClosed) {
+
+    static int64_t activeStartTime = 0;
+
+    //ESP_EARLY_LOGI("CHECK", "State: %s", fridgeDoorClosed == true ? "CLOSED" : "OPEN");
+
+    if (fridgeDoorClosed == false) {
+        if (activeStartTime == 0) {
+            activeStartTime = esp_timer_get_time() / 1000; // Convert to milliseconds
+            ESP_LOGI("CHECK", "Door timer start time: %lld", activeStartTime);
+        } else {
+            int64_t elapsedTime = (esp_timer_get_time() / 1000) - activeStartTime;
+            //ESP_LOGI("CHECK", "Elapsed time: %lld", elapsedTime);
+            if (elapsedTime > ACTIVE_THRESHOLD_MS) {
+                enableNotificationSound(true);
+            }
+        }
+    } else {
+        activeStartTime = 0;
+        enableNotificationSound(false);
+    }
+}
+
 void read_photo_sensor_task(void *pvParameters) {
-    static int prevState = 0;
+    static bool prevState = false;
     while (1) {
-        int sensorState = gpio_get_level(PHOTO_SENSOR_PIN);
-        if (sensorState != prevState) {
-            prevState = sensorState;
-            ESP_EARLY_LOGI("PHOTO", "Triggered! State: %d", sensorState);
-            if( sensorState == 0 ) {
+        bool fridgeDoorClosed = gpio_get_level(PHOTO_SENSOR_PIN) == 0 ? false : true;
+        if (fridgeDoorClosed != prevState) {
+            prevState = fridgeDoorClosed;
+            ESP_EARLY_LOGI("PHOTO", "Triggered! State: %s", fridgeDoorClosed == true ? "CLOSED" : "OPEN");
+            if( fridgeDoorClosed == false ) {
                 gpio_set_level(BLINK_GPIO, 1);
             }
             else {
                 gpio_set_level(BLINK_GPIO, 0);
             }
         }
+
+        check_sensor_active_time(fridgeDoorClosed);
+
         vTaskDelay(pdMS_TO_TICKS(50)); // Delay 50ms (20 times per second)
+
     }
 }
 

+ 28 - 119
main/sound.c

@@ -10,102 +10,15 @@
 
 #define DAC_CHANNEL       1
 #define DAC_PIN           GPIO_NUM_25   // GPIO pin for DAC output GPIO25=Channel 1
-#define TONE_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)
-
-typedef struct {
-    int frequency;      // Frequency in Hz
-    int duration;       // Duration in ms
-    int quietDuration;  // The quiet duration after each note
-} Note;
-
-// Super Mario Bros. short intro melody
-/*Note melody[] = {
-    {660, 100}, {0, 150}, {660, 100}, {0, 300}, {660, 100}, {0, 300}, {510, 100}, {660, 100}, {770, 100}, {0, 550}, 
-    {380, 100}, {0, 500}, {510, 100}, {0, 300}, {380, 100}, {0, 300}, {320, 100}, {0, 700}  // End of melody
-};*/
-/*Note melody[] = {
-    {440, 500}, {440, 500}, {440, 500}, {349, 350}, {523, 150}, {440, 500}, {349, 350}, {523, 150}, {440, 1000},
-    {659, 500}, {659, 500}, {659, 500}, {698, 350}, {523, 150}, {415, 500}, {349, 350}, {523, 150}, {440, 1000}, 
-};*/
-/*Note melody[] = {
-    {1318, 150}, {2637, 150}, {2093, 150}, {2349, 150}, {3136, 300}
-};*/
-
-/*Note melody[] = {
-    {320, 200, 200},{320, 200, 200},{320, 200, 200},{320, 200, 200},{320, 200, 200},{320, 200, 200},{320, 200, 200},
-    
-    {660, 100, 150}, {660, 100, 300}, {660, 100, 300}, {510, 100, 100}, {660, 100, 300}, {770, 100, 550}, {380, 100, 575},
-    {510, 100, 450}, {380, 100, 400}, {320, 100, 500}, {440, 100, 300}, {480, 80, 330}, {450, 100, 150}, {430, 100, 300},
-    {380, 100, 200}, {660, 80, 200}, {760, 50, 150}, {860, 100, 300}, {700, 80, 150}, {760, 50, 350}, {660, 80, 300},
-    {520, 80, 150}, {580, 80, 150}, {480, 80, 500},
-
-    {510, 100, 450}, {380, 100, 400}, {320, 100, 500}, {440, 100, 300}, {480, 80, 330}, {450, 100, 150}, {430, 100, 300},
-    {380, 100, 200}, {660, 80, 200}, {760, 50, 150}, {860, 100, 300}, {700, 80, 150}, {760, 50, 350}, {660, 80, 300},
-    {520, 80, 150}, {580, 80, 150}, {480, 80, 500},
-
-    {500, 100, 300}, {760, 100, 100}, {720, 100, 150}, {680, 100, 150}, {620, 150, 300}, {650, 150, 300}, {380, 100, 150},
-    {430, 100, 150}, {500, 100, 300}, {430, 100, 150}, {500, 100, 100}, {570, 100, 220}, {500, 100, 300}, {760, 100, 100},
-    {720, 100, 150}, {680, 100, 150}, {620, 150, 300}, {650, 200, 300},
-
-    {1020, 80, 300}, {1020, 80, 150}, {1020, 80, 300}, {380, 100, 300}, {500, 100, 300}, {760, 100, 100}, {720, 100, 150},
-    {680, 100, 150}, {620, 150, 300}, {650, 150, 300}, {380, 100, 150}, {430, 100, 150}, {500, 100, 300}, {430, 100, 150},
-    {500, 100, 100}, {570, 100, 420}, {585, 100, 450}, {550, 100, 420}, {500, 100, 360}, {380, 100, 300}, {500, 100, 300},
-
-    {500, 100, 150}, {500, 100, 300}, {500, 100, 300}, {760, 100, 100}, {720, 100, 150}, {680, 100, 150}, {620, 150, 300},
-    {650, 150, 300}, {380, 100, 150}, {430, 100, 150}, {500, 100, 300}, {430, 100, 150}, {500, 100, 100}, {570, 100, 220},
-    {500, 100, 300}, {760, 100, 100}, {720, 100, 150}, {680, 100, 150}, {620, 150, 300}, {650, 200, 300},
-
-    {1020, 80, 300}, {1020, 80, 150}, {1020, 80, 300}, {380, 100, 300}, {500, 100, 300}, {760, 100, 100}, {720, 100, 150},
-    {680, 100, 150}, {620, 150, 300}, {650, 150, 300}, {380, 100, 150}, {430, 100, 150}, {500, 100, 300}, {430, 100, 150},
-    {500, 100, 100}, {570, 100, 420}, {585, 100, 450}, {550, 100, 420}, {500, 100, 360}, {380, 100, 300}, {500, 100, 300},
-
-    {500, 60, 150}, {500, 80, 300}, {500, 60, 350}, {500, 80, 150}, {580, 80, 350}, {660, 80, 150}, {500, 80, 300},
-    {430, 80, 150}, {380, 80, 600}, {500, 60, 150}, {500, 80, 300}, {500, 60, 350}, {500, 80, 150}, {580, 80, 150},
-    {660, 80, 550}, {870, 80, 325}, {760, 80, 600},
-
-    {500, 60, 150}, {500, 80, 300}, {500, 60, 350}, {500, 80, 150}, {580, 80, 350}, {660, 80, 150}, {500, 80, 300},
-    {430, 80, 150}, {380, 80, 600}, {660, 100, 150}, {660, 100, 300}, {660, 100, 300}, {510, 100, 100}, {660, 100, 300},
-    {770, 100, 550}, {380, 100, 575}
-};*/
-
-const Note melody[] = {
-    {196.00, 152, 0},
-    {261.63, 152, 39},
-    {329.63, 151, 160},
-    {392.00, 152, 201},
-    {523.25, 152, 318},
-    {659.26, 151, 361},
-    {783.99, 456, 480},
-    {659.26, 456, 536},
-    {207.65, 152, 960},
-    {261.63, 152, 999},
-    {311.13, 151, 1120},
-    {415.30, 152, 1161},
-    {523.25, 152, 1278},
-    {622.25, 151, 1321},
-    {830.61, 456, 1440},
-    {622.25, 456, 1496},
-    {233.08, 152, 1920},
-    {293.66, 152, 1959},
-    {349.23, 151, 2080},
-    {466.16, 152, 2121},
-    {587.33, 152, 2238},
-    {698.46, 151, 2281},
-    {932.33, 456, 2400},
-    {932.33, 152, 2456},
-    {932.33, 152, 2863},
-    {932.33, 151, 2616},
-    {659.26, 456, 3025},
-};
-
-int melody_size = sizeof(melody) / sizeof(melody[0]);
+#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 = 2;
+    int semitones = 0;
 
     int t_freq = frequency * pow(2.0, semitones / 12.0);
 
@@ -118,14 +31,13 @@ static void play_tone(int frequency, float duration, int quietDuration) {
         dac_cw_generator_config(&cosineConf);
         dac_output_enable(DAC_CHANNEL_1);
     }
-    vTaskDelay(pdMS_TO_TICKS(duration));
-    //dac_output_disable(DAC_CHANNEL_1);
-    //vTaskDelay(pdMS_TO_TICKS(quietDuration));
-}
 
-void play_melody() {
-    for (int i = 0; i < melody_size; i++) {
-        play_tone(melody[i].frequency, melody[i].duration, melody[i].quietDuration);
+    // 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);
 }
@@ -135,41 +47,38 @@ 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_enable(DAC_CHANNEL_1);
-
-    //vTaskDelay(pdMS_TO_TICKS(1000)); // Delay 1 second
-
     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(5000));
-        play_melody();
+        vTaskDelay(pdMS_TO_TICKS(1000));
+        if( soundEnabled ) {
+            play_tone(TONE_FREQ, TONE_DURATION, QUIET_DURATION);
+        }
     }
-
-    ESP_LOGI("SOUND", "Sound task starts....");
 }
 
 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;
+}

+ 5 - 1
main/sound.h

@@ -1,2 +1,6 @@
 
-void initSound();
+#include <stdbool.h>
+
+void initSound();
+
+void enableNotificationSound(bool enable);