FBRLogger final version

Dependencies:   EthernetInterface MSCAN Nanopb SDFileSystem mbed-rtos mbed

main.cpp

Committer:
intrinseca
Date:
2013-02-17
Revision:
3:32206cf84eb4
Parent:
2:2400fab06b33
Child:
4:66928695da01

File content as of revision 3:32206cf84eb4:

#include "SDHCFileSystem.h"
#include "CANComms.h"
#include "State.h"
#include <stdint.h>
#include <fstream>
#include <iomanip>

#define LOGGING_INTERVAL    0.1
#define ANALOG_SCALE        3.3

State car;
CANComms* can;

SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board

AnalogIn analogInputs[] = {p15, p16, p17, p18, p19, p20};

Ticker sample;

char logFileName[50];

void writeLog_string(const char *data);
void writeLog_data(const char *data, unsigned char length);

bool file_exists(const char * filename)
{
    if (FILE * file = fopen(filename, "r")) {
        fclose(file);
        return true;
    }
    return false;
}

void take_sample()
{
    ofstream out;

    float value;

    out.open(logFileName);

    // Write the Analog Sensors
    for(int i = 0; i < 6; i++) {
        value = analogInputs[i].read() * ANALOG_SCALE;
        out << setw(10) << value << ",";
    }

    //Write the ECU data (in binary form)
    out.write(reinterpret_cast<char*>(&car), sizeof(State));
    
    out << endl;
    out.close();
}

int main()
{
    char logIndex = 0;

    printf("FBR CAN Data Logger\n");

    mkdir("/sd/fbr", 0777);

    do {
        sprintf(&logFileName[0], "/sd/fbr/log.%d", logIndex);
        logIndex++;
    } while(file_exists(&logFileName[0]));

    printf("Log File: %s\n", &logFileName[0]);

    printf("Listening Started\n");

    can = new CANComms(&car, true, LOGGING_INTERVAL);
    sample.attach(&take_sample, LOGGING_INTERVAL);

    while(true) {
        __wfi();
    }
}