mirror of
https://github.com/portapack-mayhem/mayhem-firmware.git
synced 2026-09-10 08:39:29 +00:00
Feature/signal hunter (#3245)
This commit is contained in:
@@ -769,6 +769,13 @@ set(MODE_CPPSRC
|
||||
)
|
||||
DeclareTargets(PTON tones)
|
||||
|
||||
### Signal Hunter
|
||||
|
||||
set(MODE_CPPSRC
|
||||
proc_signal_hunter.cpp
|
||||
)
|
||||
DeclareTargets(HUNT signal_hunter)
|
||||
|
||||
|
||||
|
||||
### Time Sink
|
||||
|
||||
@@ -0,0 +1,189 @@
|
||||
/*
|
||||
* Copyright (C) 2026 Matej Sochan
|
||||
*
|
||||
* This file is part of PortaPack.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; see the file COPYING. If not, write to
|
||||
* the Free Software Foundation, Inc., 51 Franklin Street,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include "proc_signal_hunter.hpp"
|
||||
#include "event_m4.hpp"
|
||||
#include "portapack_shared_memory.hpp"
|
||||
#include "dsp_fir_taps.hpp"
|
||||
|
||||
void SignalHunterProcessor::configure() {
|
||||
decim_0.configure(taps_200k_decim_0.taps);
|
||||
|
||||
window_idx = 0;
|
||||
window_sum = 0;
|
||||
for (auto& v : window_buf) v = 0;
|
||||
|
||||
reset_hunt_state();
|
||||
configured = true;
|
||||
}
|
||||
|
||||
void SignalHunterProcessor::reset_hunt_state() {
|
||||
iq_ring_idx = 0;
|
||||
for (auto& v : iq_ring) v = complex16_t{0, 0};
|
||||
hangtime_counter = 0;
|
||||
hunt_state = HuntState::IDLE;
|
||||
flush_pending = false;
|
||||
}
|
||||
|
||||
void SignalHunterProcessor::execute(const buffer_c8_t& buffer) {
|
||||
// Process stream closure.
|
||||
// Deferred teardown ensures exclusive mutation of the hunt state by the BasebandThread,
|
||||
// preventing data races with on_message().
|
||||
if (stream_close_requested.exchange(false)) {
|
||||
stream.reset();
|
||||
reset_hunt_state();
|
||||
}
|
||||
|
||||
// CRITICAL IPC FIX: If M0 is tearing down the capture thread, it needs one last buffer
|
||||
// to unblock buffers.get() and exit gracefully. We MUST continue decimating and writing
|
||||
// as long as the stream exists, even if hunting was set to false by a manual UI stop.
|
||||
if (!hunting && !stream) return;
|
||||
|
||||
const auto out = decim_0.execute(buffer, dst_buffer);
|
||||
feed_channel_stats(out);
|
||||
|
||||
// Pre-roll flush: first execute() call after CaptureConfigMessage creates stream.
|
||||
if (flush_pending && stream_active && stream) {
|
||||
// Write ring buffer from oldest to newest sample (2 chunks due to wrap-around)
|
||||
size_t first = IQ_RING_SAMPLES - flush_start_idx;
|
||||
stream->write(&iq_ring[flush_start_idx], first * sizeof(complex16_t));
|
||||
if (flush_start_idx > 0)
|
||||
stream->write(&iq_ring[0], flush_start_idx * sizeof(complex16_t));
|
||||
flush_pending = false;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < out.count; i++) {
|
||||
auto s = out.p[i];
|
||||
|
||||
iq_ring[iq_ring_idx] = s;
|
||||
iq_ring_idx = (iq_ring_idx + 1) % IQ_RING_SAMPLES;
|
||||
|
||||
uint32_t energy = ((int32_t)s.real() * s.real() + (int32_t)s.imag() * s.imag()) >> 16;
|
||||
window_sum -= window_buf[window_idx];
|
||||
window_buf[window_idx] = energy;
|
||||
window_sum += energy;
|
||||
window_idx = (window_idx + 1) % WINDOW_SIZE;
|
||||
|
||||
uint32_t avg = window_sum / WINDOW_SIZE;
|
||||
|
||||
switch (hunt_state.load()) {
|
||||
case HuntState::IDLE:
|
||||
// Only trigger new recordings if we are actively hunting.
|
||||
// Prevents a stray trigger from starting a new capture right after manual STOP.
|
||||
if (hunting && (avg > energy_threshold)) {
|
||||
HunterTriggerMessage msg{};
|
||||
msg.energy = avg;
|
||||
shared_memory.application_queue.push(msg);
|
||||
hunt_state = HuntState::AWAITING_STREAM;
|
||||
}
|
||||
break;
|
||||
|
||||
case HuntState::AWAITING_STREAM:
|
||||
break;
|
||||
|
||||
case HuntState::RECORDING:
|
||||
if (avg < energy_threshold) {
|
||||
hangtime_counter = hangtime_samples_limit;
|
||||
hunt_state = HuntState::HANGTIME;
|
||||
}
|
||||
break;
|
||||
|
||||
case HuntState::HANGTIME:
|
||||
if (avg > energy_threshold) {
|
||||
hunt_state = HuntState::RECORDING;
|
||||
} else if (--hangtime_counter == 0) {
|
||||
HunterStopMessage stop_msg{};
|
||||
shared_memory.application_queue.push(stop_msg);
|
||||
hunt_state = HuntState::AWAITING_CLOSE;
|
||||
}
|
||||
break;
|
||||
|
||||
case HuntState::AWAITING_CLOSE:
|
||||
// Do not transition to IDLE by ourselves — wait for CaptureConfigMessage(nullptr)
|
||||
// which arrives via BasebandCapture destructor after CaptureThread is destroyed.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// CRITICAL: Continue writing to stream whenever it exists, even in AWAITING_CLOSE state,
|
||||
// and even after a manual STOP — M0's CaptureThread needs this final data to unblock.
|
||||
if (stream_active && stream) {
|
||||
stream->write(out.p, sizeof(complex16_t) * out.count);
|
||||
}
|
||||
}
|
||||
|
||||
void SignalHunterProcessor::on_message(const Message* const message) {
|
||||
switch (message->id) {
|
||||
case Message::ID::HunterConfig: {
|
||||
const auto& m = *reinterpret_cast<const HunterConfigMessage*>(message);
|
||||
energy_threshold = m.energy_threshold;
|
||||
|
||||
// Convert hangtime to post-decimation samples:
|
||||
// 1 ms = 250 samples @ 250 kHz post-decimation rate (from 2 MHz baseband / 8x decimator)
|
||||
// This dynamic hangtime allows configurable silence tolerance (e.g., 500 ms)
|
||||
hangtime_samples_limit = m.hangtime_ms * 250;
|
||||
|
||||
// Addresses "WHAT-IF" integer underflow
|
||||
if (hangtime_samples_limit == 0) {
|
||||
hangtime_samples_limit = 1;
|
||||
}
|
||||
|
||||
if (!configured) configure();
|
||||
|
||||
// DO NOT close the stream here if (!m.start).
|
||||
// M4 must keep producing buffers until M0 explicitly sends CaptureConfig(nullptr)
|
||||
// to prevent the M0 CaptureThread from deadlocking in buffers.get().
|
||||
hunting = m.start;
|
||||
break;
|
||||
}
|
||||
|
||||
case Message::ID::CaptureConfig: {
|
||||
const auto& m = *reinterpret_cast<const CaptureConfigMessage*>(message);
|
||||
|
||||
if (m.config) {
|
||||
// Synchronous allocation guarantees validity of m.config pointer
|
||||
// (must not be deferred into execute(), see prior HardFault).
|
||||
stream = std::make_unique<StreamInput>(m.config);
|
||||
flush_start_idx = iq_ring_idx; // Snapshot: current write position
|
||||
flush_pending = true; // execute() will process ring buffer pre-roll
|
||||
stream_active = true;
|
||||
hunt_state = HuntState::RECORDING;
|
||||
} else {
|
||||
// This is the ONLY safe place to initiate stream teardown —
|
||||
// it arrives after M0's CaptureThread has already fully drained
|
||||
// and exited, so it's safe for execute() to reset() the stream.
|
||||
stream_active = false;
|
||||
stream_close_requested = true;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
EventDispatcher event_dispatcher{std::make_unique<SignalHunterProcessor>()};
|
||||
event_dispatcher.run();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright (C) 2026 Matej Sochan
|
||||
*
|
||||
* This file is part of PortaPack.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; see the file COPYING. If not, write to
|
||||
* the Free Software Foundation, Inc., 51 Franklin Street,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#ifndef __PROC_SIGNAL_HUNTER_H__
|
||||
#define __PROC_SIGNAL_HUNTER_H__
|
||||
|
||||
#include <atomic>
|
||||
|
||||
#include "baseband_processor.hpp"
|
||||
#include "baseband_thread.hpp"
|
||||
#include "rssi_thread.hpp"
|
||||
#include "message.hpp"
|
||||
#include "dsp_decimate.hpp"
|
||||
#include "stream_input.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <array>
|
||||
#include <memory>
|
||||
|
||||
class SignalHunterProcessor : public BasebandProcessor {
|
||||
public:
|
||||
void execute(const buffer_c8_t& buffer) override;
|
||||
void on_message(const Message* const message) override;
|
||||
|
||||
private:
|
||||
static constexpr size_t baseband_fs = 2000000;
|
||||
|
||||
// Hardcoded Decimate-by-8 FIR filter.
|
||||
// 2,000,000 Hz / 8 = 250,000 Hz target sample rate.
|
||||
// Used to avoid SD Card bottlenecks
|
||||
dsp::decimate::FIRC8xR16x24FS4Decim8 decim_0{};
|
||||
|
||||
std::array<complex16_t, 512> dst_buffer_data{};
|
||||
const buffer_c16_t dst_buffer{dst_buffer_data.data(), dst_buffer_data.size()};
|
||||
|
||||
// --- Energy sliding window for threshold detection ---
|
||||
static constexpr size_t WINDOW_SIZE = 64;
|
||||
uint32_t window_buf[WINDOW_SIZE]{};
|
||||
size_t window_idx{0};
|
||||
uint32_t window_sum{0};
|
||||
|
||||
std::atomic<bool> flush_pending{false};
|
||||
size_t flush_start_idx{0};
|
||||
|
||||
// Pre-trigger IQ ring buffer
|
||||
// IQ_RING_SAMPLES = 2048 provides ~8ms pre-roll buffer (2048 / 250kHz).
|
||||
// This captures OOK preambles efficiently while maintaining strict 8KB flush size to prevent
|
||||
// Out-Of-Memory (OOM) / HardFault crashes on M0 CaptureThread, which has only 16KB buffer constraint.
|
||||
static constexpr size_t IQ_RING_SAMPLES = 2048;
|
||||
std::array<complex16_t, IQ_RING_SAMPLES> iq_ring{};
|
||||
size_t iq_ring_idx{0};
|
||||
|
||||
uint32_t energy_threshold{5000};
|
||||
|
||||
std::atomic<bool> hunting{false};
|
||||
std::atomic<bool> stream_active{false};
|
||||
std::atomic<bool> stream_close_requested{false};
|
||||
|
||||
bool configured{false};
|
||||
|
||||
// State machine tracking signal detection lifecycle
|
||||
enum class HuntState {
|
||||
// Awaiting energy threshold crossing
|
||||
IDLE,
|
||||
// Trigger sent; waiting for CaptureConfigMessage from M0 to create stream
|
||||
AWAITING_STREAM,
|
||||
// Stream active; writing live decimated samples
|
||||
RECORDING,
|
||||
// Energy dropped below threshold; checking if signal returns
|
||||
HANGTIME,
|
||||
// Stop sent; waiting for CaptureConfigMessage(nullptr) from M0 to destroy stream
|
||||
AWAITING_CLOSE
|
||||
};
|
||||
|
||||
std::atomic<HuntState> hunt_state{HuntState::IDLE};
|
||||
|
||||
uint32_t hangtime_counter{0};
|
||||
// Dynamic hangtime configuration: holds sample count in post-decimation domain (250 kHz rate).
|
||||
// Converted from milliseconds: hangtime_ms * 250 samples/ms.
|
||||
// Set dynamically via HunterConfigMessage to allow configurable silence tolerance.
|
||||
uint32_t hangtime_samples_limit{0};
|
||||
|
||||
std::unique_ptr<StreamInput> stream{};
|
||||
|
||||
void configure();
|
||||
void reset_hunt_state();
|
||||
|
||||
BasebandThread baseband_thread{baseband_fs, this, baseband::Direction::Receive};
|
||||
RSSIThread rssi_thread{};
|
||||
};
|
||||
|
||||
#endif /*__PROC_SIGNAL_HUNTER_H__*/
|
||||
Reference in New Issue
Block a user