mirror of
https://github.com/meshcore-dev/MeshCore.git
synced 2026-03-29 08:39:56 +00:00
109 lines
2.3 KiB
C++
109 lines
2.3 KiB
C++
#include <Arduino.h>
|
|
#include <target.h>
|
|
#include <helpers/ArduinoHelpers.h>
|
|
#include <helpers/IdentityStore.h>
|
|
#include "KissModem.h"
|
|
|
|
#if defined(NRF52_PLATFORM)
|
|
#include <InternalFileSystem.h>
|
|
#elif defined(RP2040_PLATFORM)
|
|
#include <LittleFS.h>
|
|
#elif defined(ESP32)
|
|
#include <SPIFFS.h>
|
|
#endif
|
|
|
|
StdRNG rng;
|
|
mesh::LocalIdentity identity;
|
|
KissModem* modem;
|
|
|
|
void halt() {
|
|
while (1) ;
|
|
}
|
|
|
|
void loadOrCreateIdentity() {
|
|
#if defined(NRF52_PLATFORM)
|
|
InternalFS.begin();
|
|
IdentityStore store(InternalFS, "");
|
|
#elif defined(ESP32)
|
|
SPIFFS.begin(true);
|
|
IdentityStore store(SPIFFS, "/identity");
|
|
#elif defined(RP2040_PLATFORM)
|
|
LittleFS.begin();
|
|
IdentityStore store(LittleFS, "/identity");
|
|
store.begin();
|
|
#else
|
|
#error "Filesystem not defined"
|
|
#endif
|
|
|
|
if (!store.load("_main", identity)) {
|
|
identity = radio_new_identity();
|
|
while (identity.pub_key[0] == 0x00 || identity.pub_key[0] == 0xFF) {
|
|
identity = radio_new_identity();
|
|
}
|
|
store.save("_main", identity);
|
|
}
|
|
}
|
|
|
|
void onSetRadio(float freq, float bw, uint8_t sf, uint8_t cr) {
|
|
radio_set_params(freq, bw, sf, cr);
|
|
}
|
|
|
|
void onSetTxPower(uint8_t power) {
|
|
radio_set_tx_power(power);
|
|
}
|
|
|
|
void onSetSyncWord(uint8_t sync_word) {
|
|
radio_set_sync_word(sync_word);
|
|
}
|
|
|
|
void setup() {
|
|
board.begin();
|
|
|
|
if (!radio_init()) {
|
|
halt();
|
|
}
|
|
|
|
radio_driver.begin();
|
|
|
|
rng.begin(radio_get_rng_seed());
|
|
loadOrCreateIdentity();
|
|
|
|
Serial.begin(115200);
|
|
uint32_t start = millis();
|
|
while (!Serial && millis() - start < 3000) delay(10);
|
|
delay(100);
|
|
|
|
modem = new KissModem(Serial, identity, rng);
|
|
modem->setRadioCallback(onSetRadio);
|
|
modem->setTxPowerCallback(onSetTxPower);
|
|
modem->setSyncWordCallback(onSetSyncWord);
|
|
modem->begin();
|
|
}
|
|
|
|
void loop() {
|
|
modem->loop();
|
|
|
|
uint8_t packet[KISS_MAX_PACKET_SIZE];
|
|
uint16_t len;
|
|
|
|
if (modem->getPacketToSend(packet, &len)) {
|
|
radio_driver.startSendRaw(packet, len);
|
|
while (!radio_driver.isSendComplete()) {
|
|
delay(1);
|
|
}
|
|
radio_driver.onSendFinished();
|
|
modem->onTxComplete(true);
|
|
}
|
|
|
|
uint8_t rx_buf[256];
|
|
int rx_len = radio_driver.recvRaw(rx_buf, sizeof(rx_buf));
|
|
|
|
if (rx_len > 0) {
|
|
int8_t snr = (int8_t)(radio_driver.getLastSNR() * 4);
|
|
int8_t rssi = (int8_t)radio_driver.getLastRSSI();
|
|
modem->onPacketReceived(snr, rssi, rx_buf, rx_len);
|
|
}
|
|
|
|
radio_driver.loop();
|
|
}
|