Move lots of capture_thread code to .cpp.

This commit is contained in:
Jared Boone 2016-05-02 12:50:49 -07:00
parent 697df07501
commit eeb4645242
2 changed files with 108 additions and 103 deletions

View File

@ -21,5 +21,109 @@
#include "capture_thread.hpp"
#include "portapack_shared_memory.hpp"
#include "hackrf_gpio.hpp"
using namespace hackrf::one;
// StreamOutput ///////////////////////////////////////////////////////////
class StreamOutput {
public:
StreamOutput(CaptureConfig* const config);
~StreamOutput();
size_t available() {
return fifo->len();
}
size_t read(void* const data, const size_t length) {
return fifo->out(reinterpret_cast<uint8_t*>(data), length);
}
static FIFO<uint8_t>* fifo;
private:
CaptureConfig* const config;
};
FIFO<uint8_t>* StreamOutput::fifo = nullptr;
StreamOutput::StreamOutput(
CaptureConfig* const config
) : config { config }
{
shared_memory.baseband_queue.push_and_wait(
CaptureConfigMessage { config }
);
fifo = config->fifo;
}
StreamOutput::~StreamOutput() {
fifo = nullptr;
shared_memory.baseband_queue.push_and_wait(
CaptureConfigMessage { nullptr }
);
}
// CaptureThread //////////////////////////////////////////////////////////
Thread* CaptureThread::thread = nullptr;
CaptureThread::CaptureThread(
std::unique_ptr<Writer> writer,
size_t write_size_log2,
size_t buffer_count_log2
) : config { write_size_log2, buffer_count_log2 },
writer { std::move(writer) }
{
// Need significant stack for FATFS
thread = chThdCreateFromHeap(NULL, 1024, NORMALPRIO + 10, CaptureThread::static_fn, this);
}
CaptureThread::~CaptureThread() {
if( thread ) {
chThdTerminate(thread);
chEvtSignal(thread, EVT_MASK_CAPTURE_THREAD);
const auto success = chThdWait(thread);
thread = nullptr;
if( !success ) {
led_tx.on();
}
}
}
void CaptureThread::check_fifo_isr() {
// TODO: Prevent over-signalling by transmitting a set of
// flags from the baseband core.
const auto fifo = StreamOutput::fifo;
if( fifo ) {
chEvtSignalI(thread, EVT_MASK_CAPTURE_THREAD);
}
}
msg_t CaptureThread::run() {
const size_t write_size = 1U << config.write_size_log2;
const auto write_buffer = std::make_unique<uint8_t[]>(write_size);
if( !write_buffer ) {
return false;
}
StreamOutput stream { &config };
while( !chThdShouldTerminate() ) {
if( stream.available() >= write_size ) {
if( stream.read(write_buffer.get(), write_size) != write_size ) {
return false;
}
if( !writer->write(write_buffer.get(), write_size) ) {
return false;
}
} else {
chEvtWaitAny(EVT_MASK_CAPTURE_THREAD);
}
}
return true;
}

View File

@ -26,11 +26,6 @@
#include "event_m0.hpp"
#include "portapack_shared_memory.hpp"
#include "hackrf_gpio.hpp"
using namespace hackrf::one;
#include <cstdint>
#include <cstddef>
#include <utility>
@ -41,77 +36,20 @@ public:
virtual ~Writer() = default;
};
class StreamOutput {
public:
StreamOutput(
CaptureConfig* const config
) : config { config }
{
shared_memory.baseband_queue.push_and_wait(
CaptureConfigMessage { config }
);
fifo = config->fifo;
}
~StreamOutput() {
fifo = nullptr;
shared_memory.baseband_queue.push_and_wait(
CaptureConfigMessage { nullptr }
);
}
size_t available() {
return fifo->len();
}
size_t read(void* const data, const size_t length) {
return fifo->out(reinterpret_cast<uint8_t*>(data), length);
}
static FIFO<uint8_t>* fifo;
private:
CaptureConfig* const config;
};
class CaptureThread {
public:
CaptureThread(
std::unique_ptr<Writer> writer,
size_t write_size_log2,
size_t buffer_count_log2
) : config { write_size_log2, buffer_count_log2 },
writer { std::move(writer) }
{
// Need significant stack for FATFS
thread = chThdCreateFromHeap(NULL, 1024, NORMALPRIO + 10, CaptureThread::static_fn, this);
}
~CaptureThread() {
if( thread ) {
chThdTerminate(thread);
chEvtSignal(thread, EVT_MASK_CAPTURE_THREAD);
const auto success = chThdWait(thread);
thread = nullptr;
if( !success ) {
led_tx.on();
}
}
}
);
~CaptureThread();
const CaptureConfig& state() const {
return config;
}
static void check_fifo_isr() {
// TODO: Prevent over-signalling by transmitting a set of
// flags from the baseband core.
const auto fifo = StreamOutput::fifo;
if( fifo ) {
chEvtSignalI(thread, EVT_MASK_CAPTURE_THREAD);
}
}
static void check_fifo_isr();
private:
CaptureConfig config;
@ -123,44 +61,7 @@ private:
return obj->run();
}
msg_t run() {
const size_t write_size = 1U << config.write_size_log2;
const auto write_buffer = std::make_unique<uint8_t[]>(write_size);
if( !write_buffer ) {
return false;
}
StreamOutput stream { &config };
while( !chThdShouldTerminate() ) {
if( stream.available() >= write_size ) {
if( !transfer(stream, write_buffer.get(), write_size) ) {
return false;
}
} else {
chEvtWaitAny(EVT_MASK_CAPTURE_THREAD);
}
}
return true;
}
bool transfer(StreamOutput& stream, uint8_t* const write_buffer, const size_t write_size) {
bool success = false;
led_usb.on();
const auto bytes_to_write = stream.read(write_buffer, write_size);
if( bytes_to_write == write_size ) {
if( writer->write(write_buffer, write_size) ) {
success = true;
}
}
led_usb.off();
return success;
}
msg_t run();
};
#endif/*__CAPTURE_THREAD_H__*/