Пример #1
0
int main(int argc, char *argv[])
{
	struct sigaction sa;
	int ret;
	sigset_t sigset;
	int flags;
	struct parsed_options options;

	init_parsed_options(&options);
	parse_options(&options, argc, argv);

	device = comedi_open(options.filename);
	if(!device){
		perror(options.filename);
		exit(1);
	}

	out_subd = 2;

	config_output();

	fcntl(comedi_fileno(device), F_SETOWN, getpid());
	flags = fcntl(comedi_fileno(device),F_GETFL);
	ret = fcntl(comedi_fileno(device),F_SETFL,flags|O_ASYNC);
	//ret = fcntl(comedi_fileno(device),F_SETFL,O_NONBLOCK|O_ASYNC);
	if(ret<0)perror("fcntl");

	memset(&sa,0,sizeof(sa));
	sa.sa_handler = &sigio_handler;
	ret = sigaction(SIGIO,&sa,NULL);
	if(ret<0)perror("sigaction");

	sigemptyset(&sigset);
	sigaddset(&sigset,SIGIO);
	ret = sigprocmask(SIG_UNBLOCK,&sigset,NULL);
	if(ret<0)perror("sigprocmask");

#if 0
	{
	struct sched_param p;
	memset(&p,0,sizeof(p));
	p.sched_priority = 1;
	ret = sched_setscheduler(0,SCHED_FIFO,&p);
	if(ret<0)perror("sched_setscheduler");
	}
#endif

	do_cmd_1(device, options.subdevice);

	return 0;
}
Пример #2
0
void do_cmd(comedi_t *dev,comedi_cmd *cmd)
{
	int total=0;
	int ret;
	int go;

	ret=comedi_command_test(dev,cmd);

	printf("test ret=%d\n",ret);
	if(ret<0){
		printf("errno=%d\n",errno);
		comedi_perror("comedi_command_test");
		return;
	}

	dump_cmd(stdout,cmd);

	ret=comedi_command_test(dev,cmd);

	printf("test ret=%d\n",ret);
	if(ret<0){
		printf("errno=%d\n",errno);
		comedi_perror("comedi_command_test");
		return;
	}

	dump_cmd(stdout,cmd);

	ret=comedi_command(dev,cmd);

	printf("ret=%d\n",ret);
	if(ret<0){
		printf("errno=%d\n",errno);
		comedi_perror("comedi_command");
		return;
	}

	go=1;
	while(go){
		ret=read(comedi_fileno(dev),buf,BUFSZ);
		if(ret<0){
			if(errno==EAGAIN){
				printf("EAGAIN\n");
				usleep(10000);
			}else{
				go = 0;
				perror("read");
			}
		}else if(ret==0){
			go = 0;
		}else{
			//int i;

			total+=ret;
			//printf("read %d %d\n",ret,total);
			//printf("count = %d\n",count);
			//print_time();
		}
	}
}
Пример #3
0
int main(int argc, char *argv[])
{
	comedi_cmd cmd;
	int i;
	struct parsed_options options;

	init_parsed_options(&options);
	parse_options(&options, argc, argv);

	device = comedi_open(options.filename);
	if(!device){
		perror(options.filename);
		exit(1);
	}

	fcntl(comedi_fileno(device), F_SETFL, O_NONBLOCK);

	for(i = 0; i < n_chans; i++){
		chanlist[i] = CR_PACK(options.channel + i, options.range, options.aref);
	}

	prepare_cmd(device, &cmd, options.subdevice);

	do_cmd(device, &cmd);

	return 0;
}
Пример #4
0
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;
}
Пример #5
0
int laytube_toFile(tubeID *pTube){

    int file_flags[2];


    /* Check COMEDI file descriptor */
    pTube->mouth = comedi_fileno(pTube->dev);

    file_flags[0] = fcntl(pTube->mouth, F_GETFD);

    // Is fd open? ...
    if (file_flags[0] == EBADF){
        return(-1);
    }


    // Check destition file descriptor
    pTube->tail = pTube->dest;

    file_flags[1] = fcntl(pTube->tail, F_GETFD);

    // Is fd open? ...
    if (file_flags[1] == EBADF){
        return(-2);
    }

    /* Create new thread instance of data copying process */
    pthread_create(&(pTube->threadNo), NULL, _laytube_toFile, (void *)pTube );

    /* Block laytube_toFile from returning until new pTube is setup or failes */
    while ( !(pTube->tubeStatus & TUBE_INPLACE) || (pTube->tubeStatus & TUBE_FAILED) ){
        usleep(100);  /* 1/10 sec */
    }

    if (pTube->tubeStatus & TUBE_FAILED){
        return(-3);
    }else{
        return(0);
    }


}
Пример #6
0
int main(int argc, char *argv[])
{
	int ret;
	comedi_cmd cmd;
	struct parsed_options options;

	init_parsed_options(&options);
	parse_options(&options, argc, argv);

	device = comedi_open(options.filename);
	if(!device){
		perror(options.filename);
		exit(1);
	}

	out_subd = 0;

	config_output();

	ret = fcntl(comedi_fileno(device),F_SETFL,O_NONBLOCK|O_ASYNC);
	if(ret<0)perror("fcntl");

#if 0
	{
	struct sched_param p;

	memset(&p,0,sizeof(p));
	p.sched_priority = 1;
	ret = sched_setscheduler(0,SCHED_FIFO,&p);
	if(ret<0)perror("sched_setscheduler");
	}
#endif

	prepare_cmd(device, &cmd, options.subdevice);

	do_cmd(device,&cmd);

	return 0;
}
Пример #7
0
		/**
		 * Start the the ADC sampling, unless the external trigger command
		 * is set it will begin sampling as soon as the usbdux device has been
		 * initialized.
		 * The final three arguments are very important to get right
		 * as the recorder thread will use them to determine where to write it's samples and
		 * when it's reached end of the buffer. It doesn't care about the c_buffer class
		 * at all.
		 * @param arg_device name of the comedi device e.g., 'comedi0'
		 * @param arg_sample_rate the rate the usb_dux should sample with in hertz (max=3[Mhz]})
		 * @param arg_start_address the start address of the circle buffer sample should be loading into
		 * @param arg_end_address the end address of the cicle buffer.
		 * @param arg_buffer_size byte size of the buffer.
		 */
		int start_Sampling(char* arg_device, uint32_t arg_sample_rate, 
				char* arg_start_address, char* arg_end_address, int arg_buffer_size){

			_channel_amount = 16;
			_sample_rate = arg_sample_rate;

			unsigned int chanlist[_channel_amount];
			unsigned int convert_arg = 1e9 / _sample_rate;
			int ret;

			comedi_cmd *cmd = (comedi_cmd *) calloc(1, sizeof(comedi_cmd));
			_device = comedi_open(arg_device);


			int buffer = 1048576*40;
			buffer = (buffer * 4096)/4096;

			if(comedi_set_max_buffer_size(_device, 0, buffer) < 0 ){
				printf("Failed to set max buffer size to %i bytes\n", buffer);
				return -1;
			} else printf("Maximum buffer size set to %i bytes\n", comedi_get_max_buffer_size(_device,0));

			if(comedi_set_buffer_size(_device, 0, buffer) < 0){
				printf("Failed to set buffer size to %iBytes\n", buffer);
				return -1;
			} else printf("Buffer size set to %iBytes\n", comedi_get_buffer_size(_device,0));

			if(!_device){
				errx(1, "unable to open device");
				comedi_perror("/dev/comedi0");
				return -1;
			}

			for(int i = 0; i < _channel_amount; i++)	
				chanlist[i] = i;

			comedi_set_global_oor_behavior(COMEDI_OOR_NUMBER);

			if((ret = comedi_get_cmd_generic_timed(_device, 0, cmd, _channel_amount,0)) < 0){
				printf("comedi_get_cmd_generic_timed failed\n");
				return ret;
			}

			cmd->chanlist 	= chanlist;
			cmd->stop_src	= TRIG_NONE;
			cmd->stop_arg	= 0;
			cmd->convert_arg = convert_arg;
		
			//setup the sampling to start from the trigger.
			//cmd->start_src = TRIG_EXT 
			//cmd->start_arg = 1;


			/* call test twice because different things are tested?
			 * if tests are successful run sampling command */
			if((ret = comedi_command_test(_device, cmd)) != 0
					|| (ret = comedi_command_test(_device, cmd)) != 0
					|| (ret = comedi_command(_device, cmd)) < 0){

				fprintf(stderr, "err: %d\n", ret);
				return -1;
			}

			FILE* dux_fp;

			if((dux_fp = fdopen(comedi_fileno(_device), "r")) <= 0)
				comedi_perror("fdopen");

			char* write_address = arg_start_address;
			
			Utility::SNAP_SAMPLE = 0;
			Utility::LAST_SAMPLE = 0;

			uint64_t active_sample = 0;
			uint32_t tmp_block = 0;
			int samples_pr_block = 4096/sizeof(Type);

		
			while((ret = fread(write_address, 1, 4096,dux_fp)) >= 0){

				write_address += 4096;
				tmp_block += 1;

				active_sample += samples_pr_block;
				Utility::LAST_SAMPLE += samples_pr_block;

				if(tmp_block >= Utility::SNAPSHOT_BLOCK_SIZE){
					//printf("signalling serial snapshotter\n");
					tmp_block = 0;
					Utility::SNAP_SAMPLE = active_sample;
					Utility::SNAP_READY = true;
					Utility::CV.notify_one();
				}

				if(write_address == arg_end_address){
					write_address -= arg_buffer_size;
					//printf("resetting to beginning of buffer\n");
				}

				if(_stop == true)
					break;
			}

			if(ret < 0)
				perror("read");

			comedi_cancel(_device, 0);
			return 0;
		}
Пример #8
0
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;
				}
			}
		}
	}
}
Пример #9
0
int test_mmap(void)
{
	comedi_cmd cmd;
	unsigned char *buf;
	unsigned int chanlist[1];
	int go;
	int fails;
	int total=0;
	int ret;
	void *b;
	unsigned char *adr;
	unsigned char *map;
	unsigned int flags;
	int i;
	unsigned sample_size;
	unsigned map_len;

	flags = comedi_get_subdevice_flags(device,subdevice);

	/* attempt to make subdevice the current 'read' subdevice */
	if(flags&SDF_CMD_READ) comedi_set_read_subdevice(device,subdevice);
	if(!(flags&SDF_CMD) || (comedi_get_read_subdevice(device)!=subdevice)){
		printf("not applicable\n");
		return 0;
	}
	if(flags & SDF_LSAMPL) sample_size = sizeof(lsampl_t);
	else sample_size = sizeof(sampl_t);
	map_len = sample_size * N_SAMPLES;

	if(comedi_get_cmd_generic_timed(device, subdevice, &cmd, 1, 1)<0){
		printf("E: comedi_get_cmd_generic_timed failed\n");
		return 0;
	}

	buf=malloc(sample_size * N_SAMPLES);

	map = mmap(NULL, map_len,PROT_READ, MAP_SHARED, comedi_fileno(device),0);
	if(!map){
		printf("E: mmap() failed\n");
		return 0;
	}

	/* test readability */
	for(adr = map; adr < map + map_len; adr += PAGE_SIZE){
		ret=test_segfault(adr);
		if(ret){
			printf("E: %p failed\n",adr);
		}else{
			printf("%p ok\n",adr);
		}
	}

	if(realtime)cmd.flags |= TRIG_RT;

	cmd.chanlist = chanlist;
	cmd.scan_end_arg = 1;
	cmd.stop_arg = N_SAMPLES;
	cmd.chanlist_len = 1;
	chanlist[0] = CR_PACK(0,0,0);

	comedi_command(device,&cmd);

	go=1;
	b=buf;
	while(go){
		ret = read(comedi_fileno(device), b,
				(N_SAMPLES * sample_size) - total);
		if(ret<0){
			if(errno==EAGAIN){
				usleep(10000);
			}else{
				go = 0;
				perror("read");
			}
		}else if(ret==0){
			go = 0;
		}else{
			total += ret;
			b += ret;
			if(verbose) printf("read %d %d\n",ret,total);
			if(total >= (N_SAMPLES * sample_size)){
				go = 0;
			}
		}
	}

	fails = 0;
	for(i=0;i<total;i++){
		if(buf[i]!=map[i]){
			if(fails==0)printf("E: mmap compare failed\n");
			printf("offset %d (read=%02x mmap=%02x)\n",i,
				buf[i], map[i]);
			fails++;
			if(fails>10)break;
		}
	}
	if(fails==0) printf("compare ok\n");

	munmap(map, map_len);

	/* test if area is really unmapped */
	for(adr = map; adr < map + map_len; adr += PAGE_SIZE){
		ret=test_segfault(adr);
		if(ret){
			printf("%p segfaulted (ok)\n",adr);
		}else{
			printf("E: %p still mapped\n",adr);
		}
	}

	free(buf);

	return 0;
}
Пример #10
0
static int openBoard( int board ) {
    MY_BOARD_HARDWARE *pBoardHard = myHardwareBoardList + board;
    MY_BOARD_SOFTWARE *pBoardSoft = mySoftwareBoardList + board;

    fprintf( stderr, "openBoard[%d]\n", board );
    fprintf( stderr, "sizeof(lsampl_t)=%d\n", sizeof(lsampl_t) );
    fprintf( stderr, "sizeof(sampl_t)=%d\n", sizeof(sampl_t) );

    if (pBoardHard == NULL || pBoardSoft == NULL) return 1;

    pBoardHard->p_comediFileHandle = comedi_open( pBoardHard->devicePath );

    if (pBoardHard->p_comediFileHandle == NULL) {
        fprintf( stderr, "comedi_open failed for board[%d] %s\n", board, pBoardHard->devicePath );
        return 1;
    }
    fprintf( stderr, "opened file\n" );
    
    pBoardHard->subDeviceNumber =
    comedi_find_subdevice_by_type(
        pBoardHard->p_comediFileHandle,
        COMEDI_SUBD_AI,
        0
    );
    
    if (pBoardHard->subDeviceNumber < 0) {
        fprintf( stderr, "subDeviceNum < 0 failed for board[%d]\n", board );
        return 1;
    }
    fprintf( stderr, "get subDevice=%d\n", pBoardHard->subDeviceNumber );

    int subDevFlags = comedi_get_subdevice_flags(
        pBoardHard->p_comediFileHandle,
        pBoardHard->subDeviceNumber
    );
    if (subDevFlags & SDF_PACKED) {
        fprintf( stderr, "subDevice using bit per channel\n" );
    } else {
        if (subDevFlags & SDF_LSAMPL) {
            pBoardHard->valueIsLsampl_t = 1;
            fprintf( stderr, "subDevice using long word lsampl_t per channel\n" );
        } else {
            fprintf( stderr, "subDevice using word sampl_t per channel\n" );
        }
    }

    pBoardHard->buffer_size_in_byte =
    comedi_get_buffer_size(
        pBoardHard->p_comediFileHandle,
        pBoardHard->subDeviceNumber
    );
    fprintf( stderr, "board[%d] hardware buffer size in BYTES=%d\n",
    board, pBoardHard->buffer_size_in_byte );
    if (pBoardHard->buffer_size_in_byte <= 0) {
        fprintf( stderr, "board buffer size faile\n" );
        return 1;
    }

    if (pBoardHard->valueIsLsampl_t) {
        pBoardHard->buffer_size_in_sample = pBoardHard->buffer_size_in_byte / sizeof(lsampl_t);
    } else {
        pBoardHard->buffer_size_in_sample = pBoardHard->buffer_size_in_byte / sizeof(sampl_t);
    }

    pBoardHard->buffer = mmap(
        NULL,
        pBoardHard->buffer_size_in_byte,
        PROT_READ,
        MAP_SHARED,
        comedi_fileno(pBoardHard->p_comediFileHandle),
        0
    );
    if (pBoardHard->buffer == MAP_FAILED) {
        fprintf( stderr, "mmap failed for board[%d]\n", board );
        return 1;
    }

    int numChannel = comedi_get_n_channels(
        pBoardHard->p_comediFileHandle,
        pBoardHard->subDeviceNumber
    );
    if (numChannel != pBoardSoft->numberOfActiveAnalogInputs) {
        fprintf( stderr, "number of channel for board[%d] from comedi=%d harccode=%d\n",
        board, numChannel, pBoardSoft->numberOfActiveAnalogInputs );
    }

#ifdef SUPPORT_CHANNEL_SPECIFIC_RANGE
    pBoardHard->maxDataIsChannelSpecific =
    comedi_maxdata_is_chan_specific(
        pBoardHard->p_comediFileHandle,
        pBoardHard->subDeviceNumber
    );
    pBoardHard->rangeIsChannelSpecific =
    comedi_maxdata_is_chan_specific(
        pBoardHard->p_comediFileHandle,
        pBoardHard->subDeviceNumber
    );
    if (pBoardHard->maxDataIsChannelSpecific) {
        fprintf( stderr, "board[%d] supports different max data per channel\n", board );
    }
    if (pBoardHard->rangeIsChannelSpecific) {
        fprintf( stderr, "board[%d] supports different range per channel\n", board );
    }
#endif

    return 0;
}
Пример #11
0
int main(int argc, char *argv[])
{
	comedi_cmd c,*cmd=&c;
	struct header_info header;
	long int t_samples, t_bytes, t_sleep;
	long int ret;
	int dt_usec, usec_elapsed;
//	struct timeval start,end;
	struct parsed_options options;
	unsigned short *samples; //, *junk;
	struct timeval now,then;
	long long unsigned int exec = 0;
	time_t t;

	umask(000);

	signal(SIGINT,do_depart);

	if (geteuid() != 0) {
		fprintf(stderr,"Must be setuid root to renice self.\n");
		exit(0);
	}

	cmd_init_parsed_options(&options);
	cmd_parse_options(&options, argc, argv);

	t_samples = options.n_chan * options.samples;
	t_bytes = t_samples * 2;
	t_sleep = (long int) DMA_OVERSCAN_MULT*(1e6/options.freq)*options.samples;
	printf("freq = %f, tsamp = %li, tby = %li, tsleep = %li.\n",options.freq,t_samples,t_bytes,t_sleep);


	/* Test for failful options */
	if (options.freq <= 1/(options.cadence + 0.00005)) {
		fprintf(stderr,"Acquisition frequency is faster than sampling frequency!  FAIL!\n");
		exit(ACQERR_BADINPUT);
	}

	if (options.samples > 16777216) {
		fprintf(stderr,"acq_d can't take more than 2^24 samples per set!  Try acq_c.\n");
		exit(ACQERR_BADINPUT);
	}

	ret = nice(-20);
	fprintf(stderr,"I've been niced to %li.\n",ret);
	if (ret != -20) printf("  WARNING!! Data collection could not set nice=-20. Data loss is probable at high speed.");

	dt_usec = options.cadence * 1e6;
	gettimeofday(&then, NULL);

	/* open the device */
	dev = comedi_open(options.devfile);
	if (!dev) {
		comedi_perror(options.devfile);
		exit(ACQERR_BADDEV);
	}

//	printf("%li samples in %i-byte buffer.\n\n",t_samples,comedi_get_buffer_size(dev,options.subdevice));

	prepare_cmd_lib(dev,options,cmd);

	printf("Testing command...");
	ret = comedi_command_test(dev, cmd);
	if (ret < 0) {
		comedi_perror("comedi_command_test");
		if(errno == EIO){
			fprintf(stderr,"Ummm... this subdevice doesn't support commands\n");
		}
		exit(ACQERR_BADCMD);
	}
	printf("%s...",cmdtest_messages[ret]);
	ret = comedi_command_test(dev, cmd);
	printf("%s...",cmdtest_messages[ret]);
	ret = comedi_command_test(dev, cmd);
	printf("%s...\n",cmdtest_messages[ret]);
	if (ret < 0) {
		fprintf(stderr,"Bad command, and unable to fix.\n");
		dump_cmd(stderr, cmd);
		exit(ACQERR_BADCMD);
	}
	dump_cmd(stdout,cmd);
	comedi_close(dev);

	//fprintf(stderr,"%i scan -- %i chan -- %li samples -- %li bytes\n",options.samples,options.n_chan,t_samples,t_bytes);

	/* Print data file header */
//	sprintf(dataheader,	"[system]\n"
//				"whoami = \"%s\"")

	samples = (unsigned short*) malloc(t_bytes);
//	junk = (unsigned short*) malloc(t_bytes);
	if ((samples == NULL)) { // | (junk == NULL)) {
		fprintf(stderr,"Error allocating sample memory!\n");
		exit(ACQERR_BADMEM);
	}

	printf("Starting acquisition...\n");
	/*Open the serial port*/
	/*serial = (FILE*)fopen("/dev/ttyS0","w");*/
	

	/* Do what we're here to do */
	while (running) {
		if (exec == options.sets) {
			break;
		}
		exec++;

       serial_toggle();

		/* open the device */
		dev = comedi_open(options.devfile);
		if (!dev) {
			comedi_perror(options.devfile);
			exit(ACQERR_BADDEV);
		}

		gettimeofday(&now, NULL);
		usec_elapsed = (now.tv_sec - then.tv_sec) * 1e6 + (now.tv_usec - then.tv_usec);
		while (usec_elapsed < dt_usec) {
			usleep(USLEEP_GRAN);
			gettimeofday(&now, NULL);
			usec_elapsed = (now.tv_sec - then.tv_sec) * 1e6 + (now.tv_usec - then.tv_usec);
		}
		/* start the command */
		ret = comedi_command(dev, cmd);
		if (ret < 0) {
			comedi_perror("comedi_command");
			exit(ACQERR_BADCMD);
		}

		then = now;

		/*
		 * Insert enough waiting to get through most of the sampling
		 */
		/*serial_toggle();*/
		usleep(t_sleep);

		/*
		 * Wait until the buffer has our data.
		 */

		int exsleeps = 0;
		do {
			usleep(USLEEP_GRAN);
			comedi_poll(dev,options.subdevice);
			ret = comedi_get_buffer_contents(dev,options.subdevice);
			exsleeps++;
		} while(ret < t_bytes);

//		printf("Waited %ius for additional data.\n",exsleeps);

		/*
		 * Get teh dataz.
		 */
		ret = read(comedi_fileno(dev), samples, t_bytes);
		if (ret < t_bytes) {
			fprintf(stderr,"Read mismatch!  Got %li of %li bytes (%li/%li samples).\n",ret,t_bytes,ret/2,t_samples);
		}
		header.num_read = ret/2;

		// collect & discard overscan
/*		ret = comedi_get_buffer_contents(dev,options.subdevice);
		if (realloc(junk,ret) == NULL) {
			fprintf(stderr,"Error in oversample realloc?\n");
			exit(ACQERR_BADMEM);
		}
		if (ret > 0) {
			ret = read(comedi_fileno(dev),junk,ret);
		}*/

		comedi_close(dev);

		// Prepare header
		t = time(NULL);
		sprintf(header.site_id,"%s",site_str);
		header.start_time = t;
		header.start_timeval = now;
		header.num_channels=options.n_chan;
		header.channel_flags=0x0F;
		header.num_samples=options.samples;
		header.sample_frequency=options.freq;
		header.time_between_acquisitions=options.cadence;
		header.byte_packing=0;
		header.code_version=current_code_version;

		// Save latest to monitor file
		if (strcmp(options.monfile,"") != 0) {
			out = open(options.monfile,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
			ret = errno;
			if (out > 0) {
				ret = write(out,&header,sizeof(header));
				ret = write(out,samples,2*header.num_read);
				close(out);
			}
			else {
				fprintf(stderr,"Unable to open monitor file %s: %s.\n",options.monfile,strerror(ret));
				exit(ACQERR_BADOUT);
			}
		}

		if (options.cadence >= 1.0 && options.verbose) printf("\tSaved latest to: %s\n",options.monfile);

		// Append to output file
		if (strcmp(options.outfile,"") != 0) {
			out = open(options.outfile,O_WRONLY|O_APPEND|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
			ret = errno;
			if (out > 0) {
				ret = write(out,samples,2*header.num_read);
				close(out);
				if (options.cadence >= 1.0 && options.verbose) printf("\tAppended to: %s\n",options.outfile);
			}
			else {
				fprintf(stderr,"Unable to open output file %s: %s.\n",options.outfile,strerror(ret));
				exit(ACQERR_BADOUT);
			}
		}
	}

	if (dev != NULL)
		comedi_close(dev);
	if (out != 0)
		close(out);

	printf("\nEnd.\n");

	exit(0);
}
Пример #12
0
static PyObject*
comediModule(PyObject* self, PyObject* args)
{
	char *device;
	int subdevice;
	int channel;
	int range;
	int aref;
	int n_chan;
	int n_scan;
	float freq;
    if (!PyArg_ParseTuple(args, "siiiiiif", &device, &subdevice, &channel, &range, &aref, &n_chan, &n_scan, &freq))
        return NULL;
    
	comedi_t *dev;
	comedi_cmd c,*cmd=&c;
	int ret;
	int total=0;
	int i;
	struct timeval start,end;
	int subdev_flags;
	lsampl_t raw;
	struct parsed_options options;

	init_parsed_options(&options);
	options.filename = device;
	options.subdevice = subdevice;
	options.channel = channel;
	options.range = range;
	options.aref = aref;
	options.n_chan = n_chan;
	options.n_scan = n_scan;
	options.freq = freq;

	/* open the device */
	dev = comedi_open(options.filename);
	if(!dev){
		comedi_perror(options.filename);
		exit(1);
	}

	// Print numbers for clipped inputs
	comedi_set_global_oor_behavior(COMEDI_OOR_NUMBER);

	/* Set up channel list */
	for(i = 0; i < options.n_chan; i++){
		chanlist[i] = CR_PACK(options.channel + i, options.range, options.aref);
		range_info[i] = comedi_get_range(dev, options.subdevice, options.channel, options.range);
		maxdata[i] = comedi_get_maxdata(dev, options.subdevice, options.channel);
	}
	prepare_cmd_lib(dev, options.subdevice, options.n_scan, options.n_chan, 1e9 / options.freq, cmd);

	fprintf(stderr, "command before testing:\n");
	dump_cmd(stderr, cmd);

	ret = comedi_command_test(dev, cmd);
	if(ret < 0){
		comedi_perror("comedi_command_test");
		if(errno == EIO){
			fprintf(stderr,"Ummm... this subdevice doesn't support commands\n");
		}
		exit(1);
	}
	fprintf(stderr,"first test returned %d (%s)\n", ret,
			cmdtest_messages[ret]);
	dump_cmd(stderr, cmd);

	ret = comedi_command_test(dev, cmd);
	if(ret < 0){
		comedi_perror("comedi_command_test");
		exit(1);
	}
	fprintf(stderr,"second test returned %d (%s)\n", ret,
			cmdtest_messages[ret]);
	if(ret!=0){
		dump_cmd(stderr, cmd);
		fprintf(stderr, "Error preparing command\n");
		exit(1);
	}

	/* this is only for informational purposes */
	gettimeofday(&start, NULL);
	fprintf(stderr,"start time: %ld.%06ld\n", start.tv_sec, start.tv_usec);

	/* start the command */
	ret = comedi_command(dev, cmd);
	if(ret < 0){
		comedi_perror("comedi_command");
		exit(1);
	}
	subdev_flags = comedi_get_subdevice_flags(dev, options.subdevice);

	const int ndim = 2;
	npy_intp nd[2] = {n_scan, n_chan};
	
	PyObject *myarray;
	double *array_buffer;
	myarray = PyArray_SimpleNew(ndim, nd, NPY_DOUBLE);
	Py_INCREF(myarray);
	array_buffer = (double *)PyArray_DATA(myarray);
	
	while(1){
		ret = read(comedi_fileno(dev),buf,BUFSZ);
		if(ret < 0){
			/* some error occurred */
			perror("read");
			break;
		}else if(ret == 0){
			/* reached stop condition */
			break;
		}else{
			static int col = 0;
			double j = 0.0;
			
			int bytes_per_sample;
			total += ret;
			if(options.verbose)fprintf(stderr, "read %d %d\n", ret, total);
			if(subdev_flags & SDF_LSAMPL)
				bytes_per_sample = sizeof(lsampl_t);
			else
				bytes_per_sample = sizeof(sampl_t);
			for(i = 0; i < ret / bytes_per_sample; i++){
				if(subdev_flags & SDF_LSAMPL) {
					raw = ((lsampl_t *)buf)[i];
				} else {
					raw = ((sampl_t *)buf)[i];
				}
				*array_buffer++ = print_datum(raw, col, 1);
				col++;
				if(col == options.n_chan){
					col=0;
					j++;
					printf("\n");
				}
			}
		}
	}

	/* this is only for informational purposes */
	gettimeofday(&end,NULL);
	fprintf(stderr,"end time: %ld.%06ld\n", end.tv_sec, end.tv_usec);

	end.tv_sec -= start.tv_sec;
	if(end.tv_usec < start.tv_usec){
		end.tv_sec--;
		end.tv_usec += 1000000;
	}
	end.tv_usec -= start.tv_usec;
	fprintf(stderr,"time: %ld.%06ld\n", end.tv_sec, end.tv_usec);
    
	return myarray;
}
Пример #13
0
int PLLReferenceGeneration()
 {
    //Initial test function to try out Real time stuff.
    int m, i=0, err, n;
    lsampl_t data_to_card;
    static comedi_t * dev;
    static int OutputFIFOBufferSize;
    static int PLLGenerationBufferSize;
	unsigned int maxdata;
	unsigned int chanlist[16];
	int ret;
    static lsampl_t data[PLLGenerationBufferNPoints]; //this is the buffer used to send data points out
    comedi_cmd cmd;

    dev = comedi_open(device_names[PLLReferenceGenerationChannel.board_number]);

    //Check the size of the output buffer
    OutputFIFOBufferSize = comedi_get_buffer_size(dev, PLLReferenceGenerationChannel.subdevice);
    rt_printk("OutputFIFO Buffer size is %i\n", OutputFIFOBufferSize);

    //Set the actual buffer size that we will be using to half this and the number of data points to one fourth
    //Now configure the output channel using a Comedi instruction
    //BufferSize is initially set to be double the number of PLLGenerationBufferNPoints
    PLLGenerationBufferSize = 2*PLLGenerationBufferNPoints;


	maxdata = comedi_get_maxdata(dev, PLLReferenceGenerationChannel.subdevice, PLLReferenceGenerationChannel.channel);
	rt_printk("PLL Reference channel max data is %i\n", maxdata);

	offset = maxdata / 2;
	amplitude = maxdata - offset;

	memset(&cmd,0,sizeof(cmd));
	cmd.subdev = PLLReferenceGenerationChannel.subdevice;
	cmd.flags = CMDF_WRITE;
	cmd.start_src = TRIG_INT;
	cmd.start_arg = 0;
	cmd.scan_begin_src = TRIG_TIMER;
	cmd.scan_begin_arg = PLLGenMinPulseTime;  //minimum update time for the
	cmd.convert_src = TRIG_NOW;
	cmd.convert_arg = 0;
	cmd.scan_end_src = TRIG_COUNT;
	cmd.scan_end_arg = NCHAN; //only one channel
	cmd.stop_src = TRIG_NONE;
	cmd.stop_arg = 0;

	cmd.chanlist = chanlist;
	cmd.chanlist_len = NCHAN;

	chanlist[0] = CR_PACK(PLLReferenceGenerationChannel.channel, AO_RANGE, AREF_GROUND);

	dds_init(PLLWaveformFrequency, PLLUpdateFrequency);

	err = comedi_command_test(dev, &cmd);
	if (err < 0) {
		comedi_perror("comedi_command_test");
		exit(1);
	}

	err = comedi_command_test(dev, &cmd);
	if (err < 0) {
		comedi_perror("comedi_command_test");
		exit(1);
	}

	if ((err = comedi_command(dev, &cmd)) < 0) {
		comedi_perror("comedi_command");
		exit(1);
	}

	dds_output(data,PLLGenerationBufferNPoints);
	n = PLLGenerationBufferNPoints * sizeof(sampl_t);
	m = write(comedi_fileno(dev), (void *)data, n);
	if(m < 0){
		perror("write");
		exit(1);
	}else if(m < n)
	{
		fprintf(stderr, "failed to preload output buffer with %i bytes, is it too small?\n"
			"See the --write-buffer option of comedi_config\n", n);
		exit(1);
	}

    if(!(PLLRefGen_Task = rt_task_init_schmod(nam2num( "PLLReferenceGeneration" ), // Name
                                        2, // Priority
                                        0, // Stack Size
                                        0, //, // max_msg_size
                                        SCHED_FIFO, // Policy
                                        CPUMAP ))) // cpus_allowed
        {
            printf("ERROR: Cannot initialize pll reference generation task\n");
            exit(1);
        }

    //specify that this is to run on one CPU
    rt_set_runnable_on_cpuid(PLLRefGen_Task, 1);
    //Convert samp_time, which is in nanoseconds, to tick time
    //sampling_interval = nano2count(SAMP_TIME);  //Converts a value from
                                                //nanoseconds to internal count units.
    mlockall(MCL_CURRENT|MCL_FUTURE);
    rt_make_hard_real_time();
    PLLUpdateTime = nano2count(PLLGenerationLoopTime);
    rt_printk("PLL generation update time is %f12 \n",count2nano((float) PLLUpdateTime));
    // Let's make this task periodic..
    expected = rt_get_time() + 100*PLLUpdateTime;



    rt_task_make_periodic(PLLRefGen_Task, expected, PLLUpdateTime); //period in counts
    //rt_task_resume(Sinewaveloop_Task);
    PLLGenerationOn = TRUE;



    // Concurrent function Loop


     //rt_printk("SineWaveAmplitude is is %f \n",SineWaveAmplitude);
     //rt_printk("SineWaveFrequency is %f \n",SineWaveFrequency);
     //rt_printk("sine_loop_running is %d \n",sine_loop_running);
     //rt_printk("SAMP_TIME is %d \n",SAMP_TIME);
     start_time = (float)rt_get_time_ns()/1E9; //in seconds
     old_time = start_time;
     rt_printk("PLLReferenceGenerationChannel board_it is %p \n",PLLReferenceGenerationChannel.board_id);
     rt_printk("PLLReferenceGenerationChannel devicename is %p \n",*(PLLReferenceGenerationChannel.devicename));
     rt_printk("PLLReferenceGenerationChannel boardname is %p \n",*(PLLReferenceGenerationChannel.boardname));
     rt_printk("PLLReferenceGenerationChannel subdevice is %d \n",PLLReferenceGenerationChannel.subdevice);
     rt_printk("PLLReferenceGenerationChannel channel is %d \n",PLLReferenceGenerationChannel.channel);
     OutputValue = 1;
     PLLGenerationBufferSize = comedi_get_buffer_size(dev, PLLReferenceGenerationChannel.subdevice);
     //sine_loop_running = 0;  //set this to 0 for testing
     while(PLLGenerationOn)
     {
        i++; // Count Loops.
        current_time = (float)rt_get_time_ns()/1E9;
        //rt_printk("LOOP %d,-- Period time: %f12 %f12\n",i, current_time - old_time,count2nano((float)sampling_interval)/1E9);
        OutputValue = SineWaveAmplitude*sin(2*PI*SineWaveFrequency*(current_time-start_time));
        //OutputValue = -1*OutputValue;
        //rt_printk("OutputValue is %f12 \n",OutputValue);
        data_to_card = (lsampl_t) nearbyint(((OutputValue - MinOutputVoltage)/OutputRange)*MaxOutputBits);
        //m=rt_comedi_command_data_write(AnalogOutputChannel.board_id, AnalogOutputChannel.subdevice, NCHAN, data_to_card);
        comedi_lock(dev, AnalogOutputChannel.subdevice);
        m=comedi_data_write(dev, AnalogOutputChannel.subdevice, AnalogOutputChannel.channel, AO_RANGE, AREF_DIFF, data_to_card);
        comedi_unlock(dev, AnalogOutputChannel.subdevice);
//        m=comedi_data_write(AnalogOutputChannel.board_id, AnalogOutputChannel.subdevice,
//               AnalogOutputChannel.channel, AO_RANGE, AREF_GROUND, data_to_card);
        //rt_printk("Data_to_card is %d; result from rt_comedi_command_data_write is %d \n",data_to_card, m);
        //rt_printk("LOOP %d,-- AO Out time: %f12 \n",i, (float)rt_get_time_ns()/1E9 - current_time);
        //rt_printk("Data_to_card is %d \n",data_to_card);
        //old_time = current_time;
/*        if (i== 100000)
        {
            sine_loop_running = 0;
            //printf("LOOP -- run: %d %d\n ",keep_on_running,&keep_on_running);
            //printf("RTAI LOOP -- run: %d \n ",i);
            break;
        }
*/
        rt_task_wait_period(); // And waits until the end of the period.

    }
    rt_make_soft_real_time();
    comedi_close(dev);
    rt_task_delete(Sinewaveloop_Task); //Self termination at end.

    pthread_exit(NULL);
    return 0;
 }
Пример #14
0
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;
}
Пример #15
0
void do_cmd(comedi_t *dev,comedi_cmd *cmd)
{
	int total=0;
	int ret;
	int go;
	fd_set rdset;
	struct timeval timeout;

	ret=comedi_command_test(dev,cmd);

	printf("test ret=%d\n",ret);
	if(ret<0){
		printf("errno=%d\n",errno);
		comedi_perror("comedi_command_test");
		return;
	}

	dump_cmd(stdout,cmd);

	ret=comedi_command_test(dev,cmd);

	printf("test ret=%d\n",ret);
	if(ret<0){
		printf("errno=%d\n",errno);
		comedi_perror("comedi_command_test");
		return;
	}

	dump_cmd(stdout,cmd);

	comedi_set_read_subdevice(dev, cmd->subdev);
	ret = comedi_get_read_subdevice(dev);
	if (ret < 0 || ret != cmd->subdev) {
		fprintf(stderr,
			"failed to change 'read' subdevice from %d to %d\n",
			ret, cmd->subdev);
		return;
	}

	ret=comedi_command(dev,cmd);

	printf("ret=%d\n",ret);
	if(ret<0){
		printf("errno=%d\n",errno);
		comedi_perror("comedi_command");
		return;
	}

	go=1;
	while(go){
		FD_ZERO(&rdset);
		FD_SET(comedi_fileno(device),&rdset);
		timeout.tv_sec = 0;
		timeout.tv_usec = 50000;
		ret = select(comedi_fileno(dev)+1,&rdset,NULL,NULL,&timeout);
		//printf("select returned %d\n",ret);
		if(ret<0){
			perror("select");
		}else if(ret==0){
			/* hit timeout */
			printf("timeout, polling...\n");
			ret = comedi_poll(device,cmd->subdev);
			printf("poll returned %d\n",ret);
		}else if(FD_ISSET(comedi_fileno(device),&rdset)){
			/* comedi file descriptor became ready */
			//printf("comedi file descriptor ready\n");
			ret=read(comedi_fileno(dev),buf,sizeof(buf));
			printf("read returned %d\n",ret);
			if(ret<0){
				if(errno==EAGAIN){
					go = 0;
					perror("read");
				}
			}else if(ret==0){
				go = 0;
			}else{
				int i;
				total+=ret;
				//printf("read %d %d\n",ret,total);
				for(i=0;i<ret/sizeof(sampl_t);i++){
					printf("%d\n",buf[i]);
				}
			}
		}else{
			/* unknown file descriptor became ready */
			printf("unknown file descriptor ready\n");
		}
	}
}
Пример #16
0
int main(int argc, char *argv[])
{
	comedi_t *dev;
	comedi_cmd c,*cmd=&c;
	int ret;
	int total=0;
	int i;
	struct timeval start,end;
	int subdev_flags;
	lsampl_t raw;
	struct parsed_options options;

	init_parsed_options(&options);
	parse_options(&options, argc, argv);

	/* The following variables used in this demo
	 * can be modified by command line
	 * options.  When modifying this demo, you may want to
	 * change them here. */
	options.filename = "/dev/comedi0";
	options.subdevice = 0;
	options.channel = 0;
	options.range = 6;  
	options.aref = AREF_DIFF;
	options.n_chan = 8;
	options.n_scan = 100000;
	options.freq = 1000.0;
	options.physical = 1;

	/* open the device */
	dev = comedi_open(options.filename);
	if(!dev){
		comedi_perror(options.filename);
		exit(1);
	}

	// Print numbers for clipped inputs
	comedi_set_global_oor_behavior(COMEDI_OOR_NUMBER);

	/* Set up channel list */
	//for(i = 0; i < options.n_chan/2; i++){
	for(i = 0; i < options.n_chan; i++){	
		chanlist[i] = CR_PACK(options.channel + i, options.range, options.aref);
		range_info[i] = comedi_get_range(dev, options.subdevice, options.channel, options.range);
		maxdata[i] = comedi_get_maxdata(dev, options.subdevice, options.channel);
	}

/*
    for(i = options.n_chan/2; i < options.n_chan; i++){
        fprintf(stderr,"%d: %d\n", i, options.channel + i + 8);
        chanlist[i] = CR_PACK(options.channel + i + 8, options.range, options.aref);
        range_info[i] = comedi_get_range(dev, options.subdevice, options.channel, options.range);
        maxdata[i] = comedi_get_maxdata(dev, options.subdevice, options.channel);
    }    
*/

	/* prepare_cmd_lib() uses a Comedilib routine to find a
	 * good command for the device.  prepare_cmd() explicitly
	 * creates a command, which may not work for your device. */
	prepare_cmd_lib(dev, options.subdevice, options.n_scan, options.n_chan, 1e9 / options.freq, cmd);
	//prepare_cmd(dev, options.subdevice, options.n_scan, options.n_chan, 1e9 / options.freq, cmd);

	fprintf(stderr, "command:\n");
	dump_cmd(stderr, cmd);

	/* this is only for informational purposes */
	gettimeofday(&start, NULL);
	fprintf(stderr,"start time: %ld.%06ld\n", start.tv_sec, start.tv_usec);

	/* start the command */
	ret = comedi_command(dev, cmd);
	if(ret < 0){
		comedi_perror("comedi_command");
		exit(1);
	}
	subdev_flags = comedi_get_subdevice_flags(dev, options.subdevice);
	while(1){
		ret = read(comedi_fileno(dev),buf,BUFSZ);
		if(ret < 0){
			/* some error occurred */
			perror("read");
			continue;
		}else if(ret == 0){
			/* reached stop condition */
			break;
		}else{
			static int col = 0;
			int bytes_per_sample;
			total += ret;
			if(options.verbose)fprintf(stderr, "read %d %d\n", ret, total);
			if(subdev_flags & SDF_LSAMPL)
				bytes_per_sample = sizeof(lsampl_t);
			else
				bytes_per_sample = sizeof(sampl_t);
			for(i = 0; i < ret / bytes_per_sample; i++){
				if(subdev_flags & SDF_LSAMPL) {
					raw = ((lsampl_t *)buf)[i];
				} else {
					raw = ((sampl_t *)buf)[i];
				}
				print_datum(raw, col, options.physical);
				col++;
				if(col == options.n_chan){
					printf("\n");
					col=0;
				}
			}
		}
	}

	/* this is only for informational purposes */
	gettimeofday(&end,NULL);
	fprintf(stderr,"end time: %ld.%06ld\n", end.tv_sec, end.tv_usec);

	end.tv_sec -= start.tv_sec;
	if(end.tv_usec < start.tv_usec){
		end.tv_sec--;
		end.tv_usec += 1000000;
	}
	end.tv_usec -= start.tv_usec;
	fprintf(stderr,"time: %ld.%06ld\n", end.tv_sec, end.tv_usec);

	return 0;
}
Пример #17
0
void do_cmd(comedi_t *dev,comedi_cmd *cmd)
{
	int total=0;
	int ret;
	int go;
	fd_set rdset;
	struct timeval timeout;

	ret=comedi_command_test(dev,cmd);

	printf("test ret=%d\n",ret);
	if(ret<0){
		printf("errno=%d\n",errno);
		comedi_perror("comedi_command_test");
		return;
	}

	dump_cmd(stdout,cmd);

	ret=comedi_command_test(dev,cmd);

	printf("test ret=%d\n",ret);
	if(ret<0){
		printf("errno=%d\n",errno);
		comedi_perror("comedi_command_test");
		return;
	}

	dump_cmd(stdout,cmd);

	ret=comedi_command(dev,cmd);

	printf("ret=%d\n",ret);
	if(ret<0){
		printf("errno=%d\n",errno);
		comedi_perror("comedi_command");
		return;
	}

	go=1;
	while(go){
		FD_ZERO(&rdset);
		FD_SET(comedi_fileno(dev),&rdset);
		timeout.tv_sec = 0;
		timeout.tv_usec = 50000;
		ret = select(comedi_fileno(dev)+1,&rdset,NULL,NULL,&timeout);
		if(ret<0){
			perror("select");
		}else if(ret==0){
			/* timeout */
		}else if(FD_ISSET(comedi_fileno(dev),&rdset)){
			ret=read(comedi_fileno(dev),buf,BUFSZ);
			if(ret<0){
				if(errno==EAGAIN){
					go = 0;
					perror("read");
				}
			}else if(ret==0){
				go = 0;
			}else{
				//int i;

				total+=ret;
				//printf("read %d %d\n",ret,total);
				//printf("count = %d\n",count);
				do_toggle();
#if 0
				for(i=0;i<ret;i+=sizeof(sampl_t)){
					do_toggle();
				}
#endif
			}
		}
	}
}
Пример #18
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();
}