Nathaniel Honka / Mbed 2 deprecated Data-Management-Honka

Dependencies:   ExoController MODSERIAL SDFileSystem_HelloWorld UI-Honka mbed

Fork of Data Management by HEL's Angels

main.cpp

Committer:
mzling
Date:
2015-02-14
Revision:
8:f8fd59fe3e11
Parent:
4:46d4917f2464
Child:
9:26e200175e0e

File content as of revision 8:f8fd59fe3e11:

#include "mbed.h"
#include "MODSERIAL.h"
#include "initDatabed.h"
#include "UserInterface.h"

Timer t_debug;
UserInterface userInterface;


bool check_errfile()
{
    printf("checking errfile\r\n");
    /* This line causes error, reason unknown */
    int *errors = new int[1];
    printf("about to open errfile for reading");
    err.open_for_read("/sd/", "error.txt");
    err.read_from_start(1, errors);
    err.close();
    printf("Errors[0]: %d\r\n", errors[0]);
    if (errors[0] == 1) {
        return false;
    }
    printf("Sucess--no errors detected\r\n");
    return true;
}

bool check_sdcard()
{
    int testArray[] = {1, 2, 3, 4};
    //int badArray[] = {1,2,3,3};
    int *output = new int[4];
    printf("created output array\r\n");
    testFile.write(4, testArray);
    printf("wrote to testfile\r\n");
    //testFile.write(4, badArray); 
    testFile.read(4, output);
    printf("read to output\r\n");
    for (int i = 0; i < 4; i++) {
        if (testArray[i] != output[i]) {
            printf("ERROR--test read failed\r\n");
            int error[] = {0x0001};
            //err("/sd/", "error.txt", true);
            err.open_for_write("/sd/", "error.txt");
            err.write(1, error);
            int *check = new int[1];
            err.read_from_start(1, check);
            err.close();
            printf("%d writtten to ERR\r\n", check[0]);
            return false;
        }
    }
    pc.printf("Success! Test read worked\r\n");
    return true;
}
/**
int main() {
//    check_sdcard();
  //  int[] errors = new int[10];
   // err.read(10, errors);
    //if (error.hasFlag) {
     //stop startup;
     //}
    printf("Hello world!\r\n");
    pc.baud(9600);
    interrupt.attach(&attachment, 0.002);
}

int main()
{
    pc.printf("ENTERING MAIN\r\n");
    if (!check_errfile() || !check_sdcard()) {
        pc.printf("Check failed...aborting power-on\r\n");
        return 1;
    }
}
*/

int main()
{
    printf("ENTERING MAIN\r\n");
    if (!check_errfile() || !check_sdcard()) {
   //   if (!check_sdcard()) {
        printf("Check failed...aborting power-on\r\n");
        return 1;
    }
    printf("ERRFILE/SDCARD checks passed!\r\n");
    int dataIn[4], dataOut[4], buffer;
    dataOut[0]=7;
    dataOut[1]=30;
    mainPower=1;//Don't turn on for initial testing
    initializeDatabed();
    comm.reply(dataOut[0]);//make this the first message
    printf("DataBed On!\r\n");
    int counter=0;
    int ii=0;
    //float blah=1;
    //t_debug.start();
    while (1) { //Run these functions as fast as possible

        if(comm.receive()) {
            dataIn[0] = comm.read();
            comm.reply(UI);         // Make this the next reply
            while(dataIn[0]==7) {
                if(comm.receive()) {
                    dataIn[1]=comm.read();
                    comm.reply(22);
                    break;
                    dataIn[0]=0;
                }
            }
            while(dataIn[1]==30) {
                if(comm.receive()) {
                    dataIn[2]=comm.read();
                    comm.reply(5);
                    pc.printf("Fault: %d, %d ", leftFault.read(), rightFault.read());
                    pc.printf("Data1: %u, Data2: %u, Data3: %u\r\n", dataIn[0], dataIn[1], dataIn[2]);
                    dataIn[1]=0;
                    break;
                }
            }
        if(comm.receive()) {
            buffer = comm.read();
            if(buffer==0xFE) { //end of message
                ii=0;
                dataIn[3]=buffer;
                comm.reply(5);
                counter++;
                if(BMS.read(msg)) {
                    if(msg.id==0x18FF8320) {
                        //pc.printf("%x\r\n",msg.data[0]);
                    }
                }
                phone.attachment();
            } else if(buffer==0xFF) { //beginning of message
                ii=1;
                dataIn[0]=buffer;
                comm.reply(UI);
            } else if(ii==1) {
                ii++;
                dataIn[1]=buffer;
                comm.reply(52);
            } else if(ii==2) {
                ii++;
                dataIn[2]=buffer;
                comm.reply(25);
            }

        }
        if(counter>=15) {
            UI=0;//set the UI to 0 unless it's changed in checkUI_Xbee()
            userInterface.readBuffer();
            userInterface.checkUI_XBee(); //check UI from Xbee
            xbeeUI.rxBufferFlush();
            counter=0;
           // pc.printf("UI: %d\r\n", UI);
        }
        counter++;
      }

    }
}