ESP32 Native version of Blinky, featureful controller code for WS2811/WS2812/NeoPixels
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

235 satır
5.9KB

  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_PERF
  68. //#define PROFILE_MEM
  69. // Entry Point
  70. extern "C" void app_main(void) {
  71. // Initialize NVS for WiFi Data
  72. esp_err_t ret = nvs_flash_init();
  73. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  74. ESP_ERROR_CHECK(nvs_flash_erase());
  75. ret = nvs_flash_init();
  76. }
  77. ESP_ERROR_CHECK(ret);
  78. // WiFi
  79. config_wifi();
  80. // mDNS
  81. start_mdns_service("blinky-jr");
  82. // HTTP Server
  83. httpd_handle_t server = start_webserver();
  84. // OTA Server
  85. start_ota_serv(server);
  86. // Dummy Filesystem
  87. start_filesystem();
  88. // TODO: Scrape this out
  89. log_dir("/spiffs");
  90. Device device("blinky-jr");
  91. cJSON *json = cJSON_Parse(read_file(config_path).c_str());
  92. if (json) {
  93. ESP_LOGI(TAG, "Setting config");
  94. device.set_json_config(json);
  95. cJSON_Delete(json);
  96. }
  97. LEDStrip *LEDs = new SPI_LEDs(39); //new TerminalLEDs();
  98. while (true) {
  99. Presets::reload();
  100. ESP_LOGI(TAG, "Configuring LEDs");
  101. device.lock();
  102. const std::string effect = device.get_effect();
  103. bool is_on = device.is_on();
  104. int length = device.get_strip_length();
  105. cJSON *json = device.make_json_config_locked();
  106. device.unlock();
  107. if (length != LEDs->getLength()) {
  108. LEDs->setLength(length);
  109. }
  110. const Pattern *pattern = Presets::find(effect);
  111. if (pattern) {
  112. ESP_LOGI(TAG, "Setting pattern '%s'", effect.c_str());
  113. LEDs->setPattern(pattern);
  114. char *config = cJSON_PrintUnformatted(json);
  115. write_file(config_path, config);
  116. ESP_LOGI(TAG, "Saved config");
  117. cJSON_free(config);
  118. } else {
  119. ESP_LOGW(TAG, "Could not find pattern '%s'", effect.c_str());
  120. pattern = LEDs->getPattern();
  121. }
  122. cJSON_Delete(json);
  123. if (length <= 0 || !pattern || !is_on) {
  124. if (!is_on) ESP_LOGI(TAG, "Device off");
  125. if (length <= 0) ESP_LOGW(TAG, "No LEDs configured");
  126. if (!pattern) ESP_LOGW(TAG, "No LED pattern set");
  127. ESP_LOGI(TAG, "Waiting for new config");
  128. device.wait();
  129. } else {
  130. ESP_LOGI(TAG, "Starting animation");
  131. int period_us = 1000000 / 60;
  132. int64_t target_us = time_us();
  133. int64_t profile_start = target_us;
  134. #ifdef PROFILE_PERF
  135. int64_t total_delay_us = 0;
  136. int n_delays = 0;
  137. int64_t after_sleep_us = 0;
  138. int n_processes = 0;
  139. int64_t total_process_us = 0;
  140. #endif // PROFILE_PERF
  141. while (true) {
  142. int64_t now = time_us();
  143. #ifdef PROFILE_PERF
  144. if (after_sleep_us > 0) {
  145. total_process_us += now - after_sleep_us;
  146. ++n_processes;
  147. }
  148. #endif // PROFILE_PERF
  149. if (now - profile_start >= 5000000) {
  150. #ifdef PROFILE_MEM
  151. heap_caps_print_heap_info(MALLOC_CAP_8BIT);
  152. #endif // PROFILE_MEM
  153. #ifdef PROFILE_PERF
  154. ESP_LOGI(TAG, "Average delay: %f ms", (double)total_delay_us / (n_delays * 1000));
  155. total_delay_us = 0;
  156. n_delays = 0;
  157. ESP_LOGI(TAG, "Average processing: %f us", (double)total_process_us / n_processes);
  158. total_process_us = 0;
  159. n_processes = 0;
  160. #endif // PROFILE_PERF
  161. profile_start = now;
  162. }
  163. int64_t delay_us = target_us - now;
  164. if (delay_us < 0) delay_us = 0;
  165. if (device.wait(delay_us / 1000)) {
  166. // If the semaphore is set, go back to top of outer loop
  167. break;
  168. }
  169. target_us = now + delay_us + period_us;
  170. #ifdef PROFILE_PERF
  171. after_sleep_us = time_us();
  172. total_delay_us += (after_sleep_us - now);
  173. ++n_delays;
  174. #endif // PROFILE_PERF
  175. LEDs->step();
  176. LEDs->show();
  177. }
  178. }
  179. }
  180. // Spin
  181. }