main.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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);
  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. disp->print("Please wait...");
  94. disp->endFrame();
  95. }
  96. #endif
  97. if (!radio_init()) { halt(); }
  98. fast_rng.begin(radio_get_rng_seed());
  99. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  100. InternalFS.begin();
  101. store.begin();
  102. the_mesh.begin(
  103. #ifdef DISPLAY_CLASS
  104. disp != NULL
  105. #else
  106. false
  107. #endif
  108. );
  109. #ifdef BLE_PIN_CODE
  110. char dev_name[32+16];
  111. sprintf(dev_name, "%s%s", BLE_NAME_PREFIX, the_mesh.getNodeName());
  112. serial_interface.begin(dev_name, the_mesh.getBLEPin());
  113. #else
  114. serial_interface.begin(Serial);
  115. #endif
  116. the_mesh.startInterface(serial_interface);
  117. #elif defined(RP2040_PLATFORM)
  118. LittleFS.begin();
  119. store.begin();
  120. the_mesh.begin(
  121. #ifdef DISPLAY_CLASS
  122. disp != NULL
  123. #else
  124. false
  125. #endif
  126. );
  127. //#ifdef WIFI_SSID
  128. // WiFi.begin(WIFI_SSID, WIFI_PWD);
  129. // serial_interface.begin(TCP_PORT);
  130. // #elif defined(BLE_PIN_CODE)
  131. // char dev_name[32+16];
  132. // sprintf(dev_name, "%s%s", BLE_NAME_PREFIX, the_mesh.getNodeName());
  133. // serial_interface.begin(dev_name, the_mesh.getBLEPin());
  134. #if defined(SERIAL_RX)
  135. companion_serial.setPins(SERIAL_RX, SERIAL_TX);
  136. companion_serial.begin(115200);
  137. serial_interface.begin(companion_serial);
  138. #else
  139. serial_interface.begin(Serial);
  140. #endif
  141. the_mesh.startInterface(serial_interface);
  142. #elif defined(ESP32)
  143. SPIFFS.begin(true);
  144. store.begin();
  145. the_mesh.begin(
  146. #ifdef DISPLAY_CLASS
  147. disp != NULL
  148. #else
  149. false
  150. #endif
  151. );
  152. #ifdef WIFI_SSID
  153. WiFi.begin(WIFI_SSID, WIFI_PWD);
  154. serial_interface.begin(TCP_PORT);
  155. #elif defined(BLE_PIN_CODE)
  156. char dev_name[32+16];
  157. sprintf(dev_name, "%s%s", BLE_NAME_PREFIX, the_mesh.getNodeName());
  158. serial_interface.begin(dev_name, the_mesh.getBLEPin());
  159. #elif defined(SERIAL_RX)
  160. companion_serial.setPins(SERIAL_RX, SERIAL_TX);
  161. companion_serial.begin(115200);
  162. serial_interface.begin(companion_serial);
  163. #else
  164. serial_interface.begin(Serial);
  165. #endif
  166. the_mesh.startInterface(serial_interface);
  167. #else
  168. #error "need to define filesystem"
  169. #endif
  170. sensors.begin();
  171. #ifdef DISPLAY_CLASS
  172. ui_task.begin(disp, &sensors, the_mesh.getNodePrefs()); // still want to pass this in as dependency, as prefs might be moved
  173. #endif
  174. }
  175. void loop() {
  176. the_mesh.loop();
  177. sensors.loop();
  178. #ifdef DISPLAY_CLASS
  179. ui_task.loop();
  180. #endif
  181. }