main.cpp 5.9 KB

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