Files
salmanoff/include/user/stimulusBuffer.h
T

125 lines
2.9 KiB
C++
Raw Normal View History

2025-10-25 18:56:30 -04:00
#ifndef _STIMULUS_BUFFER_H
#define _STIMULUS_BUFFER_H
2025-11-06 15:03:26 -04:00
#include <boostAsioLinkageFix.h>
2025-10-25 18:56:30 -04:00
#include <vector>
#include <memory>
#include <cstdint>
#include <atomic>
#include <mutex>
#include <functional>
#include <iostream>
#include <chrono>
#include <config.h>
#include <boost/asio/io_service.hpp>
#include <boost/asio/deadline_timer.hpp>
#include <spinLock.h>
#include <user/spMcRingBuffer.h>
2025-11-09 22:09:19 -04:00
#include "stimulusFrame.h"
2025-10-25 19:42:48 -04:00
#include "deviceAttachmentSpec.h"
2025-10-25 18:56:30 -04:00
namespace smo {
namespace stim_buff {
/**
* StimulusBuffer manages a collection of stimulus frames with simultaneity stamps.
*
* This buffer is designed to hold stimulus frames that have been assembled
* from raw sensor data (e.g., Livox Avia point cloud data) and are ready
* for processing by the mind layer.
*
* The buffer provides thread-safe operations for adding frames, retrieving
* frames, and managing the buffer state.
*/
class StimulusBuffer
{
2025-10-25 19:32:10 -04:00
public:
class PcloudFormatDesc
{
public:
enum class Format
{
XYZ,
XYZI,
};
public:
Format format;
};
2025-10-25 18:56:30 -04:00
public:
2025-10-25 19:42:48 -04:00
explicit StimulusBuffer(
const std::shared_ptr<device::DeviceAttachmentSpec>
&deviceAttachmentSpec,
size_t nSlots,
const SpMcRingBuffer::InputEngineConstraints& ringBufferConstraints,
2025-11-04 00:49:15 -04:00
boost::asio::io_service& ioService_)
: deviceAttachmentSpec(deviceAttachmentSpec),
ringBuffer(nSlots, ringBufferConstraints),
2025-11-04 00:49:15 -04:00
ioService(ioService_),
shouldContinue(false), timer(ioService),
nDeferrals(0)
2025-10-25 23:04:12 -04:00
{}
virtual ~StimulusBuffer() = default;
2025-10-25 18:56:30 -04:00
// Non-copyable, movable
StimulusBuffer(const StimulusBuffer&) = delete;
StimulusBuffer& operator=(const StimulusBuffer&) = delete;
StimulusBuffer(StimulusBuffer&&) = default;
StimulusBuffer& operator=(StimulusBuffer&&) = default;
// Control methods
2025-11-01 22:03:28 -04:00
virtual void start()
{
std::cout << __func__ << ": Starting stimulus buffer for device "
<< deviceAttachmentSpec->deviceSelector << std::endl;
shouldContinue = true;
nDeferrals = 0;
scheduleNextTimeout();
}
2025-11-01 22:03:28 -04:00
virtual void stop();
void allowNextStimulusFrame()
{ frameAssemblyRateLimiter.release(); }
protected:
// Virtual functions for derived classes to override
virtual int getStopDelayMs() const
{
return CONFIG_STIMBUFF_FRAME_PERIOD_MS;
}
virtual void stimFrameProductionTimesliceInd() = 0;
private:
void onTimeout(const boost::system::error_code& error);
2025-10-25 23:04:12 -04:00
public:
std::shared_ptr<device::DeviceAttachmentSpec> deviceAttachmentSpec;
2025-11-09 22:09:19 -04:00
std::vector<StimulusFrame> frames_;
protected:
SpinLock frameAssemblyRateLimiter;
SpMcRingBuffer ringBuffer;
private:
boost::asio::io_service& ioService;
protected:
SpinLock shouldContinueLock;
bool shouldContinue;
private:
boost::asio::deadline_timer timer;
size_t nDeferrals;
std::chrono::high_resolution_clock::time_point deferralStartTime;
void scheduleNextTimeout(int delayMs = CONFIG_STIMBUFF_FRAME_PERIOD_MS);
};
2025-10-25 18:56:30 -04:00
} // namespace stim_buff
} // namespace smo
#endif // _STIMULUS_BUFFER_H