mirror of
https://github.com/eried/portapack-mayhem.git
synced 2025-08-03 20:24:13 -04:00
Extract BufferExchange, simplify threading.
This commit is contained in:
parent
84334ef818
commit
5dfb53263a
6 changed files with 161 additions and 81 deletions
|
@ -174,6 +174,7 @@ set(CPPSRC
|
|||
file.cpp
|
||||
log_file.cpp
|
||||
${COMMON}/png_writer.cpp
|
||||
${COMMON}/buffer_exchange.cpp
|
||||
capture_thread.cpp
|
||||
io_file.cpp
|
||||
io_wave.cpp
|
||||
|
|
|
@ -22,67 +22,20 @@
|
|||
#include "capture_thread.hpp"
|
||||
|
||||
#include "baseband_api.hpp"
|
||||
#include "buffer_exchange.hpp"
|
||||
|
||||
// BufferExchange ///////////////////////////////////////////////////////////
|
||||
|
||||
class BufferExchange {
|
||||
public:
|
||||
BufferExchange(CaptureConfig* const config);
|
||||
~BufferExchange();
|
||||
|
||||
#if defined(LPC43XX_M0)
|
||||
StreamBuffer* get() {
|
||||
StreamBuffer* p { nullptr };
|
||||
fifo_buffers_for_application->out(p);
|
||||
return p;
|
||||
struct BasebandCapture {
|
||||
BasebandCapture(CaptureConfig* const config) {
|
||||
baseband::capture_start(config);
|
||||
}
|
||||
|
||||
bool put(StreamBuffer* const p) {
|
||||
return fifo_buffers_for_baseband->in(p);
|
||||
~BasebandCapture() {
|
||||
baseband::capture_stop();
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(LPC43XX_M4)
|
||||
StreamBuffer* get() {
|
||||
StreamBuffer* p { nullptr };
|
||||
fifo_buffers_for_baseband->out(p);
|
||||
return p;
|
||||
}
|
||||
|
||||
bool put(StreamBuffer* const p) {
|
||||
return fifo_buffers_for_application->in(p);
|
||||
}
|
||||
#endif
|
||||
|
||||
static FIFO<StreamBuffer*>* fifo_buffers_for_baseband;
|
||||
static FIFO<StreamBuffer*>* fifo_buffers_for_application;
|
||||
|
||||
private:
|
||||
CaptureConfig* const config;
|
||||
};
|
||||
|
||||
FIFO<StreamBuffer*>* BufferExchange::fifo_buffers_for_baseband = nullptr;
|
||||
FIFO<StreamBuffer*>* BufferExchange::fifo_buffers_for_application = nullptr;
|
||||
|
||||
BufferExchange::BufferExchange(
|
||||
CaptureConfig* const config
|
||||
) : config { config }
|
||||
{
|
||||
baseband::capture_start(config);
|
||||
fifo_buffers_for_baseband = config->fifo_buffers_empty;
|
||||
fifo_buffers_for_application = config->fifo_buffers_full;
|
||||
}
|
||||
|
||||
BufferExchange::~BufferExchange() {
|
||||
fifo_buffers_for_baseband = nullptr;
|
||||
fifo_buffers_for_application = nullptr;
|
||||
baseband::capture_stop();
|
||||
}
|
||||
|
||||
// CaptureThread //////////////////////////////////////////////////////////
|
||||
|
||||
Thread* CaptureThread::thread = nullptr;
|
||||
|
||||
CaptureThread::CaptureThread(
|
||||
std::unique_ptr<stream::Writer> writer,
|
||||
size_t write_size,
|
||||
|
@ -101,23 +54,11 @@ CaptureThread::CaptureThread(
|
|||
CaptureThread::~CaptureThread() {
|
||||
if( thread ) {
|
||||
chThdTerminate(thread);
|
||||
chEvtSignal(thread, event_mask_loop_wake);
|
||||
chThdWait(thread);
|
||||
thread = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void CaptureThread::check_fifo_isr() {
|
||||
// TODO: Prevent over-signalling by transmitting a set of
|
||||
// flags from the baseband core.
|
||||
const auto fifo = BufferExchange::fifo_buffers_for_application;
|
||||
if( fifo ) {
|
||||
if( !fifo->is_empty() ) {
|
||||
chEvtSignalI(thread, event_mask_loop_wake);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
msg_t CaptureThread::static_fn(void* arg) {
|
||||
auto obj = static_cast<CaptureThread*>(arg);
|
||||
const auto error = obj->run();
|
||||
|
@ -132,20 +73,17 @@ msg_t CaptureThread::static_fn(void* arg) {
|
|||
}
|
||||
|
||||
Optional<File::Error> CaptureThread::run() {
|
||||
BasebandCapture capture { &config };
|
||||
BufferExchange buffers { &config };
|
||||
|
||||
while( !chThdShouldTerminate() ) {
|
||||
auto buffer = buffers.get();
|
||||
if( buffer ) {
|
||||
auto write_result = writer->write(buffer->data(), buffer->size());
|
||||
if( write_result.is_error() ) {
|
||||
return write_result.error();
|
||||
}
|
||||
buffer->empty();
|
||||
buffers.put(buffer);
|
||||
} else {
|
||||
chEvtWaitAny(event_mask_loop_wake);
|
||||
auto write_result = writer->write(buffer->data(), buffer->size());
|
||||
if( write_result.is_error() ) {
|
||||
return write_result.error();
|
||||
}
|
||||
buffer->empty();
|
||||
buffers.put(buffer);
|
||||
}
|
||||
|
||||
return { };
|
||||
|
|
|
@ -48,16 +48,12 @@ public:
|
|||
return config;
|
||||
}
|
||||
|
||||
static void check_fifo_isr();
|
||||
|
||||
private:
|
||||
static constexpr auto event_mask_loop_wake = EVENT_MASK(0);
|
||||
|
||||
CaptureConfig config;
|
||||
std::unique_ptr<stream::Writer> writer;
|
||||
std::function<void()> success_callback;
|
||||
std::function<void(File::Error)> error_callback;
|
||||
static Thread* thread;
|
||||
Thread* thread;
|
||||
|
||||
static msg_t static_fn(void* arg);
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@
|
|||
|
||||
#include "irq_controls.hpp"
|
||||
|
||||
#include "capture_thread.hpp"
|
||||
#include "buffer_exchange.hpp"
|
||||
|
||||
#include "ch.h"
|
||||
|
||||
|
@ -48,7 +48,7 @@ CH_IRQ_HANDLER(M4Core_IRQHandler) {
|
|||
CH_IRQ_PROLOGUE();
|
||||
|
||||
chSysLockFromIsr();
|
||||
CaptureThread::check_fifo_isr();
|
||||
BufferExchange::handle_isr();
|
||||
EventDispatcher::check_fifo_isr();
|
||||
chSysUnlockFromIsr();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue