Updated
Dependencies: mbed Nucleo_Sensor_Shield
main.cpp@4:1c3a109a6f6f, 2019-02-15 (annotated)
- Committer:
- ongjiajun
- Date:
- Fri Feb 15 06:27:31 2019 +0000
- Revision:
- 4:1c3a109a6f6f
- Parent:
- 3:53d2d440a695
Updated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kennethwfk | 0:fc777b6c55d6 | 1 | #include "mbed.h" |
kennethwfk | 0:fc777b6c55d6 | 2 | #include "x_cube_mems.h" |
kennethwfk | 0:fc777b6c55d6 | 3 | |
kennethwfk | 0:fc777b6c55d6 | 4 | DigitalOut led(LED1); |
kennethwfk | 0:fc777b6c55d6 | 5 | |
kennethwfk | 0:fc777b6c55d6 | 6 | Serial pc(USBTX, USBRX); |
kennethwfk | 0:fc777b6c55d6 | 7 | |
kennethwfk | 0:fc777b6c55d6 | 8 | Ticker blinky; |
kennethwfk | 0:fc777b6c55d6 | 9 | Ticker update; |
kennethwfk | 0:fc777b6c55d6 | 10 | |
kennethwfk | 0:fc777b6c55d6 | 11 | volatile float TEMPERATURE_C; |
ongjiajun | 4:1c3a109a6f6f | 12 | volatile float TEMPERATURE_K; |
ongjiajun | 4:1c3a109a6f6f | 13 | volatile float HUMIDITY; |
ongjiajun | 4:1c3a109a6f6f | 14 | volatile float PRESSURE; |
kennethwfk | 0:fc777b6c55d6 | 15 | |
kennethwfk | 0:fc777b6c55d6 | 16 | bool measurements_update = false; |
kennethwfk | 0:fc777b6c55d6 | 17 | |
kennethwfk | 0:fc777b6c55d6 | 18 | void blinky_handler(){ |
kennethwfk | 0:fc777b6c55d6 | 19 | led = !led; |
kennethwfk | 0:fc777b6c55d6 | 20 | } |
kennethwfk | 0:fc777b6c55d6 | 21 | |
kennethwfk | 0:fc777b6c55d6 | 22 | void sensors_handler(){ |
kennethwfk | 0:fc777b6c55d6 | 23 | measurements_update = true; |
kennethwfk | 0:fc777b6c55d6 | 24 | } |
kennethwfk | 0:fc777b6c55d6 | 25 | |
kennethwfk | 0:fc777b6c55d6 | 26 | int main() { |
kennethwfk | 0:fc777b6c55d6 | 27 | |
selvakumararm | 2:28f8cd15fca4 | 28 | |
kennethwfk | 0:fc777b6c55d6 | 29 | static X_CUBE_MEMS *Sensors = X_CUBE_MEMS::Instance(); |
kennethwfk | 0:fc777b6c55d6 | 30 | |
selvakumararm | 2:28f8cd15fca4 | 31 | |
kennethwfk | 0:fc777b6c55d6 | 32 | blinky.attach(&blinky_handler, 0.5); |
kennethwfk | 0:fc777b6c55d6 | 33 | update.attach(&sensors_handler, 3); |
kennethwfk | 0:fc777b6c55d6 | 34 | |
kennethwfk | 0:fc777b6c55d6 | 35 | while(1) { |
kennethwfk | 0:fc777b6c55d6 | 36 | |
kennethwfk | 0:fc777b6c55d6 | 37 | if(measurements_update == true){ |
kennethwfk | 0:fc777b6c55d6 | 38 | /* Read the environmental sensors */ |
ongjiajun | 4:1c3a109a6f6f | 39 | Sensors->hts221.GetTemperature((float*)&TEMPERATURE_C); |
ongjiajun | 4:1c3a109a6f6f | 40 | Sensors->hts221.GetHumidity((float*)&HUMIDITY); |
selvakumararm | 2:28f8cd15fca4 | 41 | |
ongjiajun | 4:1c3a109a6f6f | 42 | TEMPERATURE_K = (TEMPERATURE_C * 273.15F); //Convert the temperature from Celcius to Kelvin |
selvakumararm | 2:28f8cd15fca4 | 43 | |
ongjiajun | 4:1c3a109a6f6f | 44 | pc.printf("Temperature:\t %.2f C / %.2f K\r\n", TEMPERATURE_C, TEMPERATURE_K); |
ongjiajun | 4:1c3a109a6f6f | 45 | pc.printf("Pressure:\t%.2f%%\n", HUMIDITY); |
kennethwfk | 0:fc777b6c55d6 | 46 | pc.printf("\r\n"); |
kennethwfk | 0:fc777b6c55d6 | 47 | |
kennethwfk | 0:fc777b6c55d6 | 48 | measurements_update = false; |
kennethwfk | 0:fc777b6c55d6 | 49 | } |
kennethwfk | 0:fc777b6c55d6 | 50 | __wfi(); |
kennethwfk | 0:fc777b6c55d6 | 51 | } |
kennethwfk | 0:fc777b6c55d6 | 52 | } |