ESP32 Native version of Blinky, featureful controller code for WS2811/WS2812/NeoPixels
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
3.8KB

  1. static const char *TAG = "blinky";
  2. #include <esp_log.h>
  3. #include <freertos/FreeRTOS.h>
  4. #include <freertos/task.h>
  5. #include <esp_spiffs.h>
  6. #include <nvs_flash.h>
  7. #include "wifi.hpp"
  8. #include "http_serv.hpp"
  9. #include "ota.hpp"
  10. #include "device.hpp"
  11. #include "utils.hpp"
  12. #include "leds.hpp"
  13. #include "presets.hpp"
  14. #include "utils.hpp"
  15. // mDNS / NetBIOS
  16. #include <mdns.h>
  17. #include <lwip/apps/netbiosns.h>
  18. static void start_mdns_service(const char *hostname) {
  19. // Initialize mDNS service
  20. ESP_ERROR_CHECK(mdns_init());
  21. // Set hostname
  22. mdns_hostname_set(hostname);
  23. // Set default instance
  24. mdns_instance_name_set("Blinky Lights");
  25. // NetBIOS too
  26. netbiosns_init();
  27. netbiosns_set_name(hostname);
  28. }
  29. // Dummy FS
  30. static void start_filesystem() {
  31. ESP_LOGI(TAG, "Initializing filesystem");
  32. esp_vfs_spiffs_conf_t conf = {
  33. .base_path = "/spiffs",
  34. .partition_label = NULL,
  35. .max_files = 5,
  36. .format_if_mount_failed = true
  37. };
  38. // Use settings defined above to initialize and mount SPIFFS filesystem.
  39. // Note: esp_vfs_spiffs_register is an all-in-one convenience function.
  40. ESP_ERROR_CHECK(esp_vfs_spiffs_register(&conf));
  41. }
  42. #include <string>
  43. #include <dirent.h>
  44. #include <sys/stat.h>
  45. #include <errno.h>
  46. static void log_dir(const std::string &path) {
  47. struct stat st;
  48. int err = stat(path.c_str(), &st);
  49. if (err < 0) {
  50. ESP_LOGE(TAG, "%s %d", path.c_str(), errno);
  51. } else {
  52. ESP_LOGI(TAG, "%s %d", path.c_str(), st.st_size);
  53. }
  54. DIR *dir;
  55. if ((dir = opendir(path.c_str())) == NULL) {
  56. //ESP_LOGE(TAG, "Failed to open %s", path.c_str());
  57. return;
  58. }
  59. struct dirent *de;
  60. while ((de = readdir(dir)) != NULL) {
  61. log_dir(path + "/" + de->d_name);
  62. }
  63. closedir(dir);
  64. }
  65. // Entry Point
  66. extern "C" void app_main(void) {
  67. // Initialize NVS for WiFi Data
  68. esp_err_t ret = nvs_flash_init();
  69. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  70. ESP_ERROR_CHECK(nvs_flash_erase());
  71. ret = nvs_flash_init();
  72. }
  73. ESP_ERROR_CHECK(ret);
  74. // WiFi
  75. config_wifi();
  76. // mDNS
  77. start_mdns_service("blinky-jr");
  78. // HTTP Server
  79. httpd_handle_t server = start_webserver();
  80. // OTA Server
  81. start_ota_serv(server);
  82. // Dummy Filesystem
  83. start_filesystem();
  84. //log_dir("/spiffs");
  85. Device device("blinky-jr");
  86. // TODO: Load config from SPIFFS
  87. auto LEDs = TerminalLEDs(20);
  88. /*** *** ***/
  89. LEDs.setPattern(Presets::find("rainbow"));
  90. /*** *** ***/
  91. while (true) {
  92. ESP_LOGI(TAG, "Configuring LEDs");
  93. device.lock();
  94. const Pattern *pattern = Presets::find(device.get_effect());
  95. if (pattern) {
  96. LEDs.setPattern(pattern);
  97. } else {
  98. ESP_LOGW(TAG, "Could not find pattern '%s'", device.get_effect());
  99. pattern = LEDs.getPattern();
  100. }
  101. // TODO: Save Config to SPIFFS
  102. bool is_on = device.is_on();
  103. device.unlock();
  104. if (!pattern || !is_on) {
  105. if (!is_on) ESP_LOGI(TAG, "Device off");
  106. if (!pattern) ESP_LOGW(TAG, "No LED pattern set");
  107. ESP_LOGI(TAG, "Waiting for new config");
  108. device.wait();
  109. } else {
  110. ESP_LOGI(TAG, "Starting animation");
  111. int period_us = 1000000;
  112. int64_t target_us = time_us();
  113. while (true) {
  114. int64_t delay_us = target_us - time_us();
  115. if (delay_us < 0) delay_us = 0;
  116. if (device.wait(delay_us / 1000)) {
  117. // If the semaphore is set, go back to top of outer loop
  118. break;
  119. }
  120. LEDs.step();
  121. LEDs.show();
  122. target_us += period_us;
  123. }
  124. }
  125. }
  126. // Spin
  127. }