-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHarpSyncInput.cpp
More file actions
114 lines (88 loc) · 3.7 KB
/
Copy pathHarpSyncInput.cpp
File metadata and controls
114 lines (88 loc) · 3.7 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
/*
------------------------------------------------------------------
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 "HarpSyncInput.h"
using namespace OnixSourcePlugin;
HarpSyncInput::HarpSyncInput (std::string name, std::string hubName, const oni_dev_idx_t deviceIdx_, std::shared_ptr<Onix1> oni_ctx)
: OnixDevice (name, hubName, HarpSyncInput::getDeviceType(), deviceIdx_, oni_ctx)
{
setEnabled (false);
StreamInfo harpTimeStream = StreamInfo (
OnixDevice::createStreamName ({ getHubName(), getName(), "HarpTime" }),
"Harp clock time corresponding to the local acquisition ONIX clock count",
getStreamIdentifier(),
1,
1,
"HarpTime",
ContinuousChannel::Type::AUX,
1.0f,
"s",
{ "" },
"harptime");
streamInfos.add (harpTimeStream);
for (int i = 0; i < numFrames; i++)
eventCodes[i] = 0;
}
OnixDeviceType HarpSyncInput::getDeviceType()
{
return OnixDeviceType::HARPSYNCINPUT;
}
int HarpSyncInput::configureDevice()
{
if (deviceContext == nullptr || ! deviceContext->isInitialized())
throw error_str ("Device context is not initialized properly for " + getName());
return deviceContext->writeRegister (deviceIdx, (uint32_t) HarpSyncInputRegisters::ENABLE, (oni_reg_val_t) (isEnabled() ? 1 : 0));
}
bool HarpSyncInput::updateSettings()
{
return deviceContext->writeRegister (deviceIdx, (oni_reg_addr_t) HarpSyncInputRegisters::SOURCE, (oni_reg_val_t) HarpSyncSource::Breakout) == ONI_ESUCCESS;
}
void HarpSyncInput::startAcquisition()
{
currentFrame = 0;
sampleNumber = 0;
}
void HarpSyncInput::addSourceBuffers (OwnedArray<DataBuffer>& sourceBuffers)
{
sourceBuffers.add (new DataBuffer (streamInfos.getFirst().getNumChannels(), (int) streamInfos.getFirst().getSampleRate() * bufferSizeInSeconds));
harpTimeBuffer = sourceBuffers.getLast();
}
void HarpSyncInput::processFrames()
{
oni_frame_t* frame;
while (frameQueue.try_dequeue (frame))
{
// 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);
uint32_t* dataPtr = (uint32_t*) frame->data;
harpTimeSamples[currentFrame] = *(dataPtr + 2) + 1;
oni_destroy_frame (frame);
sampleNumbers[currentFrame] = sampleNumber++;
currentFrame++;
if (currentFrame >= numFrames)
{
shouldAddToBuffer = true;
currentFrame = 0;
}
if (shouldAddToBuffer)
{
shouldAddToBuffer = false;
harpTimeBuffer->addToBuffer (harpTimeSamples, sampleNumbers, timestamps, eventCodes, numFrames);
}
}
}