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.

238 line
6.1KB

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