|
- static const char *TAG = "blinky";
- #include <esp_log.h>
-
- #include <freertos/FreeRTOS.h>
- #include <freertos/task.h>
-
- #include <esp_spiffs.h>
- #include <nvs_flash.h>
-
- #include "wifi.hpp"
- #include "http_serv.hpp"
- #include "ota.hpp"
- #include "device.hpp"
-
-
- // mDNS / NetBIOS
-
- #include <mdns.h>
- #include <lwip/apps/netbiosns.h>
-
- static void start_mdns_service(const char *hostname) {
- // Initialize mDNS service
- ESP_ERROR_CHECK(mdns_init());
-
- // Set hostname
- mdns_hostname_set(hostname);
-
- // Set default instance
- mdns_instance_name_set("Blinky Lights");
-
- // NetBIOS too
- netbiosns_init();
- netbiosns_set_name(hostname);
- }
-
-
- // Dummy FS
-
- static void start_filesystem() {
- ESP_LOGI(TAG, "Initializing filesystem");
-
- esp_vfs_spiffs_conf_t conf = {
- .base_path = "/spiffs",
- .partition_label = NULL,
- .max_files = 5,
- .format_if_mount_failed = true
- };
-
- // Use settings defined above to initialize and mount SPIFFS filesystem.
- // Note: esp_vfs_spiffs_register is an all-in-one convenience function.
- ESP_ERROR_CHECK(esp_vfs_spiffs_register(&conf));
- }
-
- #include <string>
- #include <dirent.h>
- #include <sys/stat.h>
- #include <errno.h>
-
- static void log_dir(const std::string &path) {
- struct stat st;
- int err = stat(path.c_str(), &st);
- if (err < 0) {
- ESP_LOGE(TAG, "%s %d", path.c_str(), errno);
- } else {
- ESP_LOGI(TAG, "%s %d", path.c_str(), st.st_size);
- }
-
- DIR *dir;
- if ((dir = opendir(path.c_str())) == NULL) {
- //ESP_LOGE(TAG, "Failed to open %s", path.c_str());
- return;
- }
-
- struct dirent *de;
- while ((de = readdir(dir)) != NULL) {
- log_dir(path + "/" + de->d_name);
- }
-
- closedir(dir);
- }
-
-
- int64_t now_us() {
- struct timeval tv_now;
- gettimeofday(&tv_now, NULL);
- return (int64_t)tv_now.tv_sec * 1000000L + (int64_t)tv_now.tv_usec;
- }
-
-
- // Entry Point
-
- extern "C" void app_main(void) {
- // Initialize NVS for WiFi Data
- esp_err_t ret = nvs_flash_init();
- if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
- ESP_ERROR_CHECK(nvs_flash_erase());
- ret = nvs_flash_init();
- }
- ESP_ERROR_CHECK(ret);
-
- // WiFi
- config_wifi();
-
- // mDNS
- start_mdns_service("blinky-jr");
-
- // HTTP Server
- httpd_handle_t server = start_webserver();
-
- // OTA Server
- start_ota_serv(server);
-
- // Dummy Filesystem
- start_filesystem();
- log_dir("/spiffs");
-
- // TODO: Something useful
- Device device("blinky-jr");
-
- /*
- int period_us = 1000000;
- int64_t target_us = now_us();
- while (true) {
- int64_t delay_us = target_us - now_us();
- if (delay_us < 0) delay_us = 0;
- if (device.wait(delay_us / 1000))
- ESP_LOGI(TAG, "Reload!");
- else {
- ESP_LOGI(TAG, "Timeout!");
- target_us += period_us;
- }
- }
- */
- while (device.wait()) ESP_LOGI(TAG, "Reload!");
-
- // Spin
- }
|