ESP32 Native version of Blinky, featureful controller code for WS2811/WS2812/NeoPixels
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

237 行
6.0KB

  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. // Trash the old preset in case we can't find the set effect
  100. LEDs->setPattern(NULL);
  101. Presets::reload();
  102. ESP_LOGI(TAG, "Configuring LEDs");
  103. device.lock();
  104. const std::string effect = device.get_effect();
  105. bool is_on = device.is_on();
  106. int length = device.get_strip_length();
  107. cJSON *json = device.make_json_config_locked();
  108. device.unlock();
  109. if (length != LEDs->getLength()) {
  110. LEDs->setLength(length);
  111. }
  112. const Pattern *pattern = Presets::find(effect);
  113. if (pattern) {
  114. ESP_LOGI(TAG, "Setting pattern '%s'", effect.c_str());
  115. LEDs->setPattern(pattern);
  116. char *config = cJSON_PrintUnformatted(json);
  117. write_file(config_path, config);
  118. ESP_LOGI(TAG, "Saved config");
  119. cJSON_free(config);
  120. } else {
  121. ESP_LOGW(TAG, "Could not find pattern '%s'", effect.c_str());
  122. pattern = LEDs->getPattern();
  123. }
  124. cJSON_Delete(json);
  125. if (length <= 0 || !pattern || !is_on) {
  126. if (!is_on) ESP_LOGI(TAG, "Device off");
  127. if (length <= 0) ESP_LOGW(TAG, "No LEDs configured");
  128. if (!pattern) ESP_LOGW(TAG, "No LED pattern set");
  129. ESP_LOGI(TAG, "Waiting for new config");
  130. device.wait();
  131. } else {
  132. ESP_LOGI(TAG, "Starting animation");
  133. int period_us = 1000000 / 60;
  134. int64_t target_us = time_us();
  135. int64_t profile_start = target_us;
  136. #ifdef PROFILE_PERF
  137. int64_t total_delay_us = 0;
  138. int n_delays = 0;
  139. int64_t after_sleep_us = 0;
  140. int n_processes = 0;
  141. int64_t total_process_us = 0;
  142. #endif // PROFILE_PERF
  143. while (true) {
  144. int64_t now = time_us();
  145. #ifdef PROFILE_PERF
  146. if (after_sleep_us > 0) {
  147. total_process_us += now - after_sleep_us;
  148. ++n_processes;
  149. }
  150. #endif // PROFILE_PERF
  151. if (now - profile_start >= 5000000) {
  152. #ifdef PROFILE_MEM
  153. heap_caps_print_heap_info(MALLOC_CAP_8BIT);
  154. #endif // PROFILE_MEM
  155. #ifdef PROFILE_PERF
  156. ESP_LOGI(TAG, "Average delay: %f ms", (double)total_delay_us / (n_delays * 1000));
  157. total_delay_us = 0;
  158. n_delays = 0;
  159. ESP_LOGI(TAG, "Average processing: %f us", (double)total_process_us / n_processes);
  160. total_process_us = 0;
  161. n_processes = 0;
  162. #endif // PROFILE_PERF
  163. profile_start = now;
  164. }
  165. int64_t delay_us = target_us - now;
  166. if (delay_us < 0) delay_us = 0;
  167. if (device.wait(delay_us / 1000)) {
  168. // If the semaphore is set, go back to top of outer loop
  169. break;
  170. }
  171. target_us = now + delay_us + period_us;
  172. #ifdef PROFILE_PERF
  173. after_sleep_us = time_us();
  174. total_delay_us += (after_sleep_us - now);
  175. ++n_delays;
  176. #endif // PROFILE_PERF
  177. LEDs->step();
  178. LEDs->show();
  179. }
  180. }
  181. }
  182. // Spin
  183. }