void ComediScope::updateTime() { QString s; if (!rec_file) { if (rec_filename->isEmpty()) { s.sprintf("comedirecord"); } else { if (recorded) { s=(*rec_filename)+" --- file saved"; } else { s=(*rec_filename)+" --- press REC to record "; } } } else { s = (*rec_filename) + QString().sprintf("--- rec: %ldsec", nsamples/sampling_rate ); } comediRecord->setWindowTitle( s ); char tmp[256]; for(int n=0;n<nComediDevices;n++) { for(int i=0;i<channels_in_use;i++) { float phys=comedi_to_phys(daqData[n][i], crange[n], maxdata[n]); sprintf(tmp,VOLT_FORMAT_STRING,phys); comediRecord->voltageTextEdit[n][i]->setText(tmp); } } }
double get_adc_volts(int chan, int diff, int range) { lsampl_t data[OVER_SAMPLE]; // adc burst data buffer int retval; comedi_range *ad_range; comedi_insn daq_insn; ADC_ERROR = FALSE; // global fault flag if (diff == TRUE) { aref_ai = AREF_DIFF; } else { aref_ai = AREF_GROUND; } // configure the AI channel for reads daq_insn.subdev = subdev_ai; daq_insn.chanspec = CR_PACK(chan, range, aref_ai); daq_insn.insn = INSN_READ; daq_insn.n = OVER_SAMPLE; daq_insn.data = data; retval = comedi_do_insn(it, &daq_insn); // send one instruction to the driver if (retval < 0) { comedi_perror("comedi_do_insn in get_adc_volts"); ADC_ERROR = TRUE; bmc.raw[chan] = 0; return 0.0; } ad_range = comedi_get_range(it, subdev_ai, chan, range); bmc.raw[chan] = data[0]; return comedi_to_phys(data[0], ad_range, maxdata_ai); // return the double result }
double readpin(int chan) { comedi_t *it; lsampl_t data, maxdata; comedi_range *rang; int readres; double outval; if((it=comedi_open("/dev/comedi0"))==NULL) { printf("fail open"); } comedi_set_global_oor_behavior(COMEDI_OOR_NUMBER); readres=comedi_data_read(it,subdev,chan,0,aref, & data); if(!readres) { comedi_perror("comedi_data_read: "); } rang = comedi_get_range(it, subdev, chan, 0); maxdata = comedi_get_maxdata(it, subdev, chan); outval = comedi_to_phys(data, rang, maxdata); printf("data=%d\noutval=%g\nreadres=%d\n",data, outval, readres); return outval; }
void ComediScope::writeFile() { if (!rec_file) return; if (comediRecord-> rawCheckbox->isChecked()) { fprintf(rec_file,"%ld",nsamples); } else { fprintf(rec_file,"%f",((float)nsamples)/((float)sampling_rate)); } for(int n=0;n<nComediDevices;n++) { for(int i=0;i<channels_in_use;i++) { if (comediRecord-> channel[n][i]->isActive() ) { if (comediRecord-> rawCheckbox->isChecked()) { fprintf(rec_file, "%c%d",separator,(int)(daqData[n][i])); } else { float phy=comedi_to_phys((lsampl_t)(daqData[n][i]), crange[n], maxdata[n]); fprintf(rec_file,"%c%f",separator,phy); } } } } if (ext_data_receive) { fprintf(rec_file,"%c%s",separator,ext_data_receive->getData()); } fprintf(rec_file,"\n"); }
void print_datum(lsampl_t raw, int channel_index, short physical) { double physical_value; if(!physical) { printf("%d ",raw); } else { physical_value = comedi_to_phys(raw, range_info[channel_index], maxdata[channel_index]); //printf("%d ",raw); //printf("%d:%#8.6g ",channel_index, physical_value); printf("%#8.12g ", physical_value/resistance[channel_index]); } }
//READ THE data from the card here int32 ATIDAQHardwareInterface::ReadSingleSample( float64 buffer[] ) { int32 retVal = 0; /*the return code*/ unsigned long numRawSamples; /*the number of raw gauge value samples*/ numRawSamples = m_uiNumChannels * m_uiAveragingSize; unsigned int i, j; /*loop/array indices*/ float64 * rawBuffer = new float64[numRawSamples]; /*the buffer which holds the raw, unaveraged gauge values*/ int res = 0; // retVal = comedi_do_insnlist(comediDev,&il); // if(retVal < 0){ // comedi_perror(""); // return retVal; // } for (unsigned int ch = 0; ch < m_uiNumChannels; ch++) { // buffer[ch] = comedi_to_phys(dataArray[ch], &range, maxdata); // old single read res = comedi_data_read(comediDev, 0, ch, 0, AREF_DIFF , &data); // if (res == 1){ buffer[ch] = comedi_to_phys(data, &range, maxdata); } delete []rawBuffer; return retVal; // float64 timeOut = ( numRawSamples / m_f64SamplingFrequency ) + 1; /*allow a full second // for Windows timing inaccuracies (definitely overkill, but whatever)*/ // int32 read; /*number samples read*/ // retVal = DAQmxReadAnalogF64( *m_thDAQTask, m_uiAveragingSize, timeOut, DAQmx_Val_GroupByScanNumber, // rawBuffer, numRawSamples, &read, NULL ); // // for ( i = 0; i < m_uiNumChannels; i++ ) // { // /*sum the raw values, storing the sum in the first raw data point*/ // for ( j = 1; j < m_uiAveragingSize; j++ ) // { // rawBuffer[i] += rawBuffer[ i + ( j * m_uiNumChannels ) ]; // } // /*store the average values in the output buffer*/ // buffer[i] = rawBuffer[i] / m_uiAveragingSize; // } // // delete []rawBuffer; /*gotta keep up with those unmanaged pointers*/ }
double print_datum(lsampl_t raw, int channel_index, short physical) { double physical_value; // if(!physical) { // printf("%d ",raw); // } else { physical_value = comedi_to_phys(raw, range_info[channel_index], maxdata[channel_index]); printf("%f ",physical_value); // printf("%f(f) ", physical_value); return physical_value; // } }
double get_adc_volts(int chan) { lsampl_t data; int retval; ADC_ERROR = FALSE; retval = comedi_data_read_delayed(it, subdev_ai, chan, range_ai, aref_ai, &data, 10000); if (retval < 0) { comedi_perror("comedi_data_read in get_adc_volts"); ADC_ERROR = TRUE; return 0.0; } return comedi_to_phys(data, ad_range, maxdata_ai); }
double read_volts(int subdev, int chan, int range) { lsampl_t data, maxdata; int readres; comedi_range *rang; double outval; comedi_set_global_oor_behavior(COMEDI_OOR_NUMBER); readres=comedi_data_read(it,subdev,chan,range,aref, & data); if(!readres) { comedi_perror("comedi_data_read: "); } rang = comedi_get_range(it, subdev, chan, range); maxdata = comedi_get_maxdata(it, subdev, chan); outval = comedi_to_phys(data, rang, maxdata); //printf("data=%d\noutval=%g\nreadres=%d\n",data, outval, readres); return outval; }
int bogio_read_frames(const bogio_spec *spec, bogio_buf *buf, unsigned int frames, int blocking) { int bytes, i; sampl_t *raw; /* Place to store raw samples before normalisation */ unsigned int framesize = buf->spf * sizeof(sampl_t); /* Don't read past the end of the buffer */ if (frames > buf->frames) frames = buf->frames; if (!blocking) { /* If we're not going to block, find out how many samples can be read */ unsigned int frames_ready = comedi_get_buffer_contents(spec->m_dev, spec->subdevice) / framesize; if (frames > frames_ready) frames = frames_ready; } /* Reserve a temporary buffer of the I/O device's type */ raw = (sampl_t *)alloca(frames*framesize); comedi_poll(spec->m_dev, spec->subdevice); //printf("Going to read %u bytes from fd %d, %d available... ", frames*framesize, comedi_fileno(spec->m_dev), comedi_get_buffer_contents(spec->m_dev, spec->subdevice)); fflush(stdout); /* Fill it with data */ bytes = read(comedi_fileno(spec->m_dev), raw, frames*framesize); //perror("read"); //printf("Got %d\n", bytes); /* Ensure the read completed */ if (bytes <= 0) return bytes; frames = bytes/framesize; /* Convert to bogio's native type and normalise. */ for (i = 0; i < frames * buf->spf ; i++) buf->samples[i] = (bogio_sample_t)comedi_to_phys(raw[i], spec->fsd[i], spec->m_max_sample[i]); return frames; }
double get_adc_volts(int chan, int diff, int range) { lsampl_t data[OVER_SAMPLE]; // adc burst data buffer int retval, intg, i; comedi_range *ad_range; comedi_insn daq_insn; ADC_ERROR = FALSE; // global fault flag if (diff == TRUE) { aref_ai = AREF_DIFF; } else { aref_ai = AREF_GROUND; } intg = 0; // clear the integration counter // configure the AI channel for burst reads of OVER_SAMPLE times with the passed params daq_insn.subdev = subdev_ai; daq_insn.chanspec = CR_PACK(chan, range, aref_ai); daq_insn.insn = INSN_READ; daq_insn.n = OVER_SAMPLE; daq_insn.data = data; retval = comedi_do_insn(it, &daq_insn); // send one instruction to the driver if (retval < 0) { comedi_perror("comedi_do_insn in get_adc_volts"); ADC_ERROR = TRUE; bmc.raw[chan] = 0; return 0.0; } for (i = 0; i < OVER_SAMPLE; i++) { intg += data[i]; } data[0] = intg >> OVER_SAMPLE_SHIFTS; ad_range = comedi_get_range(it, subdev_ai, chan, range); bmc.raw[chan] = data[0]; return comedi_to_phys(data[0], ad_range, ((1 << (OVER_SAMPLE_ADC + OVER_SAMPLE_BITS)) - 1)); // return the double result }
double ComediAnalogInputHardCal::read() { lsampl_t sample; comedi_data_read(m_device, m_subdevice, m_channels[0], m_range, m_aref, &sample); return comedi_to_phys(sample, m_dataRange, m_maxData) * m_inputConversionFactor; }
void print_datum(lsampl_t raw, int channel_index) { double physical_value; physical_value = comedi_to_phys(raw, range_info[channel_index], maxdata[channel_index]); printf("%#8.6g ",physical_value); }
/* -------------------------------------------------------------------- */ static long dsetRead_devAiSyncComedi(aiRecord *pai) { CONTEXT *p_myContext; int mySubdevice; int myChannel; int myRange = 0; int myAnalogReference; int myPinNumber; char p_myDeviceFile[BUFLEN]; comedi_t *p_myComediFileHandle; lsampl_t data; int maxdata; int ret; dsetLog(3, __FILE__ "[%d] -> %s(%s)\n", __LINE__, __func__, pai->name ); p_myContext = pai->dpvt; p_myComediFileHandle = p_myContext->p_comediFileHandle; myChannel = p_myContext->channel; myRange = p_myContext->range; myAnalogReference = p_myContext->analogReference; mySubdevice = p_myContext->subdevice; myPinNumber = p_myContext->pinNumber; strcpy(p_myDeviceFile, p_myContext->p_deviceFile); #if 0 dsetLog(7,__FILE__ "[%d] Subdevice = %d PinNumber =%d\n", __LINE__, mySubdevice, myPinNumber); dsetLog(7,__FILE__ "[%d] AnalogReference = %d Channel=%d\n", __LINE__, myAnalogReference, myChannel); dsetLog(7, __FILE__ "[%d] -> Range is %d\n", __LINE__, myRange); #endif maxdata = comedi_get_maxdata(p_myComediFileHandle, mySubdevice, myChannel); #if 0 dsetLog(7,__FILE__ "[%d] maxdata = %d pn=%d\n", __LINE__, maxdata, myPinNumber); dsetLog(7,__FILE__ "[%d] Data %d -> device %s\n", __LINE__, data, pai->name); dsetLog(7,__FILE__ "[%d] New Range = %lg\n", comedi_get_range(p_myComediFileHandle, mySubdevice, myChannel, myRange)); #endif ret = comedi_data_read(p_myComediFileHandle, mySubdevice, myChannel, myRange, myAnalogReference, &data); if (ret < 0) { comedi_perror(p_myDeviceFile); dsetLog(1, __FILE__ "[%d] Error: Couldn't read hardware\n", __LINE__); sleep(SLEEPTIME_ERROR); return(ERROR); } pai->val = comedi_to_phys(data, comedi_get_range(p_myComediFileHandle, mySubdevice, myChannel, myRange), maxdata); pai->udf = isnan(pai->val); dsetLog(7,__FILE__ "[%d] pai->udf is %d \n", __LINE__, pai->udf); if (pai->udf) { pai->val = 10; dsetLog(7,__FILE__ "[%d] Error: Cable is disconnected, please verify\n", __LINE__); } dsetLog(7,__FILE__ "[%d] val,rval=%lg,%d -> %s\n", __LINE__, pai->val, pai->rval, pai->name); dsetLog(3,__FILE__ "[%d] <- %s\n", __LINE__, __func__); return(NO_AUTOMATIC_CONVERSION); /*return(DO_AUTOMATIC_CONVERSION); */ }
/** * @brief read returns value in volts * @return value read from the input, softcalibrated and converted to physical value. */ double read() { return comedi_to_phys(read_sample(), range, maxdata); }
void ComediScope::paintEvent( QPaintEvent * ) { int ret; while (1) { // we need data in all the comedi devices for(int n=0;n<nComediDevices;n++) { if (!comedi_get_buffer_contents(dev[n],subdevice)) return; } for(int n=0;n<nComediDevices;n++) { int subdev_flags = comedi_get_subdevice_flags(dev[n], subdevice); int bytes_per_sample; if(subdev_flags & SDF_LSAMPL) { bytes_per_sample = sizeof(lsampl_t); } else { bytes_per_sample = sizeof(sampl_t); } unsigned char buffer[bytes_per_sample*channels_in_use]; ret = read(comedi_fileno(dev[n]), buffer, bytes_per_sample*channels_in_use); if (ret==0) { printf("BUG! No data in buffer.\n"); exit(1); } if (ret<0) { printf("\n\nError %d during read! Exiting.\n\n",ret); exit(1); } for(int i=0;i<channels_in_use;i++) { int sample; if (comediRecord->channel[n][i]->isActive()) { int ch = comediRecord->channel[n][i]->getChannel(); if(subdev_flags & SDF_LSAMPL) { sample = ((int)((lsampl_t *)buffer)[ch]); } else { sample = ((int)((sampl_t *)buffer)[ch]); } // store raw data daqData[n][i] = sample; // convert data to physical units for plotting float value = comedi_to_phys(sample, crange[n], maxdata[n]); // filtering value = comediRecord->dcSub[n][i]->filter(value); value = comediRecord->hp[n][i]->filter(value); value = comediRecord->lp[n][i]->filter(value); // remove 50Hz if (comediRecord->filterCheckbox->checkState()==Qt::Checked) { value=iirnotch[n][i]->filter(value); } if ((n==fftdevno) && (ch==fftch) && (comediRecord->fftscope)) comediRecord->fftscope->append(value); // average response if TB is slower than sampling rate adAvgBuffer[n][i] = adAvgBuffer[n][i] + value; } } } // save data if (comediRecord->recPushButton->checkState()==Qt::Checked) { writeFile(); } nsamples++; tb_counter--; // enough averaged? if (tb_counter<=0) { for(int n=0;n<nComediDevices;n++) { for(int i=0;i<channels_in_use;i++) { adAvgBuffer[n][i]=adAvgBuffer[n][i]/tb_init; } } // plot the stuff paintData(adAvgBuffer); // clear buffer tb_counter=tb_init; for(int n=0;n<nComediDevices;n++) { for(int i=0;i<channels_in_use;i++) { adAvgBuffer[n][i]=0; } } } } }
void MainWindow::timerEvent(QTimerEvent *) { unsigned char buffer[readSize]; while( comedi_get_buffer_contents(dev, COMEDI_SUB_DEVICE) > 0 ) { if( read(comedi_fileno(dev), buffer, readSize) == 0 ) { printf("Error: end of Aquisition\n"); exit(1); } int v; if( sigmaBoard ) v = ((lsampl_t *)buffer)[adChannel]; else v = ((sampl_t *)buffer)[adChannel]; double yNew = comedi_to_phys(v, crange, maxdata); if (filter50HzCheckBox->checkState()==Qt::Checked) { yNew=iirnotch->filter(yNew); } RawDataPlot->setNewData(yNew); int trialIndex = time % psthLength; if( linearAverage && psthOn ) { spikeCountData[trialIndex] += yNew; psthData[trialIndex] = spikeCountData[trialIndex] / (time/psthLength + 1); } else if( !spikeDetected && yNew>spikeThres ) { if(psthOn) { int psthIndex = trialIndex / psthBinw; spikeCountData[psthIndex] += 1; psthData[psthIndex] = ( spikeCountData[psthIndex]*1000 ) / ( psthBinw * (time/psthLength + 1) ); spikeDetected = true; } } else if( yNew < spikeThres ) { spikeDetected = false; } if( trialIndex == 0 ) psthActTrial += 1; ++time; } RawDataPlot->replot(); }
int main(int argc, char *argv[]) { comedi_cmd cmd; int err; int n, m, k; comedi_t *dev; unsigned int chanlist[100]; unsigned int maxdata; comedi_range *rng; int ret; struct parsed_options options; int fn = 256*256; sampl_t *data, *dp; float v; init_parsed_options(&options); options.subdevice = -1; options.n_chan = 100000;/* default number of samples */ parse_options(&options, argc, argv); dev = comedi_open( options.filename ); if(dev == NULL){ fprintf(stderr, "error opening %s\n", options.filename); return -1; } if(options.subdevice < 0) options.subdevice = comedi_find_subdevice_by_type(dev, COMEDI_SUBD_AI, 0); maxdata = comedi_get_maxdata(dev, options.subdevice, options.channel); rng = comedi_get_range(dev, options.subdevice, options.channel, options.range); memset(&cmd,0,sizeof(cmd)); /* cmd.subdev = options.subdevice; cmd.flags = 0; cmd.start_src = TRIG_INT; cmd.start_arg = 0; cmd.scan_begin_src = TRIG_TIMER; cmd.scan_begin_arg = 1e9 / options.freq; cmd.convert_src = TRIG_TIMER; cmd.convert_arg = 1e9 / options.freq / 50; cmd.scan_end_src = TRIG_COUNT; cmd.scan_end_arg = options.n_chan; cmd.stop_src = TRIG_COUNT; cmd.stop_arg = fn; cmd.stop_src = TRIG_NONE; cmd.stop_arg = 0; */ cmd.scan_begin_src = TRIG_TIMER; cmd.flags = TRIG_ROUND_NEAREST; err = comedi_get_cmd_generic_timed( dev, options.subdevice, &cmd, options.n_chan, 1e9 / options.freq ); cmd.start_src = TRIG_INT; cmd.start_arg = 0; cmd.scan_end_arg = options.n_chan; cmd.stop_src = TRIG_NONE; cmd.stop_arg = 0; cmd.chanlist = chanlist; cmd.chanlist_len = options.n_chan; for ( k=0; k<options.n_chan; k++ ) chanlist[k] = CR_PACK(k, options.range, options.aref); dump_cmd(stderr,&cmd); if ( cmd.scan_begin_arg > 1e9 / options.freq ) { fprintf( stderr, "frequency too high! Maximum possible is %g Hz\n", 1.0e9/cmd.scan_begin_arg ); comedi_close( dev ); return 1; } err = comedi_command_test(dev, &cmd); if (err > 0 && err != 4 ) { fprintf( stderr, "comedi_command_test returned %d\n", err ); dump_cmd(stdout,&cmd); exit(1); } err = comedi_command_test(dev, &cmd); if (err > 0 && err != 4 ) { fprintf( stderr, "comedi_command_test returned %d\n", err ); dump_cmd(stdout,&cmd); exit(1); } dump_cmd(stderr,&cmd); /* init data buffer: */ data = (sampl_t *)malloc( fn*sizeof( sampl_t ) ); if(data == NULL ){ perror("malloc\n"); exit(1); } fprintf( stderr, "execute command ...\n" ); if ((err = comedi_command(dev, &cmd)) < 0) { comedi_perror("comedi_command"); exit(1); } fprintf( stderr, "start analog input ...\n" ); ret = comedi_internal_trigger(dev, options.subdevice, 0); if(ret < 0){ perror("comedi_internal_trigger\n"); exit(1); } n = 0; while( 1 ) { m = read(comedi_fileno(dev),(void *)data,fn*sizeof( sampl_t)); if(m<0){ if ( errno != EAGAIN ) { perror("read"); exit(0); } else { fprintf( stderr, "... no more data can be read! Try later.\n" ); usleep( 100000 ); } } else { m /= sizeof( sampl_t); fprintf( stderr, "read %d samples\n",m); n+=m; } } comedi_cancel( dev, cmd.subdev ); fprintf( stderr, "finished\n" ); /* save data: */ dp = data; for ( k=0; k<n; k++ ) { v = comedi_to_phys(*dp, rng, maxdata); printf( "%g\n", v ); /* printf( "%d\n", *dp );*/ ++dp; } free( data ); comedi_close( dev ); return 0; }