main.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #ifdef DISPLAY_CLASS
  75. #include "UITask.h"
  76. UITask ui_task(&board, &serial_interface);
  77. #endif
  78. StdRNG fast_rng;
  79. SimpleMeshTables tables;
  80. MyMesh the_mesh(radio_driver, fast_rng, rtc_clock, tables, store
  81. #ifdef DISPLAY_CLASS
  82. , &ui_task
  83. #endif
  84. );
  85. /* END GLOBAL OBJECTS */
  86. void halt() {
  87. while (1) ;
  88. }
  89. void setup() {
  90. Serial.begin(115200);
  91. board.begin();
  92. #ifdef DISPLAY_CLASS
  93. DisplayDriver* disp = NULL;
  94. if (display.begin()) {
  95. disp = &display;
  96. disp->startFrame();
  97. #ifdef ST7789
  98. disp->setTextSize(2);
  99. #endif
  100. disp->drawTextCentered(disp->width() / 2, 28, "Loading...");
  101. disp->endFrame();
  102. }
  103. #endif
  104. if (!radio_init()) { halt(); }
  105. fast_rng.begin(radio_get_rng_seed());
  106. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  107. InternalFS.begin();
  108. store.begin();
  109. the_mesh.begin(
  110. #ifdef DISPLAY_CLASS
  111. disp != NULL
  112. #else
  113. false
  114. #endif
  115. );
  116. #ifdef BLE_PIN_CODE
  117. char dev_name[32+16];
  118. sprintf(dev_name, "%s%s", BLE_NAME_PREFIX, the_mesh.getNodeName());
  119. serial_interface.begin(dev_name, the_mesh.getBLEPin());
  120. #else
  121. serial_interface.begin(Serial);
  122. #endif
  123. the_mesh.startInterface(serial_interface);
  124. #elif defined(RP2040_PLATFORM)
  125. LittleFS.begin();
  126. store.begin();
  127. the_mesh.begin(
  128. #ifdef DISPLAY_CLASS
  129. disp != NULL
  130. #else
  131. false
  132. #endif
  133. );
  134. //#ifdef WIFI_SSID
  135. // WiFi.begin(WIFI_SSID, WIFI_PWD);
  136. // serial_interface.begin(TCP_PORT);
  137. // #elif defined(BLE_PIN_CODE)
  138. // char dev_name[32+16];
  139. // sprintf(dev_name, "%s%s", BLE_NAME_PREFIX, the_mesh.getNodeName());
  140. // serial_interface.begin(dev_name, the_mesh.getBLEPin());
  141. #if defined(SERIAL_RX)
  142. companion_serial.setPins(SERIAL_RX, SERIAL_TX);
  143. companion_serial.begin(115200);
  144. serial_interface.begin(companion_serial);
  145. #else
  146. serial_interface.begin(Serial);
  147. #endif
  148. the_mesh.startInterface(serial_interface);
  149. #elif defined(ESP32)
  150. SPIFFS.begin(true);
  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. #elif 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. #else
  175. #error "need to define filesystem"
  176. #endif
  177. sensors.begin();
  178. #ifdef DISPLAY_CLASS
  179. ui_task.begin(disp, &sensors, the_mesh.getNodePrefs()); // still want to pass this in as dependency, as prefs might be moved
  180. #endif
  181. }
  182. void loop() {
  183. the_mesh.loop();
  184. sensors.loop();
  185. #ifdef DISPLAY_CLASS
  186. ui_task.loop();
  187. #endif
  188. }