ESP32 Native version of Blinky, featureful controller code for WS2811/WS2812/NeoPixels
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
5.8KB

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