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.

266 lines
6.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. // 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. // Entry Point
  108. extern "C" void app_main(void) {
  109. // Initialize NVS for WiFi Data
  110. esp_err_t ret = nvs_flash_init();
  111. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  112. ESP_ERROR_CHECK(nvs_flash_erase());
  113. ret = nvs_flash_init();
  114. }
  115. ESP_ERROR_CHECK(ret);
  116. // WiFi
  117. config_wifi();
  118. // mDNS
  119. start_mdns_service("blinky-jr");
  120. // HTTP Server
  121. httpd_handle_t server = start_webserver();
  122. // OTA Server
  123. start_ota_serv(server);
  124. // Dummy Filesystem
  125. start_filesystem();
  126. // TODO: Scrape this out
  127. //log_dir("/spiffs");
  128. Device device("blinky-jr");
  129. cJSON *json = cJSON_Parse(read_file(config_path).c_str());
  130. if (json) {
  131. ESP_LOGI(TAG, "Setting config");
  132. device.set_json_config(json);
  133. cJSON_Delete(json);
  134. }
  135. LEDStrip *LEDs = new SPI_LEDs(39); //new TerminalLEDs();
  136. while (true) {
  137. ESP_LOGI(TAG, "Configuring LEDs");
  138. device.lock();
  139. const std::string &effect = device.get_effect();
  140. bool is_on = device.is_on();
  141. int length = device.get_strip_length();
  142. cJSON *json = device.make_json_config_locked();
  143. device.unlock();
  144. if (length != LEDs->getLength()) {
  145. LEDs->setLength(length);
  146. }
  147. const Pattern *pattern = Presets::find(effect);
  148. if (pattern) {
  149. LEDs->setPattern(pattern);
  150. } else {
  151. ESP_LOGW(TAG, "Could not find pattern '%s'", effect.c_str());
  152. pattern = LEDs->getPattern();
  153. }
  154. char *config = cJSON_PrintUnformatted(json);
  155. write_file(config_path, config);
  156. ESP_LOGI(TAG, "Saved config");
  157. cJSON_free(config);
  158. cJSON_Delete(json);
  159. if (length <= 0 || !pattern || !is_on) {
  160. if (!is_on) ESP_LOGI(TAG, "Device off");
  161. if (length <= 0) ESP_LOGW(TAG, "No LEDs configured");
  162. if (!pattern) ESP_LOGW(TAG, "No LED pattern set");
  163. ESP_LOGI(TAG, "Waiting for new config");
  164. device.wait();
  165. } else {
  166. ESP_LOGI(TAG, "Starting animation");
  167. int period_us = 1000000 / 60;
  168. int64_t start = time_us();
  169. int64_t target_us = start;
  170. /*
  171. int64_t total_delay_us = 0;
  172. int n_delays = 0;
  173. int64_t after_sleep_us = 0;
  174. int n_processes = 0;
  175. int64_t total_process_us = 0;
  176. */
  177. while (true) {
  178. int64_t now = time_us();
  179. /*
  180. if (after_sleep_us > 0) {
  181. total_process_us += now - after_sleep_us;
  182. ++n_processes;
  183. }
  184. if (now - start >= 5000000) {
  185. ESP_LOGI(TAG, "Average delay: %f ms", (double)total_delay_us / (n_delays * 1000));
  186. total_delay_us = 0;
  187. n_delays = 0;
  188. ESP_LOGI(TAG, "Average processing: %f us", (double)total_process_us / n_processes);
  189. total_process_us = 0;
  190. n_processes = 0;
  191. start = now;
  192. }
  193. */
  194. int64_t delay_us = target_us - now;
  195. if (delay_us < 0) delay_us = 0;
  196. if (device.wait(delay_us / 1000)) {
  197. // If the semaphore is set, go back to top of outer loop
  198. break;
  199. }
  200. /*
  201. after_sleep_us = time_us();
  202. total_delay_us += (after_sleep_us - now);
  203. ++n_delays;
  204. */
  205. LEDs->step();
  206. LEDs->show();
  207. target_us += period_us;
  208. }
  209. }
  210. }
  211. // Spin
  212. }