feat: set Morse receive floor and add bounded RSSI diagnostics

This commit is contained in:
mikecarper committed 2026-10-20 17:44:29 -07:00
1 parent 4e27d5108b
commit 265b71d77a
15 files changed
+358 -20

No files matched your search

+27 -3
View File
@@ -25,7 +25,7 @@ inline MorseAction parseMorseAction(const char* word, size_t length) {
inline const char* morseUsageError() {
#if MESH_ENABLE_MORSE_RX
return "Error: use morse send <text> | listen <minutes> | read [offset] | stats | clear | stop";
return "Error: use morse send <text> | listen <minutes> (RX >=15 WPM) | read [offset] | stats | clear | stop";
#else
return "Error: use morse send <text> | stop";
#endif
@@ -104,6 +104,7 @@ inline bool handleMorseReplyCommand(MorseReplyHandoff& handoff, Radio* radio,
if (!handoff.remoteCommand() || !listen) return false;
float minutes = 0;
if (!radio || !radio->supportsMorseRx() || !morseSettingsUsable()
|| morseSettings().wpm < MorseRx::MinWpm
|| !cli::parseDecimalStrict(argument, minutes) || minutes <= 0 || minutes > 5) return false;
const uint32_t duration = uint32_t(minutes * 60000.0f + 0.5f);
if (duration < 1000 || duration > 300000) return false;
@@ -159,19 +160,29 @@ inline void formatMorseCapturePage(Radio* radio, const char* argument,
#if defined(MESH_MORSE_HIL) && MESH_MORSE_HIL
inline void formatMorseTracePage(Radio* radio, const char* argument,
char* reply, size_t capacity) {
#if defined(MESH_MORSE_HIL_RAW_RSSI) && MESH_MORSE_HIL_RAW_RSSI
if (radio->isMorseRxActive()) {
cli::formatError(reply, capacity, "stop Morse RX before reading raw RSSI");
return;
}
#endif
uint32_t offset = 0;
if (*argument && !cli::parseUnsignedIntegerStrict(argument, offset)) {
cli::formatError(reply, capacity, "use morse trace [retained entry offset]");
return;
}
const size_t length = radio->morseTraceLength();
if (length > 512 || offset > length) {
if (length > MorseRssiTrace::Capacity || offset > length) {
snprintf(reply, capacity, "Error: trace offset must be 0-%u", unsigned(length));
return;
}
// `t` is the absolute total entry count; offset is relative to the retained
// oldest sample. A changed t between pages identifies a moving HIL window.
#if defined(MESH_MORSE_HIL_RAW_RSSI) && MESH_MORSE_HIL_RAW_RSSI
const int prefix = snprintf(reply, capacity, "> morse.hil.raw-rssi-v1 %05u/%05u t=%lu n=----- x=%u q=-0.5 ",
#else
const int prefix = snprintf(reply, capacity, "> %03u/%03u t=%lu n=--- x=%u ",
#endif
unsigned(offset), unsigned(length), (unsigned long)radio->morseTraceTotal(),
unsigned(radio->morseTraceTruncated()));
if (prefix < 0 || size_t(prefix) + 1 >= capacity) {
@@ -186,8 +197,13 @@ inline void formatMorseTracePage(Radio* radio, const char* argument,
return;
}
char row[32];
#if defined(MESH_MORSE_HIL_RAW_RSSI) && MESH_MORSE_HIL_RAW_RSSI
const int bytes = snprintf(row, sizeof(row), "%lu,%u;",
(unsigned long)entry.millis, unsigned(-2.0f * entry.rssi_dbm));
#else
const int bytes = snprintf(row, sizeof(row), "%lu,%d,%u;",
(unsigned long)entry.millis, int(entry.rssi_dbm), unsigned(entry.flags));
#endif
if (bytes < 0 || size_t(bytes) >= sizeof(row) || used + size_t(bytes) >= capacity) break;
memcpy(reply + used, row, bytes);
used += bytes;
@@ -198,11 +214,19 @@ inline void formatMorseTracePage(Radio* radio, const char* argument,
cli::formatError(reply, capacity, "reply buffer too small for trace entry");
return;
}
#if defined(MESH_MORSE_HIL_RAW_RSSI) && MESH_MORSE_HIL_RAW_RSSI
char following[6];
if (next >= length) memcpy(following, "END ", 6);
else snprintf(following, sizeof(following), "%05u", unsigned(next));
char* marker = strstr(reply, "n=-----");
if (marker) memcpy(marker + 2, following, 5);
#else
char following[4];
if (next >= length) memcpy(following, "END", 4);
else snprintf(following, sizeof(following), "%03u", unsigned(next));
char* marker = strstr(reply, "n=---");
if (marker) memcpy(marker + 2, following, 3);
#endif
}
#endif
#endif // MESH_ENABLE_MORSE_RX
@@ -494,7 +518,7 @@ inline bool handleMorseCommand(Radio* radio, const char* command,
case MorseRxResult::Overlap:
cli::formatError(reply, capacity, "Morse frequency overlaps radio or radio2; change morse.freq"); break;
case MorseRxResult::InvalidSpeed:
snprintf(reply, capacity, "Error: morse.wpm must be %u-%u", unsigned(MorseMinWpm), unsigned(MorseMaxWpm)); break;
snprintf(reply, capacity, "Error: Morse RX needs %u-%u WPM; set morse.wpm", unsigned(MorseRx::MinWpm), unsigned(MorseMaxWpm)); break;
case MorseRxResult::InvalidDuration:
cli::formatError(reply, capacity, "Morse listening must last 1-300 seconds"); break;
default:
+78
View File
@@ -0,0 +1,78 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
#if !defined(MESH_MORSE_HIL_RAW_RSSI) || MESH_MORSE_HIL_RAW_RSSI != 1 \
|| !defined(ESP32_PLATFORM) || !defined(MESH_MORSE_HIL) || !MESH_MORSE_HIL \
|| !defined(MESH_MORSE_HIL_PRODUCTION_RF) || !MESH_MORSE_HIL_PRODUCTION_RF
#error "Raw Morse RSSI requires ESP32 production-equivalent HIL diagnostics"
#endif
#ifndef MESH_MORSE_HIL_RAW_WINDOW_MS
#define MESH_MORSE_HIL_RAW_WINDOW_MS 60000
#endif
namespace mesh {
struct MorseRawRssiTraceEntry {
uint32_t millis;
float rssi_dbm;
uint8_t flags;
};
// Explicit ESP32 diagnostic only. Retain every half-dBm measurement from RX
// entry, including arming and candidates. Freeze the prefix on overflow; never
// overwrite its initial detector history or stream while the radio samples.
class MorseRawRssiTrace {
public:
static constexpr uint32_t WindowMillis = MESH_MORSE_HIL_RAW_WINDOW_MS;
static_assert(WindowMillis >= 30000 && WindowMillis <= 60000,
"Raw Morse trace window must be 30000..60000 milliseconds");
static constexpr size_t Capacity = WindowMillis / 2 + 1;
enum StopReason : uint8_t { None, Window, Full, Gap, Value };
void clear() { _length = _total = _last_elapsed = 0; _reason = None; }
size_t length() const { return _length; }
uint32_t total() const { return _total; }
bool truncated() const { return _reason != None; }
StopReason stopReason() const { return _reason; }
bool read(size_t offset, MorseRawRssiTraceEntry& out) const {
if (offset >= _length) return false;
const size_t anchor = offset / AnchorStride;
uint32_t elapsed = _anchors[anchor];
for (size_t index = anchor * AnchorStride + 1; index <= offset; ++index)
elapsed += delta(_entries[index]);
out = {elapsed, -0.5f * _entries[offset].raw_rssi, 0};
return true;
}
void sample(float rssi, uint32_t elapsed, uint8_t flags) {
(void)flags;
++_total;
if (_reason != None) return;
if (elapsed > WindowMillis) { _reason = Window; return; }
if (_length == Capacity) { _reason = Full; return; }
const uint32_t dt = _length ? elapsed - _last_elapsed : 0;
if (dt > UINT16_MAX) { _reason = Gap; return; }
// SX126x GetRssiInst is exactly -0.5 * one unsigned byte. Reject an
// unrepresentable value instead of silently rounding a different backend.
if (!(rssi >= -127.5f && rssi <= 0.0f)) { _reason = Value; return; }
const float raw = -2.0f * rssi;
if (raw != static_cast<uint16_t>(raw)) { _reason = Value; return; }
if (_length % AnchorStride == 0) _anchors[_length / AnchorStride] = elapsed;
_entries[_length++] = {uint8_t(dt), uint8_t(dt >> 8), uint8_t(raw)};
_last_elapsed = elapsed;
}
private:
static constexpr size_t AnchorStride = 256;
struct StoredEntry { uint8_t delta_low, delta_high, raw_rssi; };
static_assert(sizeof(StoredEntry) == 3, "Raw Morse entries must remain three bytes");
static uint16_t delta(const StoredEntry& entry) {
return uint16_t(entry.delta_low) | uint16_t(uint16_t(entry.delta_high) << 8);
}
StoredEntry _entries[Capacity];
uint32_t _anchors[(Capacity + AnchorStride - 1) / AnchorStride];
uint32_t _length = 0, _total = 0, _last_elapsed = 0;
StopReason _reason = None;
};
} // namespace mesh
+21 -2
View File
@@ -21,6 +21,15 @@
#error "HIL Morse fast OFF debounce flag must be 0 or 1"
#endif
#if defined(MESH_MORSE_HIL_RAW_RSSI) && MESH_MORSE_HIL_RAW_RSSI
#if MESH_MORSE_HIL_RAW_RSSI != 1 || !defined(ESP32_PLATFORM) \
|| !defined(MESH_MORSE_HIL) || !MESH_MORSE_HIL \
|| !defined(MESH_MORSE_HIL_PRODUCTION_RF) || !MESH_MORSE_HIL_PRODUCTION_RF
#error "Raw Morse RSSI requires ESP32 production-equivalent HIL diagnostics"
#endif
#include "MorseRawRssiTrace.h"
#endif
namespace mesh {
#if defined(MESH_MORSE_HIL) && MESH_MORSE_HIL
@@ -53,6 +62,10 @@ struct MorseRxDiagnostics {
};
#if defined(MESH_MORSE_HIL) && MESH_MORSE_HIL
#if defined(MESH_MORSE_HIL_RAW_RSSI) && MESH_MORSE_HIL_RAW_RSSI
using MorseRssiTraceEntry = MorseRawRssiTraceEntry;
using MorseRssiTrace = MorseRawRssiTrace;
#else
struct MorseRssiTraceEntry {
uint32_t millis;
int8_t rssi_dbm;
@@ -124,6 +137,7 @@ private:
bool _triggered = false, _dropped = false;
};
#endif
#endif
// RSSI envelope decoding, not an RF demodulator or an artificial narrow
// filter. The real receiver supplies its instantaneous power measurements.
@@ -150,6 +164,7 @@ public:
"Production-equivalent HIL requires the shipping Morse receiver clock");
#endif
static constexpr uint32_t MinDurationMillis = 1000UL;
static constexpr uint8_t MinWpm = 15;
static constexpr uint32_t MaxDurationMillis = 300000UL;
static constexpr uint32_t NoiseLearningMillis = 500UL;
static constexpr size_t CaptureCapacity = 4096;
@@ -160,7 +175,7 @@ public:
#endif
static MorseRxResult validate(uint8_t wpm, uint32_t duration_ms) {
if (!MorseTx::unitMillis(wpm)) return MorseRxResult::InvalidSpeed;
if (wpm < MinWpm || !MorseTx::unitMillis(wpm)) return MorseRxResult::InvalidSpeed;
if (duration_ms < MinDurationMillis || duration_ms > MaxDurationMillis)
return MorseRxResult::InvalidDuration;
return MorseRxResult::Started;
@@ -256,6 +271,9 @@ public:
void sample(float rssi_dbm, uint32_t now) {
if (!service(now) || !(rssi_dbm >= -180.0f && rssi_dbm <= 20.0f)) return;
#if defined(MESH_MORSE_HIL_RAW_RSSI) && MESH_MORSE_HIL_RAW_RSSI
if (_trace) _trace->sample(rssi_dbm, static_cast<uint32_t>(now - _started), 0);
#endif
_last_rssi = rssi_dbm;
const uint32_t sample_gap = static_cast<uint32_t>(now - _last_sample_at);
const float filter_gap = sample_gap > 10U ? 10U : sample_gap;
@@ -410,7 +428,8 @@ private:
#endif
void traceSample(float rssi, uint32_t now, bool raw_keyed) {
#if defined(MESH_MORSE_HIL) && MESH_MORSE_HIL
#if defined(MESH_MORSE_HIL) && MESH_MORSE_HIL \
&& !(defined(MESH_MORSE_HIL_RAW_RSSI) && MESH_MORSE_HIL_RAW_RSSI)
if (_trace) _trace->sample(rssi, static_cast<uint32_t>(now - _started),
(raw_keyed ? 1 : 0) | (_keyed ? 2 : 0) | (_candidate_valid ? 4 : 0)
| (learning(now) ? 8 : 0));