CPS-Lab* / Mbed 2 deprecated lab6

Dependencies:   ADXL362 mbed MPL3115A2

Revision:
28:0ed68c73dd28
Parent:
27:a4d5c85a55e1
Child:
30:9e127911409d
--- a/main.cpp	Tue Mar 06 15:20:39 2018 +0000
+++ b/main.cpp	Thu Mar 08 21:06:51 2018 +0000
@@ -4,7 +4,7 @@
 #include "MPL3115A2.h"
 #include <stdio.h>
 #include <time.h>
-
+ 
 DigitalOut led3(LED3);
 Serial pc(USBTX, USBRX);
 AnalogIn gpio(A4);
@@ -317,74 +317,84 @@
 
 int measureAlt()
 {
-    
+
     printf("measuring things\r\n");
-    //make output file
-    //FILE * log;
-    //time_t time = time(NULL);
-    //char* tstring = ctime(&time);
-    //char* filen = strcat(tstring, ".txt");
-    //log = fopen(filen, "w");
+
+    uint8_t id = pressure.getID();
+    printf("whoami = 0x%01x\r\n", id);
+    if(id != (uint8_t) 0xC4) {
+        printf("error: WHO_AM_I is not 0xC4; exiting\r\n");
+        return -1;
+    } else {
+        double data[1000];
+
+        int ind = 0;
 
-    double data[900];
+        //set sample_time
+        wait_ms(100);
+        uint8_t st = pressure.getCTRL_REG1();
+        uint8_t msk = 0xc7;
+        printf("CTL_REG1 = 0x%05x\r\n", st);
+        pressure.setCTRL_REG1(msk);
+        pressure.activate();
+        printf("CTL_REG1 = 0x%05x\r\n", pressure.getCTRL_REG1());
+        
 
-    int ind = 0;
-    
-    //set sample_time
-    uint8_t st = 0;
-    pressure.setCTRL_REG1(st) ;
+        //pressure stuff
+        uint8_t tmp[3] ;
+        uint32_t pres ;
 
 
-    //GPIO pin indicates sampling should begin -- if voltage == 3.3, start
-    while(ind < 900 && gpio.read() > 0.00) {
-        printf("in the while\r\n");
+        //GPIO pin indicates sampling should begin -- if voltage == 3.3, start
+        while(ind < 1000 && !pc.readable()) {
+            printf("in the while\r\n");
             //then we measure
             //pressure and temperature sampled at 10times/second (10 hz)
             //LED blinks during sampling at 1 hz (1 time/second)
             led3 = 1;
             for(int i = 0; i < 10; i++) {
-                printf("getting pressure and temp\r\n");
-                data[ind++] = pressure.getPressure();
+                pressure.readRegs(mregids[1], tmp, 3) ;
+                pres = ((tmp[0]<<16)|(tmp[1]<<8)|(tmp[2])) >> 6;
+                data[ind++] = pres;
                 data[ind++] = pressure.getTemperature();
                 wait_ms(100);
             }//for
             led3 = 0;
             for(int i = 0; i < 10; i++) {
-                //WRITE TO CONTROL REGISTER 1 and WRITE THOSE BITS TO '0'
-                // bits 5, 4, and 3
-                printf("pressure and temp 2.0\r\n");
-                data[ind++] = pressure.getPressure();
+                pressure.readRegs(mregids[1], tmp, 3) ;
+                pres = ((tmp[0]<<16)|(tmp[1]<<8)|(tmp[2])) >> 6;
+                data[ind++] = pres;
                 data[ind++] = pressure.getTemperature();
                 wait_ms(100);
             }//for
-    }
-    
-    int pind = 0;
-    int pend = ind;
-    char in;
-    int ch;
-    char p = 'p';
-    while(1) {
-        printf("p%% ");
-        while((ch = pc.getc()) != 13) {
-            pc.putc(ch);
-            //ch2 = ch;
-            //strcat(cmd, &ch2);
-            in = ch;
         }//while
-        if(in == p){
-            while(pind < pend+1){
-                pc.printf("\n\r%f\t%f", data[pind],data[pind+1]);
-                pind += 2;
+        
+        int pind = 0;
+        int pend = ind;
+        char in;
+        int ch;
+        char p = 'p';
+        bool ex = false;
+        while(!ex) {
+            printf("p%% ");
+            while((ch = pc.getc()) != 13) {
+                pc.putc(ch);
+                in = ch;
             }//while
-        }else{
-            in = 0;
-        }//if/else
-    }//while
-    //fclose(log);
-
+            if(in == p) {
+                while(pind < pend+1) {
+                    pc.printf("\n\r%f\t%f", data[pind],data[pind+1]);
+                    pind += 2;
+                    ex = true;
+                }//while
+            } else {
+                in = 0;
+            }//if/else
+        }//while
+        
+        pc.printf("\n\r");
     return 0;
-
+    }//else
 }//measureAlt
 
 int main()