src/drivers/sf0x/sf0x.cpp
/**
 * Perform some basic functional tests on the driver;
 * make sure we can collect data from the sensor in polled
 * and automatic modes.
 */
Perform some basic functional tests on the driver;
make sure we can collect data from the sensor in polled
and automatic modes.
Perform some basic functional tests on the driver ; make sure we can collect data from the sensor in polled and automatic modes .
void
test()
{
	struct range_finder_report report;
	ssize_t sz;
	int fd = open(RANGE_FINDER0_DEVICE_PATH, O_RDONLY);
	if (fd < 0) {
		err(1, "%s open failed (try 'sf0x start' if the driver is not running", RANGE_FINDER0_DEVICE_PATH);
	}
	sz = read(fd, &report, sizeof(report));
	if (sz != sizeof(report)) {
		err(1, "immediate read failed");
	}
	warnx("single read");
	warnx("val:  %0.2f m", (double)report.distance);
	warnx("time: %lld", report.timestamp);
	if (OK != ioctl(fd, SENSORIOCSPOLLRATE, 2)) {
		errx(1, "failed to set 2Hz poll rate");
	}
	for (unsigned i = 0; i < 5; i++) {
		struct pollfd fds;
		fds.fd = fd;
		fds.events = POLLIN;
		int ret = poll(&fds, 1, 2000);
		if (ret != 1) {
			warnx("timed out");
			break;
		}
		sz = read(fd, &report, sizeof(report));
		if (sz != sizeof(report)) {
			warnx("read failed: got %d vs exp. %d", sz, sizeof(report));
			break;
		}
		warnx("read #%u", i);
		warnx("val:  %0.3f m", (double)report.distance);
		warnx("time: %lld", report.timestamp);
	}
	if (OK != ioctl(fd, SENSORIOCSPOLLRATE, SENSOR_POLLRATE_DEFAULT)) {
		errx(1, "ERR: DEF RATE");
	}
	errx(0, "PASS");
}
void test ( ) { struct range_finder_report report ; ssize_t sz ; int fd = open ( RANGE_FINDER0_DEVICE_PATH , O_RDONLY ) ; if ( fd < 0 ) { err ( 1 , " " , RANGE_FINDER0_DEVICE_PATH ) ; } sz = read ( fd , & report , sizeof ( report ) ) ; if ( sz != sizeof ( report ) ) { err ( 1 , " " ) ; } warnx ( " " ) ; warnx ( " " , ( double ) report . distance ) ; warnx ( " " , report . timestamp ) ; if ( OK != ioctl ( fd , SENSORIOCSPOLLRATE , 2 ) ) { errx ( 1 , " " ) ; } for ( unsigned i = 0 ; i < 5 ; i ++ ) { struct pollfd fds ; fds . fd = fd ; fds . events = POLLIN ; int ret = poll ( & fds , 1 , 2000 ) ; if ( ret != 1 ) { warnx ( " " ) ; break ; } sz = read ( fd , & report , sizeof ( report ) ) ; if ( sz != sizeof ( report ) ) { warnx ( " " , sz , sizeof ( report ) ) ; break ; } warnx ( " " , i ) ; warnx ( " " , ( double ) report . distance ) ; warnx ( " " , report . timestamp ) ; } if ( OK != ioctl ( fd , SENSORIOCSPOLLRATE , SENSOR_POLLRATE_DEFAULT ) ) { errx ( 1 , " " ) ; } errx ( 0 , " " ) ; }
Perform some basic functional tests on the driver;
 make sure we can collect data from the sensor in polled
 and automatic modes.
Perform some basic functional tests on the driver ; make sure we can collect data from the sensor in polled and automatic modes .
/* do a simple demand read */ /* start the sensor polling at 2 Hz rate */ /* read the sensor 5x and report each value */ /* wait for data to be ready */ /* now go get it */ /* reset the sensor polling to the default rate */