-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDigitalIO.cpp
More file actions
173 lines (137 loc) · 5.92 KB
/
Copy pathDigitalIO.cpp
File metadata and controls
173 lines (137 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
------------------------------------------------------------------
Copyright (C) Open Ephys
------------------------------------------------------------------
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "DigitalIO.h"
#include "AnalogIO.h"
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)
{
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()
{
return OnixDeviceType::DIGITALIO;
}
int DigitalIO::configureDevice()
{
if (deviceContext == nullptr || ! deviceContext->isInitialized())
throw error_str ("Device context is not initialized properly for " + getName());
int rc = deviceContext->writeRegister (deviceIdx, (uint32_t) DigitalIORegisters::ENABLE, (oni_reg_val_t) (isEnabled() ? 1 : 0));
if (rc != ONI_ESUCCESS)
throw error_str ("Failed to enable the DigitalIO device.");
oni_reg_val_t baseFreqHz;
rc = deviceContext->readRegister (deviceIdx, (uint32_t) DigitalIORegisters::BASE_FREQ_HZ, &baseFreqHz);
if (rc != ONI_ESUCCESS)
throw error_str ("Could not read the base frequency register on the DigitalIO device.");
// 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.");
return rc;
}
bool DigitalIO::updateSettings()
{
return true;
}
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)
{
EventChannel::Settings settings {
EventChannel::Type::TTL,
OnixDevice::createStreamName ({ getHubName(), getName(), "Events" }),
"Digital inputs and breakout button states coming from a DigitalIO device",
getStreamIdentifier() + ".event.digital",
stream,
NumChannels
};
return settings;
}
float DigitalIO::getChannelState (uint8_t state, int channel)
{
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;
// NB: In ONI v1.0 frame clock is when the frame is created, not necessarily when the data is received.
// For local and passthrough devices, we will instead use the hub clock for the timestamp; in
// ONI v2.0 this behavior may change, and frame->time can be used instead for consistency across devices.
auto hubClock = (uint64_t*) frame->data;
timestamps[currentFrame] = deviceContext->convertTimestampToSeconds (*hubClock);
sampleNumbers[currentFrame] = sampleNumber++;
constexpr int inputDataOffset = 4;
constexpr int buttonDataOffset = inputDataOffset + 1;
uint16_t* dataPtr = (uint16_t*) frame->data;
uint64_t inputState = *(dataPtr + inputDataOffset);
uint64_t buttonState = *(dataPtr + buttonDataOffset);
for (int i = 0; i < NumDigitalInputs; i++)
{
digitalSamples[currentFrame + offset++ * NumFrames] = getChannelState (inputState, i);
}
for (int i = 0; i < NumButtons; i++)
{
digitalSamples[currentFrame + offset++ * NumFrames] = getChannelState (buttonState, i);
}
eventCodes[currentFrame] = (buttonState & 0x3F) << 8 | (inputState & 0xFF);
oni_destroy_frame (frame);
if (++currentFrame >= NumFrames)
{
digitalBuffer->addToBuffer (digitalSamples.data(), sampleNumbers.data(), timestamps.data(), eventCodes.data(), NumFrames);
currentFrame = 0;
}
}
}