SOFT253 Coursework Weather Reader

Dependencies:   LPS25H hts221

Fork of Soft253-WeatherReader by Joseph Dumpleton

Revision:
2:27b92722246b
Parent:
1:8585a8d417dc
Child:
3:d17f07a0d08d
--- a/main.cpp	Fri Apr 21 17:33:38 2017 +0000
+++ b/main.cpp	Sat Apr 22 09:27:18 2017 +0000
@@ -4,66 +4,87 @@
 #include "LPS25H.h"
 #include "DataGenerator.h"
 
-
+//Complex.Variables
 DigitalOut myled(LED1);
 I2C i2c2(I2C_SDA, I2C_SCL);
+Ticker dataTimer;
 
+//Method Headers
+void readData();
+void createDataThread();
+void printData();
+
+//Prim.Variables
 float tempCelsius = 25.50;
 float humi = 55;
+float barometerPressure = 0;
+float barometerTemperature = 0;
+float dataReadTime = 2.0;
 int humiMax = 100; 
 char cmd=0;
 uint32_t seconds = 0, minutes=0, hours=0; 
 
-//LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR);
-//HTS221 humidity(I2C_SDA, I2C_SCL);
+//Can't print data in a ticker method (not interrupt safe)
+//This is set once we have got new data, so it can be printed safely later
+bool isNewData=false;
 
-/* Test Data generator */
+// Test Data generator setup
 DataGenerator dataGen;
 
 int main(){
-    //Humidity sensor setup.
-    //humidity.init();
-    //humidity.calib();
-    
     printf("SOFT253 simple Temperature Humidity and Pressure Sensor Monitor\n\r");
     printf("Using the X-NUCLEO-IKS01A1 shield and MBED Libraries\n\r");
     
-    //printf("%#x\n\r",barometer.read_id());
+    dataTimer.attach(&readData, dataReadTime); //Attach timer to data reader.
     
     while(1) 
     {
-        /*Original temperature reader from nucleo. */
-        //humidity.ReadTempHumi(&tempCelsius, &humi);
-        
-        dataGen.ReadTempHumi(&tempCelsius, &humi);
-        
-        printf("%4.2fC %3.1f%%", tempCelsius, humi);
-        
-        /*Original line of code working with the nucleo barometer. */
-        //barometer.get();
-        //printf(" %6.1f %4.1f\r\n", barometer.pressure(), barometer.temperature());
-        
-        printf(" %6.1f %4.1f\r\n", dataGen.pressure(), dataGen.temperature());
+        if (isNewData == true){
+            printData();   
+        }
         
         /* Flicker the LED. */
         myled = 1; // LED is ON
         Thread::wait(200); // 200 ms NB 'Thread::wait(int d);' !!! d is in milliseconds! 
         myled = 0; // LED is OFF
         Thread::wait(100); // 100 ms
-      }
-  }
-  
+    }
+}
+
+/* Reads all the data in whenever the ticker interrupt is called */  
+void readData(){
+    //Check if old data has been printed if it hasn't don't do anything
+    //As you have interrupted printdata section.
+    if (isNewData == false){
+        dataGen.ReadTempHumi(&tempCelsius, &humi);
+
+        barometerPressure = dataGen.pressure();
+        barometerTemperature = dataGen.temperature();
+    
+        isNewData = true;
+    }
+}
+/* Prints the data that was read */
+void printData(){
+    printf("%4.2fC %3.1f%%", tempCelsius, humi);
+    printf(" %6.1f %4.1f\r\n", barometerPressure, barometerTemperature);
+    isNewData = false;
+}
 
 
-/*#include "mbed.h"
 
-DigitalOut led1(LED1);
+/* Old Code
+LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR);
+HTS221 humidity(I2C_SDA, I2C_SCL);
 
-// main() runs in its own thread in the OS
-int main() {
-    while (true) {
-        led1 = !led1;
-        wait(0.5);
-    }
-}
-*/
+Humidity sensor setup.
+humidity.init();
+humidity.calib();
+
+//printf("%#x\n\r",barometer.read_id());
+
+barometer.get();
+printf(" %6.1f %4.1f\r\n", barometer.pressure(), barometer.temperature());
+humidity.ReadTempHumi(&tempCelsius, &humi);    
+printf("%4.2fC %3.1f%%", tempCelsius, humi);
+*/
\ No newline at end of file