Swimate V2 without RTOS code

Dependencies:   Adafruit_GFX_128x64 DS3231 PinDetect SDFileSystem USBDevice mbed RealtimeMath MODSERIAL

main.cpp

Committer:
ellingjp
Date:
2014-05-17
Revision:
7:33a74adff0ff
Parent:
4:b962f5a783a1
Child:
8:8430a5c0914c

File content as of revision 7:33a74adff0ff:

#include "main.h"
#include "mbed.h"
#include "USBSerial.h"
#include "Adafruit_SSD1306.h"
#include "SDFileSystem.h"
#include "receive_data.h"
#include "debug.h"

// Display
#ifdef OLED_DEBUG
SPI spi0(P0_9, NC, P0_10); // mosi, miso, sclk
Adafruit_SSD1306 oled(spi0, P0_11, P0_12, P0_13); // DC, RST, CS
#endif

// SD Card
SDFileSystem sd(P0_21, P0_22, P1_15, P1_19, "sd"); // MOSI, MISO, SCLK, SSEL SPI1

// Logging vars
FILE *logFile;

// Timer
Timer totalTime;
Timer captureTime;

// Switch
InterruptIn captureSwitch(P0_16);


// State
enum state {IDLE, CAPTURE};
enum state State;

void captureSwitchISR() {
    // used for debouncing
    static int prev_time = 0;
    int curr_time = totalTime.read_ms();
    
    // Only change state after an amount of time
    // Note: abs value is necessary in case of 
    //   overflows
    if (abs(curr_time - prev_time) > 200)
        State = (State == IDLE) ? CAPTURE : IDLE;
        
    prev_time = curr_time;
}

int main(void)
{
    totalTime.start();
    
    State = IDLE;
    captureSwitch.mode(PullUp);
    captureSwitch.rise(captureSwitchISR);   
    
    while (true) {
        if (State == IDLE){
            PC_PRINT("Idling\r");
        } else if (State == CAPTURE) {
            receive_init();
            while (State == CAPTURE) {
                __disable_irq();
                receive_data();
                __enable_irq();
            }
            // data = receive_data();
            // log_data(data);
            // split = get_split(data);
            // display_split;
        } 
        // else if (State == SYNC) {
        // begin_sync();
        // }
    }
}



/* Returns false on failure, true otherwise */
//bool log_open() {
//    logFile = fopen(LOG_FILE, "a");
//    if (logFile == NULL) {
//        PC_PRINTLNF("SD card initialization error: Failed to open %s", LOG_FILE);
//        return false;
//    }
//    fprintf(logFile, "---- BEGIN NEW DATASET ----\n");
//    return true;
//}