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.

267 líneas
6.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. // mDNS / NetBIOS
  17. #include <mdns.h>
  18. #include <lwip/apps/netbiosns.h>
  19. static void start_mdns_service(const char *hostname) {
  20. // Initialize mDNS service
  21. ESP_ERROR_CHECK(mdns_init());
  22. // Set hostname
  23. mdns_hostname_set(hostname);
  24. // Set default instance
  25. mdns_instance_name_set("Blinky Lights");
  26. // NetBIOS too
  27. netbiosns_init();
  28. netbiosns_set_name(hostname);
  29. }
  30. // Dummy FS
  31. static void start_filesystem() {
  32. ESP_LOGI(TAG, "Initializing filesystem");
  33. esp_vfs_spiffs_conf_t conf = {
  34. .base_path = "/spiffs",
  35. .partition_label = NULL,
  36. .max_files = 5,
  37. .format_if_mount_failed = true
  38. };
  39. // Use settings defined above to initialize and mount SPIFFS filesystem.
  40. // Note: esp_vfs_spiffs_register is an all-in-one convenience function.
  41. ESP_ERROR_CHECK(esp_vfs_spiffs_register(&conf));
  42. }
  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 %d", 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. */
  68. static std::string read_file(const char *path) {
  69. std::string data;
  70. FILE *f = fopen(path, "r");
  71. if (!f) {
  72. ESP_LOGE(TAG, "Failed to read %s", path);
  73. return data;
  74. }
  75. constexpr size_t szbuf = 4096;
  76. char *buf = (char*)malloc(szbuf);
  77. size_t nread;
  78. while ((nread = fread(buf, 1, szbuf - 1, f)) > 0) {
  79. buf[nread] = '\0';
  80. data += buf;
  81. }
  82. fclose(f);
  83. free(buf);
  84. return data;
  85. }
  86. static void write_file(const char *path, const std::string &data) {
  87. FILE *f = fopen(path, "w");
  88. if (!f) {
  89. ESP_LOGE(TAG, "Failed to open %s", path);
  90. return;
  91. }
  92. const char *ptr = data.c_str();
  93. size_t remain = data.length();
  94. size_t nwritten = fwrite(ptr, 1, remain, f);
  95. if (nwritten != remain) {
  96. ESP_LOGE(TAG, "Failed to write to %s: %d/%d", path, nwritten, remain);
  97. }
  98. fclose(f);
  99. }
  100. std::string gen_config(bool is_on, const std::string &effect) {
  101. return std::string() + "{ "
  102. "\"state\": \"" + (is_on ? "ON" : "OFF") + "\", "
  103. "\"effect\": \"" + effect + "\""
  104. " }";
  105. }
  106. constexpr char config_path[] = "/spiffs/blinky.json";
  107. //#define PROFILE
  108. // Entry Point
  109. extern "C" void app_main(void) {
  110. // Initialize NVS for WiFi Data
  111. esp_err_t ret = nvs_flash_init();
  112. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  113. ESP_ERROR_CHECK(nvs_flash_erase());
  114. ret = nvs_flash_init();
  115. }
  116. ESP_ERROR_CHECK(ret);
  117. // WiFi
  118. config_wifi();
  119. // mDNS
  120. start_mdns_service("blinky-jr");
  121. // HTTP Server
  122. httpd_handle_t server = start_webserver();
  123. // OTA Server
  124. start_ota_serv(server);
  125. // Dummy Filesystem
  126. start_filesystem();
  127. // TODO: Scrape this out
  128. //log_dir("/spiffs");
  129. Device device("blinky-jr");
  130. cJSON *json = cJSON_Parse(read_file(config_path).c_str());
  131. if (json) {
  132. ESP_LOGI(TAG, "Setting config");
  133. device.set_json_config(json);
  134. cJSON_Delete(json);
  135. }
  136. LEDStrip *LEDs = new SPI_LEDs(39); //new TerminalLEDs();
  137. while (true) {
  138. ESP_LOGI(TAG, "Configuring LEDs");
  139. device.lock();
  140. const std::string &effect = device.get_effect();
  141. bool is_on = device.is_on();
  142. int length = device.get_strip_length();
  143. cJSON *json = device.make_json_config_locked();
  144. device.unlock();
  145. if (length != LEDs->getLength()) {
  146. LEDs->setLength(length);
  147. }
  148. const Pattern *pattern = Presets::find(effect);
  149. if (pattern) {
  150. LEDs->setPattern(pattern);
  151. } else {
  152. ESP_LOGW(TAG, "Could not find pattern '%s'", effect.c_str());
  153. pattern = LEDs->getPattern();
  154. }
  155. char *config = cJSON_PrintUnformatted(json);
  156. write_file(config_path, config);
  157. ESP_LOGI(TAG, "Saved config");
  158. cJSON_free(config);
  159. cJSON_Delete(json);
  160. if (length <= 0 || !pattern || !is_on) {
  161. if (!is_on) ESP_LOGI(TAG, "Device off");
  162. if (length <= 0) ESP_LOGW(TAG, "No LEDs configured");
  163. if (!pattern) ESP_LOGW(TAG, "No LED pattern set");
  164. ESP_LOGI(TAG, "Waiting for new config");
  165. device.wait();
  166. } else {
  167. ESP_LOGI(TAG, "Starting animation");
  168. int period_us = 1000000 / 60;
  169. int64_t start = time_us();
  170. int64_t target_us = start;
  171. #ifdef PROFILE
  172. int64_t total_delay_us = 0;
  173. int n_delays = 0;
  174. int64_t after_sleep_us = 0;
  175. int n_processes = 0;
  176. int64_t total_process_us = 0;
  177. #endif // PROFILE
  178. while (true) {
  179. int64_t now = time_us();
  180. #ifdef PROFILE
  181. if (after_sleep_us > 0) {
  182. total_process_us += now - after_sleep_us;
  183. ++n_processes;
  184. }
  185. if (now - start >= 5000000) {
  186. ESP_LOGI(TAG, "Average delay: %f ms", (double)total_delay_us / (n_delays * 1000));
  187. total_delay_us = 0;
  188. n_delays = 0;
  189. ESP_LOGI(TAG, "Average processing: %f us", (double)total_process_us / n_processes);
  190. total_process_us = 0;
  191. n_processes = 0;
  192. start = now;
  193. }
  194. #endif // PROFILE
  195. int64_t delay_us = target_us - now;
  196. if (delay_us < 0) delay_us = 0;
  197. if (device.wait(delay_us / 1000)) {
  198. // If the semaphore is set, go back to top of outer loop
  199. break;
  200. }
  201. target_us = now + delay_us + period_us;
  202. #ifdef PROFILE
  203. after_sleep_us = time_us();
  204. total_delay_us += (after_sleep_us - now);
  205. ++n_delays;
  206. #endif // PROFILE
  207. LEDs->step();
  208. LEDs->show();
  209. }
  210. }
  211. }
  212. // Spin
  213. }