/** \brief Light & proximity sensor demo application entry
 *
 * After initializing the Xplained platform and sensor boards, this application
 * attaches descriptors to the ambient light and proximity sensor devices on
 * an Xplained inertial sensor board.  The sensor data, which is formatted and
 * printed via printf() after being read, can be viewed with a serial terminal
 * application on a machine attached to the USB interface on the Xplained
 * board.
 */
int main(void)
{
	sensor_t light_dev;     /* Light sensor device descriptor */
	sensor_t prox_dev;      /* Proximity sensor device descriptor */

	/* Initialize the board (Xplained UC3 or XMEGA & Xplained Sensor boards)
	 * I/O pin mappings and any other configurable resources selected in
	 * the build configuration.
	 */
	sensor_platform_init();

	/* Attach descriptors to the defined sensor devices. */
	sensor_attach(&light_dev, SENSOR_TYPE_LIGHT, 0, 0);
	sensor_attach(&prox_dev, SENSOR_TYPE_PROXIMITY, 0, 0);

	if (light_dev.err || prox_dev.err) {
		puts("\rSensor initialization error.");

		while (true) {
			/* Error occurred, loop forever */
		}
	}

	/* Print sensor information */
	if (PRINT_BANNER) {
		static const char *const banner_format
			= "%s\r\nID = 0x%02x ver. 0x%02x\r\n"
				"Bandwidth = %d Hz  Range = +/- %d\r\n\n";

		uint32_t id;
		uint8_t version;
		int16_t freq, range;

		sensor_device_id(&light_dev, &id, &version);
		sensor_get_bandwidth(&light_dev, &freq);
		sensor_get_range(&light_dev, &range);

		printf(banner_format, light_dev.drv->caps.name,
				(unsigned)id, (unsigned)version, freq, range);

		sensor_device_id(&prox_dev, &id, &version);
		sensor_get_bandwidth(&prox_dev, &freq);
		sensor_get_range(&prox_dev, &range);

		printf(banner_format, prox_dev.drv->caps.name,
				(unsigned)id, (unsigned)version, freq, range);

		delay_ms(500);
	}

	/* Set sample interval for the light sensor */
	if (sensor_set_sample_rate(&light_dev, LIGHT_SAMPLE_RATE) != true) {
		printf("Error setting light sensor sample rate.\r\n");
	}

	/* Set sample interval for the proximity sensor */
	if (sensor_set_sample_rate(&prox_dev, PROX_SAMPLE_RATE) != true) {
		printf("Error setting proximity sensor sample rate.\r\n");
	}

	/* Select all proximity sensor channels */
	sensor_set_channel(&prox_dev, SENSOR_CHANNEL_ALL);

#if (SET_PROX_THRESHOLD == true)
	/* Manually set proximity threshold values for each channel */
	/* Otherwise, sensor will use values previously stored in nvram. */
	sensor_set_threshold(&prox_dev, SENSOR_THRESHOLD_NEAR_PROXIMITY,
			PROX_THRESHOLD);
#endif

#if (SET_PROX_CURRENT == true)
	/* Manually set LED current value for each channel */
	/* Otherwise, sensor will use default values. */
	sensor_set_current(&prox_dev, PROX_CURRENT_mA);
#endif

	/* Initialize sensor data descriptors for scaled vs. raw data. */
	static sensor_data_t light_data = {.scaled = SCALED_DATA};
	static sensor_data_t prox_data  = {.scaled = SCALED_DATA};

	while (true) {
		LED_Toggle(ACTIVITY_LED);

		/* Read sensor values */
		sensor_get_light(&light_dev, &light_data);
		sensor_get_proximity(&prox_dev, &prox_data);

		/* Print sensor values */
		if (SCALED_DATA) {
			printf("light = [%5d]\r\n",
					(int16_t)light_data.light.value);

			printf("prox  = 1:%s 2:%s 3:%s\r\n",
					prox_labels[prox_data.proximity.value[0]],
					prox_labels[prox_data.proximity.value[1]],
					prox_labels[prox_data.proximity.value[2]]);
		} else {
			printf("light = [%5d]\r\n",
					(int16_t)light_data.light.value);

			printf("prox = [%.5x, %.5x, %.5x]\r\n",
					(int16_t)prox_data.proximity.value[0],
					(int16_t)prox_data.proximity.value[1],
					(int16_t)prox_data.proximity.value[2]);
		}

		delay_ms(500);
	}

	return 0;
}
Exemple #2
0
/** \brief Proximity Sensor gesture recognition demo application entry
 *
 * This application uses a 3-channel proximity sensor to recognize simple
 * gestures.  When a proximity event occurs, the routine will wake up from
 * a low-power sleep mode and begin repeatedly sampling the proximity
 * sensor, until the proximity of the object is no longer detected.  Then
 * the beginning and ending sensor readings are compared, and the overall
 * direction of the object's movement is determined based on a lookup table.
 *
 * Once the direction is determined, it is indicate by turning on one of the
 * LEDs on the controller board and (optionally) by serial output to a
 * terminal device.  If the direction cannot be determined, all indicator
 * LEDs will be blinked rapidly.
 *
 * The application then resets by returning to a low-power sleep mode until
 * the next proximity event is detected.
 */
int main(void)
{
	uint8_t start_channels;    /* First channels detecting proximity */
	uint8_t current_channels;  /* Current channels detecting proximity */
	uint8_t end_channels;      /* Final channels detecting proximity */
	direction_t direction;     /* Calculated gesture direction */
	int i;

	/* Initialize the board (Xplained UC3 or XMEGA & Xplained Sensor boards)
	 * I/O pin mappings and any other configurable resources selected in
	 * the build configuration.
	 */
	sensor_platform_init();

	/* Turn on LEDs while initialization completes */
	LED_On(UP_LED);
	LED_On(DOWN_LED);
	LED_On(LEFT_LED);
	LED_On(RIGHT_LED);

	/* Initialize the MCU sleep manager API and specify a sleep mode. */
	sleepmgr_init();
	sleepmgr_lock_mode(SLEEP_MODE);

	/* Attach and initialize proximity sensor */
	sensor_attach(&prox_dev, SENSOR_TYPE_PROXIMITY, 0, 0);

	if (prox_dev.err) {
		puts("\r\nProximity sensor initialization error.");

		while (true) {
			/* Error occurred, loop forever */
		}
	}

#if (USE_PRINTF == true)
	uint32_t id;      /* Device ID */
	uint8_t version;  /* Device version */

	sensor_device_id(&prox_dev, &id, &version);
	printf("\r\nProximity sensor: %s    ID = 0x%02x ver. 0x%02x\r\n",
			prox_dev.drv->caps.name, (unsigned)id,
			(unsigned)version);
#endif

	/* Set sample rate */
	sensor_set_sample_rate(&prox_dev, PROX_SAMPLE_RATE);

	/* Select all proximity sensor channels */
	sensor_set_channel(&prox_dev, SENSOR_CHANNEL_ALL);
	
#if (SET_PROX_THRESHOLD == true)
	/* Manually  set proximity threshold values for each channel */
	/*  Otherwise, sensor will use values previously stored in nvram. */
	sensor_set_threshold(&prox_dev, SENSOR_THRESHOLD_NEAR_PROXIMITY,
			PROX_THRESHOLD);
#endif

#if (SET_PROX_CURRENT == true)
	/* Manually set LED current value for each channel */
	/*  Otherwise, sensor will use default values */
	sensor_set_current(&prox_dev, PROX_CURRENT_mA);
#endif

	/* Set up close proximity event to wakeup system */
	sensor_add_event(&prox_dev, SENSOR_EVENT_NEAR_PROXIMITY,
			prox_event_handler, 0, false);

	while (true) {
		/* Enable proximity event */
		sensor_enable_event(&prox_dev, SENSOR_EVENT_NEAR_PROXIMITY);

		/* Delay before putting device to sleep */
		delay_ms(10);

		/* Put device in low power sleep mode; wait for an interrupt to
		 * wake. */
		LED_Off(UP_LED);
		LED_Off(DOWN_LED);
		LED_Off(LEFT_LED);
		LED_Off(RIGHT_LED);

		/* Enter specified sleep mode */
		sleepmgr_enter_sleep();

		/* Only do sensor processing if proximity event woke device up */
		if (prox_event_occurred) {
			prox_event_occurred = false;

			/* Disable new proximity events during gesture sampling */
			sensor_disable_event(&prox_dev,
					SENSOR_EVENT_NEAR_PROXIMITY);

			/* Get starting value saved by event handler routine */
			start_channels = test_channels(&prox_data);
			end_channels = start_channels;

			/* Loop until no longer detecting proximity */
			do {
				/* Get new readings from sensor */
				sensor_get_proximity(&prox_dev, &prox_data);

				current_channels = test_channels(&prox_data);

				/* Update end value if proximity is still
				 * detected */
				if (current_channels != CHAN_NONE) {
					end_channels = current_channels;
				}
			} while (current_channels != CHAN_NONE);

			/* Get direction from lookup table based on start/end
			 * channel sets */
			direction = dir_tbl [start_channels] [end_channels];

#if USE_PRINTF
			/* Display direction */
			printf("Start: %s  End: %s  Direction: %s \r\n",
					channel_labels[start_channels],
					channel_labels[end_channels],
					direction_labels[direction]);
#endif

			/* Use LEDs to display direction */
			switch (direction) {
			case UP:
				LED_On(UP_LED);
				break;

			case DOWN:
				LED_On(DOWN_LED);
				break;

			case LEFT:
				LED_On(LEFT_LED);
				break;

			case RIGHT:
				LED_On(RIGHT_LED);
				break;

			default: 
				/* Unknown - blink all LEDs to indicate */
				for (i = 0; i < (ERR_BLINK_COUNT * 2); i++) {
					LED_Toggle(UP_LED);
					LED_Toggle(DOWN_LED);
					LED_Toggle(LEFT_LED);
					LED_Toggle(RIGHT_LED);

					delay_ms(50);
				}
				break;
			}
		}
		
		delay_ms(500);
	}
	
	return 0;
}
Exemple #3
0
/** \brief Inertial sensor demo application entry
 *
 * After initializing the Xplained platform and sensor boards, this application
 * attaches descriptors to the ambient light and proximity sensor devices on
 * an Xplained inertial sensor board.  The sensors are configured to wake up
 * the processor if given threshold values are surpassed.
 */
int main(void)
{
#if (USE_PRINTF == true)
	uint32_t id;      /* Device ID */
	uint8_t version;  /* Device version */
#endif

	/* Initialize the board (Xplained UC3 or XMEGA & Xplained Sensor boards)
	 * I/O pin mappings and any other configurable resources selected in
	 * the build configuration.
	 */
	sensor_platform_init();

	LED_On(ACTIVITY_LED);

#if (USE_PRINTF == true)
	printf("\r\n");
#endif

	/* Initialize the MCU sleep manager API and specify a sleep mode. */
	sleepmgr_init();
	sleepmgr_lock_mode(SLEEP_MODE);

#if (LIGHT_WAKE == true)
	/* Attach light sensor */
	sensor_attach(&light_dev, SENSOR_TYPE_LIGHT, 0, 0);

	if (light_dev.err) {
		puts("\r\nLight sensor initialization error.");

		while (true) {
			/* Error occurred, loop forever */
		}
	}

#  if (USE_PRINTF == true)
	sensor_device_id(&light_dev, &id, &version);
	printf("Light sensor: %s    ID = 0x%02x ver. 0x%02x\r\n",
			light_dev.drv->caps.name, (unsigned)id,
			(unsigned)version);
#  endif

	sensor_set_sample_rate(&light_dev, LIGHT_SAMPLE_RATE);

	sensor_set_threshold(&light_dev, SENSOR_THRESHOLD_HIGH_LIGHT,
			LIGHT_THRESH);

	/* Enable high light level event for wakeup */
	sensor_add_event(&light_dev, SENSOR_EVENT_HIGH_LIGHT,
			light_event, 0, true);
#endif

#if (PROX_WAKE == true)
	/* Attach proximity sensor */
	sensor_attach(&prox_dev, SENSOR_TYPE_PROXIMITY, 0, 0);

	if (prox_dev.err) {
		puts("\r\nProximity sensor initialization error.");

		while (true) {
			/* Error occurred, loop forever */
		}
	}

#  if (USE_PRINTF == true)
	sensor_device_id(&prox_dev, &id, &version);
	printf("Proximity sensor: %s    ID = 0x%02x ver. 0x%02x\r\n",
			prox_dev.drv->caps.name, (unsigned)id,
			(unsigned)version);
#  endif

	sensor_set_sample_rate(&prox_dev, PROX_SAMPLE_RATE);

	/* Select all proximity sensor channels */
	sensor_set_channel(&prox_dev, 0);

#  if (SET_PROX_THRESHOLD == true)
	/* Manually set proximity threshold values for each channel */
	/* Otherwise, sensor will use values previously stored in nvram. */
	sensor_set_threshold(&prox_dev, SENSOR_THRESHOLD_NEAR_PROXIMITY,
			PROX_THRESHOLD);
#  endif

#  if (SET_PROX_CURRENT == true)
	/* Manually set LED current value for each channel */
	/* Otherwise, sensor will use default values. */
	sensor_set_current(&prox_dev, PROX_CURRENT_mA);
#  endif

	/* Enable near proximity event for wakeup */
	sensor_add_event(&prox_dev, SENSOR_EVENT_NEAR_PROXIMITY,
			prox_event, 0, true);
#endif

	while (true) {
		LED_Off(ACTIVITY_LED);

		/* Put device in low power sleep mode; wait for an interrupt to
		 * wake. */
		sleepmgr_enter_sleep();

		/* Device has woken up */
		LED_On(ACTIVITY_LED);

#if (USE_PRINTF == true)
#  if (LIGHT_WAKE == true)
		if (light_event_occurred) {
			light_event_occurred = false;
			
			printf("light level = %5d\r\n",
					(int16_t)light_data.light.value);
		}
#  endif

#  if (PROX_WAKE == true)
		if (prox_event_occurred) {
			prox_event_occurred = false;

			printf("proximity: source channel=%d  time=%010ld  ",
					prox_channel, prox_data.timestamp);

			if (SCALED_DATA) {
				printf("Chan1:%s Chan2:%s Chan3:%s\r\n",
						prox_labels[prox_data.proximity.value[0]],
						prox_labels[prox_data.proximity.value[1]],
						prox_labels[prox_data.proximity.value[2]]);
			} else {
				printf("Chan1:%4d Chan2:%4d Chan3:%4d\r\n",
						(int16_t)prox_data.proximity.value[0],
						(int16_t)prox_data.proximity.value[1],
						(int16_t)prox_data.proximity.value[2]);
			}
		}
#  endif
#endif

		delay_ms(500);
	}

	return 0;
}
/** \brief Inertial sensor demo application entry
 *
 * After initializing the Xplained platform and sensor boards, this application
 * attaches descriptors to the accelerometer, gyroscope, and compass devices on
 * an Xplained inertial sensor board.  The sensor data, which is formatted and
 * printed via printf() after being read, can be viewed with a serial terminal
 * application on a machine attached to the USB interface on the Xplained
 * board.
 */
int main(void)
{
	sensor_t light_dev;  /* Light sensor device descriptor */
	sensor_t prox_dev;   /* Proximity sensor device descriptor */

	/* Initialize the board (Xplained UC3 or XMEGA & Xplained Sensor boards)
	 * I/O pin mappings and any other configurable resources selected in
	 * the build configuration.
	 */
	sensor_platform_init();

	/* Attach descriptors to the defined sensor devices. */
	sensor_attach(&light_dev, SENSOR_TYPE_LIGHT, 0, 0);
	sensor_attach(&prox_dev, SENSOR_TYPE_PROXIMITY, 0, 0);

	if (light_dev.err || prox_dev.err) {
		puts("\rSensor initialization error.");

		while (true) {
			/* Error occurred, loop forever */
		}
	}

	/* Set sample rates for light and proximity sensors */
	if (sensor_set_sample_rate(&light_dev, LIGHT_SAMPLE_RATE) != true) {
		printf("Error setting light sensor sample rate.\r\n");
	}

	if (sensor_set_sample_rate(&prox_dev, PROX_SAMPLE_RATE) != true) {
		printf("Error setting proximity sensor sample rate.\r\n");
	}

	/* Select all proximity sensor channels */
	sensor_set_channel(&prox_dev, SENSOR_CHANNEL_ALL);

#if (SET_PROX_THRESHOLD == true)
	/* Manually  set proximity threshold values for each channel */
	/* Otherwise, sensor will use values previously stored in nvram. */
	sensor_set_threshold(&prox_dev, SENSOR_THRESHOLD_NEAR_PROXIMITY,
			PROX_THRESHOLD);
#endif

#if (SET_PROX_CURRENT == true)
	/* Manually set LED current value for each channel */
	/* Otherwise, sensor will use default values. */
	sensor_set_current(&prox_dev, PROX_CURRENT_mA);
#endif

	/* Initialize sensor data descriptors for scaled vs. raw data. */
	static sensor_data_t light_data = {.scaled = SCALED_DATA};
	static sensor_data_t prox_data  = {.scaled = false};

	/* Wait for user to push button before continuing */
	LED_Off(ALL_LEDS);

	while (!SWITCH_PRESSED) {
		/* Just blink LED until button is pushed */
		LED_Toggle(PROMPT_LED);

		delay_ms(50);
	}

	LED_Off(PROMPT_LED);

	while (SWITCH_PRESSED) {
		/* wait until button is released */
	}

	/* Enable output streams for Atmel Data Visualizer (ADV) */
	visual_stream_init();

	while (true) {
		LED_Toggle(PROMPT_LED);

		/* Read sensor values */
		prox_data.scaled = false;
		sensor_get_light(&light_dev, &light_data);

		adv_data_send_1(LIGHT_STREAM_NUM, light_data.timestamp,
				light_data.light.value);

		delay_ms(15);
		sensor_get_proximity(&prox_dev, &prox_data);

		adv_data_send_3(PROX_STREAM_NUM, light_data.timestamp,
				prox_data.proximity.value[0],
				prox_data.proximity.value[1],
				prox_data.proximity.value[2]);

		delay_ms(15);
		prox_data.scaled = true;
		sensor_get_proximity(&prox_dev, &prox_data);

		adv_data_send_3(PROX_THRESHOLD_STREAM_NUM, light_data.timestamp,
				prox_data.proximity.value[0],
				prox_data.proximity.value[1],
				prox_data.proximity.value[2]);
	}

	return 0;
}
Exemple #5
0
/** \brief Proximity sensor threshold calibration application
 *
 * This application illustrates the use of the sensor_calibrate() function
 * to set the proximity detection thresholds in a 3-channel proximity sensor.
 * This threshold is the level at which the sensor will report that an object
 * is near the device.
 *
 * The calibration sequence requires three steps, one for each channel.  During
 * each step, an object is placed at the desired distance in front of the
 * sensor, and the user presses the button on the board to trigger a proximity
 * reading.
 *
 * After Step 3 is completed, the threshold values for the sensor are
 * calculated and are written to non-volatile (flash) memory on the
 * microcontroller.  These values will continue to be used for future
 * proximity readings, unless they are overwritten by an application
 * calling the sensor_set_threshold function for the proximity sensor
 * channel(s).
 */
int main(void)
{
	sensor_t prox_dev;           /* Proximity sensor device */
	sensor_data_t prox_data;     /* Proximity data */
	int led_num = 0;

	/* Initialize the board (Xplained UC3 or XMEGA & Xplained Sensor boards)
	 * I/O pin mappings and any other configurable resources selected in
	 * the build configuration.
	 */
	sensor_platform_init();

	LED_On(ALL_LEDS);

	/* Wait for user to press button to start */
	prompt_user("Press button to start");
	
	/* Attach descriptor and initialize the proximity sensor */
	sensor_attach(&prox_dev, SENSOR_TYPE_PROXIMITY, 0, 0);

#if (SET_PROX_CURRENT == true)
	/* Manually set LED current value for each channel */
	/* Otherwise, sensor will use default values */
	sensor_set_channel(&prox_dev, SENSOR_CHANNEL_ALL);
	sensor_set_current(&prox_dev, PROX_CURRENT_mA);
#endif

	/* Set sensor data output formats (for display after calibration
	 * complete) */
	prox_data.scaled = true;
	
	/* Perform calibration sequence */

	/* Step 1 */
	printf("Setting channel 1: ");
	prompt_user("Place object at desired distance and press button");
	(void)sensor_calibrate(&prox_dev, MANUAL_CALIBRATE, 1, NULL);

	/* Step 2 */
	printf("Setting channel 2: ");
	prompt_user("Place object at desired distance and press button");
	(void)sensor_calibrate(&prox_dev, MANUAL_CALIBRATE, 2, NULL);

	/* Step 3 */
	printf("Setting channel 3: ");
	prompt_user("Place object at desired distance and press button");
	if (sensor_calibrate(&prox_dev, MANUAL_CALIBRATE, 3, NULL) != true) {
		if (prox_dev.err == SENSOR_ERR_IO) {
			printf("Calibration failure: write error\n\r");
		} else {
			printf("Unknown error while calibrating device\n\r");
		}

		while (true) {
			/* Error occurred, loop forever */
		}
	}

	int16_t value;

	/* Display threshold values */
	sensor_set_channel(&prox_dev, 1);
	sensor_get_threshold(&prox_dev, SENSOR_THRESHOLD_NEAR_PROXIMITY,
			&value);
	printf("Channel 1 threshold = %d\r\n", value);

	sensor_set_channel(&prox_dev, 2);
	sensor_get_threshold(&prox_dev, SENSOR_THRESHOLD_NEAR_PROXIMITY,
			&value);
	printf("Channel 2 threshold = %d\r\n", value);

	sensor_set_channel(&prox_dev, 3);
	sensor_get_threshold(&prox_dev, SENSOR_THRESHOLD_NEAR_PROXIMITY,
			&value);
	printf("Channel 3 threshold = %d\r\n", value);

	/* Once the calibration is complete, the proximity status is
	 * continuously captured and displayed.
	 */

	while (true) {
		/* Change LED display */
		LED_Toggle(led_array [led_num++]);
		if (led_num >= NUM_BLINK_LEDS) {
			led_num = 0;
		}

		/* Sample proximity and display results for each channel */
		sensor_get_proximity(&prox_dev, &prox_data);

		printf("prox  = 1:%s 2:%s 3:%s\r\n",
				prox_labels[prox_data.proximity.value[0]],
				prox_labels[prox_data.proximity.value[1]],
				prox_labels[prox_data.proximity.value[2]]);

		delay_ms(500);
	}

	return 0;
}