void acq_close(struct acq* acq) { if (!acq) return; // Inform the acquisition thread to stop pthread_mutex_lock(&acq->lock); acq->running = -1; pthread_cond_signal(&acq->cond); pthread_mutex_unlock(&acq->lock); // Wait for the thread to actually finish pthread_join(acq->thid, NULL); // Close device egd_close(acq->dev); free(acq); }
struct acq* acq_init(const char* devstring, acqcb cb, void* cbdata) { struct acq* acq; int i; if (!(acq = calloc(sizeof(*acq), 1))) return NULL; // Open the connection to the device if (!(acq->dev = egd_open(devstring))) { fprintf(stderr, "Connection error: %s\n",strerror(errno)); goto exit; } // Setup the transfer to the data buffers if (configure_egd_acq(acq) || egd_acq_setup(acq->dev, 3, acq->strides, 3, acq->grp)) { fprintf(stderr, "Acq_setup: %s\n", strerror(errno)); goto exit; } acq->cb = cb; acq->cbdata = cbdata ? cbdata : acq; if (pthread_create(&acq->thid, NULL, acq_loop_fn, acq)) goto exit; return acq; exit: if (acq->dev) egd_close(acq->dev); for (i=0; i<3; i++) free(acq->buff[i]); free(acq); return NULL; }
static int read_eegsignal(int bsigcheck, int pass) { struct eegdev* dev; int type = grp[0].datatype; size_t strides[3]; void *eeg_t = NULL, *exg_t = NULL; int32_t *tri_t = NULL; int ntri, fs, i, baddata, retcode = 1; size_t tsize = (type == EGD_FLOAT ? sizeof(float) : sizeof(double)); // Reset global variable used to track the expected signal checking = 0; nstot = nsread = 0; if (!(dev = open_device(grp))) goto exit; // Get number of channels and configure structures grp[0].sensortype = egd_sensor_type("eeg"); grp[1].sensortype = egd_sensor_type("undefined"); grp[2].sensortype = egd_sensor_type("trigger"); strides[0] = grp[0].nch*tsize; strides[1] = grp[1].nch*tsize; strides[2] = grp[2].nch*sizeof(int32_t); ntri = grp[2].nch; eeg_t = calloc(strides[0], NSAMPLE); exg_t = calloc(strides[1], NSAMPLE); tri_t = calloc(strides[2], NSAMPLE); fs = print_cap(dev); if (test_chinfo(dev)) { fprintf(stderr, "\tTest_chinfo failed\n"); goto exit; } 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; } // No checking if (!bsigcheck) { if (simple_trigger_check(i, NSAMPLE, ntri, tri_t)) retcode = 2; continue; } if (type == EGD_FLOAT) baddata = check_signals_f(NSAMPLE, eeg_t, exg_t, tri_t); else baddata = check_signals_d(NSAMPLE, eeg_t, exg_t, tri_t); if (baddata) { 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 at pass %i: %s", pass, strerror(errno)); egd_close(dev); free(eeg_t); free(exg_t); free(tri_t); return retcode; }