|
- 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"
- #include "utils.hpp"
- #include "leds.hpp"
- #include "presets.hpp"
- #include "utils.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);
- }
-
-
- // 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");
-
- Device device("blinky-jr");
-
- // TODO: Load config from SPIFFS
-
- auto LEDs = TerminalLEDs(20);
- /*** *** ***/
- LEDs.setPattern(Presets::find("rainbow"));
- /*** *** ***/
-
- while (true) {
- ESP_LOGI(TAG, "Configuring LEDs");
-
- device.lock();
-
- const Pattern *pattern = Presets::find(device.get_effect());
- if (pattern) {
- LEDs.setPattern(pattern);
- } else {
- ESP_LOGW(TAG, "Could not find pattern '%s'", device.get_effect());
- pattern = LEDs.getPattern();
- }
-
- // TODO: Save Config to SPIFFS
-
- bool is_on = device.is_on();
-
- device.unlock();
-
- if (!pattern || !is_on) {
- if (!is_on) ESP_LOGI(TAG, "Device off");
- if (!pattern) ESP_LOGW(TAG, "No LED pattern set");
- ESP_LOGI(TAG, "Waiting for new config");
-
- device.wait();
-
- } else {
- ESP_LOGI(TAG, "Starting animation");
-
- int period_us = 1000000;
- int64_t target_us = time_us();
- while (true) {
- int64_t delay_us = target_us - time_us();
- if (delay_us < 0) delay_us = 0;
- if (device.wait(delay_us / 1000)) {
- // If the semaphore is set, go back to top of outer loop
- break;
- }
-
- LEDs.step();
- LEDs.show();
-
- target_us += period_us;
- }
- }
- }
-
- // Spin
- }
|