main.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <Arduino.h> // needed for PlatformIO
  2. #include <Mesh.h>
  3. #include "MyMesh.h"
  4. #ifdef DISPLAY_CLASS
  5. #include "UITask.h"
  6. static UITask ui_task(display);
  7. #endif
  8. StdRNG fast_rng;
  9. SimpleMeshTables tables;
  10. MyMesh the_mesh(board, radio_driver, *new ArduinoMillis(), fast_rng, rtc_clock, tables);
  11. void halt() {
  12. while (1) ;
  13. }
  14. static char command[MAX_POST_TEXT_LEN+1];
  15. void setup() {
  16. Serial.begin(115200);
  17. delay(1000);
  18. board.begin();
  19. #ifdef DISPLAY_CLASS
  20. if (display.begin()) {
  21. display.startFrame();
  22. display.setCursor(0, 0);
  23. display.print("Please wait...");
  24. display.endFrame();
  25. }
  26. #endif
  27. if (!radio_init()) { halt(); }
  28. fast_rng.begin(radio_driver.getRngSeed());
  29. FILESYSTEM* fs;
  30. #if defined(NRF52_PLATFORM)
  31. InternalFS.begin();
  32. fs = &InternalFS;
  33. IdentityStore store(InternalFS, "");
  34. #elif defined(RP2040_PLATFORM)
  35. LittleFS.begin();
  36. fs = &LittleFS;
  37. IdentityStore store(LittleFS, "/identity");
  38. store.begin();
  39. #elif defined(ESP32)
  40. SPIFFS.begin(true);
  41. fs = &SPIFFS;
  42. IdentityStore store(SPIFFS, "/identity");
  43. #else
  44. #error "need to define filesystem"
  45. #endif
  46. if (!store.load("_main", the_mesh.self_id)) {
  47. the_mesh.self_id = radio_new_identity(); // create new random identity
  48. int count = 0;
  49. while (count < 10 && (the_mesh.self_id.pub_key[0] == 0x00 || the_mesh.self_id.pub_key[0] == 0xFF)) { // reserved id hashes
  50. the_mesh.self_id = radio_new_identity(); count++;
  51. }
  52. store.save("_main", the_mesh.self_id);
  53. }
  54. Serial.print("Room ID: ");
  55. mesh::Utils::printHex(Serial, the_mesh.self_id.pub_key, PUB_KEY_SIZE); Serial.println();
  56. command[0] = 0;
  57. sensors.begin();
  58. the_mesh.begin(fs);
  59. #ifdef DISPLAY_CLASS
  60. ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
  61. #endif
  62. // send out initial zero hop Advertisement to the mesh
  63. #if ENABLE_ADVERT_ON_BOOT == 1
  64. the_mesh.sendSelfAdvertisement(16000, false);
  65. #endif
  66. board.onBootComplete();
  67. }
  68. void loop() {
  69. int len = strlen(command);
  70. while (Serial.available() && len < sizeof(command)-1) {
  71. char c = Serial.read();
  72. if (c != '\n') {
  73. command[len++] = c;
  74. command[len] = 0;
  75. }
  76. Serial.print(c);
  77. }
  78. if (len == sizeof(command)-1) { // command buffer full
  79. command[sizeof(command)-1] = '\r';
  80. }
  81. if (len > 0 && command[len - 1] == '\r') { // received complete line
  82. command[len - 1] = 0; // replace newline with C string null terminator
  83. char reply[160];
  84. the_mesh.handleCommand(0, command, reply); // NOTE: there is no sender_timestamp via serial!
  85. if (reply[0]) {
  86. Serial.print(" -> "); Serial.println(reply);
  87. }
  88. command[0] = 0; // reset command buffer
  89. }
  90. the_mesh.loop();
  91. sensors.loop();
  92. #ifdef DISPLAY_CLASS
  93. ui_task.loop();
  94. #endif
  95. rtc_clock.tick();
  96. }