-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPortController.cpp
171 lines (132 loc) · 4.38 KB
/
PortController.cpp
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
/*
------------------------------------------------------------------
This file is part of the Open Ephys GUI
Copyright (C) 2023 Allen Institute for Brain Science and 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 "PortController.h"
PortController::PortController(PortName port_, oni_ctx ctx_) :
OnixDevice(getPortNameString(port_), OnixDeviceType::PORT_CONTROL, (oni_dev_idx_t)port_, ctx_),
port(port_)
{
}
PortController::~PortController()
{
}
int PortController::configureDevice()
{
ONI_OK_RETURN_INT(oni_write_reg(ctx, deviceIdx, (uint32_t)PortControllerRegister::ENABLE, (uint32_t)1))
return 0;
}
void PortController::startAcquisition()
{
errorFlag = false;
}
void PortController::stopAcquisition()
{
while (!frameArray.isEmpty())
{
const GenericScopedLock<CriticalSection> frameLock(frameArray.getLock());
oni_destroy_frame(frameArray.removeAndReturn(0));
}
}
void PortController::addFrame(oni_frame_t* frame)
{
const GenericScopedLock<CriticalSection> frameLock(frameArray.getLock());
frameArray.add(frame);
}
void PortController::processFrames()
{
while (!frameArray.isEmpty())
{
const GenericScopedLock<CriticalSection> frameLock(frameArray.getLock());
oni_frame_t* frame = frameArray.removeAndReturn(0);
int8_t* dataPtr = (int8_t*)frame->data;
int dataOffset = 8;
uint32_t code = (uint32_t) *(dataPtr + dataOffset);
uint32_t data = (uint32_t) *(dataPtr + dataOffset + 1);
errorFlag = errorFlag || ((uint32_t)data & LINKSTATE_SL) == 0;
oni_destroy_frame(frame);
LOGE("Port status changed for " + getName() + ".");
}
}
void PortController::updateDiscoveryParameters(DiscoveryParameters parameters)
{
discoveryParameters = parameters;
}
DiscoveryParameters PortController::getHeadstageDiscoveryParameters(String headstage)
{
if (headstage == "Neuropixels 1.0f")
{
return DiscoveryParameters(5.0f, 7.0f, 1.0f, 0.2f);
}
return DiscoveryParameters();
}
bool PortController::configureVoltage(float voltage)
{
if (ctx == NULL) return false;
if (voltage == defaultVoltage)
{
if (discoveryParameters == DiscoveryParameters()) return false;
for (voltage = discoveryParameters.minVoltage; voltage <= discoveryParameters.maxVoltage; voltage += discoveryParameters.voltageIncrement)
{
setVoltage(voltage);
if (checkLinkState())
{
setVoltage(voltage + discoveryParameters.voltageOffset);
return checkLinkState();
}
}
}
else
{
setVoltage(voltage);
return checkLinkState();
}
return false;
}
void PortController::setVoltageOverride(float voltage, bool waitToSettle)
{
if (ctx == NULL) return;
ONI_OK(oni_write_reg(ctx, (oni_dev_idx_t)port, (oni_reg_addr_t)PortControllerRegister::PORTVOLTAGE, (oni_reg_val_t)(voltage * 10)));
if (waitToSettle) sleep_for(std::chrono::milliseconds(500));
}
void PortController::setVoltage(float voltage)
{
if (ctx == NULL) return;
ONI_OK(oni_write_reg(ctx, (oni_dev_idx_t)port, (oni_reg_addr_t)PortControllerRegister::PORTVOLTAGE, 0));
sleep_for(std::chrono::milliseconds(300));
ONI_OK(oni_write_reg(ctx, (oni_dev_idx_t)port, (oni_reg_addr_t)PortControllerRegister::PORTVOLTAGE, (oni_reg_val_t)(voltage * 10)));
sleep_for(std::chrono::milliseconds(500));
}
bool PortController::checkLinkState() const
{
if (ctx == NULL) return false;
oni_reg_val_t linkState;
int result = oni_read_reg(ctx, (oni_dev_idx_t)port, (oni_reg_addr_t)PortControllerRegister::LINKSTATE, &linkState);
if (result != 0) { LOGE(oni_error_str(result)); return false; }
else if ((linkState & LINKSTATE_SL) == 0) { LOGE("Unable to acquire communication lock."); return false; }
else return true;
}
String PortController::getPortNameString(PortName portName)
{
switch (portName)
{
case PortName::PortA:
return "Port A";
case PortName::PortB:
return "Port B";
default:
break;
}
}