Use this code as template for Question 2
Dependencies: Nucleo_Sensor_Shield mbed
main.cpp@2:28f8cd15fca4, 2018-04-10 (annotated)
- Committer:
- selvakumararm
- Date:
- Tue Apr 10 02:48:03 2018 +0000
- Revision:
- 2:28f8cd15fca4
- Parent:
- 1:be2eea2ae8e7
Exam Basic Code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kennethwfk | 0:fc777b6c55d6 | 1 | |
kennethwfk | 0:fc777b6c55d6 | 2 | |
kennethwfk | 0:fc777b6c55d6 | 3 | #include "mbed.h" |
kennethwfk | 0:fc777b6c55d6 | 4 | #include "x_cube_mems.h" |
kennethwfk | 0:fc777b6c55d6 | 5 | |
kennethwfk | 0:fc777b6c55d6 | 6 | DigitalOut led(LED1); |
kennethwfk | 0:fc777b6c55d6 | 7 | |
kennethwfk | 0:fc777b6c55d6 | 8 | Serial pc(USBTX, USBRX); |
kennethwfk | 0:fc777b6c55d6 | 9 | |
kennethwfk | 0:fc777b6c55d6 | 10 | Ticker blinky; |
kennethwfk | 0:fc777b6c55d6 | 11 | Ticker update; |
kennethwfk | 0:fc777b6c55d6 | 12 | |
kennethwfk | 0:fc777b6c55d6 | 13 | volatile float TEMPERATURE_C; |
kennethwfk | 0:fc777b6c55d6 | 14 | volatile float TEMPERATURE_F; |
kennethwfk | 0:fc777b6c55d6 | 15 | volatile float TEMPERATURE_K; |
kennethwfk | 0:fc777b6c55d6 | 16 | volatile float HUMIDITY; |
kennethwfk | 0:fc777b6c55d6 | 17 | volatile float PRESSURE; |
kennethwfk | 0:fc777b6c55d6 | 18 | |
kennethwfk | 0:fc777b6c55d6 | 19 | bool measurements_update = false; |
kennethwfk | 0:fc777b6c55d6 | 20 | |
kennethwfk | 0:fc777b6c55d6 | 21 | void blinky_handler(){ |
kennethwfk | 0:fc777b6c55d6 | 22 | led = !led; |
kennethwfk | 0:fc777b6c55d6 | 23 | } |
kennethwfk | 0:fc777b6c55d6 | 24 | |
kennethwfk | 0:fc777b6c55d6 | 25 | void sensors_handler(){ |
kennethwfk | 0:fc777b6c55d6 | 26 | measurements_update = true; |
kennethwfk | 0:fc777b6c55d6 | 27 | } |
kennethwfk | 0:fc777b6c55d6 | 28 | |
kennethwfk | 0:fc777b6c55d6 | 29 | int main() { |
kennethwfk | 0:fc777b6c55d6 | 30 | |
selvakumararm | 2:28f8cd15fca4 | 31 | |
kennethwfk | 0:fc777b6c55d6 | 32 | static X_CUBE_MEMS *Sensors = X_CUBE_MEMS::Instance(); |
kennethwfk | 0:fc777b6c55d6 | 33 | |
selvakumararm | 2:28f8cd15fca4 | 34 | |
kennethwfk | 0:fc777b6c55d6 | 35 | blinky.attach(&blinky_handler, 0.5); |
kennethwfk | 0:fc777b6c55d6 | 36 | update.attach(&sensors_handler, 3); |
kennethwfk | 0:fc777b6c55d6 | 37 | |
kennethwfk | 0:fc777b6c55d6 | 38 | while(1) { |
kennethwfk | 0:fc777b6c55d6 | 39 | |
kennethwfk | 0:fc777b6c55d6 | 40 | if(measurements_update == true){ |
kennethwfk | 0:fc777b6c55d6 | 41 | /* Read the environmental sensors */ |
kennethwfk | 0:fc777b6c55d6 | 42 | |
selvakumararm | 2:28f8cd15fca4 | 43 | <your code should be here> |
selvakumararm | 2:28f8cd15fca4 | 44 | |
selvakumararm | 2:28f8cd15fca4 | 45 | |
kennethwfk | 0:fc777b6c55d6 | 46 | pc.printf("Temperature:\t %.2f C / %.2f F / %.2f K\r\n", TEMPERATURE_C, TEMPERATURE_F, TEMPERATURE_K); |
kennethwfk | 0:fc777b6c55d6 | 47 | pc.printf("Humidity:\t %.2f%%\r\n", HUMIDITY); |
selvakumararm | 2:28f8cd15fca4 | 48 | |
kennethwfk | 0:fc777b6c55d6 | 49 | |
kennethwfk | 0:fc777b6c55d6 | 50 | pc.printf("\r\n"); |
kennethwfk | 0:fc777b6c55d6 | 51 | |
kennethwfk | 0:fc777b6c55d6 | 52 | measurements_update = false; |
kennethwfk | 0:fc777b6c55d6 | 53 | } |
kennethwfk | 0:fc777b6c55d6 | 54 | __wfi(); |
kennethwfk | 0:fc777b6c55d6 | 55 | } |
kennethwfk | 0:fc777b6c55d6 | 56 | } |