コード例 #1
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;
}
コード例 #2
0
ファイル: ui.c プロジェクト: Timvrakas/samd21_gcc
void ui_start_read(void)
{
	LED_On(LED1_GPIO);
}
コード例 #3
0
ファイル: ui.c プロジェクト: InSoonPark/asf
void ui_init(void)
{
	LED_On(LED0_GPIO);
	LED_Off(LED1_GPIO);
	LED_Off(LED2_GPIO);
}
コード例 #4
0
ファイル: ui.c プロジェクト: jrsmile/usbcdemu
void ui_init(void)
{
	LED_On(LED0);
	LED_Off(LED1);
}
コード例 #5
0
ファイル: ui.c プロジェクト: jrsmile/usbcdemu
void ui_start_write(void)
{
	LED_On(LED2);
}
コード例 #6
0
ファイル: ui.c プロジェクト: InSoonPark/asf
void ui_init(void)
{
	// Initialize LEDs
	LED_On(LED_0_PIN);
}
コード例 #7
0
ファイル: ui.c プロジェクト: AndreyMostovov/asf
void ui_wakeup(void)
{
	LED_On(LED_0_PIN);
}
コード例 #8
0
ファイル: ui.c プロジェクト: Gr3yR0n1n/SAINTCON-2015-Badge
void ui_com_tx_start(void)
{
	LED_On(LED2_GPIO);
}
コード例 #9
0
ファイル: ui.c プロジェクト: Dewb/mod
void ui_wakeup(void)
{
	LED_On();
}
コード例 #10
0
ファイル: ui.c プロジェクト: kerichsen/asf
void ui_com_overflow(void)
{
	LED_On(LED_BI1_RED);
}
コード例 #11
0
ファイル: ui.c プロジェクト: Gr3yR0n1n/SAINTCON-2015-Badge
void ui_com_open(uint8_t port)
{
	UNUSED(port);
	LED_On(LED1_GPIO);
}
コード例 #12
0
ファイル: ui.c プロジェクト: kerichsen/asf
void ui_com_error(void)
{
	LED_On(LED_BI0_RED);
}
コード例 #13
0
ファイル: ui.c プロジェクト: kerichsen/asf
void ui_com_tx_start(void)
{
	LED_On(LED_BI1_GREEN);
}
コード例 #14
0
ファイル: ui.c プロジェクト: kerichsen/asf
void ui_com_rx_start(void)
{
	LED_On(LED_BI0_GREEN);
}
コード例 #15
0
ファイル: ui.c プロジェクト: Manchen08/SAINTCON-2015-Badge
void ui_wakeup(void)
{
	LED_On(LED0);
}
コード例 #16
0
ファイル: wdt_example.c プロジェクト: thegeek82000/asf
/** \brief Main function. */
int main(void)
{
	uint8_t delay_counter;
	volatile uint16_t delay;

	/* Initialize the board.
	 * The board-specific conf_board.h file contains the configuration of
	 * the board initialization.
	 */
	sysclk_init();
	board_init();

	/* Detection of all RESET except WDT RESET. */
	if ((reset_cause_get_causes() & CHIP_RESET_CAUSE_WDT)
			!= CHIP_RESET_CAUSE_WDT) {
		state_flag = START_OF_PROG;
		reset_cause_clear_causes(CHIP_RESET_CAUSE_POR |
				CHIP_RESET_CAUSE_EXTRST |
				CHIP_RESET_CAUSE_BOD_CPU);
	} else {
		reset_cause_clear_causes(CHIP_RESET_CAUSE_WDT);
	}

	wdt_disable();

	while (true) {
		switch (state_flag) {
		case START_OF_PROG:
			/* Writing test. */
			wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_2KCLK);
			if (wdt_get_timeout_period() !=
					WDT_TIMEOUT_PERIOD_2KCLK) {
				state_flag = ERROR_STATE;
				break;
			}

			wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_1024KCLK);
			if (wdt_get_timeout_period() !=
					WDT_TIMEOUT_PERIOD_1024KCLK) {
				state_flag = ERROR_STATE;
				break;
			}

			/* Wait for 2 s. */
			delay = 2000;
			delay_ms(delay);
			state_flag = WDT_MCU_RESET;
			break;

		case WDT_MCU_RESET:
			state_flag = REFRESH_WDT;
			wdt_reset_mcu();
			break;

		case REFRESH_WDT:
			/* Enable WDT 500 ms. */
			wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_64KCLK);
			wdt_enable(SYSTEM_RESET_MODE);
			if (wdt_get_timeout_period() !=
					WDT_TIMEOUT_PERIOD_64KCLK) {
				state_flag = ERROR_STATE;
				break;
			}

			for (delay_counter = 0; delay_counter < 8;
					delay_counter++) {
				/* Wait for 8x 250 ms = 2 s. */
				delay = 250;
				delay_ms(delay);
				wdt_reset();
			}
			wdt_disable();
			state_flag = WDT_RST;
			break;

		case WDT_RST:
			state_flag = WDT_INTERRUPT;
			/* Enable WDT 2 s. */
			wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_256KCLK);
			wdt_enable(SYSTEM_RESET_MODE);
			if (wdt_get_timeout_period() !=
					WDT_TIMEOUT_PERIOD_256KCLK) {
				state_flag = ERROR_STATE;
				break;
			}

			while (true) {
				/* Wait for Watchdog reset. */
			}
		/* No break is needed */

		case WDT_INTERRUPT:
			/* Enable WDT 250 ms. */
			wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_32KCLK);
			wdt_set_interrupt_callback(wdt_timer_callback);
			wdt_enable(INTERRUPT_MODE);
			if (wdt_get_timeout_period() !=
					WDT_TIMEOUT_PERIOD_32KCLK) {
				state_flag = ERROR_STATE;
				break;
			}

			cpu_irq_enable();
			/* Wait for interrupt to get triggered */
			delay = 400;
			delay_ms(delay);
			wdt_disable();
			break;

		case WDT_RST_INTERRUPT:

			/* Enable WDT 125 ms. */
			wdt_set_timeout_period(WDT_TIMEOUT_PERIOD_16KCLK);
			wdt_set_interrupt_callback(wdt_timer_callback);
			wdt_enable(INTERRUPT_SYSTEM_RESET_MODE);
			if (wdt_get_timeout_period() !=
					WDT_TIMEOUT_PERIOD_16KCLK) {
				state_flag = ERROR_STATE;
				break;
			}

			/* Wait for 200 ms. */
			delay = 200;
			delay_ms(delay);
			wdt_reset();
			while (true) {
				/* Wait for Watchdog reset. */
			}
		/* No break is needed */

		case ERROR_STATE:

			wdt_disable();

			while (true) {
				LED_On(LED_PIN);
				/* Wait for 500 ms. */
				delay = 500;
				delay_ms(delay);
				/* Blinking. */
				LED_Off(LED_PIN);
				/* Wait for 500 ms. */
				delay = 500;
				delay_ms(delay);
			}
		/* No break is needed */

		case END_OF_PROG:
		default:
			LED_On(LED_PIN);
			while (true) {
			}
			/* No break is needed */
		}
	}
}
コード例 #17
0
ファイル: ui.c プロジェクト: marekr/asf
void ui_process(uint16_t framenumber)
{
	bool b_btn_state, sucess;
	static bool btn_last_state = false;
	static bool sequence_running = false;
	static uint8_t u8_sequence_pos = 0;
	uint8_t u8_value;
	static uint16_t cpt_sof = 0;

	if ((framenumber % 1000) == 0) {
		LED_On(LED0_GPIO);
	}
	if ((framenumber % 1000) == 500) {
		LED_Off(LED0_GPIO);
	}
	// Scan process running each 2ms
	cpt_sof++;
	if ((cpt_sof % 2) == 0) {
		return;
	}

	// Scan buttons on switch 0 to send keys sequence
	b_btn_state = (!gpio_pin_is_high(GPIO_PUSH_BUTTON_1)) ? true : false;
	if (b_btn_state != btn_last_state) {
		btn_last_state = b_btn_state;
		sequence_running = true;
	}

	// Sequence process running each period
	if (SEQUENCE_PERIOD > cpt_sof) {
		return;
	}
	cpt_sof = 0;

	if (sequence_running) {
		// Send next key
		u8_value = ui_sequence[u8_sequence_pos].u8_value;
		if (u8_value!=0) {
			if (ui_sequence[u8_sequence_pos].b_modifier) {
				if (ui_sequence[u8_sequence_pos].b_down) {
					sucess = udi_hid_kbd_modifier_down(u8_value);
				} else {
					sucess = udi_hid_kbd_modifier_up(u8_value);
				}
			} else {
				if (ui_sequence[u8_sequence_pos].b_down) {
					sucess = udi_hid_kbd_down(u8_value);
				} else {
					sucess = udi_hid_kbd_up(u8_value);
				}
			}
			if (!sucess) {
				return; // Retry it on next schedule
			}
		}
		// Valid sequence position
		u8_sequence_pos++;
		if (u8_sequence_pos >=
			sizeof(ui_sequence) / sizeof(ui_sequence[0])) {
			u8_sequence_pos = 0;
			sequence_running = false;
		}
	}
}
コード例 #18
0
ファイル: ui.c プロジェクト: AndreyMostovov/asf
void ui_device_sof_action(void)
{
	uint16_t framenumber;
	static uint8_t cpt_sof = 0;
	static int32_t move_count = MOUSE_MOVE_COUNT;
	static uint8_t move_dir = MOVE_UP;

	if (!ui_device_b_mouse_enable) {
		return;
	}

	framenumber = udd_get_frame_number();
	if ((framenumber % 1000) == 0) {
		LED_On(LED0);
	}

	if ((framenumber % 1000) == 500) {
		LED_Off(LED0);
	}

	/* Scan process running each 2ms */
	cpt_sof++;
	if (2 > cpt_sof) {
		return;
	}

	cpt_sof = 0;

	/* Uses buttons to move mouse */
	if (!ioport_get_pin_level(GPIO_PUSH_BUTTON_0)) {
		move_count--;
		switch (move_dir) {
		case MOVE_UP:
			udi_hid_mouse_moveY(-MOUSE_MOVE_RANGE);
			if (move_count < 0) {
				move_dir = MOVE_RIGHT;
				move_count = MOUSE_MOVE_COUNT;
			}

			break;

		case MOVE_RIGHT:
			udi_hid_mouse_moveX(+MOUSE_MOVE_RANGE);
			if (move_count < 0) {
				move_dir = MOVE_DOWN;
				move_count = MOUSE_MOVE_COUNT;
			}

			break;

		case MOVE_DOWN:
			udi_hid_mouse_moveY(+MOUSE_MOVE_RANGE);
			if (move_count < 0) {
				move_dir = MOVE_LEFT;
				move_count = MOUSE_MOVE_COUNT;
			}

			break;

		case MOVE_LEFT:
			udi_hid_mouse_moveX(-MOUSE_MOVE_RANGE);
			if (move_count < 0) {
				move_dir = MOVE_UP;
				move_count = MOUSE_MOVE_COUNT;
			}

			break;
		}
	}
}
コード例 #19
0
ファイル: ui.c プロジェクト: AndreyMostovov/asf
void ui_init(void)
{
	/* Initialize LEDs */
	LED_On(LED_0_PIN);
}
コード例 #20
0
void ui_com_tx_start(void)
{
    ui_data_transfer = true;
    LED_On(LED_0_PIN);
}
コード例 #21
0
ファイル: drop_detector.c プロジェクト: InSoonPark/asf
/**
 * \brief Example application entry routine
 */
int main(void)
{
	/* The sensor_platform_init() function will initialize the system
	 * clock and sensor bus support in addition to configuring the
	 * XMEGA-A3BU and Sensor Xplained boards.
	 *
	 * Use gfx_mono_init() to initialize the monochrome graphical system
	 * API then write a splash screen after enabling the LCD display
	 * backlight and setting the contrast.
	 *
	 * The MCU is going to be put in a sleep mode, so initialize the
	 * sleep manager API with a call to the sleepmgr_init() routine.
	 */
	sensor_platform_init();
	gfx_mono_init();
	sleepmgr_init();

	gpio_set_pin_high(NHD_C12832A1Z_BACKLIGHT);
	st7565r_set_contrast(ST7565R_DISPLAY_CONTRAST_MIN);

	/* Attach an accelerometer on a Sensors Xplained board. */
	sensor_t accelerometer;
	sensor_attach(&accelerometer, SENSOR_TYPE_ACCELEROMETER, 0, 0);

	/* Enable the accelerometer low-g (free fall) event. */
	sensor_enable_event(&accelerometer, SENSOR_EVENT_LOW_G);

	/* Set the free fall threshold (low-g event), bandwidth and range. */
	sensor_set_threshold(&accelerometer, SENSOR_THRESHOLD_LOW_G,
			LOW_G_THRESHOLD);
	sensor_set_bandwidth(&accelerometer, BANDWIDTH);
	sensor_set_range(&accelerometer, RANGE);

	while (true) {
		/* Put the accelerometer into a low-power mode (if available). */
		sensor_set_state(&accelerometer, SENSOR_STATE_LOW_POWER);

		LED_Off(ACCEL_LED);

		clear_screen();

		/* Display the "armed" message and put the MCU in sleep mode. */
		gfx_mono_draw_string("ATMEL Drop Demo\r\nXMEGA Powered Down\r\n"
				"g Sensor Armed", 1, 5, &sysfont);

		sleepmgr_lock_mode(SLEEP_MODE);
		sleepmgr_enter_sleep();

		/* The following runs after the MCU has been woken by an
		 * external low-g interrupt from the accelerometer.
		 *
		 * Turn on the red LED while falling and put the accelerometer
		 * into a high-power mode (if available) to sample date points.
		 */
		LED_On(ACCEL_LED);

		sensor_set_state(&accelerometer, SENSOR_STATE_HIGHEST_POWER);

		static scalar_t acceleration_waveform[DATA_SAMPLE_COUNT];
		scalar_t acceleration_max = 0;

		for (int data_count = 0; data_count < DATA_SAMPLE_COUNT;
				++data_count) {
			acceleration_waveform[data_count] = 0;

			for (int i = 0; i < SAMPLE_AVG_COUNT; ++i) {
				/* Calculate the gravity vector magnitude. */
				vector3_t gvec;
				sensor_get_vector(&accelerometer, &gvec);

				scalar_t const acceleration_magnitude
					= vector3_magnitude(&gvec);

				/* Store the maximum g magnitude for this
				 * sub-group. */
				if (acceleration_magnitude >
						acceleration_waveform[data_count]) {
					acceleration_waveform[data_count]
						= acceleration_magnitude;
				}

				/* Store the maximum g magnitude for the whole
				 * data set. */
				if (acceleration_magnitude > acceleration_max) {
					acceleration_max = acceleration_magnitude;
				}
			}
		}

		clear_screen();

		/* Turn the max acceleration into a string and convert to g. */
		static char max_g_string[20];

		if (acceleration_max > LOW_G_SATURATION) {
			sprintf(max_g_string, "g Sensor Saturated");
		} else {
			sprintf(max_g_string, "Peak = %02.2f g",
					acceleration_max / 1000);
		}

		/* Print the max g on the monochrome display. */
		gfx_mono_draw_string("Drop Detected", 1, 5, &sysfont);
		gfx_mono_draw_string(max_g_string, 1, 13, &sysfont);
		gfx_mono_draw_string("Press SW1 for chart", 1, 21, &sysfont);

		do {
			LED_Toggle(ACCEL_LED);
			delay_ms(100);
		} while (!switch_pressed(SW1));

		/* Plot the collected data points to create the waveform chart. */
		clear_screen();
		screen_border();

		for (int data_count = 0; data_count < DATA_SAMPLE_COUNT; ++data_count) {
			gfx_mono_draw_filled_circle(data_count,
					32 -
					(acceleration_waveform[data_count] / 500), 1,
					GFX_PIXEL_SET, GFX_WHOLE);
		}

		do {
			LED_Toggle(PROMPT_LED);
			delay_ms(100);
		} while (!switch_pressed(SW1));

		LED_Off(PROMPT_LED);
	}
}
コード例 #22
0
ファイル: main.c プロジェクト: poppe34/Pop-and-Zig
	void debug_test(void)
	{
		LED_On(LED3_GPIO);
	}	
コード例 #23
0
ファイル: ui.c プロジェクト: jrsmile/usbcdemu
void ui_start_read(void)
{
	LED_On(LED2);
}
コード例 #24
0
/* Interrupt on "pin change" from push button to do wakeup on USB
 * Note:
 * This interrupt is enable when the USB host enable remote wakeup feature
 * This interrupt wakeup the CPU if this one is in idle mode
 */
static void ui_wakeup_handler(void)
{
	/* It is a wakeup then send wakeup USB */
	udc_remotewakeup();
	LED_On(LED_0_PIN);
}
コード例 #25
0
ファイル: ui.c プロジェクト: Timvrakas/samd21_gcc
void ui_init(void)
{
	// Initialize LEDs
	LED_On(LED0_GPIO);
	LED_Off(LED1_GPIO);
}
コード例 #26
0
void ui_process(uint16_t framenumber)
{
	bool b_btn_state, success;
	static bool btn_last_state = false;
	static bool sequence_running = false;
	static uint8_t sequence_pos = 0;
	uint8_t value;
	static uint16_t cpt_sof = 0;

	if ((framenumber % 1000) == 0) {
		LED_On(LED_0_PIN);
	}
	if ((framenumber % 1000) == 500) {
		LED_Off(LED_0_PIN);
	}
	// Scan process running each 2ms
	cpt_sof++;
	if ((cpt_sof % 2) == 0) {
		return;
	}

	// Scan buttons on switch 0 to send keys sequence
	b_btn_state = (!port_pin_get_input_level(BUTTON_0_PIN));
	if (b_btn_state != btn_last_state) {
		btn_last_state = b_btn_state;
		if (btn_wakeup) {
			if (!b_btn_state) {
				btn_wakeup = false;
			}
		} else {
			sequence_running = true;
		}
	}

	// Sequence process running each period
	if (SEQUENCE_PERIOD > cpt_sof) {
		return;
	}
	cpt_sof = 0;

	if (sequence_running) {
		// Send next key
		value = ui_sequence[sequence_pos].value;
		if (value!=0) {
			if (ui_sequence[sequence_pos].b_modifier) {
				if (ui_sequence[sequence_pos].b_down) {
					success = udi_hid_kbd_modifier_down(value);
				} else {
					success = udi_hid_kbd_modifier_up(value);
				}
			} else {
				if (ui_sequence[sequence_pos].b_down) {
					success = udi_hid_kbd_down(value);
				} else {
					success = udi_hid_kbd_up(value);
				}
			}
			if (!success) {
				return; // Retry it on next schedule
			}
		}
		// Valid sequence position
		sequence_pos++;
		if (sequence_pos >=
			sizeof(ui_sequence) / sizeof(ui_sequence[0])) {
			sequence_pos = 0;
			sequence_running = false;
		}
	}
}
コード例 #27
0
ファイル: ui.c プロジェクト: Timvrakas/samd21_gcc
void ui_start_write(void)
{
	LED_On(LED1_GPIO);
}
コード例 #28
0
ファイル: ui.c プロジェクト: Manchen08/SAINTCON-2015-Badge
void ui_init(void)
{
	// Initialize LEDs
	LED_On(LED0);
}
コード例 #29
0
ファイル: ui.c プロジェクト: InSoonPark/asf
void ui_wakeup(void)
{
	LED_On(LED0_GPIO);
}
コード例 #30
0
ファイル: ui.c プロジェクト: zhengwang721/ASF
void ui_init(void)
{
	LED_On(LED0);
}