satdump/src-testing/main.cpp

129 lines
3.3 KiB
C++
Raw Normal View History

/**********************************************************************
* This file is used for testing random stuff without running the
* whole of SatDump, which comes in handy for debugging individual
* elements before putting them all together in modules...
*
* If you are an user, ignore this file which will not be built by
* default, and if you're a developper in need of doing stuff here...
* Go ahead!
*
* Don't judge the code you might see in there! :)
**********************************************************************/
#include "logger.h"
2024-03-10 21:16:46 +01:00
2024-08-08 22:52:18 +02:00
#include "common/dsp/complex.h"
#include "common/ndsp/block.h"
#include <unistd.h>
2024-06-23 20:30:39 +02:00
2024-08-08 22:52:18 +02:00
#include <functional>
2024-05-24 20:59:18 +02:00
2024-08-08 22:52:18 +02:00
struct FloatBuffer
{
uint64_t max;
uint64_t cnt;
float *dat;
};
2024-07-06 13:11:34 +02:00
2024-08-08 22:52:18 +02:00
std::shared_ptr<NaFiFo> create_nafifo_floatbuffer(int size)
2024-07-06 13:11:34 +02:00
{
2024-08-08 22:52:18 +02:00
auto alloc = [size]() -> void *
{
FloatBuffer *ptr = new FloatBuffer;
ptr->max = size;
ptr->cnt = 0;
ptr->dat = new float[size];
return ptr;
};
auto dealloc = [](void *p) -> void
{
FloatBuffer *ptr = (FloatBuffer *)p;
delete[] ptr->dat;
delete ptr;
};
2024-07-06 13:11:34 +02:00
2024-08-08 22:52:18 +02:00
auto ptr = std::make_shared<NaFiFo>();
2024-07-06 13:11:34 +02:00
2024-08-08 22:52:18 +02:00
ptr->init(100, sizeof(float), alloc, dealloc);
2024-07-06 13:11:34 +02:00
2024-08-08 22:52:18 +02:00
return ptr;
}
2024-07-06 13:11:34 +02:00
2024-08-08 22:52:18 +02:00
class TestBlock : public ndsp::Block
{
private:
void work()
2024-07-06 13:11:34 +02:00
{
2024-08-08 22:52:18 +02:00
if (!inputs[0]->read())
{
FloatBuffer *rbuf = (FloatBuffer *)inputs[0]->read_buf();
FloatBuffer *wbuf = (FloatBuffer *)outputs[0]->write_buf();
for (int i = 0; i < rbuf->cnt; i++)
wbuf->dat[i] = rbuf->dat[i] * 100;
wbuf->cnt = rbuf->cnt;
outputs[0]->write();
inputs[0]->flush();
}
2024-07-06 13:11:34 +02:00
}
2024-08-08 22:52:18 +02:00
public:
TestBlock()
: ndsp::Block("test_block", {{sizeof(float)}}, {{sizeof(float)}})
{
}
2024-07-15 12:44:35 +02:00
2024-08-08 22:52:18 +02:00
void start()
{
outputs[0] = create_nafifo_floatbuffer(((FloatBuffer *)inputs[0]->read_buf())->max * 2);
ndsp::Block::start();
}
};
2024-07-06 13:11:34 +02:00
2024-02-23 01:22:22 +01:00
int main(int argc, char *argv[])
{
initLogger();
2024-08-08 22:52:18 +02:00
std::shared_ptr<NaFiFo> fifo1 = create_nafifo_floatbuffer(8192);
2024-06-23 20:30:39 +02:00
2024-08-08 22:52:18 +02:00
TestBlock testBlock1;
2024-06-23 20:30:39 +02:00
2024-08-08 22:52:18 +02:00
testBlock1.connect_input(0, fifo1);
testBlock1.start();
2024-07-15 12:44:35 +02:00
2024-08-08 22:52:18 +02:00
bool should_run = true;
2024-06-23 20:30:39 +02:00
2024-08-08 22:52:18 +02:00
std::thread thread_tx([fifo1, &should_run]()
{
while(should_run) {
((FloatBuffer*) fifo1->read_buf())->dat[0] = 1;
((FloatBuffer*) fifo1->read_buf())->dat[1] = 2;
((FloatBuffer*) fifo1->read_buf())->cnt = 2;
fifo1->write();
} });
2024-07-06 13:11:34 +02:00
2024-08-08 22:52:18 +02:00
std::thread thread_rx([&testBlock1]()
{
while (!testBlock1.outputs[0]->read())
{
FloatBuffer *wbuf = (FloatBuffer *)testBlock1.outputs[0]->read_buf();
for (int i = 0; i < wbuf->cnt; i++)
printf("%f, ", wbuf->dat[i]);
printf("\n");
testBlock1.outputs[0]->flush();
} });
2024-06-23 20:30:39 +02:00
2024-08-08 22:52:18 +02:00
sleep(2);
2024-06-23 20:30:39 +02:00
2024-08-08 22:52:18 +02:00
should_run = false;
fifo1->stop();
if (thread_tx.joinable())
thread_tx.join();
2024-06-23 20:30:39 +02:00
2024-08-08 22:52:18 +02:00
testBlock1.stop();
2024-05-12 22:09:30 +02:00
2024-08-08 22:52:18 +02:00
if (thread_rx.joinable())
thread_rx.join();
}