Jake Billingham
/
computingproject
JKDVSK`
Fork of mbed_menu by
Diff: menu_menu.cpp
- Revision:
- 2:2b7eb44d597d
- Parent:
- 1:4938aa677cb3
- Child:
- 3:551061b40ccc
diff -r 4938aa677cb3 -r 2b7eb44d597d menu_menu.cpp --- a/menu_menu.cpp Wed Nov 08 10:46:19 2017 +0000 +++ b/menu_menu.cpp Wed Nov 08 11:36:05 2017 +0000 @@ -1,118 +1,124 @@ +//include relevant libraries #include "mbed.h" #include "stdio.h" #include "math.h" #include "ADXL362.h" + +//define constants high and low for entire program #define HIGH 1 #define LOW 0 -DigitalOut myled1(LED1); /* LED1 */ + +//Define LED to be used to show mbed is operational +DigitalOut myled1(LED1); +//Define serial connection Serial pc(USBTX, USBRX); +//Define accelerometer ADXL362 adxl362(p11, p12, p13, p10); +//Set up local file system to allow writing of txt files LocalFileSystem local("local"); + +//??? int matlabdata; char usercmd; - + +//Define paramater strings for communication over USB char N[1000]; //Number of samples char T [1000]; //Sample period int main() { - FILE *samplesettings; //Create file with pointer + //Create file with pointer + FILE *samplesettings; FILE *timesettings; - - char res[100];//string to store user command + char res[100];//String to store user command matlabdata=pc.scanf("%s",res);//Get user command from matlab -while(1) -{ - if(!strcmp(res,"1")==0)//if user command in matlab is 1 write the sample settings into a file + while(1) + { + if(!strcmp(res,"1")==0)//if user command in matlab is 1 write the sample settings into a file { - myled1 = HIGH; - char N[1000];//To store the number of samples which is received from the matlab - pc.scanf("%s\n",&N);//to get the number of samples input from the matlab - samplesettings = fopen("/local/samplesettings.txt", "w");//Open file for writing - fprintf(samplesettings,"%s\n",N); //Writes settings values onto file - fclose(samplesettings);//close file - break; - - - } - else if(!strcmp(res,"2")==0)//If user command is 2 search the file for sample settings + myled1 = HIGH; + char N[1000];//To store the number of samples which is received from the matlab + pc.scanf("%s\n",&N);//to get the number of samples input from the matlab + samplesettings = fopen("/local/samplesettings.txt", "w");//Open file for writing + fprintf(samplesettings,"%s\n",N); //Writes settings values onto file + fclose(samplesettings);//close file + break; + } + else if(!strcmp(res,"2")==0)//If user command is 2 search the file for sample settings + { + //Define variables for output + char value1[1000]; + samplesettings = fopen("/local/samplesettings.txt" , "r"); //Open file for reading + fseek(samplesettings, 0 ,SEEK_SET); //Sets cursor to start of file + fscanf(samplesettings,"%s\n", value1);//Scans fo the value of N + pc.printf("%s\n", value1);//Prints the value of N + fclose(samplesettings); //Close file + break; + } + else if(!strcmp(res,"3")==0)//If user command recieved is 3 then write the time settings into a file + { + char T[1000];//To store value of T recieved from matlab + pc.scanf("%s\n",&T);//To scan value of T from matlab + timesettings = fopen("/local/timesettings.txt", "w");//Open file for writing + fprintf(timesettings,"%s\n",T); //Writes settings values onto file + fclose(timesettings);//Close file + break; + } + else if(!strcmp(res,"4")==0)//If 4 is recieved as a user command then read the time settings + { + //Define variables for output + char value2[100]; + samplesettings = fopen("/local/timesettings.txt" , "r"); //Open file for reading + fseek(timesettings, 0 ,SEEK_SET); //Sets cursor to start of file + fscanf(timesettings,"%s\n", value2);//Reads the time period from the file + pc.printf("%s\n", value2);//Outsputs the time period + fclose(timesettings); //Close file + break; + } + else if(!strcmp(res,"5")==0)//If the user command is 5 then we collect data + { + //Initialise accelerometer + adxl362.init_spi(); + adxl362.init_adxl362(); + wait(0.1); + int8_t xdata, ydata, zdata; + for(int i=0; i<atoi(N); i++) { - //Define variables for output - char value1[1000]; - samplesettings = fopen("/local/samplesettings.txt" , "r"); //Open file for reading - fseek(samplesettings, 0 ,SEEK_SET); //Sets cursor to start of file - fscanf(samplesettings,"%s\n", value1);//Scans fo the value of N - pc.printf("%s\n", value1);//Prints the value of N - fclose(samplesettings); //Close file - break; - - } - else if(!strcmp(res,"3")==0)//If user command recieved is 3 then write the time settings into a file - { - char T[1000];//To store value of T recieved from matlab - pc.scanf("%s\n",&T);//To scan value of T from matlab - timesettings = fopen("/local/timesettings.txt", "w");//Open file for writing - fprintf(timesettings,"%s\n",T); //Writes settings values onto file - fclose(timesettings);//Close file - break; - - } - else if(!strcmp(res,"4")==0)//If 4 is recieved as a user command then read the time settings - { - //Define variables for output - char value2[100]; - samplesettings = fopen("/local/timesettings.txt" , "r"); //Open file for reading - fseek(timesettings, 0 ,SEEK_SET); //Sets cursor to start of file - fscanf(timesettings,"%s\n", value2);//Reads the time period from the file - pc.printf("%s\n", value2);//Outsputs the time period - fclose(timesettings); //Close file - break; - } - else if(!strcmp(res,"5")==0)//If the user command is 5 then we collect data - { - //Define vaiables - adxl362.init_spi(); - adxl362.init_adxl362(); - wait(0.1); - int8_t xdata, ydata, zdata; - for(int i=0; i<atoi(N); i++) - { - adxl362.ACC_GetXYZ8(&xdata,&ydata,&zdata);//Fetch readings from accelerometer - pc.printf("%+04d\n", xdata); - pc.printf("%+04d\n", ydata); - pc.printf("%+04d\n", zdata); - wait(atoi(T)); - - //Open data file - FILE *data; - data = fopen("/local/data.txt", "w"); - //Data collection loop using sample rate and number of samples - for(int i=0; i<atoi(N); i++) - { - //Get accelerometer data - adxl362.ACC_GetXYZ8(&xdata,&ydata,&zdata); - //Print data to data file - fprintf(data, "%+04d", xdata); - fprintf(data, "%+04d", ydata); - fprintf(data, "%+04d", zdata); - //Attempt to print same data to MATLAB if connection is not null - if(pc != NULL) - { - pc.printf("%+04d\n", xdata); - pc.printf("%+04d\n", ydata); - pc.printf("%+04d\n", zdata); - } - //Pause to ensure sample rate - wait(atoi(T)); - } - fclose(data); - break; - - } - } - } + adxl362.ACC_GetXYZ8(&xdata,&ydata,&zdata);//Fetch readings from accelerometer + pc.printf("%+04d\n", xdata); + pc.printf("%+04d\n", ydata); + pc.printf("%+04d\n", zdata); + wait(atoi(T)); + + //Open data file + FILE *data; + data = fopen("/local/data.txt", "w"); + //Data collection loop using sample rate and number of samples + for(int i=0; i<atoi(N); i++) + { + //Get accelerometer data + adxl362.ACC_GetXYZ8(&xdata,&ydata,&zdata); + //Print data to data file + fprintf(data, "%+04d", xdata); + fprintf(data, "%+04d", ydata); + fprintf(data, "%+04d", zdata); + //Attempt to print same data to MATLAB if connection is not null + if(pc != NULL) + { + pc.printf("%+04d\n", xdata); + pc.printf("%+04d\n", ydata); + pc.printf("%+04d\n", zdata); + } + //Pause to ensure sample rate + wait(atoi(T)); + } + fclose(data); + break; + } + } + } } \ No newline at end of file