mirror of
https://github.com/portapack-mayhem/mayhem-firmware.git
synced 2025-02-28 23:27:20 +00:00
Move FIFO and write size to CaptureConfig structure.
Now configured from baseband, where write size and appropriate FIFO size is known.
This commit is contained in:
parent
01fc6b9bc9
commit
91ee2dbb67
@ -322,7 +322,7 @@ void AnalogAudioView::record_start() {
|
||||
return;
|
||||
}
|
||||
|
||||
capture_thread = std::make_unique<CaptureThread>(filename);
|
||||
capture_thread = std::make_unique<CaptureThread>(filename, 12, 2);
|
||||
button_record.set_bitmap(&bitmap_stop);
|
||||
}
|
||||
|
||||
|
@ -112,7 +112,7 @@ void CaptureAppView::on_record() {
|
||||
return;
|
||||
}
|
||||
|
||||
capture_thread = std::make_unique<CaptureThread>(filename);
|
||||
capture_thread = std::make_unique<CaptureThread>(filename, 14, 1);
|
||||
button_record.set_bitmap(&bitmap_stop);
|
||||
}
|
||||
}
|
||||
|
@ -21,4 +21,5 @@
|
||||
|
||||
#include "capture_thread.hpp"
|
||||
|
||||
FIFO<uint8_t>* StreamOutput::fifo = nullptr;
|
||||
Thread* CaptureThread::thread = nullptr;
|
||||
|
@ -37,13 +37,19 @@ using namespace hackrf::one;
|
||||
|
||||
class StreamOutput {
|
||||
public:
|
||||
StreamOutput() {
|
||||
StreamOutput(
|
||||
const size_t write_size_log2,
|
||||
const size_t buffer_count_log2
|
||||
) : config { write_size_log2, buffer_count_log2 }
|
||||
{
|
||||
shared_memory.baseband_queue.push_and_wait(
|
||||
CaptureConfigMessage { &config }
|
||||
);
|
||||
fifo = config.fifo;
|
||||
}
|
||||
|
||||
~StreamOutput() {
|
||||
fifo = nullptr;
|
||||
shared_memory.baseband_queue.push_and_wait(
|
||||
CaptureConfigMessage { nullptr }
|
||||
);
|
||||
@ -57,6 +63,8 @@ public:
|
||||
return config.fifo->out(reinterpret_cast<uint8_t*>(data), length);
|
||||
}
|
||||
|
||||
static FIFO<uint8_t>* fifo;
|
||||
|
||||
private:
|
||||
CaptureConfig config;
|
||||
};
|
||||
@ -64,9 +72,13 @@ private:
|
||||
class CaptureThread {
|
||||
public:
|
||||
CaptureThread(
|
||||
std::string file_path
|
||||
) : file_path { std::move(file_path) },
|
||||
write_buffer { std::make_unique<std::array<uint8_t, write_size>>() }
|
||||
std::string file_path,
|
||||
size_t write_size_log2,
|
||||
size_t buffer_count_log2
|
||||
) : write_size_log2 { write_size_log2 },
|
||||
write_size { 1U << write_size_log2 },
|
||||
buffer_count_log2 { buffer_count_log2 },
|
||||
file_path { std::move(file_path) }
|
||||
{
|
||||
// Need significant stack for FATFS
|
||||
thread = chThdCreateFromHeap(NULL, 1024, NORMALPRIO + 10, CaptureThread::static_fn, this);
|
||||
@ -90,16 +102,17 @@ public:
|
||||
static void check_fifo_isr() {
|
||||
// TODO: Prevent over-signalling by transmitting a set of
|
||||
// flags from the baseband core.
|
||||
if( thread ) {
|
||||
const auto fifo = StreamOutput::fifo;
|
||||
if( fifo ) {
|
||||
chEvtSignalI(thread, EVT_MASK_CAPTURE_THREAD);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr size_t write_size = 16384;
|
||||
|
||||
const size_t write_size_log2;
|
||||
const size_t write_size;
|
||||
const size_t buffer_count_log2;
|
||||
const std::string file_path;
|
||||
std::unique_ptr<std::array<uint8_t, write_size>> write_buffer;
|
||||
File file;
|
||||
static Thread* thread;
|
||||
|
||||
@ -113,12 +126,17 @@ private:
|
||||
return false;
|
||||
}
|
||||
|
||||
StreamOutput stream;
|
||||
const auto write_buffer = std::make_unique<uint8_t[]>(write_size);
|
||||
if( !write_buffer ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
StreamOutput stream { write_size_log2, buffer_count_log2 };
|
||||
|
||||
while( !chThdShouldTerminate() ) {
|
||||
chEvtWaitAny(EVT_MASK_CAPTURE_THREAD);
|
||||
|
||||
while( stream.available() >= write_buffer->size() ) {
|
||||
while( stream.available() >= write_size ) {
|
||||
if( !transfer(stream, write_buffer.get()) ) {
|
||||
return false;
|
||||
}
|
||||
@ -128,14 +146,14 @@ private:
|
||||
return true;
|
||||
}
|
||||
|
||||
bool transfer(StreamOutput& stream, std::array<uint8_t, write_size>* const write_buffer) {
|
||||
bool transfer(StreamOutput& stream, uint8_t* const write_buffer) {
|
||||
bool success = false;
|
||||
|
||||
led_usb.on();
|
||||
|
||||
const auto bytes_to_write = stream.read(write_buffer->data(), write_buffer->size());
|
||||
if( bytes_to_write == write_buffer->size() ) {
|
||||
if( file.write(write_buffer->data(), write_buffer->size()) ) {
|
||||
const auto bytes_to_write = stream.read(write_buffer, write_size);
|
||||
if( bytes_to_write == write_size ) {
|
||||
if( file.write(write_buffer, write_size) ) {
|
||||
success = true;
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,6 @@
|
||||
#include "event_m0.hpp"
|
||||
|
||||
#include "portapack.hpp"
|
||||
#include "portapack_shared_memory.hpp"
|
||||
|
||||
#include "sd_card.hpp"
|
||||
|
||||
@ -47,7 +46,7 @@ CH_IRQ_HANDLER(M4Core_IRQHandler) {
|
||||
|
||||
chSysLockFromIsr();
|
||||
CaptureThread::check_fifo_isr();
|
||||
EventDispatcher::events_flag_isr(EVT_MASK_APPLICATION);
|
||||
EventDispatcher::check_fifo_isr();
|
||||
chSysUnlockFromIsr();
|
||||
|
||||
creg::m4txevent::clear();
|
||||
|
@ -28,6 +28,7 @@
|
||||
#include "ui_painter.hpp"
|
||||
|
||||
#include "portapack.hpp"
|
||||
#include "portapack_shared_memory.hpp"
|
||||
|
||||
#include "message.hpp"
|
||||
|
||||
@ -58,6 +59,12 @@ public:
|
||||
|
||||
void set_display_sleep(const bool sleep);
|
||||
|
||||
static inline void check_fifo_isr() {
|
||||
if( !shared_memory.application_queue.is_empty() ) {
|
||||
events_flag_isr(EVT_MASK_APPLICATION);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void events_flag(const eventmask_t events) {
|
||||
if( thread_event_loop ) {
|
||||
chEvtSignal(thread_event_loop, events);
|
||||
|
@ -100,7 +100,7 @@ void NarrowbandAMAudio::configure(const AMConfigureMessage& message) {
|
||||
|
||||
void NarrowbandAMAudio::capture_config(const CaptureConfigMessage& message) {
|
||||
if( message.config ) {
|
||||
audio_output.set_stream(std::make_unique<StreamInput>(14, *message.config));
|
||||
audio_output.set_stream(std::make_unique<StreamInput>(*message.config));
|
||||
} else {
|
||||
audio_output.set_stream(nullptr);
|
||||
}
|
||||
|
@ -85,7 +85,7 @@ void CaptureProcessor::on_message(const Message* const message) {
|
||||
|
||||
void CaptureProcessor::capture_config(const CaptureConfigMessage& message) {
|
||||
if( message.config ) {
|
||||
stream = std::make_unique<StreamInput>(15, *message.config);
|
||||
stream = std::make_unique<StreamInput>(*message.config);
|
||||
} else {
|
||||
stream.reset();
|
||||
}
|
||||
|
@ -88,7 +88,7 @@ void NarrowbandFMAudio::configure(const NBFMConfigureMessage& message) {
|
||||
|
||||
void NarrowbandFMAudio::capture_config(const CaptureConfigMessage& message) {
|
||||
if( message.config ) {
|
||||
audio_output.set_stream(std::make_unique<StreamInput>(14, *message.config));
|
||||
audio_output.set_stream(std::make_unique<StreamInput>(*message.config));
|
||||
} else {
|
||||
audio_output.set_stream(nullptr);
|
||||
}
|
||||
|
@ -117,7 +117,7 @@ void WidebandFMAudio::configure(const WFMConfigureMessage& message) {
|
||||
|
||||
void WidebandFMAudio::capture_config(const CaptureConfigMessage& message) {
|
||||
if( message.config ) {
|
||||
audio_output.set_stream(std::make_unique<StreamInput>(15, *message.config));
|
||||
audio_output.set_stream(std::make_unique<StreamInput>(*message.config));
|
||||
} else {
|
||||
audio_output.set_stream(nullptr);
|
||||
}
|
||||
|
@ -22,18 +22,21 @@
|
||||
#ifndef __STREAM_INPUT_H__
|
||||
#define __STREAM_INPUT_H__
|
||||
|
||||
#include "portapack_shared_memory.hpp"
|
||||
|
||||
#include "message.hpp"
|
||||
#include "fifo.hpp"
|
||||
|
||||
#include "lpc43xx_cpp.hpp"
|
||||
using namespace lpc43xx;
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
|
||||
class StreamInput {
|
||||
public:
|
||||
StreamInput(const size_t K, CaptureConfig& config) :
|
||||
K { K },
|
||||
StreamInput(CaptureConfig& config) :
|
||||
K { config.write_size_log2 + config.buffer_count_log2 },
|
||||
event_bytes_mask { (1UL << config.write_size_log2) - 1 },
|
||||
data { std::make_unique<uint8_t[]>(1UL << K) },
|
||||
fifo { data.get(), K }
|
||||
{
|
||||
@ -58,7 +61,7 @@ public:
|
||||
|
||||
private:
|
||||
const size_t K;
|
||||
const uint64_t event_bytes_mask = (1ULL << (K - 2)) - 1;
|
||||
const uint64_t event_bytes_mask;
|
||||
uint64_t bytes_written = 0;
|
||||
std::unique_ptr<uint8_t[]> data;
|
||||
FIFO<uint8_t> fifo;
|
||||
|
@ -411,7 +411,18 @@ public:
|
||||
};
|
||||
|
||||
struct CaptureConfig {
|
||||
FIFO<uint8_t>* fifo { nullptr };
|
||||
const size_t write_size_log2;
|
||||
const size_t buffer_count_log2;
|
||||
FIFO<uint8_t>* fifo;
|
||||
|
||||
constexpr CaptureConfig(
|
||||
const size_t write_size_log2,
|
||||
const size_t buffer_count_log2
|
||||
) : write_size_log2 { write_size_log2 },
|
||||
buffer_count_log2 { buffer_count_log2 },
|
||||
fifo { nullptr }
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
class CaptureConfigMessage : public Message {
|
||||
|
@ -70,6 +70,10 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
bool is_empty() const {
|
||||
return fifo.is_empty();
|
||||
}
|
||||
|
||||
private:
|
||||
FIFO<uint8_t> fifo;
|
||||
Mutex mutex_write;
|
||||
@ -92,10 +96,6 @@ private:
|
||||
return fifo.len();
|
||||
}
|
||||
|
||||
bool is_empty() const {
|
||||
return fifo.is_empty();
|
||||
}
|
||||
|
||||
bool push(const void* const buf, const size_t len) {
|
||||
chMtxLock(&mutex_write);
|
||||
const auto result = fifo.in_r(buf, len);
|
||||
|
Loading…
x
Reference in New Issue
Block a user