mirror of
https://github.com/meshcore-dev/MeshCore.git
synced 2026-04-20 22:13:47 +00:00
Add KISS Modem firmware
This commit is contained in:
parent
acca73f57e
commit
c786cfe613
5 changed files with 706 additions and 0 deletions
108
examples/kiss_modem/main.cpp
Normal file
108
examples/kiss_modem/main.cpp
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
#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();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue