Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: ADXL362 mbed MPL3115A2
Diff: main.cpp
- Revision:
- 26:19823b051994
- Parent:
- 25:ded2ac3f76e4
- Child:
- 27:a4d5c85a55e1
--- a/main.cpp Thu Mar 01 15:04:59 2018 +0000 +++ b/main.cpp Fri Mar 02 18:06:12 2018 +0000 @@ -2,14 +2,19 @@ #include "ADXL362.h" #include "string.h" #include "MPL3115A2.h" +#include <stdio.h> +#include <time.h> DigitalOut led3(LED3); +Serial pc(USBTX, USBRX); +AnalogIn gpio(A4); //map of register values and names for the adxl int aregids[34]; char* aregnames[34]; //populating the map -void initmap() { +void initmap() +{ aregids[0] = 0x00; aregids[1] = 0x01; aregids[2] = 0x02; @@ -44,7 +49,7 @@ aregids[31] = 0x2C; aregids[32] = 0x2D; aregids[33] = 0x2E; - + aregnames[0] = "DEVID_AD"; aregnames[1] = "DEVID_MST"; aregnames[2] = "PARTID"; @@ -85,7 +90,8 @@ char mregids[46]; char* mregnames[46]; //populating the map -void minitmap() { +void minitmap() +{ mregids[0] = 0x00; mregids[1] = 0x01; mregids[2] = 0x02; @@ -132,7 +138,7 @@ mregids[43] = 0x2B; mregids[44] = 0x2C; mregids[45] = 0x2D; - + mregnames[0] = "STATUS"; mregnames[1] = "OUT_P_MSB"; mregnames[2] = "OUT_P_CSB"; @@ -187,40 +193,41 @@ // 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. +// The I2C address of the pressure sensor is fixed at 0x60. MPL3115A2 pressure(PB_7, PB_6, 0x60); -int adxl362_reg_print(int s, int l){ +int adxl362_reg_print(int s, int l) +{ //check s is between 0x00 and 0x2E - if(s > 0x2E || s < 0x00){ + if(s > 0x2E || s < 0x00) { printf("adx: requires start between 0x00 and 0x2E; given %d (0x%01x)\n", s,s); return -1; - }else if(l < 0){ + } else if(l < 0) { //check length > 0 printf("adx: requires length >= 0, given %d\n", l); return -1; - }else{ + } else { // start sending stuff //first check DEVID_AD == 0xAD uint8_t devid = adxl362.read_reg(ADXL362::DEVID_AD); - printf("devid = %d\n", devid); - if(devid != 0xAD){ + //printf("devid = %d\n", devid); + if(devid != 0xAD) { printf("error: DEVID_AD is not 0xAD; exiting\n"); return -1; - }else{ + } else { //now do stuff //if length == 0, send everything from s to end of registers //else print from s to l // formula : range = end - start - length // area = start + range int stop = 0; - if(l == 0){ + if(l == 0) { stop = 34; - }else{ + } else { stop = s + l; } int in = s; - while(in < stop){ + while(in < stop) { //get register name char * name = aregnames[in]; //GET NAME ADXL362::ADXL362_register_t reg = (ADXL362::ADXL362_register_t) aregids[in]; @@ -234,8 +241,9 @@ return 0; } -void knocks(){ - int8_t x,y,z, initx, inity, initz; +void knocks() +{ + int8_t x,y,z, initx, inity, initz; //initial values of x, y, and z. //These values are the baseline for the accelerometer; knocks are detected // in relation to these initial values @@ -244,14 +252,14 @@ initz=adxl362.scanz_u8(); int count = 0; int bs = 7; - + while(1) { //get the values of x, y, and z to test for a knock x=adxl362.scanx_u8(); y=adxl362.scany_u8(); z=adxl362.scanz_u8(); - //if x, y, or z vary from baseline by at least 3, a knock has occured. - if(x > initx+bs || y > inity+bs || z > initz+bs || x < initx-bs || y < inity-bs || z < initz-bs){ + //if x, y, or z vary from baseline by at least bs, a knock has occured. + if(x > initx+bs || y > inity+bs || z > initz+bs || x < initx-bs || y < inity-bs || z < initz-bs) { led3 = 1; count++; printf("number of knocks: %d\r\n", count); @@ -263,18 +271,19 @@ } } -int mpl3115_reg_print(int s, int l){ - if(s > 0x2D || s < 0x00){ +int mpl3115_reg_print(int s, int l) +{ + if(s > 0x2D || s < 0x00) { printf("mpl: requires start between 0x00 and 0x20; given %d (0x%01x)\n\r", s,s); return -1; - }else if(l < 0){ + } else if(l < 0) { //check length > 0 printf("mpl: requires length >= 0, given %d\n\r", l); return -1; } else { //else do stuff uint8_t id = pressure.getID(); - printf("whoami = 0x%01x", id); + //printf("whoami = 0x%01x", id); if(id != 0xC4) { printf("error: WHO_AM_I is not 0xC4; exiting\n"); return -1; @@ -286,7 +295,7 @@ // area = start + range int stop = 0; if(l == 0) { - stop = 34; + stop = 46; } else { stop = s + l; } @@ -304,21 +313,93 @@ }//else }//else return 0; -}//print +}//print mpl + +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"); + + double data[900]; + + int ind = 0; + -int main() { + //GPIO pin indicates sampling should begin -- if voltage == 3.3, start + while(ind < 900 && gpio.read() > 0.00) { + 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(); + data[ind++] = pressure.getTemperature(); + wait_ms(1); + }//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(); + data[ind++] = pressure.getTemperature(); + wait_ms(1); + }//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; + }//while + }else{ + in = 0; + }//if/else + }//while + //fclose(log); + + return 0; + +}//measureAlt + +int main() +{ initmap(); minitmap(); adxl362.reset(); + pressure.activate(); wait_ms(600); // we need to wait at least 500ms after ADXL362 reset adxl362.set_mode(ADXL362::MEASUREMENT); int start, length; start = 0; length = 0; //while(1){ - adxl362_reg_print(start, length); - mpl3115_reg_print(start, length); + //adxl362_reg_print(start, length); + // mpl3115_reg_print(start, length); //} - knocks(); - + //knocks(); + + measureAlt(); + //while(1); }