main.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include <Arduino.h> // needed for PlatformIO
  2. #include <Mesh.h>
  3. #include "MyMesh.h"
  4. // Believe it or not, this std C function is busted on some platforms!
  5. static uint32_t _atoi(const char* sp) {
  6. uint32_t n = 0;
  7. while (*sp && *sp >= '0' && *sp <= '9') {
  8. n *= 10;
  9. n += (*sp++ - '0');
  10. }
  11. return n;
  12. }
  13. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  14. #include <InternalFileSystem.h>
  15. #if defined(QSPIFLASH)
  16. #include <CustomLFS_QSPIFlash.h>
  17. DataStore store(InternalFS, QSPIFlash, rtc_clock);
  18. #else
  19. #if defined(EXTRAFS)
  20. #include <CustomLFS.h>
  21. CustomLFS ExtraFS(0xD4000, 0x19000, 128);
  22. DataStore store(InternalFS, ExtraFS, rtc_clock);
  23. #else
  24. DataStore store(InternalFS, rtc_clock);
  25. #endif
  26. #endif
  27. #elif defined(RP2040_PLATFORM)
  28. #include <LittleFS.h>
  29. DataStore store(LittleFS, rtc_clock);
  30. #elif defined(ESP32)
  31. #include <SPIFFS.h>
  32. DataStore store(SPIFFS, rtc_clock);
  33. #endif
  34. #ifdef ESP32
  35. #ifdef WIFI_SSID
  36. #include <helpers/esp32/SerialWifiInterface.h>
  37. SerialWifiInterface serial_interface;
  38. #ifndef TCP_PORT
  39. #define TCP_PORT 5000
  40. #endif
  41. #elif defined(BLE_PIN_CODE)
  42. #include <helpers/esp32/SerialBLEInterface.h>
  43. SerialBLEInterface serial_interface;
  44. #elif defined(SERIAL_RX)
  45. #include <helpers/ArduinoSerialInterface.h>
  46. ArduinoSerialInterface serial_interface;
  47. HardwareSerial companion_serial(1);
  48. #else
  49. #include <helpers/ArduinoSerialInterface.h>
  50. ArduinoSerialInterface serial_interface;
  51. #endif
  52. #elif defined(RP2040_PLATFORM)
  53. //#ifdef WIFI_SSID
  54. // #include <helpers/rp2040/SerialWifiInterface.h>
  55. // SerialWifiInterface serial_interface;
  56. // #ifndef TCP_PORT
  57. // #define TCP_PORT 5000
  58. // #endif
  59. // #elif defined(BLE_PIN_CODE)
  60. // #include <helpers/rp2040/SerialBLEInterface.h>
  61. // SerialBLEInterface serial_interface;
  62. #if defined(SERIAL_RX)
  63. #include <helpers/ArduinoSerialInterface.h>
  64. ArduinoSerialInterface serial_interface;
  65. HardwareSerial companion_serial(1);
  66. #else
  67. #include <helpers/ArduinoSerialInterface.h>
  68. ArduinoSerialInterface serial_interface;
  69. #endif
  70. #elif defined(NRF52_PLATFORM)
  71. #ifdef BLE_PIN_CODE
  72. #include <helpers/nrf52/SerialBLEInterface.h>
  73. SerialBLEInterface serial_interface;
  74. #else
  75. #include <helpers/ArduinoSerialInterface.h>
  76. ArduinoSerialInterface serial_interface;
  77. #endif
  78. #elif defined(STM32_PLATFORM)
  79. #include <helpers/ArduinoSerialInterface.h>
  80. ArduinoSerialInterface serial_interface;
  81. #else
  82. #error "need to define a serial interface"
  83. #endif
  84. /* GLOBAL OBJECTS */
  85. #ifdef DISPLAY_CLASS
  86. #include "UITask.h"
  87. UITask ui_task(&board, &serial_interface);
  88. #endif
  89. StdRNG fast_rng;
  90. SimpleMeshTables tables;
  91. MyMesh the_mesh(radio_driver, fast_rng, rtc_clock, tables, store
  92. #ifdef DISPLAY_CLASS
  93. , &ui_task
  94. #endif
  95. );
  96. /* END GLOBAL OBJECTS */
  97. void halt() {
  98. while (1) ;
  99. }
  100. void setup() {
  101. Serial.begin(115200);
  102. board.begin();
  103. #ifdef DISPLAY_CLASS
  104. DisplayDriver* disp = NULL;
  105. if (display.begin()) {
  106. disp = &display;
  107. disp->startFrame();
  108. #ifdef ST7789
  109. disp->setTextSize(2);
  110. #endif
  111. disp->drawTextCentered(disp->width() / 2, 28, "Loading...");
  112. disp->endFrame();
  113. }
  114. #endif
  115. if (!radio_init()) { halt(); }
  116. fast_rng.begin(radio_driver.getRngSeed());
  117. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  118. InternalFS.begin();
  119. #if defined(QSPIFLASH)
  120. if (!QSPIFlash.begin()) {
  121. // debug output might not be available at this point, might be too early. maybe should fall back to InternalFS here?
  122. MESH_DEBUG_PRINTLN("CustomLFS_QSPIFlash: failed to initialize");
  123. } else {
  124. MESH_DEBUG_PRINTLN("CustomLFS_QSPIFlash: initialized successfully");
  125. }
  126. #else
  127. #if defined(EXTRAFS)
  128. ExtraFS.begin();
  129. #endif
  130. #endif
  131. store.begin();
  132. the_mesh.begin(
  133. #ifdef DISPLAY_CLASS
  134. disp != NULL
  135. #else
  136. false
  137. #endif
  138. );
  139. #ifdef BLE_PIN_CODE
  140. serial_interface.begin(BLE_NAME_PREFIX, the_mesh.getNodePrefs()->node_name, the_mesh.getBLEPin());
  141. #else
  142. serial_interface.begin(Serial);
  143. #endif
  144. the_mesh.startInterface(serial_interface);
  145. #elif defined(RP2040_PLATFORM)
  146. LittleFS.begin();
  147. store.begin();
  148. the_mesh.begin(
  149. #ifdef DISPLAY_CLASS
  150. disp != NULL
  151. #else
  152. false
  153. #endif
  154. );
  155. //#ifdef WIFI_SSID
  156. // WiFi.begin(WIFI_SSID, WIFI_PWD);
  157. // serial_interface.begin(TCP_PORT);
  158. // #elif defined(BLE_PIN_CODE)
  159. // char dev_name[32+16];
  160. // sprintf(dev_name, "%s%s", BLE_NAME_PREFIX, the_mesh.getNodeName());
  161. // serial_interface.begin(dev_name, the_mesh.getBLEPin());
  162. #if defined(SERIAL_RX)
  163. companion_serial.setPins(SERIAL_RX, SERIAL_TX);
  164. companion_serial.begin(115200);
  165. serial_interface.begin(companion_serial);
  166. #else
  167. serial_interface.begin(Serial);
  168. #endif
  169. the_mesh.startInterface(serial_interface);
  170. #elif defined(ESP32)
  171. SPIFFS.begin(true);
  172. store.begin();
  173. the_mesh.begin(
  174. #ifdef DISPLAY_CLASS
  175. disp != NULL
  176. #else
  177. false
  178. #endif
  179. );
  180. #ifdef WIFI_SSID
  181. board.setInhibitSleep(true); // prevent sleep when WiFi is active
  182. WiFi.begin(WIFI_SSID, WIFI_PWD);
  183. serial_interface.begin(TCP_PORT);
  184. #elif defined(BLE_PIN_CODE)
  185. serial_interface.begin(BLE_NAME_PREFIX, the_mesh.getNodePrefs()->node_name, the_mesh.getBLEPin());
  186. #elif defined(SERIAL_RX)
  187. companion_serial.setPins(SERIAL_RX, SERIAL_TX);
  188. companion_serial.begin(115200);
  189. serial_interface.begin(companion_serial);
  190. #else
  191. serial_interface.begin(Serial);
  192. #endif
  193. the_mesh.startInterface(serial_interface);
  194. #else
  195. #error "need to define filesystem"
  196. #endif
  197. sensors.begin();
  198. #if ENV_INCLUDE_GPS == 1
  199. the_mesh.applyGpsPrefs();
  200. #endif
  201. #ifdef DISPLAY_CLASS
  202. ui_task.begin(disp, &sensors, the_mesh.getNodePrefs()); // still want to pass this in as dependency, as prefs might be moved
  203. #endif
  204. }
  205. void loop() {
  206. the_mesh.loop();
  207. sensors.loop();
  208. #ifdef DISPLAY_CLASS
  209. ui_task.loop();
  210. #endif
  211. rtc_clock.tick();
  212. }