main.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include <Arduino.h>
  2. #include <target.h>
  3. #include <helpers/ArduinoHelpers.h>
  4. #include <helpers/IdentityStore.h>
  5. #include "KissModem.h"
  6. #if defined(NRF52_PLATFORM)
  7. #include <InternalFileSystem.h>
  8. #elif defined(RP2040_PLATFORM)
  9. #include <LittleFS.h>
  10. #elif defined(ESP32)
  11. #include <SPIFFS.h>
  12. #endif
  13. #define NOISE_FLOOR_CALIB_INTERVAL_MS 2000
  14. #define AGC_RESET_INTERVAL_MS 30000
  15. StdRNG rng;
  16. mesh::LocalIdentity identity;
  17. KissModem* modem;
  18. static uint32_t next_noise_floor_calib_ms = 0;
  19. static uint32_t next_agc_reset_ms = 0;
  20. void halt() {
  21. while (1) ;
  22. }
  23. void loadOrCreateIdentity() {
  24. #if defined(NRF52_PLATFORM)
  25. InternalFS.begin();
  26. IdentityStore store(InternalFS, "");
  27. #elif defined(ESP32)
  28. SPIFFS.begin(true);
  29. IdentityStore store(SPIFFS, "/identity");
  30. #elif defined(RP2040_PLATFORM)
  31. LittleFS.begin();
  32. IdentityStore store(LittleFS, "/identity");
  33. store.begin();
  34. #else
  35. #error "Filesystem not defined"
  36. #endif
  37. if (!store.load("_main", identity)) {
  38. identity = radio_new_identity();
  39. while (identity.pub_key[0] == 0x00 || identity.pub_key[0] == 0xFF) {
  40. identity = radio_new_identity();
  41. }
  42. store.save("_main", identity);
  43. }
  44. }
  45. void onSetRadio(float freq, float bw, uint8_t sf, uint8_t cr) {
  46. radio_set_params(freq, bw, sf, cr);
  47. }
  48. void onSetTxPower(uint8_t power) {
  49. radio_set_tx_power(power);
  50. }
  51. float onGetCurrentRssi() {
  52. return radio_driver.getCurrentRSSI();
  53. }
  54. void onGetStats(uint32_t* rx, uint32_t* tx, uint32_t* errors) {
  55. *rx = radio_driver.getPacketsRecv();
  56. *tx = radio_driver.getPacketsSent();
  57. *errors = radio_driver.getPacketsRecvErrors();
  58. }
  59. void setup() {
  60. board.begin();
  61. if (!radio_init()) {
  62. halt();
  63. }
  64. radio_driver.begin();
  65. rng.begin(radio_get_rng_seed());
  66. loadOrCreateIdentity();
  67. Serial.begin(115200);
  68. uint32_t start = millis();
  69. while (!Serial && millis() - start < 3000) delay(10);
  70. delay(100);
  71. sensors.begin();
  72. modem = new KissModem(Serial, identity, rng, radio_driver, board, sensors);
  73. modem->setRadioCallback(onSetRadio);
  74. modem->setTxPowerCallback(onSetTxPower);
  75. modem->setGetCurrentRssiCallback(onGetCurrentRssi);
  76. modem->setGetStatsCallback(onGetStats);
  77. modem->begin();
  78. }
  79. void loop() {
  80. modem->loop();
  81. uint8_t packet[KISS_MAX_PACKET_SIZE];
  82. uint16_t len;
  83. // trigger noise floor calibration
  84. if ((uint32_t)(millis() - next_noise_floor_calib_ms) >= NOISE_FLOOR_CALIB_INTERVAL_MS) {
  85. radio_driver.triggerNoiseFloorCalibrate(0);
  86. next_noise_floor_calib_ms = millis();
  87. }
  88. radio_driver.loop();
  89. if (modem->getPacketToSend(packet, &len)) {
  90. radio_driver.startSendRaw(packet, len);
  91. while (!radio_driver.isSendComplete()) {
  92. delay(1);
  93. }
  94. radio_driver.onSendFinished();
  95. modem->onTxComplete(true);
  96. }
  97. if ((uint32_t)(millis() - next_agc_reset_ms) >= AGC_RESET_INTERVAL_MS) {
  98. radio_driver.resetAGC();
  99. next_agc_reset_ms = millis();
  100. }
  101. uint8_t rx_buf[256];
  102. int rx_len = radio_driver.recvRaw(rx_buf, sizeof(rx_buf));
  103. if (rx_len > 0) {
  104. int8_t snr = (int8_t)(radio_driver.getLastSNR() * 4);
  105. int8_t rssi = (int8_t)radio_driver.getLastRSSI();
  106. modem->onPacketReceived(snr, rssi, rx_buf, rx_len);
  107. }
  108. }