/** \brief Pressure demo application entry * * After initializing sensor platform board resources, this demonstration will * attach and initialize a barometric sensor installed on the development board. * In the case of the Atmel Xplained development boards, for example, the * platform should be fitted and built for a Sensors Xplained Pressure sensor * board. */ int main(void) { /* 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 a descriptor to the existing sensor device. */ sensor_t barometer; sensor_attach(&barometer, SENSOR_TYPE_BAROMETER, 0, 0); if (barometer.err) { puts("\rSensor initialization error."); while (true) { /* Error occurred, loop forever */ } } /* Set the barometer sample mode & altimeter reference values. */ sensor_set_state(&barometer, SENSOR_STATE_LOWEST_POWER); pressure_sea_level(MSL_PRESSURE); if (PRINT_BANNER) { uint32_t id; uint8_t ver; sensor_device_id(&barometer, &id, &ver); printf( "%s\r\nID = 0x%02x ver. 0x%02x\r\n %d-bit Resolution\r\n", barometer.drv->caps.name, (unsigned)id, (unsigned)ver, barometer.hal->resolution); } while (true) { static float P_old = 0; const float P_new = average(barometric_pressure, &barometer); if (fabs(P_new - P_old) > meters_to_pascals(0.5)) { printf("P = %.2f hPa, altimeter: %.1f m\r", (P_new / 100), pressure_altitude(P_new)); } P_old = P_new; } }
/** \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; }
int main(void) { /* Initialize the board. * The board-specific conf_board.h file contains the configuration of * the board initialization. */ board_init(); pmic_init(); sysclk_init(); sensor_platform_init(); rtc_init(); PORTE.DIRSET = 0x01; // Init the RTC CLK.RTCCTRL = 0x05; // while ( !( OSC_STATUS & OSC_RC32KRDY_bm ) ); /* Wait for the int. 32kHz oscillator to stabilize. */ PMIC_CTRL |= 0x01; // Set Int. priority level to low in PMIC while( ( RTC_STATUS & 0x01 ) ); // Needed B 4 writing to RTC PER / CNT registers RTC.PER = 0x0400; RTC.CTRL = 0x01; RTC.INTCTRL = 0x01; //Set this to match the interrupt level in PMIC_CTRL sensor_attach(&barometer, SENSOR_TYPE_BAROMETER, 0, 0); sensor_set_state(&barometer, SENSOR_STATE_HIGHEST_POWER); press_data.scaled = true; temp_data.scaled = true; // USART options. static usart_rs232_options_t USART_SERIAL_OPTIONS = { .baudrate = USART_SERIAL_EXAMPLE_BAUDRATE, .charlength = USART_SERIAL_CHAR_LENGTH, .paritytype = USART_SERIAL_PARITY, .stopbits = USART_SERIAL_STOP_BIT }; // Initialize usart driver in RS232 mode usart_init_rs232(USART_SERIAL_EXAMPLE, &USART_SERIAL_OPTIONS); // Send "message header" sendUARTdata(tx_buf, 22); // sysclk_rtcsrc_enable(SYSCLK_SRC_RC2MHZ); // rtc_init(); if (barometer.err) { sendUARTdata(press_err, 39); } else { memset(tx_buf2, 0, 128); sensor_device_id(&barometer, &id, &ver); sprintf((char*)tx_buf2, "%s\r\n\r\nSensor ID: 0x%02x ver: 0x%02x\r\n%d bit resolution\r\n\r\n", barometer.drv->caps.name, (unsigned)id, (unsigned)ver, barometer.hal->resolution); sendUARTdata(tx_buf2, sizeof(tx_buf2)); } sei(); while (true) { } }
/** \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; }
/** \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 accel; /* Accelerometer device descriptor */ sensor_t compass; /* Magnetic compass device descriptor */ sensor_t gyro; /* Gyroscope 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(&gyro, SENSOR_TYPE_GYROSCOPE, 0, 0); sensor_attach(&accel, SENSOR_TYPE_ACCELEROMETER, 0, 0); sensor_attach(&compass, SENSOR_TYPE_COMPASS, 0, 0); if (gyro.err || accel.err || compass.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(&gyro, &id, &version); sensor_get_bandwidth(&gyro, &freq); sensor_get_range(&gyro, &range); printf(banner_format, gyro.drv->caps.name, (unsigned)id, (unsigned)version, freq, range); sensor_device_id(&accel, &id, &version); sensor_get_bandwidth(&accel, &freq); sensor_get_range(&accel, &range); printf(banner_format, accel.drv->caps.name, (unsigned)id, (unsigned)version, freq, range); sensor_device_id(&compass, &id, &version); sensor_get_bandwidth(&compass, &freq); sensor_get_range(&compass, &range); printf(banner_format, compass.drv->caps.name, (unsigned)id, (unsigned)version, freq, range); delay_ms(500); } /* Initialize sensor data descriptors for scaled vs. raw data. */ static sensor_data_t accel_data = {.scaled = SCALED_DATA}; static sensor_data_t compass_data = {.scaled = SCALED_DATA}; static sensor_data_t gyro_data = {.scaled = SCALED_DATA}; static sensor_data_t temp_data = {.scaled = SCALED_DATA}; while (true) { LED_Toggle(ACTIVITY_LED); /* Read sensor values */ sensor_get_acceleration(&accel, &accel_data); sensor_get_rotation(&gyro, &gyro_data); sensor_get_temperature(&gyro, &temp_data); /* Get temp from gyro */ if (SCALED_DATA) { /* Get calculated magnetic heading from compass */ sensor_get_heading(&compass, &compass_data); } else { /* Get raw magnetic field readings (X,Y,Z) */ sensor_get_field(&compass, &compass_data); } /* Print sensor values */ if (SCALED_DATA) { printf("acc = [%5d, %5d, %5d]\r\n", (int16_t)accel_data.axis.x, (int16_t)accel_data.axis.y, (int16_t)accel_data.axis.z); printf("rot = [%5d, %5d, %5d]\r\n", (int16_t)gyro_data.axis.x, (int16_t)gyro_data.axis.y, (int16_t)gyro_data.axis.z); printf("heading %5d, inclination %5d, strength %5d\r\n", (uint16_t)compass_data.heading.direction, (int16_t)compass_data.heading.inclination, (uint16_t)compass_data.heading.strength); printf("T = %d C\r\n\n", (int16_t)temp_data.temperature.value); } else { printf("acc = [%.5x, %.5x, %.5x]\r\n", (uint16_t)accel_data.axis.x, (uint16_t)accel_data.axis.y, (uint16_t)accel_data.axis.z); printf("rot = [%.5x, %.5x, %.5x]\r\n", (uint16_t)gyro_data.axis.x, (uint16_t)gyro_data.axis.y, (uint16_t)gyro_data.axis.z); printf("field = [%.5x, %.5x, %.5x]\r\n", (int16_t)compass_data.axis.x, (int16_t)compass_data.axis.y, (int16_t)compass_data.axis.z); printf("T = %.5x\r\n\n", (uint16_t)temp_data.temperature.value); } delay_ms(500); } return 0; }