1
0

main.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <stdio.h>
  2. #include "freertos/FreeRTOS.h"
  3. #include "freertos/task.h"
  4. #include "driver/gpio.h"
  5. #include "esp_log.h"
  6. #include "led_strip.h"
  7. #include "sdkconfig.h"
  8. static const char *TAG = "example";
  9. /* Use project configuration menu (idf.py menuconfig) to choose the GPIO to blink,
  10. or you can edit the following line and set a number here.
  11. */
  12. #define BLINK_GPIO CONFIG_BLINK_GPIO
  13. #define PHOTO_SENSOR_PIN GPIO_NUM_18
  14. static uint8_t s_led_state = 0;
  15. #ifdef CONFIG_BLINK_LED_RMT
  16. static led_strip_t *pStrip_a;
  17. static void blink_led(void)
  18. {
  19. /* If the addressable LED is enabled */
  20. if (s_led_state) {
  21. /* Set the LED pixel using RGB from 0 (0%) to 255 (100%) for each color */
  22. pStrip_a->set_pixel(pStrip_a, 0, 16, 16, 16);
  23. /* Refresh the strip to send data */
  24. pStrip_a->refresh(pStrip_a, 100);
  25. } else {
  26. /* Set all LED off to clear all pixels */
  27. pStrip_a->clear(pStrip_a, 50);
  28. }
  29. }
  30. static void configure_led(void)
  31. {
  32. ESP_LOGI(TAG, "Example configured to blink addressable LED!");
  33. /* LED strip initialization with the GPIO and pixels number*/
  34. pStrip_a = led_strip_init(CONFIG_BLINK_LED_RMT_CHANNEL, BLINK_GPIO, 1);
  35. /* Set all LED off to clear all pixels */
  36. pStrip_a->clear(pStrip_a, 50);
  37. }
  38. #elif CONFIG_BLINK_LED_GPIO
  39. /*static void blink_led(void)
  40. {
  41. // Set the GPIO level according to the state (LOW or HIGH)
  42. gpio_set_level(BLINK_GPIO, s_led_state);
  43. }*/
  44. static void configure_led(void)
  45. {
  46. ESP_LOGI(TAG, "Example configured to blink GPIO LED!");
  47. gpio_reset_pin(BLINK_GPIO);
  48. /* Set the GPIO as a push/pull output */
  49. gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
  50. }
  51. #endif
  52. // Interrupt service routine (ISR)
  53. static void IRAM_ATTR photo_sensor_isr_handler(void *arg) {
  54. int level = gpio_get_level(PHOTO_SENSOR_PIN);
  55. ESP_EARLY_LOGI("PHOTO", "Triggered! State: %d", level);
  56. if( level == 0 ) {
  57. gpio_set_level(BLINK_GPIO, 0);
  58. }
  59. else {
  60. gpio_set_level(BLINK_GPIO, 1);
  61. }
  62. }
  63. // GPIO setup
  64. void setup_photo_sensor_interrupt() {
  65. gpio_config_t io_conf = {
  66. .intr_type = GPIO_INTR_ANYEDGE, // Trigger on both rising & falling edges
  67. .pin_bit_mask = (1ULL << PHOTO_SENSOR_PIN),
  68. .mode = GPIO_MODE_INPUT,
  69. .pull_up_en = GPIO_PULLUP_DISABLE,
  70. .pull_down_en = GPIO_PULLDOWN_DISABLE
  71. };
  72. gpio_config(&io_conf);
  73. // Install GPIO ISR service
  74. gpio_install_isr_service(0);
  75. gpio_isr_handler_add(PHOTO_SENSOR_PIN, photo_sensor_isr_handler, NULL);
  76. ESP_LOGI("PHOTO", "Photomicrosensor interrupt configured on GPIO %d", PHOTO_SENSOR_PIN);
  77. }
  78. void app_main(void)
  79. {
  80. /* Configure the peripheral according to the LED type */
  81. configure_led();
  82. setup_photo_sensor_interrupt();
  83. while (1) {
  84. /*ESP_LOGI(TAG, "Turning the LED %s!", s_led_state == true ? "ON" : "OFF");
  85. blink_led();
  86. // Toggle the LED state
  87. s_led_state = !s_led_state;*/
  88. vTaskDelay(CONFIG_BLINK_PERIOD / portTICK_PERIOD_MS);
  89. }
  90. }