hw
Dependencies: mbed LPS22HB LSM6DSL LSM303AGR HTS221
main.cpp@0:9c859fc3ee1a, 2020-04-23 (annotated)
- Committer:
- b05901043
- Date:
- Thu Apr 23 07:44:49 2020 +0000
- Revision:
- 0:9c859fc3ee1a
- Child:
- 2:efaeb347f9c7
.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
b05901043 | 0:9c859fc3ee1a | 1 | |
b05901043 | 0:9c859fc3ee1a | 2 | #include "mbed.h" |
b05901043 | 0:9c859fc3ee1a | 3 | #include "XNucleoIKS01A2.h" |
b05901043 | 0:9c859fc3ee1a | 4 | |
b05901043 | 0:9c859fc3ee1a | 5 | /* Instantiate the expansion board */ |
b05901043 | 0:9c859fc3ee1a | 6 | |
b05901043 | 0:9c859fc3ee1a | 7 | |
b05901043 | 0:9c859fc3ee1a | 8 | // Create a DigitalOut objects for the LED |
b05901043 | 0:9c859fc3ee1a | 9 | DigitalOut led(LED1); |
b05901043 | 0:9c859fc3ee1a | 10 | |
b05901043 | 0:9c859fc3ee1a | 11 | // Create a Serial objects to communicate via USB |
b05901043 | 0:9c859fc3ee1a | 12 | Serial pc(USBTX, USBRX); |
b05901043 | 0:9c859fc3ee1a | 13 | |
b05901043 | 0:9c859fc3ee1a | 14 | // Create 2 Ticker objects for a recurring interrupts. One for blink a LED and the other one to update the sensor lectures periodicly. |
b05901043 | 0:9c859fc3ee1a | 15 | Ticker blinky; |
b05901043 | 0:9c859fc3ee1a | 16 | Ticker update; |
b05901043 | 0:9c859fc3ee1a | 17 | |
b05901043 | 0:9c859fc3ee1a | 18 | volatile float TEMPERATURE_C; |
b05901043 | 0:9c859fc3ee1a | 19 | volatile float TEMPERATURE_F; |
b05901043 | 0:9c859fc3ee1a | 20 | volatile float TEMPERATURE_K; |
b05901043 | 0:9c859fc3ee1a | 21 | volatile float HUMIDITY; |
b05901043 | 0:9c859fc3ee1a | 22 | volatile float PRESSURE; |
b05901043 | 0:9c859fc3ee1a | 23 | |
b05901043 | 0:9c859fc3ee1a | 24 | bool measurements_update = false; |
b05901043 | 0:9c859fc3ee1a | 25 | |
b05901043 | 0:9c859fc3ee1a | 26 | void blinky_handler(){ |
b05901043 | 0:9c859fc3ee1a | 27 | led = !led; |
b05901043 | 0:9c859fc3ee1a | 28 | } |
b05901043 | 0:9c859fc3ee1a | 29 | |
b05901043 | 0:9c859fc3ee1a | 30 | void sensors_handler(){ |
b05901043 | 0:9c859fc3ee1a | 31 | measurements_update = true; |
b05901043 | 0:9c859fc3ee1a | 32 | } |
b05901043 | 0:9c859fc3ee1a | 33 | |
b05901043 | 0:9c859fc3ee1a | 34 | int main() { |
b05901043 | 0:9c859fc3ee1a | 35 | |
b05901043 | 0:9c859fc3ee1a | 36 | /* Create a sensor shield object */ |
b05901043 | 0:9c859fc3ee1a | 37 | // static X_CUBE_MEMS *Sensors = X_CUBE_MEMS::Instance(); |
b05901043 | 0:9c859fc3ee1a | 38 | static XNucleoIKS01A2 *Sensors = XNucleoIKS01A2::instance(D14, D15, D4, D5); |
b05901043 | 0:9c859fc3ee1a | 39 | |
b05901043 | 0:9c859fc3ee1a | 40 | /* Attach a function to be called by the Ticker objects at a specific interval in seconds*/ |
b05901043 | 0:9c859fc3ee1a | 41 | blinky.attach(&blinky_handler, 0.5); |
b05901043 | 0:9c859fc3ee1a | 42 | update.attach(&sensors_handler, 3); |
b05901043 | 0:9c859fc3ee1a | 43 | |
b05901043 | 0:9c859fc3ee1a | 44 | while(1) { |
b05901043 | 0:9c859fc3ee1a | 45 | if(measurements_update == true){ |
b05901043 | 0:9c859fc3ee1a | 46 | /* Read the environmental sensors */ |
b05901043 | 0:9c859fc3ee1a | 47 | Sensors->ht_sensor->get_temperature((float *)&TEMPERATURE_C); |
b05901043 | 0:9c859fc3ee1a | 48 | Sensors->ht_sensor->get_humidity((float *)&HUMIDITY); |
b05901043 | 0:9c859fc3ee1a | 49 | Sensors->pt_sensor->get_pressure((float *)&PRESSURE); |
b05901043 | 0:9c859fc3ee1a | 50 | |
b05901043 | 0:9c859fc3ee1a | 51 | TEMPERATURE_F = (TEMPERATURE_C * 1.8f) + 32.0f; //Convert the temperature from Celsius to Fahrenheit |
b05901043 | 0:9c859fc3ee1a | 52 | TEMPERATURE_K = (TEMPERATURE_C + 273.15f); //Convert the temperature from Celsius to Kelvin |
b05901043 | 0:9c859fc3ee1a | 53 | pc.printf("Temperature:\t %.2f C / %.2f F / %.2f K\r\n", TEMPERATURE_C, TEMPERATURE_F, TEMPERATURE_K); |
b05901043 | 0:9c859fc3ee1a | 54 | pc.printf("Humidity:\t %.2f%%\r\n", HUMIDITY); |
b05901043 | 0:9c859fc3ee1a | 55 | pc.printf("Pressure:\t %.2f hPa\r\n", PRESSURE); |
b05901043 | 0:9c859fc3ee1a | 56 | pc.printf("\r\n"); |
b05901043 | 0:9c859fc3ee1a | 57 | |
b05901043 | 0:9c859fc3ee1a | 58 | measurements_update = false; |
b05901043 | 0:9c859fc3ee1a | 59 | } |
b05901043 | 0:9c859fc3ee1a | 60 | __wfi(); |
b05901043 | 0:9c859fc3ee1a | 61 | } |
b05901043 | 0:9c859fc3ee1a | 62 | } |