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.

243 lines
6.2KB

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