Skip to content

Commit

Permalink
example fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
David Michaeli committed Mar 16, 2024
1 parent 1b34c56 commit ffbd7af
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion examples/cpp_api/async_sample_process/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ add_executable(test_app main.cpp)
target_include_directories(test_app PRIVATE ${CARIBOULITE_INCLUDE_DIRS})

# Link against the cariboulite library
target_link_libraries(test_app PRIVATE ${CARIBOULITE_LIBRARIES} -lcariboulite)
target_link_libraries(test_app PRIVATE ${CARIBOULITE_LIBRARIES} -lcariboulite pthread)
2 changes: 1 addition & 1 deletion examples/cpp_api/async_sample_process/circular_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class circular_buffer {
return size() >= length;
});

if (size() < length)
if (size() == 0)
{
return 0;
}
Expand Down
34 changes: 23 additions & 11 deletions examples/cpp_api/async_sample_process/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ typedef enum
{
app_state_setup = 0,
app_state_sampling = 1,
app_state_sleaping = 2,
app_state_sleeping = 2,
} appState_en;

typedef struct
Expand All @@ -42,8 +42,8 @@ typedef struct
bool requested_to_quit;

// buffers & threads
circular_buffer<std::complex<float>> rx_fifo;
thread *dsp_thread;
circular_buffer<std::complex<float>> *rx_fifo;
std::thread *dsp_thread;
} appContext_st;

static appContext_st app = {0};
Expand Down Expand Up @@ -116,7 +116,7 @@ float RSSI(const std::complex<float>* signal, size_t num_of_samples)
return 10 * log10(mean_of_squares);
}

// Consumer thread
// Consumer (parallel DSP) thread
void dataConsumerThread(appContext_st* app)
{
std::cout << "Data consumer thread started" << std::endl;
Expand All @@ -126,14 +126,21 @@ void dataConsumerThread(appContext_st* app)
while (app->running)
{
// get the number of elements in the fifo
size_t num_lements = app.rx_fifo.size();
size_t num_lements = app->rx_fifo->size();
if (num_lements == 0)
{
std::this_thread::sleep_for(std::chrono::milliseconds(50))
std::this_thread::sleep_for(std::chrono::milliseconds(50));
continue;
}

app.rx_fifo.get(local_dsp_buffer, (num_lements>(RX_CHUNK_SAMPLES*4)) ? (RX_CHUNK_SAMPLES * 4) : num_lements);
size_t actual_read = app->rx_fifo->get(local_dsp_buffer, (num_lements>(RX_CHUNK_SAMPLES*4)) ? (RX_CHUNK_SAMPLES * 4) : num_lements);
if (actual_read)
{
// here goes the DSP
app->num_samples_read_so_far += actual_read;
float rssi = RSSI(local_dsp_buffer, actual_read);
printf("DSP EPOCH: %ld, NUM_READ: %ld, RSSI: %.3f\n", app->epoch, app->num_samples_read_so_far, rssi);
}
}

std::cout << "Data consumer thread exitting" << std::endl;
Expand All @@ -152,11 +159,12 @@ void receivedSamples(CaribouLiteRadio* radio, const std::complex<float>* samples
std::cout << std::endl;

// push the received samples in the fifo
app.rx_fifo.put(samples, num_samples);
app.rx_fifo->put(samples, num_samples);
}


// Main entry
// ==========================================================================================
// A simple asynchronous DSP flow
// ==========================================================================================
int main ()
{
// try detecting the board before getting the instance
Expand Down Expand Up @@ -188,6 +196,7 @@ int main ()
{
//----------------------------------------------
case app_state_setup:
std::cout << "Starting Epoch " << app.epoch << std::endl;
// an example periodic radio setup stage
app.radio->SetRxGain(app.gain);
app.radio->SetFrequency(app.freq);
Expand All @@ -212,7 +221,10 @@ int main ()
case app_state_sleeping:
std::this_thread::sleep_for(std::chrono::milliseconds(TIME_BETWEEN_EPOCHS * 1000));
app.epoch ++;


// reset the fifo
app.rx_fifo->reset();

// either go the next epoch or quit the program
// this is an example flow but other possibilities exist
if (!app.requested_to_quit) app.state = app_state_setup;
Expand Down

0 comments on commit ffbd7af

Please sign in to comment.