1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- /*
- * SPDX-FileCopyrightText: 2022 Helmut Pozimski <helmut@pozimski.eu>
- *
- * SPDX-License-Identifier: GPL-2.0-only
- */
- #include <time.h>
- #include <freertos/FreeRTOS.h>
- #include <freertos/event_groups.h>
- #include <driver/gpio.h>
- #include <nvs_flash.h>
- #include <esp_log.h>
- #include <esp_wifi.h>
- #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"
- 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) {
- ESP_ERROR_CHECK(nvs_flash_init());
- ESP_ERROR_CHECK(esp_netif_init());
- ESP_ERROR_CHECK(esp_event_loop_create_default());
- wifi_init();
- wifi_start();
- ESP_ERROR_CHECK(storage_init("a1"));
- 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 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);
- 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, 7, NULL);
- xTaskCreate(alarm_task, "alarm_task", 2048, (void*) &alarm_task_parameters, 7, NULL);
- }
|