-
Notifications
You must be signed in to change notification settings - Fork 2
/
ttl-pulses.cpp
173 lines (154 loc) · 4.67 KB
/
ttl-pulses.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
172
173
/*
Copyright (C) 2011 Georgia Institute of Technology
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 <ttl-pulses.h>
extern "C" Plugin::Object *createRTXIPlugin(void)
{
return new TTL();
}
static DefaultGUIModel::variable_t vars[] = {
{ "TTL Signal", "TTL Signal", DefaultGUIModel::OUTPUT, }, // output(0)
{
"TTL Duration (s)", "Duration of pulse", DefaultGUIModel::PARAMETER
| DefaultGUIModel::DOUBLE,
},
{
"TTL Pulses (#)", "Number of pulses", DefaultGUIModel::PARAMETER
| DefaultGUIModel::DOUBLE,
},
{
"TTL Freq (Hz)", "Frequency measured between pulse onsets",
DefaultGUIModel::PARAMETER | DefaultGUIModel::DOUBLE,
},
{
"TTL Delay (s)", "Time to wait between trials",
DefaultGUIModel::PARAMETER | DefaultGUIModel::DOUBLE,
},
{
"Repeat (#)", "Number of trials", DefaultGUIModel::PARAMETER
| DefaultGUIModel::DOUBLE,
},
{
"Current Trial", "Current trial being executed", DefaultGUIModel::STATE
},
{
"Current Pulse", "Current pulse being executed", DefaultGUIModel::STATE
},
};
static size_t num_vars = sizeof(vars) / sizeof(DefaultGUIModel::variable_t);
TTL::TTL(void) : DefaultGUIModel("TTL Pulses", ::vars, ::num_vars),
dt(RT::System::getInstance()->getPeriod() * 1e-9),
ttlDuration(.25), ttlNumPulses(1), ttlFreq(1), ttlDelay(.5),
maxtrials(1), trial(0), idx(0)
{
setWhatsThis(
"<p><b>TTL:</b><br>This module outputs a train of TTL pulses (0-5V square wave). This can"
"be connected to either an analog or digital output channel.</p>");
DefaultGUIModel::createGUI(vars, num_vars);
initParameters();
update(INIT);
refresh();
QTimer::singleShot(0, this, SLOT(resizeMe()));
}
TTL::~TTL(void) {}
void TTL::execute(void)
{
if (trial < maxtrials) { // run trial
if (idx < stimTTL.size()) {
output(0) = stimTTL[idx++];
if (stimTTL[idx] < stimTTL.at(idx-1))
currentTTL++;
} else {
trial++;
idx = 0;
}
} // end single trial
else { // all trials are done, send signal to holding current module, and pause
output(0) = 0;
pauseButton->setChecked(true);
} // end protocol
}
void TTL::update(DefaultGUIModel::update_flags_t flag)
{
switch (flag) {
case INIT:
setParameter("Repeat (#)", QString::number(maxtrials)); // initially 1
setParameter("TTL Duration (s)", QString::number(ttlDuration)); // initially 1
setParameter("TTL Pulses (#)", QString::number(ttlNumPulses)); // initially 1
setParameter("TTL Freq (Hz)", QString::number(ttlFreq)); // initially 1
setParameter("TTL Delay (s)", QString::number(ttlDelay)); // initially 1
setState("Current Trial", trial); // initially 0
setState("Current Pulse", currentTTL); // initially 0
break;
case MODIFY:
if (getParameter("TTL Duration (s)").toDouble() >= (getParameter("TTL Freq (Hz)").toDouble())) {
QMessageBox::critical(this,"TTL",tr(
"The TTL pulses will overlap with the specified pulse duration and frequency.\n"));
} else {
ttlDuration = getParameter("TTL Duration (s)").toDouble();
ttlFreq = getParameter("TTL Freq (Hz)").toDouble();
ttlNumPulses = getParameter("TTL Pulses (#)").toDouble();
ttlDelay = getParameter("TTL Delay (s)").toDouble();
makeTTL();
}
maxtrials = getParameter("Repeat (#)").toDouble();
makeTTL();
break;
case PAUSE:
output(0) = 0;
bookkeep();
break;
case PERIOD:
dt = RT::System::getInstance()->getPeriod() * 1e-9; // sec
idx = 0;
update(MODIFY);
break;
default:
break;
}
}
// custom functions
void TTL::initParameters()
{
maxtrials = 1;
ttlDuration = .25; // s
ttlNumPulses = 1;
ttlFreq = 1; // Hz
ttlDelay = .5; // s
trial = 0;
currentTTL = 0;
bookkeep();
makeTTL();
}
void TTL::bookkeep()
{
trial = 0;
idx = 0;
currentTTL = 0;
}
void TTL::makeTTL()
{
stimTTL.clear();
for (int i = 0; i < ttlDelay / dt; i++) { // initial delay in trial before starting ttl
stimTTL.push_back(0);
}
for (int n = 0; n < ttlNumPulses; n++) {
for (int i = 0; i < ttlDuration / dt; i++) {
stimTTL.push_back(5);
}
// fill in zeros for frequency
for (int i = 0; i < ((1 / ttlFreq) - ttlDuration) / dt; i++) {
stimTTL.push_back(0);
}
}
}