Karl Chuang / Mbed 2 deprecated sensor-hw

Dependencies:   mbed LPS22HB LSM6DSL LSM303AGR HTS221

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"
00003 #include "XNucleoIKS01A2.h"
00004 
00005 /* Instantiate the expansion board */
00006 static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5);
00007 
00008 /* Retrieve the composing elements of the expansion board */
00009 static LSM303AGRMagSensor *magnetometer = mems_expansion_board->magnetometer;
00010 static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor;
00011 static LPS22HBSensor *press_temp = mems_expansion_board->pt_sensor;
00012 static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro;
00013 static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer;
00014 
00015 // Create a DigitalOut objects for the LED
00016 DigitalOut led(LED1);
00017 
00018 // Create a Serial objects to communicate via USB
00019 Serial pc(USBTX, USBRX);
00020 
00021 // Create 2 Ticker objects for a recurring interrupts. One for blink a LED and the other one to update the sensor lectures periodicly.
00022 Ticker blinky; 
00023 Ticker update;
00024 
00025 volatile float TEMPERATURE_C;
00026 volatile float TEMPERATURE_F;
00027 volatile float TEMPERATURE_K;
00028 volatile float HUMIDITY;
00029 volatile float PRESSURE;
00030 
00031 bool measurements_update = false;
00032 
00033 void blinky_handler(){
00034     led = !led;
00035 }
00036 
00037 void sensors_handler(){
00038     measurements_update = true;
00039 }
00040 
00041 int main() {
00042     
00043     /* Create a sensor shield object */
00044     // static X_CUBE_MEMS *Sensors = X_CUBE_MEMS::Instance();
00045 
00046     /* Enable all sensors */
00047     hum_temp->enable();
00048     press_temp->enable();
00049     magnetometer->enable();
00050     accelerometer->enable();
00051     acc_gyro->enable_x();
00052     acc_gyro->enable_g();
00053 
00054       /* Attach a function to be called by the Ticker objects at a specific interval in seconds*/
00055     blinky.attach(&blinky_handler, 0.5);
00056     update.attach(&sensors_handler, 3);
00057     
00058     while(1) {
00059         if(measurements_update == true){
00060             /* Read the environmental sensors  */
00061             hum_temp->get_temperature((float *)&TEMPERATURE_C);
00062             hum_temp->get_humidity((float *)&HUMIDITY);
00063             press_temp->get_pressure((float *)&PRESSURE);
00064             
00065             TEMPERATURE_F = (TEMPERATURE_C * 1.8f) + 32.0f; //Convert the temperature from Celsius to Fahrenheit
00066             TEMPERATURE_K = (TEMPERATURE_C + 273.15f);          //Convert the temperature from Celsius to Kelvin
00067             pc.printf("Temperature:\t %.2f C / %.2f F / %.2f K\r\n", TEMPERATURE_C, TEMPERATURE_F, TEMPERATURE_K);
00068             pc.printf("Humidity:\t %.2f%%\r\n", HUMIDITY);
00069             pc.printf("Pressure:\t %.2f hPa\r\n", PRESSURE); 
00070             pc.printf("\r\n");
00071                     
00072             measurements_update = false;
00073         }
00074         __wfi();
00075     }
00076 }