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.

216 lines
5.6KB

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