esp32_alarm_clock_main.c 2.4 KB

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