11

Dependencies:   ADXL362 ATParser MPL3115A2 mbed

Fork of Lab7 by CPS-Lab*

main.cpp

Committer:
fanbsun
Date:
2018-03-06
Revision:
1:2630bf98c8fe
Parent:
0:9a45e4f07b10

File content as of revision 1:2630bf98c8fe:

#include "mbed.h"
#include "MPL3115A2.h"
#include "ADXL362.h"
#include <string>
#include <math.h>


ADXL362 ADXL362(PA_0,PA_7,PA_6,PA_1);
Serial pc(SERIAL_TX, SERIAL_RX);
DigitalOut myled(LED1);
DigitalOut powerpin(PA_8);

// 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);

int main() {
    int8_t  a;
    double p, t;
    double v = 0.0;
    int cnt = 0;
    myled = 0;
    powerpin = 0;
    char buffer[32];
    int i;
    char c;
    char *strs[4];
       
    pc.printf("Board is Running\n\r");
    
    wait_ms(600); // we need to wait at least 500ms after ADXL362 reset
    ADXL362.set_mode(ADXL362::MEASUREMENT);

    while(1)
    {
        i = 0;
        pc.printf("cps%% "); // print the line prefix
           
        while(1)
        {   
            c = pc.getc(); // grab input from serial
            pc.putc(c);
            
            if(c == 0x0d) // stop if return is pressed
            {
                break;
            }
            buffer[i] = c; // write the individual characters to buffer.
            i++;
        }      
 
    strs[0] = strtok(buffer, " ");
    strs[1] = strtok(NULL, " ");
    strs[2] = strtok(NULL, " ");
    
    
    if (strcmp(strs[0], "p") == 0)
   {
     
    while(1)
    {
        if(powerpin)
        {
        myled = 1;
        p=pressure_sensor.getPressure();
        t=pressure_sensor.getTemperature();
    
        pc.printf("temp_%d = %f C ; pressure_%d = %f \n\r Pa",cnt,t,cnt,p);
        cnt++;
    
        wait_ms(100);
        a=ADXL362.scanz_u8();
    
        v = v + double(a-71) * 0.1;
        pc.printf(" velocity = %f \n\r",v);
        }
        else
        {
        myled = 0;
        }
   }
   }
   else
     {
      pc.printf("\n\rERR\n\r"); // Error message
     }
     
}
}