ESP32 Native version of Blinky, featureful controller code for WS2811/WS2812/NeoPixels
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

227 řádky
5.7KB

  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. #include "spi_leds.hpp"
  16. // mDNS / NetBIOS
  17. #include <mdns.h>
  18. #include <lwip/apps/netbiosns.h>
  19. static void start_mdns_service(const char *hostname) {
  20. // Initialize mDNS service
  21. ESP_ERROR_CHECK(mdns_init());
  22. // Set hostname
  23. mdns_hostname_set(hostname);
  24. // Set default instance
  25. mdns_instance_name_set("Blinky Lights");
  26. // NetBIOS too
  27. netbiosns_init();
  28. netbiosns_set_name(hostname);
  29. }
  30. // Dummy FS
  31. static void start_filesystem() {
  32. ESP_LOGI(TAG, "Initializing filesystem");
  33. esp_vfs_spiffs_conf_t conf = {
  34. .base_path = "/spiffs",
  35. .partition_label = NULL,
  36. .max_files = 5,
  37. .format_if_mount_failed = true
  38. };
  39. // Use settings defined above to initialize and mount SPIFFS filesystem.
  40. // Note: esp_vfs_spiffs_register is an all-in-one convenience function.
  41. ESP_ERROR_CHECK(esp_vfs_spiffs_register(&conf));
  42. }
  43. #include <string>
  44. #include <dirent.h>
  45. #include <sys/stat.h>
  46. #include <errno.h>
  47. static void log_dir(const std::string &path) {
  48. struct stat st;
  49. int err = stat(path.c_str(), &st);
  50. if (err < 0) {
  51. ESP_LOGE(TAG, "%s %d", path.c_str(), errno);
  52. } else {
  53. ESP_LOGI(TAG, "%s %d", path.c_str(), st.st_size);
  54. }
  55. DIR *dir;
  56. if ((dir = opendir(path.c_str())) == NULL) {
  57. //ESP_LOGE(TAG, "Failed to open %s", path.c_str());
  58. return;
  59. }
  60. struct dirent *de;
  61. while ((de = readdir(dir)) != NULL) {
  62. log_dir(path + "/" + de->d_name);
  63. }
  64. closedir(dir);
  65. }
  66. constexpr char config_path[] = "/spiffs/blinky.json";
  67. //#define PROFILE
  68. // Entry Point
  69. extern "C" void app_main(void) {
  70. // Initialize NVS for WiFi Data
  71. esp_err_t ret = nvs_flash_init();
  72. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  73. ESP_ERROR_CHECK(nvs_flash_erase());
  74. ret = nvs_flash_init();
  75. }
  76. ESP_ERROR_CHECK(ret);
  77. // WiFi
  78. config_wifi();
  79. // mDNS
  80. start_mdns_service("blinky-jr");
  81. // HTTP Server
  82. httpd_handle_t server = start_webserver();
  83. // OTA Server
  84. start_ota_serv(server);
  85. // Dummy Filesystem
  86. start_filesystem();
  87. // TODO: Scrape this out
  88. log_dir("/spiffs");
  89. Device device("blinky-jr");
  90. cJSON *json = cJSON_Parse(read_file(config_path).c_str());
  91. if (json) {
  92. ESP_LOGI(TAG, "Setting config");
  93. device.set_json_config(json);
  94. cJSON_Delete(json);
  95. }
  96. LEDStrip *LEDs = new SPI_LEDs(39); //new TerminalLEDs();
  97. while (true) {
  98. Presets::reload();
  99. ESP_LOGI(TAG, "Configuring LEDs");
  100. device.lock();
  101. const std::string &effect = device.get_effect();
  102. bool is_on = device.is_on();
  103. int length = device.get_strip_length();
  104. cJSON *json = device.make_json_config_locked();
  105. device.unlock();
  106. if (length != LEDs->getLength()) {
  107. LEDs->setLength(length);
  108. }
  109. const Pattern *pattern = Presets::find(effect);
  110. if (pattern) {
  111. ESP_LOGI(TAG, "Setting pattern '%s'", effect.c_str());
  112. LEDs->setPattern(pattern);
  113. } else {
  114. ESP_LOGW(TAG, "Could not find pattern '%s'", effect.c_str());
  115. pattern = LEDs->getPattern();
  116. }
  117. char *config = cJSON_PrintUnformatted(json);
  118. write_file(config_path, config);
  119. ESP_LOGI(TAG, "Saved config");
  120. cJSON_free(config);
  121. cJSON_Delete(json);
  122. if (length <= 0 || !pattern || !is_on) {
  123. if (!is_on) ESP_LOGI(TAG, "Device off");
  124. if (length <= 0) ESP_LOGW(TAG, "No LEDs configured");
  125. if (!pattern) ESP_LOGW(TAG, "No LED pattern set");
  126. ESP_LOGI(TAG, "Waiting for new config");
  127. device.wait();
  128. } else {
  129. ESP_LOGI(TAG, "Starting animation");
  130. int period_us = 1000000 / 60;
  131. int64_t start = time_us();
  132. int64_t target_us = start;
  133. #ifdef PROFILE
  134. int64_t total_delay_us = 0;
  135. int n_delays = 0;
  136. int64_t after_sleep_us = 0;
  137. int n_processes = 0;
  138. int64_t total_process_us = 0;
  139. #endif // PROFILE
  140. while (true) {
  141. int64_t now = time_us();
  142. #ifdef PROFILE
  143. if (after_sleep_us > 0) {
  144. total_process_us += now - after_sleep_us;
  145. ++n_processes;
  146. }
  147. if (now - start >= 5000000) {
  148. ESP_LOGI(TAG, "Average delay: %f ms", (double)total_delay_us / (n_delays * 1000));
  149. total_delay_us = 0;
  150. n_delays = 0;
  151. ESP_LOGI(TAG, "Average processing: %f us", (double)total_process_us / n_processes);
  152. total_process_us = 0;
  153. n_processes = 0;
  154. start = now;
  155. }
  156. #endif // PROFILE
  157. int64_t delay_us = target_us - now;
  158. if (delay_us < 0) delay_us = 0;
  159. if (device.wait(delay_us / 1000)) {
  160. // If the semaphore is set, go back to top of outer loop
  161. break;
  162. }
  163. target_us = now + delay_us + period_us;
  164. #ifdef PROFILE
  165. after_sleep_us = time_us();
  166. total_delay_us += (after_sleep_us - now);
  167. ++n_delays;
  168. #endif // PROFILE
  169. LEDs->step();
  170. LEDs->show();
  171. }
  172. }
  173. }
  174. // Spin
  175. }