Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions Source/Devices/AnalogIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ AnalogIO::AnalogIO(std::string name, std::string hubName, const oni_dev_idx_t de
OnixDevice::createStreamName({ getHubName(), name, "AnalogInput" }),
"Analog Input data",
getStreamIdentifier(),
getNumChannels(),
numChannels,
getSampleRate(),
"AnalogInput",
ContinuousChannel::Type::ADC,
Expand Down Expand Up @@ -181,11 +181,6 @@ void AnalogIO::setDataType(AnalogIODataType type)
dataType = type;
}

int AnalogIO::getNumChannels()
{
return numChannels;
}

void AnalogIO::startAcquisition()
{
currentFrame = 0;
Expand Down Expand Up @@ -247,22 +242,16 @@ void AnalogIO::processFrame(uint64_t eventWord)

if (currentFrame >= numFrames)
{
shouldAddToBuffer = true;
currentFrame = 0;
}

if (shouldAddToBuffer)
{
shouldAddToBuffer = false;
analogInputBuffer->addToBuffer(analogInputSamples.data(), sampleNumbers, timestamps, eventCodes, numFrames);

analogInputSamples.fill(0);
currentFrame = 0;
}
}

void AnalogIO::processFrames()
{
while (frameQueue.peek() != nullptr )
while (frameQueue.peek() != nullptr)
{
processFrame();
}
Expand Down
6 changes: 1 addition & 5 deletions Source/Devices/AnalogIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,8 @@ namespace OnixSourcePlugin
AnalogIODataType getDataType() const;
void setDataType(AnalogIODataType type);

int getNumChannels();

static OnixDeviceType getDeviceType();

static constexpr int framesToAverage = 4; // NB: Downsampling from 100 kHz to 25 kHz
static int getSampleRate();

int getNumberOfFrames();
Expand All @@ -103,6 +100,7 @@ namespace OnixSourcePlugin
DataBuffer* analogInputBuffer = nullptr;

static constexpr int AnalogIOFrequencyHz = 100000;
static constexpr int framesToAverage = 4; // NB: Downsampling from 100 kHz to 25 kHz

static constexpr int numFrames = 25;
static constexpr int numChannels = 12;
Expand All @@ -119,8 +117,6 @@ namespace OnixSourcePlugin
unsigned short currentAverageFrame = 0;
int sampleNumber = 0;

bool shouldAddToBuffer = false;

std::array<float, numFrames* numChannels> analogInputSamples;

double timestamps[numFrames];
Expand Down
92 changes: 0 additions & 92 deletions Source/Devices/AuxiliaryIO.cpp

This file was deleted.

62 changes: 0 additions & 62 deletions Source/Devices/AuxiliaryIO.h

This file was deleted.

1 change: 0 additions & 1 deletion Source/Devices/DeviceList.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

*/

#include "AuxiliaryIO.h"
#include "AnalogIO.h"
#include "Bno055.h"
#include "DigitalIO.h"
Expand Down
93 changes: 68 additions & 25 deletions Source/Devices/DigitalIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,37 @@
using namespace OnixSourcePlugin;

DigitalIO::DigitalIO(std::string name, std::string hubName, const oni_dev_idx_t deviceIdx_, std::shared_ptr<Onix1> oni_ctx)
: OnixDevice(name, hubName, DigitalIO::getDeviceType(), deviceIdx_, oni_ctx), eventWords(64)
: OnixDevice(name, hubName, DigitalIO::getDeviceType(), deviceIdx_, oni_ctx)
{
StreamInfo digitalInputStream = StreamInfo(
OnixDevice::createStreamName({ getHubName(), name, "DigitalInputs" }),
"Digital Inputs data",
getStreamIdentifier(),
NumDigitalInputs,
AnalogIO::getSampleRate(),
"CH",
ContinuousChannel::Type::AUX,
1.0,
"u", // NB: Digital data is unitless by definition
{},
{ "input" });
streamInfos.add(digitalInputStream);

StreamInfo digitalButtonStream = StreamInfo(
OnixDevice::createStreamName({ getHubName(), name, "DigitalButtons" }),
"Digital Buttons data",
getStreamIdentifier(),
NumButtons,
AnalogIO::getSampleRate(),
"",
ContinuousChannel::Type::AUX,
1.0,
"u", // NB: Digital data is unitless by definition
{ "Moon", "Triangle", "X", "Check", "Circle", "Square" },
{ "input" });
streamInfos.add(digitalButtonStream);

eventCodes.fill(0);
}

OnixDeviceType DigitalIO::getDeviceType()
Expand All @@ -49,7 +78,9 @@ int DigitalIO::configureDevice()
if (rc != ONI_ESUCCESS)
throw error_str("Could not read the base frequency register on the DigitalIO device.");

uint32_t periodTicks = baseFreqHz / (uint32_t)AnalogIO::getSampleRate();
// NB: Two states are not accounted for when comparing clock ticks on the hardware,
// therefore the periodTicks variable must be decreased by 2 to get the correct sample rate.
uint32_t periodTicks = (baseFreqHz / (uint32_t)AnalogIO::getSampleRate()) - 2u;
rc = deviceContext->writeRegister(deviceIdx, (uint32_t)DigitalIORegisters::SAMPLE_PERIOD, periodTicks);
if (rc != ONI_ESUCCESS)
throw error_str("Could not write the sample rate for polling to the DigitalIO device.");
Expand All @@ -64,10 +95,16 @@ bool DigitalIO::updateSettings()

void DigitalIO::startAcquisition()
{
currentFrame = 0;
sampleNumber = 0;

digitalSamples.fill(0);
}

void DigitalIO::addSourceBuffers(OwnedArray<DataBuffer>& sourceBuffers)
{
sourceBuffers.add(new DataBuffer(NumChannels, (int)streamInfos.getFirst().getSampleRate() * bufferSizeInSeconds));
digitalBuffer = sourceBuffers.getLast();
}

EventChannel::Settings DigitalIO::getEventChannelSettings(DataStream* stream)
Expand All @@ -78,48 +115,54 @@ EventChannel::Settings DigitalIO::getEventChannelSettings(DataStream* stream)
"Digital inputs and breakout button states coming from a DigitalIO device",
getStreamIdentifier() + ".event.digital",
stream,
numButtons + numDigitalInputs
NumChannels
};

return settings;
}

int DigitalIO::getNumberOfWords()
float DigitalIO::getChannelState(uint8_t state, int channel)
{
return eventWords.size_approx();
}
return (state & (1 << channel)) >> channel; // NB: Return the state of the specified channel
};

void DigitalIO::processFrames()
{
oni_frame_t* frame;
while (frameQueue.try_dequeue(frame))
{
size_t offset = 0;

uint16_t* dataPtr = (uint16_t*)frame->data;
uint64_t timestamp = deviceContext->convertTimestampToSeconds(frame->time);

int dataOffset = 4;
timestamps[currentFrame] = deviceContext->convertTimestampToSeconds(frame->time);
sampleNumbers[currentFrame] = sampleNumber++;

uint64_t portState = *(dataPtr + dataOffset);
uint64_t buttonState = *(dataPtr + dataOffset + 1);
constexpr int inputDataOffset = 4;
constexpr int buttonDataOffset = inputDataOffset + 1;

uint64_t ttlEventWord = (buttonState & 0x3F) << 8 | (portState & 0xFF);
eventWords.enqueue(ttlEventWord);
uint64_t inputState = *(dataPtr + inputDataOffset);
uint64_t buttonState = *(dataPtr + buttonDataOffset);

oni_destroy_frame(frame);
}
}
for (int i = 0; i < NumDigitalInputs; i++)
{
digitalSamples[currentFrame + offset++ * NumFrames] = getChannelState(inputState, i);
}

uint64_t DigitalIO::getEventWord()
{
uint64_t eventWord;
if (eventWords.try_dequeue(eventWord))
return eventWord;
for (int i = 0; i < NumButtons; i++)
{
digitalSamples[currentFrame + offset++ * NumFrames] = getChannelState(buttonState, i);
}

return 0;
}
eventCodes[currentFrame] = (buttonState & 0x3F) << 8 | (inputState & 0xFF);

bool DigitalIO::hasEventWord()
{
return eventWords.peek() != nullptr;
oni_destroy_frame(frame);

if (++currentFrame >= NumFrames)
{
digitalBuffer->addToBuffer(digitalSamples.data(), sampleNumbers.data(), timestamps.data(), eventCodes.data(), NumFrames);

currentFrame = 0;
}
}
}
Loading
Loading