esp32_alarm_clock_main.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. static TaskHandle_t alarm_task_handle;
  48. static alarm_parameters alarm_task_parameters = {
  49. .button_pressed_flag = &button_pressed_flag,
  50. .task_handle = &alarm_task_handle
  51. };
  52. struct tm current_time;
  53. init_peripherals(&button_pressed_flag);
  54. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server));
  55. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));
  56. server = start_webserver(&alarm_task_handle);
  57. ds3231_read_date_time(&current_time);
  58. sntp_start(21600);
  59. if (current_time.tm_year == 0) {
  60. await_sntp_sync();
  61. }
  62. xTaskCreate(display_update_task, "display_update_task", 2048, NULL, 7, NULL);
  63. xTaskCreate(alarm_task, "alarm_task", 2048, (void*) &alarm_task_parameters, 7, NULL);
  64. }