ESP32 Native version of Blinky, featureful controller code for WS2811/WS2812/NeoPixels
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

263 satır
6.5KB

  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. #include <string>
  44. #include <dirent.h>
  45. #include <sys/stat.h>
  46. #include <errno.h>
  47. static void log_dir(const std::string &path) {
  48. struct stat st;
  49. int err = stat(path.c_str(), &st);
  50. if (err < 0) {
  51. ESP_LOGE(TAG, "%s %d", path.c_str(), errno);
  52. } else {
  53. ESP_LOGI(TAG, "%s %d", path.c_str(), st.st_size);
  54. }
  55. DIR *dir;
  56. if ((dir = opendir(path.c_str())) == NULL) {
  57. //ESP_LOGE(TAG, "Failed to open %s", path.c_str());
  58. return;
  59. }
  60. struct dirent *de;
  61. while ((de = readdir(dir)) != NULL) {
  62. log_dir(path + "/" + de->d_name);
  63. }
  64. closedir(dir);
  65. }
  66. static std::string read_file(const char *path) {
  67. std::string data;
  68. FILE *f = fopen(path, "r");
  69. if (!f) {
  70. ESP_LOGE(TAG, "Failed to read %s", path);
  71. return data;
  72. }
  73. constexpr size_t szbuf = 4096;
  74. char *buf = (char*)malloc(szbuf);
  75. size_t nread;
  76. while ((nread = fread(buf, 1, szbuf - 1, f)) > 0) {
  77. buf[nread] = '\0';
  78. data += buf;
  79. }
  80. fclose(f);
  81. free(buf);
  82. return data;
  83. }
  84. static void write_file(const char *path, const std::string &data) {
  85. FILE *f = fopen(path, "w");
  86. if (!f) {
  87. ESP_LOGE(TAG, "Failed to open %s", path);
  88. return;
  89. }
  90. const char *ptr = data.c_str();
  91. size_t remain = data.length();
  92. size_t nwritten = fwrite(ptr, 1, remain, f);
  93. if (nwritten != remain) {
  94. ESP_LOGE(TAG, "Failed to write to %s: %d/%d", path, nwritten, remain);
  95. }
  96. fclose(f);
  97. }
  98. std::string gen_config(bool is_on, const std::string &effect) {
  99. return std::string() + "{ "
  100. "\"state\": \"" + (is_on ? "ON" : "OFF") + "\", "
  101. "\"effect\": \"" + effect + "\""
  102. " }";
  103. }
  104. constexpr char config_path[] = "/spiffs/blinky.json";
  105. // Entry Point
  106. extern "C" void app_main(void) {
  107. // Initialize NVS for WiFi Data
  108. esp_err_t ret = nvs_flash_init();
  109. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  110. ESP_ERROR_CHECK(nvs_flash_erase());
  111. ret = nvs_flash_init();
  112. }
  113. ESP_ERROR_CHECK(ret);
  114. // WiFi
  115. config_wifi();
  116. // mDNS
  117. start_mdns_service("blinky-jr");
  118. // HTTP Server
  119. httpd_handle_t server = start_webserver();
  120. // OTA Server
  121. start_ota_serv(server);
  122. // Dummy Filesystem
  123. start_filesystem();
  124. // TODO: Scrape this out
  125. log_dir("/spiffs");
  126. Device device("blinky-jr");
  127. cJSON *json = cJSON_Parse(read_file(config_path).c_str());
  128. if (json) {
  129. ESP_LOGI(TAG, "Setting config");
  130. device.set_json_config(json);
  131. cJSON_Delete(json);
  132. }
  133. LEDStrip *LEDs = new SPI_LEDs(39); //new TerminalLEDs();
  134. while (true) {
  135. ESP_LOGI(TAG, "Configuring LEDs");
  136. device.lock();
  137. const std::string &effect = device.get_effect();
  138. bool is_on = device.is_on();
  139. int length = device.get_strip_length();
  140. cJSON *json = device.make_json_config_locked();
  141. device.unlock();
  142. if (length != LEDs->getLength()) {
  143. LEDs->setLength(length);
  144. }
  145. const Pattern *pattern = Presets::find(effect);
  146. if (pattern) {
  147. LEDs->setPattern(pattern);
  148. } else {
  149. ESP_LOGW(TAG, "Could not find pattern '%s'", effect.c_str());
  150. pattern = LEDs->getPattern();
  151. }
  152. char *config = cJSON_PrintUnformatted(json);
  153. write_file(config_path, config);
  154. ESP_LOGI(TAG, "Saved config");
  155. cJSON_free(config);
  156. cJSON_Delete(json);
  157. if (length <= 0 || !pattern || !is_on) {
  158. if (!is_on) ESP_LOGI(TAG, "Device off");
  159. if (length <= 0) ESP_LOGW(TAG, "No LEDs configured");
  160. if (!pattern) ESP_LOGW(TAG, "No LED pattern set");
  161. ESP_LOGI(TAG, "Waiting for new config");
  162. device.wait();
  163. } else {
  164. ESP_LOGI(TAG, "Starting animation");
  165. int period_us = 1000000 / 60;
  166. int64_t start = time_us();
  167. int64_t target_us = start;
  168. int64_t total_delay_us = 0;
  169. int n_delays = 0;
  170. int64_t after_sleep_us = 0;
  171. int n_processes = 0;
  172. int64_t total_process_us = 0;
  173. while (true) {
  174. int64_t now = time_us();
  175. if (after_sleep_us > 0) {
  176. total_process_us += now - after_sleep_us;
  177. ++n_processes;
  178. }
  179. if (now - start >= 5000000) {
  180. ESP_LOGI(TAG, "Average delay: %f ms", (double)total_delay_us / (n_delays * 1000));
  181. total_delay_us = 0;
  182. n_delays = 0;
  183. ESP_LOGI(TAG, "Average processing: %f us", (double)total_process_us / n_processes);
  184. total_process_us = 0;
  185. n_processes = 0;
  186. start = now;
  187. }
  188. int64_t delay_us = target_us - now;
  189. if (delay_us < 0) delay_us = 0;
  190. if (device.wait(delay_us / 1000)) {
  191. // If the semaphore is set, go back to top of outer loop
  192. break;
  193. }
  194. after_sleep_us = time_us();
  195. total_delay_us += (after_sleep_us - now);
  196. ++n_delays;
  197. LEDs->step();
  198. LEDs->show();
  199. target_us += period_us;
  200. }
  201. }
  202. }
  203. // Spin
  204. }