CPS-Lab / Mbed 2 deprecated Lab7

Dependencies:   MPL3115A2 mbed

Revision:
6:2568b22678fc
Parent:
4:1b449b5bbfe4
Child:
7:f2c4408367f7
--- a/lab7.cpp	Thu Mar 01 22:19:54 2018 +0000
+++ b/lab7.cpp	Fri Mar 02 18:29:34 2018 +0000
@@ -1,25 +1,58 @@
 #include "mbed.h"
 #include <iostream>
+#include <fstream>
 #include "MPL3115A2.h"
 
+
+
 Serial pc(SERIAL_TX, SERIAL_RX);
 DigitalOut myled(LED1);
+DigitalIn startStop(PA_4);
 //Selects SDA as I2C1_SDA on pin PB_7
 //Selects SCL on I2C1_SCL on pin PB_6
 //the I2c address of the pressure sensor is fixed at 0x60
 MPL3115A2 pressure_sensor(PB_7, PB_6, 0x60);
-
-
+using namespace std;
 
 int mpl3115_reg_print(int start, int length);
 void register_map_mpl3115A2(int i, int value);
+int collect_data();
+
+
+#define ARRAY_SIZE 50
+double *pressure_arr = (double *)malloc(sizeof(double) * ARRAY_SIZE);
+double *temp_arr = (double *)malloc(sizeof(double) * ARRAY_SIZE);
+double *pressPtr = pressure_arr;
+double *tempPtr = temp_arr;
+int buffer_full = 0;
 
 
 
 int main() {
     printf("\n\rMPL3115A2 test\n\r");
+    mpl3115_reg_print(26, 2);
+    // set control register 1 bit 7 to 1 (sets the pressure sensor to altimeter mode)
+    uint8_t d;
+    pressure_sensor.readRegs(0x26, &d, 1);
+    d = d | 0b10000001;
+    pressure_sensor.writeRegs(&d, 1);
     
-    
+  /*  while(1) {
+        // don't overflow buffer
+        if(buffer_full >= ARRAY_SIZE)
+            return 0;
+            
+        printf("nooooope\n\r");
+        if(startStop.read() == 1) {
+            printf("it worked\n\r");
+            collect_data();
+            int offset = 0;
+            for(int i = 0; i < ARRAY_SIZE; i++)
+                printf("%lf\t%lf\n\r", *(pressure_arr + offset), *(temp_arr + offset));
+                offset += sizeof(double);
+        }       
+    }
+  */  
    
     printf("start\r\n");
     mpl3115_reg_print(0, 0);
@@ -255,4 +288,32 @@
     printf("%x:\tOFF_H= 0x%.2x\r\n",i, value);
     break;
     }
-}
\ No newline at end of file
+}
+
+int collect_data() {
+        int counter = 0;
+        // while GPIO is 1
+        while(startStop.read() == 1) {
+            // read temp and pressure data
+            *tempPtr = pressure_sensor.getTemperature();
+            *pressPtr = pressure_sensor.getPressure();
+            wait_ms(100); // wait 100 ms makes sampling rate 10Hz
+            //increment pointers
+            tempPtr = tempPtr + sizeof(double);
+            pressPtr = pressPtr + sizeof(double);
+            buffer_full++;
+            counter++;
+            if(counter % 10 == 0) {
+                myled = !myled;    
+            }
+        }
+        *tempPtr = -3; // values that will indicate a sample is complete
+        *pressPtr = -3;
+        tempPtr = tempPtr + sizeof(double);
+        pressPtr = pressPtr + sizeof(double);
+        buffer_full++;
+        myled = 0;
+        return 0;
+        
+}
+