menu for accelerometer

Dependencies:   ADXL362 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers menu_menu.cpp Source File

menu_menu.cpp

00001 #include "mbed.h"
00002 #include "stdio.h"
00003 #include "math.h"
00004 #include "ADXL362.h"
00005 #define HIGH 1
00006 #define LOW 0
00007 DigitalOut myled1(LED1); /* LED1 */
00008 Serial pc(USBTX, USBRX);
00009 ADXL362 adxl362(p11, p12, p13, p10);
00010 LocalFileSystem local("local"); 
00011 int matlabdata;
00012 char usercmd;
00013     
00014 char N[1000]; //Number of samples
00015 char T [1000]; //Sample period 
00016 
00017 
00018 int main()
00019 {
00020     FILE *samplesettings; //Create file with pointer
00021     FILE *timesettings;
00022     
00023     
00024     char res[100];//string to store user command
00025         
00026     matlabdata=pc.scanf("%s",res);//Get user command from matlab
00027    
00028 while(1)
00029 {    
00030     if(!strcmp(res,"1")==0)//if user command in matlab is 1 write the sample settings into a file 
00031         {  
00032         myled1 = HIGH;
00033         char N[1000];//To store the number of samples which is received from the matlab
00034         pc.scanf("%s\n",&N);//to get the number of  samples input from the matlab
00035         samplesettings = fopen("/local/samplesettings.txt", "w");//Open file for writing
00036         fprintf(samplesettings,"%s\n",N); //Writes settings values onto file
00037         fclose(samplesettings);//close file
00038         break;
00039 
00040 
00041             }
00042             else if(!strcmp(res,"2")==0)//If user command is 2 search the file for sample settings
00043             {
00044                         //Define variables for output
00045                         char value1[1000];
00046                         samplesettings = fopen("/local/samplesettings.txt" , "r"); //Open file for reading
00047                         fseek(samplesettings, 0 ,SEEK_SET); //Sets cursor to start of file
00048                         fscanf(samplesettings,"%s\n", value1);//Scans fo the value of N
00049                         pc.printf("%s\n", value1);//Prints the value of N
00050                         fclose(samplesettings); //Close file
00051                         break;
00052 
00053                         }
00054                         else if(!strcmp(res,"3")==0)//If user command recieved is 3 then write the time settings into a file
00055                         {
00056                                     char T[1000];//To store value of T recieved from matlab
00057                                     pc.scanf("%s\n",&T);//To scan value of T from matlab
00058                                     timesettings = fopen("/local/timesettings.txt", "w");//Open file for writing
00059                                     fprintf(timesettings,"%s\n",T); //Writes settings values onto file
00060                                     fclose(timesettings);//Close file
00061                                     break;
00062                                     
00063                                     }
00064                                     else if(!strcmp(res,"4")==0)//If 4 is recieved as a user command then read the time settings 
00065                                     {
00066                                     //Define variables for output
00067                                     char value2[100];
00068                                     samplesettings = fopen("/local/timesettings.txt" , "r"); //Open file for reading
00069                                     fseek(timesettings, 0 ,SEEK_SET); //Sets cursor to start of file
00070                                     fscanf(timesettings,"%s\n", value2);//Reads the time period from the file
00071                                     pc.printf("%s\n", value2);//Outsputs the time period 
00072                                     fclose(timesettings); //Close file
00073                                     break; 
00074                                     }
00075                                     else if(!strcmp(res,"5")==0)//If the user command is 5 then we collect data 
00076                                     {
00077                                         //Define vaiables 
00078                                         adxl362.init_spi();
00079                                         adxl362.init_adxl362();
00080                                         wait(0.1);
00081                                         int8_t xdata, ydata, zdata;
00082                                         for(int i=0; i<atoi(N); i++)
00083                                         {
00084                                             adxl362.ACC_GetXYZ8(&xdata,&ydata,&zdata);//Fetch readings from accelerometer
00085                                             pc.printf("%+04d\n", xdata);
00086                                             pc.printf("%+04d\n", ydata);
00087                                             pc.printf("%+04d\n", zdata);
00088                                             wait(atoi(T));
00089                                             
00090                                             //Open data file
00091                                             FILE *data;
00092                                             data = fopen("/local/data.txt", "w");
00093                                             //Data collection loop using sample rate and number of samples 
00094                                             for(int i=0; i<atoi(N); i++)
00095                                             {
00096                                                 //Get accelerometer data
00097                                                 adxl362.ACC_GetXYZ8(&xdata,&ydata,&zdata);
00098                                                 //Print data to data file
00099                                                 fprintf(data, "%+04d", xdata);
00100                                                 fprintf(data, "%+04d", ydata);
00101                                                 fprintf(data, "%+04d", zdata);
00102                                                 //Attempt to print same data to MATLAB if connection is not null
00103                                                 if(pc != NULL)
00104                                                 {
00105                                                     pc.printf("%+04d\n", xdata);
00106                                                     pc.printf("%+04d\n", ydata);
00107                                                     pc.printf("%+04d\n", zdata);
00108                                                     }
00109                                                     //Pause to ensure sample rate
00110                                                     wait(atoi(T));
00111                                                     }
00112                                                     fclose(data);
00113                                                     break;
00114                                                     
00115      }
00116      }
00117      }                                               
00118 }