SkyLok/chip_test_example/lora_tx.cpp
2026-05-28 11:36:20 +02:00

81 lines
2.3 KiB
C++

#include <cstdio>
#include <cstring>
#include <chrono>
#include <thread>
#include "lr1121_malnus.hpp"
static void applyPaPreset(lr1121::Config &cfg, bool hp_mode)
{
cfg.pa_sel = hp_mode ? 0x01 : 0x00;
cfg.tx_dbm = hp_mode ? 14 : 10;
}
static bool parseArgs(int argc, char **argv, bool &verbose, bool &do_reset, bool &hp_mode)
{
for (int i = 1; i < argc; ++i) {
if (std::strcmp(argv[i], "-v") == 0) {
verbose = true;
continue;
}
if (std::strcmp(argv[i], "--reset") == 0) {
do_reset = true;
continue;
}
if (std::strcmp(argv[i], "--hp") == 0) {
hp_mode = true;
continue;
}
if (std::strcmp(argv[i], "--lp") == 0) {
hp_mode = false;
continue;
}
std::fprintf(stderr, "Unknown option: %s\nUsage: sudo ./lora_tx [-v] [--reset] [--lp|--hp]\n", argv[i]);
return false;
}
return true;
}
int main(int argc, char **argv)
{
lr1121::Config cfg;
cfg.freq_hz = lr1121::FREQ_433;
cfg.busy_gpio = 24;
cfg.reset_gpio = 25;
cfg.dio9_gpio = 4;
cfg.sf = 0x07;
cfg.bw = 0x04;
cfg.cr = 0x01;
bool do_reset = false;
bool hp_mode = false;
if (!parseArgs(argc, argv, cfg.verbose, do_reset, hp_mode)) return 2;
applyPaPreset(cfg, hp_mode);
std::printf("TX %u Hz, %s PA, %d dBm, gpio(busy=%u reset=%u dio9=%u)%s\n",
cfg.freq_hz, cfg.pa_sel ? "HP" : "LP", static_cast<int>(cfg.tx_dbm),
cfg.busy_gpio, cfg.reset_gpio, cfg.dio9_gpio, cfg.verbose ? " [v]" : "");
lr1121::Radio radio;
if (!radio.begin(cfg)) {
std::fprintf(stderr, "ERROR: radio init failed\n");
return 1;
}
if (do_reset) {
std::puts("Applying soft settings reset...");
if (!radio.softResetSettings()) {
std::fprintf(stderr, "ERROR: soft settings reset failed\n");
return 1;
}
}
std::puts("Radio OK - sending...");
for (int n = 0; ; ++n) {
char msg[32];
const int len = std::snprintf(msg, sizeof(msg), "hello %d", n);
const bool ok = radio.send(reinterpret_cast<const uint8_t *>(msg), static_cast<uint8_t>(len));
std::printf("[%04d] %s: %s\n", n, msg, ok ? "OK" : "FAIL");
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}