FBRLogger final version

Dependencies:   EthernetInterface MSCAN Nanopb SDFileSystem mbed-rtos mbed

main.cpp

Committer:
intrinseca
Date:
2013-02-17
Revision:
2:2400fab06b33
Parent:
1:5f51069d5e09
Child:
3:32206cf84eb4

File content as of revision 2:2400fab06b33:

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

State car;
CANComms* can;

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

AnalogIn accX(p19);
AnalogIn accY(p20);

Ticker sample;

char logFileName[50];

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

void writeLog_string(const char *data)
{
    writeLog_data(data, strlen(data));
}

void writeLog_data(const char *data, unsigned char length)
{
    /*FILE *logFile;
    
    logFile = fopen(&logFileName[0], "a");
    
    if(logFile == NULL)
    {
        error("Could not open file for write\n");
    }
    
    fwrite(data, 1,  length, logFile);
    
    fclose(logFile);*/
}

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

void take_sample()
{
    float x;
    float y;
    
    x = accX.read();
    y = accY.read();
    
    printf("X: %4.3f Y:%4.3f\n", x, y);
}

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 = CANComms(&car);

    sample.attach(&take_sample, 0.5);

    while(true)
    {
        __wfi();
    }
}