浏览代码

Interrupt solution. Misses triggering.

Thomas Chef 4 月之前
父节点
当前提交
3941c918c8
共有 5 个文件被更改,包括 41 次插入60 次删除
  1. 1 1
      CMakeLists.txt
  2. 1 1
      Makefile
  3. 0 43
      example_test.py
  4. 1 1
      main/CMakeLists.txt
  5. 38 14
      main/blink_example_main.c

+ 1 - 1
CMakeLists.txt

@@ -5,4 +5,4 @@ cmake_minimum_required(VERSION 3.5)
 set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/led_strip)
 
 include($ENV{IDF_PATH}/tools/cmake/project.cmake)
-project(blink)
+project(fridgeSensor)

+ 1 - 1
Makefile

@@ -3,7 +3,7 @@
 # project subdirectory.
 #
 
-PROJECT_NAME := blink
+PROJECT_NAME := fridgeSensor
 
 EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/led_strip
 

+ 0 - 43
example_test.py

@@ -1,43 +0,0 @@
-#!/usr/bin/env python
-
-from __future__ import division, print_function, unicode_literals
-
-import hashlib
-import os
-import re
-
-import ttfw_idf
-from tiny_test_fw import Utility
-
-
-def verify_elf_sha256_embedding(dut):
-    elf_file = os.path.join(dut.app.binary_path, 'blink.elf')
-    sha256 = hashlib.sha256()
-    with open(elf_file, 'rb') as f:
-        sha256.update(f.read())
-    sha256_expected = sha256.hexdigest()
-
-    dut.reset()
-    sha256_reported = dut.expect(re.compile(r'ELF file SHA256:\s+([a-f0-9]+)'), timeout=5)[0]
-
-    Utility.console_log('ELF file SHA256: %s' % sha256_expected)
-    Utility.console_log('ELF file SHA256 (reported by the app): %s' % sha256_reported)
-    # the app reports only the first several hex characters of the SHA256, check that they match
-    if not sha256_expected.startswith(sha256_reported):
-        raise ValueError('ELF file SHA256 mismatch')
-
-
-@ttfw_idf.idf_example_test(env_tag='Example_GENERIC', target=['esp32', 'esp32c3'])
-def test_examples_blink(env, extra_data):
-    dut = env.get_dut('blink', 'examples/get-started/blink')
-    binary_file = os.path.join(dut.app.binary_path, 'blink.bin')
-    bin_size = os.path.getsize(binary_file)
-    ttfw_idf.log_performance('blink_bin_size', '{}KB'.format(bin_size // 1024))
-
-    dut.start_app()
-
-    verify_elf_sha256_embedding(dut)
-
-
-if __name__ == '__main__':
-    test_examples_blink()

+ 1 - 1
main/CMakeLists.txt

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

+ 38 - 14
main/blink_example_main.c

@@ -1,11 +1,3 @@
-/* Blink Example
-
-   This example code is in the Public Domain (or CC0 licensed, at your option.)
-
-   Unless required by applicable law or agreed to in writing, this
-   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
-   CONDITIONS OF ANY KIND, either express or implied.
-*/
 #include <stdio.h>
 #include "freertos/FreeRTOS.h"
 #include "freertos/task.h"
@@ -20,6 +12,7 @@ static const char *TAG = "example";
    or you can edit the following line and set a number here.
 */
 #define BLINK_GPIO CONFIG_BLINK_GPIO
+#define PHOTO_SENSOR_PIN GPIO_NUM_18
 
 static uint8_t s_led_state = 0;
 
@@ -51,11 +44,11 @@ static void configure_led(void)
 
 #elif CONFIG_BLINK_LED_GPIO
 
-static void blink_led(void)
+/*static void blink_led(void)
 {
-    /* Set the GPIO level according to the state (LOW or HIGH)*/
+    // Set the GPIO level according to the state (LOW or HIGH)
     gpio_set_level(BLINK_GPIO, s_led_state);
-}
+}*/
 
 static void configure_led(void)
 {
@@ -67,17 +60,48 @@ static void configure_led(void)
 
 #endif
 
+// Interrupt service routine (ISR)
+static void IRAM_ATTR photo_sensor_isr_handler(void *arg) {
+    int level = gpio_get_level(PHOTO_SENSOR_PIN);
+    ESP_EARLY_LOGI("PHOTO", "Triggered! State: %d", level);
+    if( level == 0 ) {
+        gpio_set_level(BLINK_GPIO, 0);
+    }
+    else {
+        gpio_set_level(BLINK_GPIO, 1);
+    }
+}
+
+// GPIO setup
+void setup_photo_sensor_interrupt() {
+    gpio_config_t io_conf = {
+        .intr_type = GPIO_INTR_ANYEDGE,  // Trigger on both rising & falling edges
+        .pin_bit_mask = (1ULL << PHOTO_SENSOR_PIN),
+        .mode = GPIO_MODE_INPUT,
+        .pull_up_en = GPIO_PULLUP_DISABLE,  
+        .pull_down_en = GPIO_PULLDOWN_DISABLE
+    };
+    gpio_config(&io_conf);
+
+    // Install GPIO ISR service
+    gpio_install_isr_service(0);
+    gpio_isr_handler_add(PHOTO_SENSOR_PIN, photo_sensor_isr_handler, NULL);
+
+    ESP_LOGI("PHOTO", "Photomicrosensor interrupt configured on GPIO %d", PHOTO_SENSOR_PIN);
+}
+
 void app_main(void)
 {
 
     /* Configure the peripheral according to the LED type */
     configure_led();
+    setup_photo_sensor_interrupt();
 
     while (1) {
-        ESP_LOGI(TAG, "Turning the LED %s!", s_led_state == true ? "ON" : "OFF");
+        /*ESP_LOGI(TAG, "Turning the LED %s!", s_led_state == true ? "ON" : "OFF");
         blink_led();
-        /* Toggle the LED state */
-        s_led_state = !s_led_state;
+        // Toggle the LED state
+        s_led_state = !s_led_state;*/
         vTaskDelay(CONFIG_BLINK_PERIOD / portTICK_PERIOD_MS);
     }
 }