Files
deckenlampe/main/main.cpp
T

168 lines
3.8 KiB
C++
Raw Normal View History

2021-07-12 18:55:53 +02:00
#include "sdkconfig.h"
// esp-idf includes
#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#if defined(CONFIG_ESP_TASK_WDT_PANIC) || defined(CONFIG_ESP_TASK_WDT)
#include <freertos/task.h>
#include <esp_task_wdt.h>
#endif
#ifdef CONFIG_APP_ROLLBACK_ENABLE
#include <esp_ota_ops.h>
#endif
2021-07-13 12:22:32 +02:00
#include <esp_system.h>
2021-07-12 18:55:53 +02:00
// Arduino includes
2021-07-13 13:58:27 +02:00
#include <Wire.h>
2021-07-12 18:55:53 +02:00
// local includes
#include "tickchrono.h"
2021-07-15 13:17:07 +02:00
#include "myconfig.h"
#include "mywifi.h"
2021-08-01 15:17:15 +02:00
#include "myble.h"
2021-07-15 13:17:07 +02:00
#include "webserver.h"
#include "mymdns.h"
#include "mymqtt.h"
2021-08-06 15:50:44 +02:00
#include "myota.h"
2021-07-15 13:17:07 +02:00
#include "feature_lamp.h"
#include "feature_switch.h"
#include "feature_dht.h"
#include "feature_tsl.h"
#include "feature_bmp.h"
2021-07-15 14:12:01 +02:00
#include "espchrono.h"
2021-07-12 18:55:53 +02:00
using namespace std::chrono_literals;
2021-07-15 13:17:07 +02:00
namespace deckenlampe {
2021-07-12 18:55:53 +02:00
namespace {
constexpr const char * const TAG = "MAIN";
2021-07-13 10:31:36 +02:00
2021-07-12 18:55:53 +02:00
} // namespace
2021-07-15 13:17:07 +02:00
} // namespace deckenlamp
2021-07-12 18:55:53 +02:00
extern "C" void app_main()
{
2021-07-15 13:17:07 +02:00
using namespace deckenlampe;
2021-07-12 18:55:53 +02:00
#if defined(CONFIG_ESP_TASK_WDT_PANIC) || defined(CONFIG_ESP_TASK_WDT)
{
const auto taskHandle = xTaskGetCurrentTaskHandle();
if (!taskHandle)
{
ESP_LOGE(TAG, "could not get handle to current main task!");
}
else if (const auto result = esp_task_wdt_add(taskHandle); result != ESP_OK)
{
ESP_LOGE(TAG, "could not add main task to watchdog: %s", esp_err_to_name(result));
}
}
#endif
#ifdef CONFIG_APP_ROLLBACK_ENABLE
esp_ota_img_states_t ota_state;
if (const esp_partition_t * const running = esp_ota_get_running_partition())
{
if (const auto result = esp_ota_get_state_partition(running, &ota_state); result == ESP_ERR_NOT_FOUND)
ota_state = ESP_OTA_IMG_VALID;
else if (result != ESP_OK)
{
ESP_LOGE(TAG, "esp_ota_get_state_partition() failed with %s", esp_err_to_name(result));
ota_state = ESP_OTA_IMG_UNDEFINED;
}
}
else
{
ESP_LOGE(TAG, "esp_ota_get_running_partition() returned nullptr");
ota_state = ESP_OTA_IMG_UNDEFINED;
}
#endif
2021-07-15 13:17:07 +02:00
init_config();
2021-07-12 18:55:53 +02:00
2021-07-15 13:17:07 +02:00
init_lamp();
2021-07-12 18:55:53 +02:00
2021-07-15 13:17:07 +02:00
init_switch();
2021-07-15 16:38:04 +02:00
if (config::enable_i2c.value())
2021-07-13 13:58:27 +02:00
{
ESP_LOGI(TAG, "calling Wire.begin()...");
2021-07-15 16:38:04 +02:00
const auto result = Wire.begin(config::pins_sda.value(), config::pins_scl.value());
2021-07-13 13:58:27 +02:00
ESP_LOGI(TAG, "finished with %s", result ? "true" : "false");
}
2021-07-15 13:17:07 +02:00
init_wifi();
2021-07-13 13:58:27 +02:00
2021-08-01 15:17:15 +02:00
init_ble();
2021-07-15 13:17:07 +02:00
init_webserver();
2021-07-13 13:58:27 +02:00
2021-07-15 13:17:07 +02:00
init_mdns();
2021-07-13 13:58:27 +02:00
2021-07-15 13:17:07 +02:00
init_mqtt();
2021-07-13 13:58:27 +02:00
2021-08-06 15:50:44 +02:00
init_ota();
2021-07-15 13:17:07 +02:00
init_dht();
2021-07-13 13:58:27 +02:00
2021-07-15 13:17:07 +02:00
init_tsl();
2021-07-13 15:04:21 +02:00
2021-07-15 13:17:07 +02:00
init_bmp();
2021-07-13 16:37:56 +02:00
2021-07-12 18:55:53 +02:00
while (true)
{
#if defined(CONFIG_ESP_TASK_WDT_PANIC) || defined(CONFIG_ESP_TASK_WDT)
if (const auto result = esp_task_wdt_reset(); result != ESP_OK)
ESP_LOGE(TAG, "esp_task_wdt_reset() failed with %s", esp_err_to_name(result));
#endif
2021-07-15 16:38:04 +02:00
update_config();
vPortYield();
update_lamp();
vPortYield();
update_wifi();
vPortYield();
update_webserver();
vPortYield();
update_mdns();
vPortYield();
update_mqtt();
vPortYield();
2021-08-06 15:50:44 +02:00
update_ota();
vPortYield();
2021-07-15 16:38:04 +02:00
update_dht();
vPortYield();
update_tsl();
vPortYield();
update_bmp();
vPortYield();
update_switch();
vPortYield();
2021-07-12 18:55:53 +02:00
#ifdef CONFIG_APP_ROLLBACK_ENABLE
if (ota_state == ESP_OTA_IMG_PENDING_VERIFY)
{
const auto result = esp_ota_mark_app_valid_cancel_rollback();
ESP_LOG_LEVEL_LOCAL((result == ESP_OK ? ESP_LOG_INFO : ESP_LOG_ERROR), TAG, "esp_ota_mark_app_valid_cancel_rollback() returned: %s", esp_err_to_name(result));
ota_state = ESP_OTA_IMG_VALID;
}
#endif
2021-07-15 16:38:04 +02:00
vTaskDelay(std::chrono::ceil<espcpputils::ticks>(20ms).count());
2021-07-12 18:55:53 +02:00
}
}
2021-07-13 10:31:36 +02:00
2021-07-13 16:37:56 +02:00
auto espchrono::local_clock::timezone() noexcept -> time_zone
{
return time_zone{1h, DayLightSavingMode::EuropeanSummerTime};
}