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