main.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #elif defined(RP2040_PLATFORM)
  16. #include <LittleFS.h>
  17. #elif defined(ESP32)
  18. #include <SPIFFS.h>
  19. #endif
  20. #ifdef ESP32
  21. #ifdef WIFI_SSID
  22. #include <helpers/esp32/SerialWifiInterface.h>
  23. SerialWifiInterface serial_interface;
  24. #ifndef TCP_PORT
  25. #define TCP_PORT 5000
  26. #endif
  27. #elif defined(BLE_PIN_CODE)
  28. #include <helpers/esp32/SerialBLEInterface.h>
  29. SerialBLEInterface serial_interface;
  30. #elif defined(SERIAL_RX)
  31. #include <helpers/ArduinoSerialInterface.h>
  32. ArduinoSerialInterface serial_interface;
  33. HardwareSerial companion_serial(1);
  34. #else
  35. #include <helpers/ArduinoSerialInterface.h>
  36. ArduinoSerialInterface serial_interface;
  37. #endif
  38. #elif defined(RP2040_PLATFORM)
  39. //#ifdef WIFI_SSID
  40. // #include <helpers/rp2040/SerialWifiInterface.h>
  41. // SerialWifiInterface serial_interface;
  42. // #ifndef TCP_PORT
  43. // #define TCP_PORT 5000
  44. // #endif
  45. // #elif defined(BLE_PIN_CODE)
  46. // #include <helpers/rp2040/SerialBLEInterface.h>
  47. // SerialBLEInterface serial_interface;
  48. #if defined(SERIAL_RX)
  49. #include <helpers/ArduinoSerialInterface.h>
  50. ArduinoSerialInterface serial_interface;
  51. HardwareSerial companion_serial(1);
  52. #else
  53. #include <helpers/ArduinoSerialInterface.h>
  54. ArduinoSerialInterface serial_interface;
  55. #endif
  56. #elif defined(NRF52_PLATFORM)
  57. #ifdef BLE_PIN_CODE
  58. #include <helpers/nrf52/SerialBLEInterface.h>
  59. SerialBLEInterface serial_interface;
  60. #else
  61. #include <helpers/ArduinoSerialInterface.h>
  62. ArduinoSerialInterface serial_interface;
  63. #endif
  64. #elif defined(STM32_PLATFORM)
  65. #include <helpers/ArduinoSerialInterface.h>
  66. ArduinoSerialInterface serial_interface;
  67. #else
  68. #error "need to define a serial interface"
  69. #endif
  70. /* GLOBAL OBJECTS */
  71. StdRNG fast_rng;
  72. SimpleMeshTables tables;
  73. MyMesh the_mesh(radio_driver, fast_rng, rtc_clock, tables);
  74. #ifdef DISPLAY_CLASS
  75. #include "UITask.h"
  76. UITask ui_task(&board);
  77. #endif
  78. /* END GLOBAL OBJECTS */
  79. void halt() {
  80. while (1) ;
  81. }
  82. void setup() {
  83. Serial.begin(115200);
  84. board.begin();
  85. #ifdef DISPLAY_CLASS
  86. DisplayDriver* disp = NULL;
  87. if (display.begin()) {
  88. disp = &display;
  89. disp->startFrame();
  90. disp->print("Please wait...");
  91. disp->endFrame();
  92. }
  93. #endif
  94. if (!radio_init()) { halt(); }
  95. fast_rng.begin(radio_get_rng_seed());
  96. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  97. InternalFS.begin();
  98. the_mesh.begin(InternalFS,
  99. #ifdef DISPLAY_CLASS
  100. disp != NULL
  101. #else
  102. false
  103. #endif
  104. );
  105. #ifdef BLE_PIN_CODE
  106. char dev_name[32+16];
  107. sprintf(dev_name, "%s%s", BLE_NAME_PREFIX, the_mesh.getNodeName());
  108. serial_interface.begin(dev_name, the_mesh.getBLEPin());
  109. #else
  110. serial_interface.begin(Serial);
  111. #endif
  112. the_mesh.startInterface(serial_interface);
  113. #elif defined(RP2040_PLATFORM)
  114. LittleFS.begin();
  115. the_mesh.begin(LittleFS,
  116. #ifdef DISPLAY_CLASS
  117. disp != NULL
  118. #else
  119. false
  120. #endif
  121. );
  122. //#ifdef WIFI_SSID
  123. // WiFi.begin(WIFI_SSID, WIFI_PWD);
  124. // serial_interface.begin(TCP_PORT);
  125. // #elif defined(BLE_PIN_CODE)
  126. // char dev_name[32+16];
  127. // sprintf(dev_name, "%s%s", BLE_NAME_PREFIX, the_mesh.getNodeName());
  128. // serial_interface.begin(dev_name, the_mesh.getBLEPin());
  129. #if defined(SERIAL_RX)
  130. companion_serial.setPins(SERIAL_RX, SERIAL_TX);
  131. companion_serial.begin(115200);
  132. serial_interface.begin(companion_serial);
  133. #else
  134. serial_interface.begin(Serial);
  135. #endif
  136. the_mesh.startInterface(serial_interface);
  137. #elif defined(ESP32)
  138. SPIFFS.begin(true);
  139. the_mesh.begin(SPIFFS,
  140. #ifdef DISPLAY_CLASS
  141. disp != NULL
  142. #else
  143. false
  144. #endif
  145. );
  146. #ifdef WIFI_SSID
  147. WiFi.begin(WIFI_SSID, WIFI_PWD);
  148. serial_interface.begin(TCP_PORT);
  149. #elif defined(BLE_PIN_CODE)
  150. char dev_name[32+16];
  151. sprintf(dev_name, "%s%s", BLE_NAME_PREFIX, the_mesh.getNodeName());
  152. serial_interface.begin(dev_name, the_mesh.getBLEPin());
  153. #elif defined(SERIAL_RX)
  154. companion_serial.setPins(SERIAL_RX, SERIAL_TX);
  155. companion_serial.begin(115200);
  156. serial_interface.begin(companion_serial);
  157. #else
  158. serial_interface.begin(Serial);
  159. #endif
  160. the_mesh.startInterface(serial_interface);
  161. #else
  162. #error "need to define filesystem"
  163. #endif
  164. sensors.begin();
  165. #ifdef DISPLAY_CLASS
  166. ui_task.begin(disp, the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION, the_mesh.getBLEPin());
  167. #endif
  168. }
  169. void loop() {
  170. the_mesh.loop();
  171. sensors.loop();
  172. }