esp32_alarm_clock_main.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #include "cleanup_task.h"
  23. #include "wifi_management_task.h"
  24. static const char* STORAGE_NAMESPACE = "a1";
  25. static void IRAM_ATTR gpio_interrupt_handler(void *args)
  26. {
  27. uint8_t* button_pressed_flag = (uint8_t*)args;
  28. *button_pressed_flag = 1;
  29. }
  30. static void init_peripherals(uint8_t* button_pressed_flag, uint8_t* wifi_enabled_flag) {
  31. ESP_ERROR_CHECK(nvs_flash_init());
  32. ESP_ERROR_CHECK(esp_netif_init());
  33. ESP_ERROR_CHECK(esp_event_loop_create_default());
  34. wifi_init();
  35. wifi_start();
  36. *wifi_enabled_flag = 1;
  37. ESP_ERROR_CHECK(storage_init(STORAGE_NAMESPACE));
  38. ds3231_init(DS3231_SDA_PIN, DS3231_SCL_PIN);
  39. tm1637_init(TM1637_CLK_PIN, TM1637_DIO_PIN, true, 1);
  40. esp_rom_gpio_pad_select_gpio(BUTTON_INTERRUPT_PIN);
  41. gpio_set_direction(BUTTON_INTERRUPT_PIN, GPIO_MODE_INPUT);
  42. gpio_pulldown_en(BUTTON_INTERRUPT_PIN);
  43. gpio_pullup_dis(BUTTON_INTERRUPT_PIN);
  44. gpio_set_intr_type(BUTTON_INTERRUPT_PIN, GPIO_INTR_POSEDGE);
  45. gpio_install_isr_service(0);
  46. gpio_isr_handler_add(BUTTON_INTERRUPT_PIN, gpio_interrupt_handler, (void*)button_pressed_flag);
  47. }
  48. void app_main(void) {
  49. static httpd_handle_t server;
  50. static uint8_t button_pressed_flag = 0;
  51. static uint8_t wifi_enabled_flag = 0;
  52. static TaskHandle_t alarm_task_handle;
  53. static alarm_parameters alarm_task_parameters = {
  54. .button_pressed_flag = &button_pressed_flag,
  55. .task_handle = &alarm_task_handle
  56. };
  57. struct tm current_time;
  58. init_peripherals(&button_pressed_flag, &wifi_enabled_flag);
  59. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server));
  60. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));
  61. server = start_webserver(&alarm_task_handle);
  62. ds3231_read_date_time(&current_time);
  63. sntp_start(21600);
  64. if (current_time.tm_year == 0) {
  65. await_sntp_sync();
  66. }
  67. xTaskCreate(display_update_task, "display_update_task", 2048, NULL, 8, NULL);
  68. xTaskCreate(alarm_task, "alarm_task", 2048, (void*) &alarm_task_parameters, 7, NULL);
  69. xTaskCreate(cleanup_task, "cleanup_task", 2048, (void*) STORAGE_NAMESPACE, 1, NULL);
  70. xTaskCreate(wifi_management_task, "wifi_management_task", 2048, (void*) &wifi_enabled_flag, 1, NULL);
  71. }