ESP32 Native version of Blinky, featureful controller code for WS2811/WS2812/NeoPixels
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

218 líneas
5.7KB

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