ESP32 Native version of Blinky, featureful controller code for WS2811/WS2812/NeoPixels
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

138 lignes
2.9KB

  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. // mDNS / NetBIOS
  12. #include <mdns.h>
  13. #include <lwip/apps/netbiosns.h>
  14. static void start_mdns_service(const char *hostname) {
  15. // Initialize mDNS service
  16. ESP_ERROR_CHECK(mdns_init());
  17. // Set hostname
  18. mdns_hostname_set(hostname);
  19. // Set default instance
  20. mdns_instance_name_set("Blinky Lights");
  21. // NetBIOS too
  22. netbiosns_init();
  23. netbiosns_set_name(hostname);
  24. }
  25. // Dummy FS
  26. static void start_filesystem() {
  27. ESP_LOGI(TAG, "Initializing filesystem");
  28. esp_vfs_spiffs_conf_t conf = {
  29. .base_path = "/spiffs",
  30. .partition_label = NULL,
  31. .max_files = 5,
  32. .format_if_mount_failed = true
  33. };
  34. // Use settings defined above to initialize and mount SPIFFS filesystem.
  35. // Note: esp_vfs_spiffs_register is an all-in-one convenience function.
  36. ESP_ERROR_CHECK(esp_vfs_spiffs_register(&conf));
  37. }
  38. #include <string>
  39. #include <dirent.h>
  40. #include <sys/stat.h>
  41. #include <errno.h>
  42. static void log_dir(const std::string &path) {
  43. struct stat st;
  44. int err = stat(path.c_str(), &st);
  45. if (err < 0) {
  46. ESP_LOGE(TAG, "%s %d", path.c_str(), errno);
  47. } else {
  48. ESP_LOGI(TAG, "%s %d", path.c_str(), st.st_size);
  49. }
  50. DIR *dir;
  51. if ((dir = opendir(path.c_str())) == NULL) {
  52. //ESP_LOGE(TAG, "Failed to open %s", path.c_str());
  53. return;
  54. }
  55. struct dirent *de;
  56. while ((de = readdir(dir)) != NULL) {
  57. log_dir(path + "/" + de->d_name);
  58. }
  59. closedir(dir);
  60. }
  61. int64_t now_us() {
  62. struct timeval tv_now;
  63. gettimeofday(&tv_now, NULL);
  64. return (int64_t)tv_now.tv_sec * 1000000L + (int64_t)tv_now.tv_usec;
  65. }
  66. // Entry Point
  67. extern "C" void app_main(void) {
  68. // Initialize NVS for WiFi Data
  69. esp_err_t ret = nvs_flash_init();
  70. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  71. ESP_ERROR_CHECK(nvs_flash_erase());
  72. ret = nvs_flash_init();
  73. }
  74. ESP_ERROR_CHECK(ret);
  75. // WiFi
  76. config_wifi();
  77. // mDNS
  78. start_mdns_service("blinky-jr");
  79. // HTTP Server
  80. httpd_handle_t server = start_webserver();
  81. // OTA Server
  82. start_ota_serv(server);
  83. // Dummy Filesystem
  84. start_filesystem();
  85. log_dir("/spiffs");
  86. // TODO: Something useful
  87. Device device("blinky-jr");
  88. /*
  89. int period_us = 1000000;
  90. int64_t target_us = now_us();
  91. while (true) {
  92. int64_t delay_us = target_us - now_us();
  93. if (delay_us < 0) delay_us = 0;
  94. if (device.wait(delay_us / 1000))
  95. ESP_LOGI(TAG, "Reload!");
  96. else {
  97. ESP_LOGI(TAG, "Timeout!");
  98. target_us += period_us;
  99. }
  100. }
  101. */
  102. while (device.wait()) ESP_LOGI(TAG, "Reload!");
  103. // Spin
  104. }