Fork for marginal Data Management modifications.

Dependencies:   ExoController MODSERIAL SDFileSystem_HelloWorld UI-Honka mbed

Fork of Data Management by HEL's Angels

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MODSERIAL.h"
00003 #include "initDatabed.h"
00004 #include "UserInterface.h"
00005 
00006 Timer t_debug;
00007 UserInterface userInterface;
00008 
00009 
00010 bool check_errfile()
00011 {
00012     //printf("checking errfile\r\n");
00013     /* This line causes error, reason unknown */
00014     int *errors = new int[1];
00015     err.open_for_read("/sd/", "error.txt");
00016     
00017     //printf("Opened ERR for reading\r\n");
00018     err.read_from_start(1, errors);
00019     err.close();
00020     //printf("Errors[0]: %d\r\n", errors[0]);
00021     if (errors[0] == 1) {
00022         return false;
00023     }
00024    // printf("Sucess--no errors detected\r\n");
00025     return true;
00026 }
00027 
00028 bool check_sdcard()
00029 {
00030     int testArray[] = {1, 2, 3, 4};
00031     //int badArray[] = {1,2,3,3};
00032     int *output = new int[4];
00033     //printf("created output array\r\n");
00034     testFile.write(4, testArray);
00035     //printf("wrote to testfile\r\n");
00036     //testFile.write(4, badArray); 
00037     testFile.read(4, output);
00038     //printf("read to output\r\n");
00039     for (int i = 0; i < 4; i++) {
00040         if (testArray[i] != output[i]) {
00041             //printf("ERROR--test read failed\r\n");
00042             int error[] = {0x0001};
00043             //err("/sd/", "error.txt", true);
00044             err.open_for_write("/sd/", "error.txt");
00045             err.write(1, error);
00046             int *check = new int[1];
00047             err.read_from_start(1, check);
00048             err.close();
00049             //printf("%d writtten to ERR\r\n", check[0]);
00050             return false;
00051         }
00052     }
00053     //pc.printf("Success! Test read worked\r\n");
00054     return true;
00055 }
00056 /**
00057 void attachment() {
00058     phone.attachment();
00059 }
00060 
00061 int main() {
00062 //    check_sdcard();
00063   //  int[] errors = new int[10];
00064    // err.read(10, errors);
00065     //if (error.hasFlag) {
00066      //stop startup;
00067      //}
00068     printf("Hello world!\r\n");
00069    // pc.baud(9600);
00070     interrupt.attach(&attachment, 0.002);
00071 }
00072 /**
00073 int main()
00074 {
00075     pc.printf("ENTERING MAIN\r\n");
00076     if (!check_errfile() || !check_sdcard()) {
00077         pc.printf("Check failed...aborting power-on\r\n");
00078         return 1;
00079     }
00080 }
00081 */
00082 
00083 void reset_card() {
00084     int error[] = {0x0000};
00085     //err("/sd/", "error.txt", true);
00086     err.open_for_write("/sd/", "error.txt");
00087     err.write(1, error);
00088     err.close();
00089 }
00090 
00091 int main()
00092 {
00093     //printf("ENTERING MAIN\r\n");
00094     if (!check_errfile() || !check_sdcard()) {
00095       //if (!check_sdcard()) {
00096        // reset_card();
00097         printf("Check failed...aborting power-on\r\n");
00098         return 1;
00099     }
00100     //printf("ERRFILE/SDCARD checks passed!\r\n");
00101     int dataIn[4], dataOut[4], buffer;
00102     dataOut[0]=7;
00103     dataOut[1]=30;
00104     mainPower=1;//Don't turn on for initial testing
00105     initializeDatabed();
00106     comm.reply(dataOut[0]);//make this the first message
00107     printf("DataBed On!\r\n");
00108     int counter=0;
00109     int ii=0;
00110     //float blah=1;
00111     //t_debug.start();
00112     while (1) { //Run these functions as fast as possible
00113         boardLed1 = !boardLed1;
00114         phone.attachment();
00115         if(comm.receive()) {
00116             //boardLed1 = !boardLed1;
00117             dataIn[0] = comm.read();
00118             comm.reply(UI);         // Make this the next reply
00119             while(dataIn[0]==7) {
00120                 if(comm.receive()) {
00121                     dataIn[1]=comm.read();
00122                     comm.reply(22);
00123                     break;
00124                     dataIn[0]=0;
00125                 }
00126             }
00127             while(dataIn[1]==30) {
00128                 if(comm.receive()) {
00129                     dataIn[2]=comm.read();
00130                     comm.reply(5);
00131                     pc.printf("Fault: %d, %d ", leftFault.read(), rightFault.read());
00132                     pc.printf("Data1: %u, Data2: %u, Data3: %u\r\n", dataIn[0], dataIn[1], dataIn[2]);
00133                     dataIn[1]=0;
00134                     break;
00135                 }
00136             }
00137         if(comm.receive()) {
00138             //boardLed2 = !boardLed2;
00139             buffer = comm.read();
00140             if(buffer==0xFE) { //end of message
00141                 ii=0;
00142                 dataIn[3]=buffer;
00143                 comm.reply(5);
00144                 counter++;
00145                 if(BMS.read(msg)) {
00146                     if(msg.id==0x18FF8320) {
00147                         //pc.printf("%x\r\n",msg.data[0]);
00148                     }
00149                 }
00150                 //boardLed1 != boardLed1;
00151                // pc.printf("Entering attachment\r\n");
00152                // phone.attachment();
00153             } else if(buffer==0xFF) { //beginning of message
00154                 ii=1;
00155                 dataIn[0]=buffer;
00156                 comm.reply(UI);
00157             } else if(ii==1) {
00158                 ii++;
00159                 dataIn[1]=buffer;
00160                 comm.reply(52);
00161             } else if(ii==2) {
00162                 ii++;
00163                 dataIn[2]=buffer;
00164                 comm.reply(25);
00165             }
00166 
00167         }
00168         //boardLed1 != boardLed1;
00169         if(counter>=15) {
00170             UI = BUTTON_NONE; //set the UI to BUTTON_NONE unless it's changed in checkUI_Xbee()
00171             userInterface.readBuffer();
00172             userInterface.checkUI_XBee(); //check UI from Xbee
00173             xbeeUI.rxBufferFlush();
00174             counter=0;
00175            // pc.printf("UI: %d\r\n", UI);
00176         }
00177         counter++;
00178       }
00179         
00180     }
00181     
00182 }