![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
SOFT253 Coursework Weather Reader
Fork of Soft253-WeatherReader by
main.cpp@3:d17f07a0d08d, 2017-04-28 (annotated)
- Committer:
- jedward
- Date:
- Fri Apr 28 12:03:17 2017 +0000
- Revision:
- 3:d17f07a0d08d
- Parent:
- 2:27b92722246b
- Child:
- 4:edef041d2094
Implement FIFO buffer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jdumpleton3 | 0:b490b8486404 | 1 | #include "mbed.h" |
Jdumpleton3 | 0:b490b8486404 | 2 | #include "rtos.h" |
Jdumpleton3 | 0:b490b8486404 | 3 | #include "hts221.h" |
Jdumpleton3 | 0:b490b8486404 | 4 | #include "LPS25H.h" |
Jdumpleton3 | 1:8585a8d417dc | 5 | #include "DataGenerator.h" |
Jdumpleton3 | 0:b490b8486404 | 6 | |
Jdumpleton3 | 2:27b92722246b | 7 | //Complex.Variables |
Jdumpleton3 | 0:b490b8486404 | 8 | DigitalOut myled(LED1); |
Jdumpleton3 | 0:b490b8486404 | 9 | I2C i2c2(I2C_SDA, I2C_SCL); |
Jdumpleton3 | 2:27b92722246b | 10 | Ticker dataTimer; |
Jdumpleton3 | 0:b490b8486404 | 11 | |
Jdumpleton3 | 2:27b92722246b | 12 | //Method Headers |
Jdumpleton3 | 2:27b92722246b | 13 | void readData(); |
Jdumpleton3 | 2:27b92722246b | 14 | void createDataThread(); |
Jdumpleton3 | 2:27b92722246b | 15 | void printData(); |
jedward | 3:d17f07a0d08d | 16 | void storeNewRecord(); |
Jdumpleton3 | 2:27b92722246b | 17 | |
Jdumpleton3 | 2:27b92722246b | 18 | //Prim.Variables |
Jdumpleton3 | 0:b490b8486404 | 19 | float tempCelsius = 25.50; |
Jdumpleton3 | 0:b490b8486404 | 20 | float humi = 55; |
Jdumpleton3 | 2:27b92722246b | 21 | float barometerPressure = 0; |
Jdumpleton3 | 2:27b92722246b | 22 | float barometerTemperature = 0; |
jedward | 3:d17f07a0d08d | 23 | float dataReadTime = 0.5; |
Jdumpleton3 | 0:b490b8486404 | 24 | int humiMax = 100; |
Jdumpleton3 | 0:b490b8486404 | 25 | char cmd=0; |
Jdumpleton3 | 0:b490b8486404 | 26 | uint32_t seconds = 0, minutes=0, hours=0; |
jedward | 3:d17f07a0d08d | 27 | int headNode = 0; |
jedward | 3:d17f07a0d08d | 28 | int currentNode = 0; |
jedward | 3:d17f07a0d08d | 29 | const int maxNumRecords = 120; |
Jdumpleton3 | 0:b490b8486404 | 30 | |
Jdumpleton3 | 2:27b92722246b | 31 | //Can't print data in a ticker method (not interrupt safe) |
Jdumpleton3 | 2:27b92722246b | 32 | //This is set once we have got new data, so it can be printed safely later |
Jdumpleton3 | 2:27b92722246b | 33 | bool isNewData=false; |
Jdumpleton3 | 0:b490b8486404 | 34 | |
Jdumpleton3 | 2:27b92722246b | 35 | // Test Data generator setup |
Jdumpleton3 | 1:8585a8d417dc | 36 | DataGenerator dataGen; |
Jdumpleton3 | 0:b490b8486404 | 37 | |
jedward | 3:d17f07a0d08d | 38 | struct dataEntry{ |
jedward | 3:d17f07a0d08d | 39 | float airTemp; |
jedward | 3:d17f07a0d08d | 40 | float airPress; |
jedward | 3:d17f07a0d08d | 41 | float barTemp; |
jedward | 3:d17f07a0d08d | 42 | float barPress; |
jedward | 3:d17f07a0d08d | 43 | }tempEntry; |
jedward | 3:d17f07a0d08d | 44 | |
jedward | 3:d17f07a0d08d | 45 | dataEntry storedDataArray [maxNumRecords]; |
jedward | 3:d17f07a0d08d | 46 | |
Jdumpleton3 | 1:8585a8d417dc | 47 | int main(){ |
Jdumpleton3 | 1:8585a8d417dc | 48 | printf("SOFT253 simple Temperature Humidity and Pressure Sensor Monitor\n\r"); |
Jdumpleton3 | 1:8585a8d417dc | 49 | printf("Using the X-NUCLEO-IKS01A1 shield and MBED Libraries\n\r"); |
Jdumpleton3 | 1:8585a8d417dc | 50 | |
Jdumpleton3 | 2:27b92722246b | 51 | dataTimer.attach(&readData, dataReadTime); //Attach timer to data reader. |
Jdumpleton3 | 0:b490b8486404 | 52 | |
Jdumpleton3 | 1:8585a8d417dc | 53 | while(1) |
Jdumpleton3 | 0:b490b8486404 | 54 | { |
Jdumpleton3 | 2:27b92722246b | 55 | if (isNewData == true){ |
jedward | 3:d17f07a0d08d | 56 | storeNewRecord(); |
Jdumpleton3 | 2:27b92722246b | 57 | printData(); |
Jdumpleton3 | 2:27b92722246b | 58 | } |
Jdumpleton3 | 1:8585a8d417dc | 59 | |
Jdumpleton3 | 1:8585a8d417dc | 60 | /* Flicker the LED. */ |
Jdumpleton3 | 0:b490b8486404 | 61 | myled = 1; // LED is ON |
Jdumpleton3 | 0:b490b8486404 | 62 | Thread::wait(200); // 200 ms NB 'Thread::wait(int d);' !!! d is in milliseconds! |
Jdumpleton3 | 0:b490b8486404 | 63 | myled = 0; // LED is OFF |
Jdumpleton3 | 0:b490b8486404 | 64 | Thread::wait(100); // 100 ms |
Jdumpleton3 | 2:27b92722246b | 65 | } |
Jdumpleton3 | 2:27b92722246b | 66 | } |
Jdumpleton3 | 2:27b92722246b | 67 | |
jedward | 3:d17f07a0d08d | 68 | void storeNewRecord(){ |
jedward | 3:d17f07a0d08d | 69 | |
jedward | 3:d17f07a0d08d | 70 | if(headNode>=120){ |
jedward | 3:d17f07a0d08d | 71 | headNode = 0; |
jedward | 3:d17f07a0d08d | 72 | } |
jedward | 3:d17f07a0d08d | 73 | |
jedward | 3:d17f07a0d08d | 74 | storedDataArray[headNode] = tempEntry; |
jedward | 3:d17f07a0d08d | 75 | currentNode = headNode; |
jedward | 3:d17f07a0d08d | 76 | headNode++; |
jedward | 3:d17f07a0d08d | 77 | } |
jedward | 3:d17f07a0d08d | 78 | |
Jdumpleton3 | 2:27b92722246b | 79 | /* Reads all the data in whenever the ticker interrupt is called */ |
Jdumpleton3 | 2:27b92722246b | 80 | void readData(){ |
Jdumpleton3 | 2:27b92722246b | 81 | //Check if old data has been printed if it hasn't don't do anything |
Jdumpleton3 | 2:27b92722246b | 82 | //As you have interrupted printdata section. |
Jdumpleton3 | 2:27b92722246b | 83 | if (isNewData == false){ |
jedward | 3:d17f07a0d08d | 84 | //Random data CHANGE ME |
Jdumpleton3 | 2:27b92722246b | 85 | dataGen.ReadTempHumi(&tempCelsius, &humi); |
Jdumpleton3 | 2:27b92722246b | 86 | |
Jdumpleton3 | 2:27b92722246b | 87 | barometerPressure = dataGen.pressure(); |
Jdumpleton3 | 2:27b92722246b | 88 | barometerTemperature = dataGen.temperature(); |
Jdumpleton3 | 2:27b92722246b | 89 | |
jedward | 3:d17f07a0d08d | 90 | tempEntry.airTemp = tempCelsius; |
jedward | 3:d17f07a0d08d | 91 | tempEntry.airPress = humi; |
jedward | 3:d17f07a0d08d | 92 | tempEntry.barTemp = barometerTemperature; |
jedward | 3:d17f07a0d08d | 93 | tempEntry.barPress = barometerPressure; |
jedward | 3:d17f07a0d08d | 94 | |
Jdumpleton3 | 2:27b92722246b | 95 | isNewData = true; |
Jdumpleton3 | 2:27b92722246b | 96 | } |
Jdumpleton3 | 2:27b92722246b | 97 | } |
Jdumpleton3 | 2:27b92722246b | 98 | /* Prints the data that was read */ |
Jdumpleton3 | 2:27b92722246b | 99 | void printData(){ |
jedward | 3:d17f07a0d08d | 100 | printf("0%i %4.2fC %3.1f%%", headNode, tempEntry.airTemp, tempEntry.airPress); |
jedward | 3:d17f07a0d08d | 101 | printf(" %6.1f %4.1f\r\n", tempEntry.barPress, tempEntry.barTemp); |
Jdumpleton3 | 2:27b92722246b | 102 | isNewData = false; |
Jdumpleton3 | 2:27b92722246b | 103 | } |
Jdumpleton3 | 0:b490b8486404 | 104 | |
Jdumpleton3 | 0:b490b8486404 | 105 | |
Jdumpleton3 | 0:b490b8486404 | 106 | |
Jdumpleton3 | 2:27b92722246b | 107 | /* Old Code |
Jdumpleton3 | 2:27b92722246b | 108 | LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR); |
Jdumpleton3 | 2:27b92722246b | 109 | HTS221 humidity(I2C_SDA, I2C_SCL); |
Jdumpleton3 | 0:b490b8486404 | 110 | |
Jdumpleton3 | 2:27b92722246b | 111 | Humidity sensor setup. |
Jdumpleton3 | 2:27b92722246b | 112 | humidity.init(); |
Jdumpleton3 | 2:27b92722246b | 113 | humidity.calib(); |
Jdumpleton3 | 2:27b92722246b | 114 | |
Jdumpleton3 | 2:27b92722246b | 115 | //printf("%#x\n\r",barometer.read_id()); |
Jdumpleton3 | 2:27b92722246b | 116 | |
Jdumpleton3 | 2:27b92722246b | 117 | barometer.get(); |
Jdumpleton3 | 2:27b92722246b | 118 | printf(" %6.1f %4.1f\r\n", barometer.pressure(), barometer.temperature()); |
Jdumpleton3 | 2:27b92722246b | 119 | humidity.ReadTempHumi(&tempCelsius, &humi); |
Jdumpleton3 | 2:27b92722246b | 120 | printf("%4.2fC %3.1f%%", tempCelsius, humi); |
Jdumpleton3 | 2:27b92722246b | 121 | */ |