2015-12-10 16:03:22 -08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
*
|
|
|
|
* 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 "rssi_thread.hpp"
|
|
|
|
|
|
|
|
#include "rssi.hpp"
|
|
|
|
#include "rssi_dma.hpp"
|
|
|
|
#include "rssi_stats_collector.hpp"
|
|
|
|
|
|
|
|
#include "message.hpp"
|
|
|
|
#include "portapack_shared_memory.hpp"
|
|
|
|
|
2015-12-10 21:01:44 -08:00
|
|
|
WORKING_AREA(rssi_thread_wa, 128);
|
|
|
|
|
2016-06-24 15:34:49 -07:00
|
|
|
Thread* RSSIThread::thread = nullptr;
|
|
|
|
|
2023-07-22 23:54:17 -07:00
|
|
|
RSSIThread::RSSIThread(bool auto_start, tprio_t priority)
|
|
|
|
: priority_{priority} {
|
|
|
|
if (auto_start) start();
|
2015-12-10 16:03:22 -08:00
|
|
|
}
|
|
|
|
|
2016-06-24 15:34:49 -07:00
|
|
|
RSSIThread::~RSSIThread() {
|
2023-07-22 23:54:17 -07:00
|
|
|
if (thread) {
|
|
|
|
chThdTerminate(thread);
|
|
|
|
chThdWait(thread);
|
|
|
|
thread = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RSSIThread::start() {
|
|
|
|
if (!thread) {
|
|
|
|
thread = chThdCreateStatic(
|
|
|
|
rssi_thread_wa, sizeof(rssi_thread_wa),
|
|
|
|
priority_, ThreadBase::fn, this);
|
|
|
|
}
|
2016-06-24 15:34:49 -07:00
|
|
|
}
|
|
|
|
|
2015-12-10 16:03:22 -08:00
|
|
|
void RSSIThread::run() {
|
2023-05-19 08:16:05 +12:00
|
|
|
rf::rssi::init();
|
|
|
|
rf::rssi::dma::allocate(4, 400);
|
2015-12-10 16:32:00 -08:00
|
|
|
|
2023-05-19 08:16:05 +12:00
|
|
|
RSSIStatisticsCollector stats;
|
2015-12-10 16:03:22 -08:00
|
|
|
|
2023-05-19 08:16:05 +12:00
|
|
|
rf::rssi::start();
|
2016-06-24 15:34:49 -07:00
|
|
|
|
2023-05-19 08:16:05 +12:00
|
|
|
while (!chThdShouldTerminate()) {
|
|
|
|
// TODO: Place correct sampling rate into buffer returned here:
|
|
|
|
const auto buffer_tmp = rf::rssi::dma::wait_for_buffer();
|
|
|
|
const rf::rssi::buffer_t buffer{
|
|
|
|
buffer_tmp.p, buffer_tmp.count, sampling_rate};
|
2015-12-10 16:03:22 -08:00
|
|
|
|
2023-05-19 08:16:05 +12:00
|
|
|
stats.process(
|
|
|
|
buffer,
|
|
|
|
[](const RSSIStatistics& statistics) {
|
|
|
|
const RSSIStatisticsMessage message{statistics};
|
|
|
|
shared_memory.application_queue.push(message);
|
|
|
|
});
|
|
|
|
}
|
2015-12-10 16:32:00 -08:00
|
|
|
|
2023-05-19 08:16:05 +12:00
|
|
|
rf::rssi::stop();
|
|
|
|
rf::rssi::dma::free();
|
2015-12-10 16:03:22 -08:00
|
|
|
}
|