Jake Billingham
/
computingproject
JKDVSK`
Fork of mbed_menu by
menu_menu.cpp@1:4938aa677cb3, 2017-11-08 (annotated)
- Committer:
- mzpf46
- Date:
- Wed Nov 08 10:46:19 2017 +0000
- Revision:
- 1:4938aa677cb3
- Parent:
- 0:8d536d5c58c6
- Child:
- 2:2b7eb44d597d
Publush;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mzpf46 | 0:8d536d5c58c6 | 1 | #include "mbed.h" |
mzpf46 | 0:8d536d5c58c6 | 2 | #include "stdio.h" |
mzpf46 | 0:8d536d5c58c6 | 3 | #include "math.h" |
mzpf46 | 0:8d536d5c58c6 | 4 | #include "ADXL362.h" |
mzpf46 | 0:8d536d5c58c6 | 5 | #define HIGH 1 |
mzpf46 | 0:8d536d5c58c6 | 6 | #define LOW 0 |
mzpf46 | 0:8d536d5c58c6 | 7 | DigitalOut myled1(LED1); /* LED1 */ |
mzpf46 | 0:8d536d5c58c6 | 8 | Serial pc(USBTX, USBRX); |
mzpf46 | 0:8d536d5c58c6 | 9 | ADXL362 adxl362(p11, p12, p13, p10); |
mzpf46 | 0:8d536d5c58c6 | 10 | LocalFileSystem local("local"); |
mzpf46 | 0:8d536d5c58c6 | 11 | int matlabdata; |
mzpf46 | 0:8d536d5c58c6 | 12 | char usercmd; |
mzpf46 | 0:8d536d5c58c6 | 13 | |
mzpf46 | 0:8d536d5c58c6 | 14 | char N[1000]; //Number of samples |
mzpf46 | 0:8d536d5c58c6 | 15 | char T [1000]; //Sample period |
mzpf46 | 0:8d536d5c58c6 | 16 | |
mzpf46 | 0:8d536d5c58c6 | 17 | |
mzpf46 | 0:8d536d5c58c6 | 18 | int main() |
mzpf46 | 0:8d536d5c58c6 | 19 | { |
mzpf46 | 0:8d536d5c58c6 | 20 | FILE *samplesettings; //Create file with pointer |
mzpf46 | 1:4938aa677cb3 | 21 | FILE *timesettings; |
mzpf46 | 0:8d536d5c58c6 | 22 | |
mzpf46 | 0:8d536d5c58c6 | 23 | |
mzpf46 | 1:4938aa677cb3 | 24 | char res[100];//string to store user command |
mzpf46 | 1:4938aa677cb3 | 25 | |
mzpf46 | 1:4938aa677cb3 | 26 | matlabdata=pc.scanf("%s",res);//Get user command from matlab |
mzpf46 | 1:4938aa677cb3 | 27 | |
mzpf46 | 1:4938aa677cb3 | 28 | while(1) |
mzpf46 | 1:4938aa677cb3 | 29 | { |
mzpf46 | 1:4938aa677cb3 | 30 | if(!strcmp(res,"1")==0)//if user command in matlab is 1 write the sample settings into a file |
mzpf46 | 0:8d536d5c58c6 | 31 | { |
mzpf46 | 0:8d536d5c58c6 | 32 | myled1 = HIGH; |
mzpf46 | 1:4938aa677cb3 | 33 | char N[1000];//To store the number of samples which is received from the matlab |
mzpf46 | 1:4938aa677cb3 | 34 | pc.scanf("%s\n",&N);//to get the number of samples input from the matlab |
mzpf46 | 0:8d536d5c58c6 | 35 | samplesettings = fopen("/local/samplesettings.txt", "w");//Open file for writing |
mzpf46 | 1:4938aa677cb3 | 36 | fprintf(samplesettings,"%s\n",N); //Writes settings values onto file |
mzpf46 | 1:4938aa677cb3 | 37 | fclose(samplesettings);//close file |
mzpf46 | 0:8d536d5c58c6 | 38 | break; |
mzpf46 | 0:8d536d5c58c6 | 39 | |
mzpf46 | 1:4938aa677cb3 | 40 | |
mzpf46 | 0:8d536d5c58c6 | 41 | } |
mzpf46 | 1:4938aa677cb3 | 42 | else if(!strcmp(res,"2")==0)//If user command is 2 search the file for sample settings |
mzpf46 | 0:8d536d5c58c6 | 43 | { |
mzpf46 | 0:8d536d5c58c6 | 44 | //Define variables for output |
mzpf46 | 1:4938aa677cb3 | 45 | char value1[1000]; |
mzpf46 | 0:8d536d5c58c6 | 46 | samplesettings = fopen("/local/samplesettings.txt" , "r"); //Open file for reading |
mzpf46 | 1:4938aa677cb3 | 47 | fseek(samplesettings, 0 ,SEEK_SET); //Sets cursor to start of file |
mzpf46 | 1:4938aa677cb3 | 48 | fscanf(samplesettings,"%s\n", value1);//Scans fo the value of N |
mzpf46 | 1:4938aa677cb3 | 49 | pc.printf("%s\n", value1);//Prints the value of N |
mzpf46 | 0:8d536d5c58c6 | 50 | fclose(samplesettings); //Close file |
mzpf46 | 1:4938aa677cb3 | 51 | break; |
mzpf46 | 1:4938aa677cb3 | 52 | |
mzpf46 | 0:8d536d5c58c6 | 53 | } |
mzpf46 | 1:4938aa677cb3 | 54 | else if(!strcmp(res,"3")==0)//If user command recieved is 3 then write the time settings into a file |
mzpf46 | 1:4938aa677cb3 | 55 | { |
mzpf46 | 1:4938aa677cb3 | 56 | char T[1000];//To store value of T recieved from matlab |
mzpf46 | 1:4938aa677cb3 | 57 | pc.scanf("%s\n",&T);//To scan value of T from matlab |
mzpf46 | 1:4938aa677cb3 | 58 | timesettings = fopen("/local/timesettings.txt", "w");//Open file for writing |
mzpf46 | 1:4938aa677cb3 | 59 | fprintf(timesettings,"%s\n",T); //Writes settings values onto file |
mzpf46 | 1:4938aa677cb3 | 60 | fclose(timesettings);//Close file |
mzpf46 | 1:4938aa677cb3 | 61 | break; |
mzpf46 | 1:4938aa677cb3 | 62 | |
mzpf46 | 1:4938aa677cb3 | 63 | } |
mzpf46 | 1:4938aa677cb3 | 64 | else if(!strcmp(res,"4")==0)//If 4 is recieved as a user command then read the time settings |
mzpf46 | 1:4938aa677cb3 | 65 | { |
mzpf46 | 1:4938aa677cb3 | 66 | //Define variables for output |
mzpf46 | 1:4938aa677cb3 | 67 | char value2[100]; |
mzpf46 | 1:4938aa677cb3 | 68 | samplesettings = fopen("/local/timesettings.txt" , "r"); //Open file for reading |
mzpf46 | 1:4938aa677cb3 | 69 | fseek(timesettings, 0 ,SEEK_SET); //Sets cursor to start of file |
mzpf46 | 1:4938aa677cb3 | 70 | fscanf(timesettings,"%s\n", value2);//Reads the time period from the file |
mzpf46 | 1:4938aa677cb3 | 71 | pc.printf("%s\n", value2);//Outsputs the time period |
mzpf46 | 1:4938aa677cb3 | 72 | fclose(timesettings); //Close file |
mzpf46 | 1:4938aa677cb3 | 73 | break; |
mzpf46 | 1:4938aa677cb3 | 74 | } |
mzpf46 | 1:4938aa677cb3 | 75 | else if(!strcmp(res,"5")==0)//If the user command is 5 then we collect data |
mzpf46 | 1:4938aa677cb3 | 76 | { |
mzpf46 | 1:4938aa677cb3 | 77 | //Define vaiables |
mzpf46 | 1:4938aa677cb3 | 78 | adxl362.init_spi(); |
mzpf46 | 1:4938aa677cb3 | 79 | adxl362.init_adxl362(); |
mzpf46 | 1:4938aa677cb3 | 80 | wait(0.1); |
mzpf46 | 1:4938aa677cb3 | 81 | int8_t xdata, ydata, zdata; |
mzpf46 | 1:4938aa677cb3 | 82 | for(int i=0; i<atoi(N); i++) |
mzpf46 | 1:4938aa677cb3 | 83 | { |
mzpf46 | 1:4938aa677cb3 | 84 | adxl362.ACC_GetXYZ8(&xdata,&ydata,&zdata);//Fetch readings from accelerometer |
mzpf46 | 1:4938aa677cb3 | 85 | pc.printf("%+04d\n", xdata); |
mzpf46 | 1:4938aa677cb3 | 86 | pc.printf("%+04d\n", ydata); |
mzpf46 | 1:4938aa677cb3 | 87 | pc.printf("%+04d\n", zdata); |
mzpf46 | 1:4938aa677cb3 | 88 | wait(atoi(T)); |
mzpf46 | 1:4938aa677cb3 | 89 | |
mzpf46 | 1:4938aa677cb3 | 90 | //Open data file |
mzpf46 | 1:4938aa677cb3 | 91 | FILE *data; |
mzpf46 | 1:4938aa677cb3 | 92 | data = fopen("/local/data.txt", "w"); |
mzpf46 | 1:4938aa677cb3 | 93 | //Data collection loop using sample rate and number of samples |
mzpf46 | 1:4938aa677cb3 | 94 | for(int i=0; i<atoi(N); i++) |
mzpf46 | 1:4938aa677cb3 | 95 | { |
mzpf46 | 1:4938aa677cb3 | 96 | //Get accelerometer data |
mzpf46 | 1:4938aa677cb3 | 97 | adxl362.ACC_GetXYZ8(&xdata,&ydata,&zdata); |
mzpf46 | 1:4938aa677cb3 | 98 | //Print data to data file |
mzpf46 | 1:4938aa677cb3 | 99 | fprintf(data, "%+04d", xdata); |
mzpf46 | 1:4938aa677cb3 | 100 | fprintf(data, "%+04d", ydata); |
mzpf46 | 1:4938aa677cb3 | 101 | fprintf(data, "%+04d", zdata); |
mzpf46 | 1:4938aa677cb3 | 102 | //Attempt to print same data to MATLAB if connection is not null |
mzpf46 | 1:4938aa677cb3 | 103 | if(pc != NULL) |
mzpf46 | 1:4938aa677cb3 | 104 | { |
mzpf46 | 1:4938aa677cb3 | 105 | pc.printf("%+04d\n", xdata); |
mzpf46 | 1:4938aa677cb3 | 106 | pc.printf("%+04d\n", ydata); |
mzpf46 | 1:4938aa677cb3 | 107 | pc.printf("%+04d\n", zdata); |
mzpf46 | 1:4938aa677cb3 | 108 | } |
mzpf46 | 1:4938aa677cb3 | 109 | //Pause to ensure sample rate |
mzpf46 | 1:4938aa677cb3 | 110 | wait(atoi(T)); |
mzpf46 | 1:4938aa677cb3 | 111 | } |
mzpf46 | 1:4938aa677cb3 | 112 | fclose(data); |
mzpf46 | 1:4938aa677cb3 | 113 | break; |
mzpf46 | 1:4938aa677cb3 | 114 | |
mzpf46 | 1:4938aa677cb3 | 115 | } |
mzpf46 | 1:4938aa677cb3 | 116 | } |
mzpf46 | 1:4938aa677cb3 | 117 | } |
mzpf46 | 0:8d536d5c58c6 | 118 | } |