esp32_alarm_clock_main.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Helmut Pozimski <helmut@pozimski.eu>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0-only
  5. */
  6. #include <time.h>
  7. #include <freertos/FreeRTOS.h>
  8. #include <freertos/event_groups.h>
  9. #include <driver/gpio.h>
  10. #include <nvs_flash.h>
  11. #include <esp_log.h>
  12. #include <esp_wifi.h>
  13. #include "wifi.h"
  14. #include "configuration.h"
  15. #include "ds3231.h"
  16. #include "tm1637.h"
  17. #include "time_sync.h"
  18. #include "time_display.h"
  19. #include "alarm_task.h"
  20. #include "api.h"
  21. #include "storage.h"
  22. static void IRAM_ATTR gpio_interrupt_handler(void *args)
  23. {
  24. uint8_t* button_pressed_flag = (uint8_t*)args;
  25. *button_pressed_flag = 1;
  26. }
  27. static void init_peripherals(uint8_t* button_pressed_flag) {
  28. ESP_ERROR_CHECK(nvs_flash_init());
  29. ESP_ERROR_CHECK(esp_netif_init());
  30. ESP_ERROR_CHECK(esp_event_loop_create_default());
  31. wifi_init();
  32. wifi_start();
  33. ESP_ERROR_CHECK(storage_init("a1"));
  34. ds3231_init(DS3231_SDA_PIN, DS3231_SCL_PIN);
  35. tm1637_init(TM1637_CLK_PIN, TM1637_DIO_PIN, true, 1);
  36. esp_rom_gpio_pad_select_gpio(BUTTON_INTERRUPT_PIN);
  37. gpio_set_direction(BUTTON_INTERRUPT_PIN, GPIO_MODE_INPUT);
  38. gpio_pulldown_en(BUTTON_INTERRUPT_PIN);
  39. gpio_pullup_dis(BUTTON_INTERRUPT_PIN);
  40. gpio_set_intr_type(BUTTON_INTERRUPT_PIN, GPIO_INTR_POSEDGE);
  41. gpio_install_isr_service(0);
  42. gpio_isr_handler_add(BUTTON_INTERRUPT_PIN, gpio_interrupt_handler, (void*)button_pressed_flag);
  43. }
  44. void app_main(void) {
  45. static httpd_handle_t server;
  46. static uint8_t button_pressed_flag = 0;
  47. struct tm current_time;
  48. init_peripherals();
  49. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server));
  50. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));
  51. server = start_webserver();
  52. ds3231_read_date_time(&current_time);
  53. sntp_start(21600);
  54. if (current_time.tm_year == 0) {
  55. await_sntp_sync();
  56. }
  57. xTaskCreate(display_update_task, "display_update_task", 2048, NULL, 7, NULL);
  58. xTaskCreate(alarm_task, "alarm_task", 2048, (void*) &button_pressed_flag, 7, NULL);
  59. }