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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/*
Copyright (C) 2010-2012 EPFL (Ecole Polytechnique Fédérale de Lausanne)
Nicolas Bourdaud <nicolas.bourdaud@epfl.ch>
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 <sys/time.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <eegdev.h>
#include "fakelibs/tia-server.h"
#define DURATION 5 // in seconds
#define NSAMPLE 4
#define NEEG 7
#define NEXG 4
#define NTRI 1
#define PORT 38500
#define scaled_t float
static const char* devhost = NULL;
static int verbose = 0;
static struct grpconf grp[3] = {
{
.index = 0,
.iarray = 0,
.arr_offset = 0,
.nch = 64,
.datatype = EGD_FLOAT
},
{
.index = 0,
.iarray = 1,
.arr_offset = 0,
.nch = 0,
.datatype = EGD_FLOAT
},
{
.index = 0,
.iarray = 2,
.arr_offset = 0,
.nch = 1,
.datatype = EGD_INT32
}
};
int check_signals_f(size_t ns, const float* sig, const float* exg, const int32_t* tri)
{
size_t i=0;
int neeg = grp[0].nch;
int nexg = grp[1].nch;
int ntri = grp[2].nch;
int ich;
float expval;
int32_t exptri;
int retval = 0;
static int nstot = 0, nsread = 0;
// Identify the beginning of the expected sequence
for (; i<ns && !retval; i++) {
// Verify the values in the analog channels
for (ich=0; ich<neeg && !retval; ich++) {
expval = get_analog_val(nstot, ich);
if (sig[i*neeg+ich] != expval) {
fprintf(stderr, "\tEEG value (%f) different from the one expected (%f) at sample %zu ch:%u\n", sig[i*neeg+ich], expval, i+nsread, ich);
retval = -1;
}
}
// Verify the values in the exg channels
for (ich=0; ich<nexg && !retval; ich++) {
expval = get_analog_val(nstot, ich);
if (exg[i*nexg+ich] != expval) {
fprintf(stderr, "\tEXG value (%f) different from the one expected (%f) at sample %zu ch:%u\n", exg[i*nexg+ich], expval, i+nsread, ich);
retval = -1;
}
}
// Verify the values in the trigger channels
for (ich=0; ich<ntri && !retval; ich++) {
exptri = get_trigger_val(nstot, ich);
if (tri[i*ntri+ich] != exptri) {
fprintf(stderr, "\tTrigger value (0x%08x) different from the one expected (0x%08x) at sample %zu ch:%u\n", tri[i], exptri, i+nsread, ich);
retval = -1;
}
}
nstot++;
}
nsread += ns;
return retval;
}
static
struct eegdev* open_device(struct grpconf group[3])
{
struct eegdev* dev;
int i;
char devstring[256] = "tobiia";
const char* const sname[3] = {"eeg", "undefined", "trigger"};
if (devhost)
sprintf(devstring+strlen(devstring), "|host|%s", devhost);
sprintf(devstring+strlen(devstring), "|port|%i", PORT);
if (!(dev = egd_open(devstring)))
return NULL;
for (i=0; i<3; i++) {
group[i].sensortype = egd_sensor_type(sname[i]);
group[i].nch = egd_get_numch(dev, group[i].sensortype);
}
return dev;
}
static
int print_cap(struct eegdev* dev)
{
unsigned int sampling_freq, eeg_nmax, sensor_nmax, trigger_nmax;
char *device_type, *device_id;
int retval;
egd_get_cap(dev, EGD_CAP_DEVTYPE, &device_type);
egd_get_cap(dev, EGD_CAP_DEVTYPE, &device_id);
egd_get_cap(dev, EGD_CAP_FS, &sampling_freq);
eeg_nmax = egd_get_numch(dev, egd_sensor_type("eeg"));
sensor_nmax = egd_get_numch(dev, egd_sensor_type("undefined"));
trigger_nmax = egd_get_numch(dev, egd_sensor_type("trigger"));
retval = (int)sampling_freq;
if (!verbose)
return retval;
printf("\tsystem capabilities:\n"
"\t\tdevice type: %s\n"
"\t\tdevice model: %s\n"
"\t\tsampling frequency: %u Hz\n"
"\t\tnum EEG channels: %u\n"
"\t\tnum sensor channels: %u\n"
"\t\tnum trigger channels: %u\n",
device_type, device_type,
sampling_freq, eeg_nmax, sensor_nmax, trigger_nmax);
return retval;
}
int read_eegsignal(int bsigcheck)
{
struct eegdev* dev;
size_t strides[3];
size_t tsize = sizeof(scaled_t);
void *eeg_t = NULL, *exg_t = NULL;
int32_t *tri_t = NULL;
int i, fs, retcode = 1;
if (!(dev = open_device(grp)))
goto exit;
// Get number of channels and configure structures
strides[0] = grp[0].nch*tsize;
strides[1] = grp[1].nch*tsize;
strides[2] = grp[2].nch*sizeof(int32_t);
eeg_t = calloc(strides[0], NSAMPLE);
exg_t = calloc(strides[1], NSAMPLE);
tri_t = calloc(strides[2], NSAMPLE);
fs = print_cap(dev);
if (egd_acq_setup(dev, 3, strides, 3, grp))
goto exit;
if (egd_start(dev))
goto exit;
for (i=0; i < fs*DURATION; i += NSAMPLE) {
if (egd_get_data(dev, NSAMPLE, eeg_t, exg_t, tri_t) < 0) {
fprintf(stderr, "\tAcq failed at sample %i\n",i);
goto exit;
}
if (bsigcheck
&& check_signals_f(NSAMPLE, eeg_t, exg_t, tri_t)) {
retcode = 2;
break;
}
}
if (egd_stop(dev))
goto exit;
if (egd_close(dev))
goto exit;
dev = NULL;
if (retcode == 1)
retcode = 0;
exit:
if (retcode == 1)
fprintf(stderr, "\terror caught (%i) %s\n",errno,strerror(errno));
egd_close(dev);
free(eeg_t);
free(exg_t);
free(tri_t);
return retcode;
}
int main(int argc, char *argv[])
{
int opt, retcode = 0, bsigcheck = 0;
while ((opt = getopt(argc, argv, "c:v:s:")) != -1) {
switch (opt) {
case 'c':
bsigcheck = atoi(optarg);
break;
case 's':
devhost = optarg;
break;
case 'v':
verbose = atoi(optarg);
break;
default: /* '?' */
fprintf(stderr, "Usage: %s "
"[-c checking_expected_signals] "
"[-s server] [-v verbosity]\n",
argv[0]);
return EXIT_FAILURE;
}
}
printf("\tTesting tobiia\n");
if (bsigcheck && create_tia_server(PORT)) {
fprintf(stderr, "server creation failed: (%i) %s\n",
errno, strerror(errno));
return EXIT_FAILURE;
}
retcode = read_eegsignal(bsigcheck);
if (bsigcheck)
destroy_tia_server();
return retcode;
}
|