/* * SPDX-FileCopyrightText: 2022 Helmut Pozimski * * SPDX-License-Identifier: GPL-2.0-only */ #include #include #include #include #include #include #include #include "wifi.h" #include "configuration.h" #include "ds3231.h" #include "tm1637.h" #include "time_sync.h" #include "time_display.h" #include "alarm_task.h" #include "api.h" #include "storage.h" #include "cleanup_task.h" #include "wifi_management_task.h" static const char* STORAGE_NAMESPACE = "a1"; static void IRAM_ATTR gpio_interrupt_handler(void *args) { uint8_t* button_pressed_flag = (uint8_t*)args; *button_pressed_flag = 1; } static void init_peripherals(uint8_t* button_pressed_flag, uint8_t* wifi_enabled_flag) { ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); wifi_init(); wifi_start(); *wifi_enabled_flag = 1; ESP_ERROR_CHECK(storage_init(STORAGE_NAMESPACE)); ds3231_init(DS3231_SDA_PIN, DS3231_SCL_PIN); tm1637_init(TM1637_CLK_PIN, TM1637_DIO_PIN, true, 1); esp_rom_gpio_pad_select_gpio(BUTTON_INTERRUPT_PIN); gpio_set_direction(BUTTON_INTERRUPT_PIN, GPIO_MODE_INPUT); gpio_pulldown_en(BUTTON_INTERRUPT_PIN); gpio_pullup_dis(BUTTON_INTERRUPT_PIN); gpio_set_intr_type(BUTTON_INTERRUPT_PIN, GPIO_INTR_POSEDGE); gpio_install_isr_service(0); gpio_isr_handler_add(BUTTON_INTERRUPT_PIN, gpio_interrupt_handler, (void*)button_pressed_flag); } void app_main(void) { static httpd_handle_t server; static uint8_t button_pressed_flag = 0; static uint8_t wifi_enabled_flag = 0; static TaskHandle_t alarm_task_handle; static alarm_parameters alarm_task_parameters = { .button_pressed_flag = &button_pressed_flag, .task_handle = &alarm_task_handle }; struct tm current_time; init_peripherals(&button_pressed_flag, &wifi_enabled_flag); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server)); ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server)); server = start_webserver(&alarm_task_handle); ds3231_read_date_time(¤t_time); sntp_start(21600); if (current_time.tm_year == 0) { await_sntp_sync(); } xTaskCreate(display_update_task, "display_update_task", 2048, NULL, 8, NULL); xTaskCreate(alarm_task, "alarm_task", 2048, (void*) &alarm_task_parameters, 7, NULL); xTaskCreate(cleanup_task, "cleanup_task", 2048, (void*) STORAGE_NAMESPACE, 1, NULL); xTaskCreate(wifi_management_task, "wifi_management_task", 2048, (void*) &wifi_enabled_flag, 1, NULL); }