CPS-Lab* / Mbed 2 deprecated Lab7

Dependencies:   ADXL362 MPL3115A2 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MPL3115A2.h"
00003 #include <string>
00004 #include <math.h>
00005 #include <ATParser.h>
00006 #include "BufferedSerial.h"
00007 
00008 //Serial pc(SERIAL_TX, SERIAL_RX);
00009 DigitalOut myled(LED1);
00010 DigitalOut powerpin(PA_8); // GPIO pin
00011 BufferedSerial pc(SERIAL_TX, SERIAL_RX);
00012 BufferedSerial device(PA_9, PA_10);
00013 
00014 // Selects SDA as I2C1_SDA on pin PB_7
00015 // Selects SCL on I2C1_SCL on pin PB_6
00016 // The I2C address of the pressure sensor is fixed at 0x60. 
00017 MPL3115A2 pressure_sensor(PB_7,PB_6,0x60);
00018 void mpl3115_reg_print(int start, int length); // prototype
00019 
00020 int main() {
00021     int8_t a;
00022     uint8_t id;
00023     double p, t;
00024     double v , s;
00025     int cnt = 0;
00026     myled = 0;
00027     char c;
00028     powerpin = 0;
00029     ATParser at = ATParser(device, "\r\n", 256, 2000, false);
00030     char buffer[100];
00031     pc.baud(115200);
00032     device.baud(115200);
00033     double alt1 = pressure_sensor.getAltitude(), alt2; // initial alt
00034     
00035     mpl3115_reg_print( 0, 0);
00036     
00037     while ((id=pressure_sensor.getID())!=0xC4)// wait for the sensor to connect
00038     {
00039         wait(1);
00040     }
00041     
00042     while(1)
00043     {
00044         if(pc.getc() == 's') // if the GPIO pin is pulled high
00045         {
00046             myled != myled;
00047             //turn on and off led at 1Hz
00048             p=pressure_sensor.getPressure();
00049             t=pressure_sensor.getTemperature();
00050             at.send("Hello");
00051         }
00052         else
00053         {
00054             myled = 0; // so we know when no data is being taken
00055         }
00056     }
00057 }
00058 
00059 void mpl3115_reg_print(int start, int length)
00060 {
00061     uint8_t i, id;
00062     std::string name;
00063     uint8_t temp[1];
00064     id = pressure_sensor.getID();
00065     
00066     // check start registers
00067     if(start >= 0x00 && start <= 0x2D && length >= 0x00 && id == 0xC4)
00068     {
00069         // print all if length = 0
00070         if(length == 0)
00071         {
00072             start = 0;
00073             length = 46;
00074         }
00075     
00076         for(i = start; i < start + length; i++)
00077         {
00078             // make sure name is correct
00079             switch(i)
00080             {
00081                 case 0x00:
00082                     name = "MPL_STATUS";
00083                     break;
00084                 case 0x01:
00085                     name = "MPL_OUT_P_MSB";
00086                     break;
00087                 case 0x02:
00088                     name = "MPL_OUT_P_CSB";
00089                     break;
00090                 case 0x03:
00091                     name = "MPL_OUT_P_LSB";
00092                     break;
00093                 case 0x04:
00094                     name = "MPL_OUT_T_MSB";
00095                     break;
00096                 case 0x05:
00097                     name = "MPL_OUT_T_LSB";
00098                     break;
00099                 case 0x06:
00100                     name = "MPL_DR_STATUS";
00101                     break;
00102                 case 0x07:
00103                     name = "MPL_OUT_P_DELTA_MSB";
00104                     break;
00105                 case 0x08:
00106                     name = "MPL_OUT_P_DELTA_CSB";
00107                     break;
00108                 case 0x09:
00109                     name = "MPL_OUT_P_DELTA_LSB";
00110                     break;
00111                 case 0x0A:
00112                     name = "MPL_OUT_T_DELTA_MSB";
00113                     break;
00114                 case 0x0B:
00115                     name = "MPL_OUT_T_DELTA_LSB";
00116                     break;
00117                 case 0x0C:
00118                     name = "MPL_WHO_AM_I";
00119                     break;
00120                 case 0x0D:
00121                     name = "MPL_F_STATUS";
00122                     break;
00123                 case 0x0E:
00124                     name = "MPL_F_DATA";
00125                     break;
00126                 case 0x0F:
00127                     name = "MPL_F_SETUP";
00128                     break;
00129                 case 0x10:
00130                     name = "MPL_TIME_DLY";
00131                     break;
00132                 case 0x11:
00133                     name = "MPL_SYSMOD";
00134                     break;
00135                 case 0x12:
00136                     name = "MPL_INT_SOURCE";
00137                     break;
00138                 case 0x13:
00139                     name = "MPL_PT_DATA_CFG";
00140                     break;
00141                 case 0x14:
00142                     name = "MPL_BAR_IN_MSB";
00143                     break;
00144                 case 0x15:
00145                     name = "MPL_BAR_IN_LSB";
00146                     break;
00147                 case 0x16:
00148                     name = "MPL_P_TGT_MSB";
00149                     break;
00150                 case 0x17:
00151                     name = "MPL_P_TGT_LSB";
00152                     break;
00153                 case 0x18:
00154                     name = "MPL_T_TGT";
00155                     break;
00156                 case 0x19:
00157                     name = "MPL_P_WND_MSB";
00158                     break;
00159                 case 0x1A:
00160                     name = "MPL_P_WND_LSB";
00161                     break;
00162                 case 0x1B:
00163                     name = "MPL_T_WND";
00164                     break;
00165                 case 0x1C:
00166                     name = "MPL_P_MIN_MSB";
00167                     break;
00168                 case 0x1D:
00169                     name = "MPL_P_MIN_CSB";
00170                     break;
00171                 case 0x1E:
00172                     name = "MPL_P_MIN_LSB";
00173                     break;
00174                 case 0x1F:
00175                     name = "MPL_T_MIN_MSB";
00176                     break;
00177                 case 0x20:
00178                     name = "MPL_T_MIN_LSB";
00179                     break;
00180                 case 0x21:
00181                     name = "MPL_P_MAX_MSB";
00182                     break;
00183                 case 0x22:
00184                     name = "MPL_P_MAX_CSB";
00185                     break;
00186                 case 0x23:
00187                     name = "MPL_P_MAX_LSB";
00188                     break;
00189                 case 0x24:
00190                     name = "MPL_T_MAX_MSB";
00191                     break;
00192                 case 0x25:
00193                     name = "MPL_T_MAX_LSB";
00194                     break;
00195                 case 0x26:
00196                     name = "MPL_CTRL_REG1";
00197                     break;
00198                 case 0x27:
00199                     name = "MPL_CTRL_REG2";
00200                     break;
00201                 case 0x28:
00202                     name = "MPL_CTRL_REG3";
00203                     break;
00204                 case 0x29:
00205                     name = "MPL_CTRL_REG4";
00206                     break;
00207                 case 0x2A:
00208                     name = "MPL_CTRL_REG5";
00209                     break;
00210                 case 0x2B:
00211                     name = "MPL_OFF_P";
00212                     break;
00213                 case 0x2C:
00214                     name = "MPL_OFF_T";
00215                     break;
00216                 case 0x2D:
00217                     name = "MPL_OFF_h";
00218                     break;
00219             }
00220             // boy I do love switch statments
00221             pressure_sensor.readRegs(i, temp, 1);
00222             pc.printf("0x%x: %s=0x%x\n\r", i, name, temp[0]);
00223             // actually print the contents of the register
00224         }
00225         
00226     }   
00227 }