main.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. DataStore store(InternalFS, rtc_clock);
  16. #elif defined(RP2040_PLATFORM)
  17. #include <LittleFS.h>
  18. DataStore store(LittleFS, rtc_clock);
  19. #elif defined(ESP32)
  20. #include <SPIFFS.h>
  21. DataStore store(SPIFFS, rtc_clock);
  22. #endif
  23. #ifdef ESP32
  24. #ifdef WIFI_SSID
  25. #include <helpers/esp32/SerialWifiInterface.h>
  26. SerialWifiInterface serial_interface;
  27. #ifndef TCP_PORT
  28. #define TCP_PORT 5000
  29. #endif
  30. #elif defined(BLE_PIN_CODE)
  31. #include <helpers/esp32/SerialBLEInterface.h>
  32. SerialBLEInterface serial_interface;
  33. #elif defined(SERIAL_RX)
  34. #include <helpers/ArduinoSerialInterface.h>
  35. ArduinoSerialInterface serial_interface;
  36. HardwareSerial companion_serial(1);
  37. #else
  38. #include <helpers/ArduinoSerialInterface.h>
  39. ArduinoSerialInterface serial_interface;
  40. #endif
  41. #elif defined(RP2040_PLATFORM)
  42. //#ifdef WIFI_SSID
  43. // #include <helpers/rp2040/SerialWifiInterface.h>
  44. // SerialWifiInterface serial_interface;
  45. // #ifndef TCP_PORT
  46. // #define TCP_PORT 5000
  47. // #endif
  48. // #elif defined(BLE_PIN_CODE)
  49. // #include <helpers/rp2040/SerialBLEInterface.h>
  50. // SerialBLEInterface serial_interface;
  51. #if defined(SERIAL_RX)
  52. #include <helpers/ArduinoSerialInterface.h>
  53. ArduinoSerialInterface serial_interface;
  54. HardwareSerial companion_serial(1);
  55. #else
  56. #include <helpers/ArduinoSerialInterface.h>
  57. ArduinoSerialInterface serial_interface;
  58. #endif
  59. #elif defined(NRF52_PLATFORM)
  60. #ifdef BLE_PIN_CODE
  61. #include <helpers/nrf52/SerialBLEInterface.h>
  62. SerialBLEInterface serial_interface;
  63. #else
  64. #include <helpers/ArduinoSerialInterface.h>
  65. ArduinoSerialInterface serial_interface;
  66. #endif
  67. #elif defined(STM32_PLATFORM)
  68. #include <helpers/ArduinoSerialInterface.h>
  69. ArduinoSerialInterface serial_interface;
  70. #else
  71. #error "need to define a serial interface"
  72. #endif
  73. /* GLOBAL OBJECTS */
  74. StdRNG fast_rng;
  75. SimpleMeshTables tables;
  76. MyMesh the_mesh(radio_driver, fast_rng, rtc_clock, tables, store);
  77. #ifdef DISPLAY_CLASS
  78. #include "UITask.h"
  79. UITask ui_task(&board, &serial_interface);
  80. #endif
  81. /* END GLOBAL OBJECTS */
  82. void halt() {
  83. while (1) ;
  84. }
  85. void setup() {
  86. Serial.begin(115200);
  87. board.begin();
  88. #ifdef DISPLAY_CLASS
  89. DisplayDriver* disp = NULL;
  90. if (display.begin()) {
  91. disp = &display;
  92. disp->startFrame();
  93. #ifdef ST7789
  94. disp->setTextSize(2);
  95. #endif
  96. disp->drawTextCentered(disp->width() / 2, 28, "Loading...");
  97. disp->endFrame();
  98. }
  99. #endif
  100. if (!radio_init()) { halt(); }
  101. fast_rng.begin(radio_get_rng_seed());
  102. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  103. InternalFS.begin();
  104. store.begin();
  105. the_mesh.begin(
  106. #ifdef DISPLAY_CLASS
  107. disp != NULL
  108. #else
  109. false
  110. #endif
  111. );
  112. #ifdef BLE_PIN_CODE
  113. char dev_name[32+16];
  114. sprintf(dev_name, "%s%s", BLE_NAME_PREFIX, the_mesh.getNodeName());
  115. serial_interface.begin(dev_name, the_mesh.getBLEPin());
  116. #else
  117. serial_interface.begin(Serial);
  118. #endif
  119. the_mesh.startInterface(serial_interface);
  120. #elif defined(RP2040_PLATFORM)
  121. LittleFS.begin();
  122. store.begin();
  123. the_mesh.begin(
  124. #ifdef DISPLAY_CLASS
  125. disp != NULL
  126. #else
  127. false
  128. #endif
  129. );
  130. //#ifdef WIFI_SSID
  131. // WiFi.begin(WIFI_SSID, WIFI_PWD);
  132. // serial_interface.begin(TCP_PORT);
  133. // #elif defined(BLE_PIN_CODE)
  134. // char dev_name[32+16];
  135. // sprintf(dev_name, "%s%s", BLE_NAME_PREFIX, the_mesh.getNodeName());
  136. // serial_interface.begin(dev_name, the_mesh.getBLEPin());
  137. #if defined(SERIAL_RX)
  138. companion_serial.setPins(SERIAL_RX, SERIAL_TX);
  139. companion_serial.begin(115200);
  140. serial_interface.begin(companion_serial);
  141. #else
  142. serial_interface.begin(Serial);
  143. #endif
  144. the_mesh.startInterface(serial_interface);
  145. #elif defined(ESP32)
  146. SPIFFS.begin(true);
  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. #elif 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. #else
  171. #error "need to define filesystem"
  172. #endif
  173. sensors.begin();
  174. #ifdef DISPLAY_CLASS
  175. ui_task.begin(disp, &sensors, the_mesh.getNodePrefs()); // still want to pass this in as dependency, as prefs might be moved
  176. #endif
  177. }
  178. void loop() {
  179. the_mesh.loop();
  180. sensors.loop();
  181. #ifdef DISPLAY_CLASS
  182. ui_task.loop();
  183. #endif
  184. }