Browse Source

Works with 50mS task.

Thomas Chef 4 months ago
parent
commit
8d6ea10024
1 changed files with 23 additions and 66 deletions
  1. 23 66
      main/main.c

+ 23 - 66
main/main.c

@@ -6,74 +6,22 @@
 #include "led_strip.h"
 #include "sdkconfig.h"
 
-static const char *TAG = "example";
-
 /* 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.
 */
 #define BLINK_GPIO CONFIG_BLINK_GPIO
 #define PHOTO_SENSOR_PIN GPIO_NUM_18
 
-static uint8_t s_led_state = 0;
-
-#ifdef CONFIG_BLINK_LED_RMT
-static led_strip_t *pStrip_a;
-
-static void blink_led(void)
-{
-    /* If the addressable LED is enabled */
-    if (s_led_state) {
-        /* Set the LED pixel using RGB from 0 (0%) to 255 (100%) for each color */
-        pStrip_a->set_pixel(pStrip_a, 0, 16, 16, 16);
-        /* Refresh the strip to send data */
-        pStrip_a->refresh(pStrip_a, 100);
-    } else {
-        /* Set all LED off to clear all pixels */
-        pStrip_a->clear(pStrip_a, 50);
-    }
-}
-
 static void configure_led(void)
 {
-    ESP_LOGI(TAG, "Example configured to blink addressable LED!");
-    /* LED strip initialization with the GPIO and pixels number*/
-    pStrip_a = led_strip_init(CONFIG_BLINK_LED_RMT_CHANNEL, BLINK_GPIO, 1);
-    /* Set all LED off to clear all pixels */
-    pStrip_a->clear(pStrip_a, 50);
-}
-
-#elif CONFIG_BLINK_LED_GPIO
-
-/*static void blink_led(void)
-{
-    // Set the GPIO level according to the state (LOW or HIGH)
-    gpio_set_level(BLINK_GPIO, s_led_state);
-}*/
-
-static void configure_led(void)
-{
-    ESP_LOGI(TAG, "Example configured to blink GPIO LED!");
+    ESP_LOGI("LED", "Example configured to blink GPIO LED!");
     gpio_reset_pin(BLINK_GPIO);
     /* Set the GPIO as a push/pull output */
     gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
 }
 
-#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() {
+void setup_photo_sensor() {
     gpio_config_t io_conf = {
         .intr_type = GPIO_INTR_ANYEDGE,  // Trigger on both rising & falling edges
         .pin_bit_mask = (1ULL << PHOTO_SENSOR_PIN),
@@ -83,11 +31,25 @@ void setup_photo_sensor_interrupt() {
     };
     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 configured on GPIO %d", PHOTO_SENSOR_PIN);
+}
 
-    ESP_LOGI("PHOTO", "Photomicrosensor interrupt configured on GPIO %d", PHOTO_SENSOR_PIN);
+void read_photo_sensor_task(void *pvParameters) {
+    static int prevState = 0;
+    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 ) {
+                gpio_set_level(BLINK_GPIO, 1);
+            }
+            else {
+                gpio_set_level(BLINK_GPIO, 0);
+            }
+        }
+        vTaskDelay(pdMS_TO_TICKS(50)); // Delay 50ms (20 times per second)
+    }
 }
 
 void app_main(void)
@@ -95,13 +57,8 @@ void app_main(void)
 
     /* Configure the peripheral according to the LED type */
     configure_led();
-    setup_photo_sensor_interrupt();
+    setup_photo_sensor();
+
+    xTaskCreate(read_photo_sensor_task, "read_sensor_task", 2048, NULL, 10, NULL);
 
-    while (1) {
-        /*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;*/
-        vTaskDelay(CONFIG_BLINK_PERIOD / portTICK_PERIOD_MS);
-    }
 }