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