Swimate V2 without RTOS code

Dependencies:   Adafruit_GFX_128x64 DS3231 PinDetect SDFileSystem USBDevice mbed RealtimeMath MODSERIAL

main.cpp

Committer:
ellingjp
Date:
2014-05-28
Revision:
9:a711b5b34d73
Parent:
8:8430a5c0914c
Child:
13:227a6cfd2097
Child:
15:002bac432234

File content as of revision 9:a711b5b34d73:

#include "main.h"
#include "mbed.h"
#include "PinDetect.h"
#include "USBSerial.h"
#include "Adafruit_SSD1306.h"
#include "SDFileSystem.h"
#include "receive_data.h"
#include "log_data.h"
#include "process_data.h"
#include "debug.h"
#include "SystemTime.h"
#include "sync.h"

// Capture button stuff
#define PIN_DETECT_SAMPLE_PERIOD_uS 20000   // 20 ms sample period
#define SYNC_HELD_SAMPLES   (SYNC_HOLD_TIME_MS * 1000 / PIN_DETECT_SAMPLE_PERIOD_uS)

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

// Mode button
PinDetect captureButton(P0_16, PullUp);

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

// This is used to ensure the rising edge after a hold is ignored
bool ignore_edge = false;
void buttonHeld() {
    if (State == IDLE)
        State = SYNC;
    else
        State = IDLE;
    
    // Button was held down, so don't count the next assert
    ignore_edge = true;
}

void buttonPressed() {
    // Don't ignore subsequent edges
    if (ignore_edge) {
        ignore_edge = false;
        return;
    }
    
    if (State == IDLE)
        State = CAPTURE;
    else if (State == CAPTURE)
        State = IDLE;
}

DigitalOut led(LED1);
int main(void)
{
    SystemTime::start();

    State = IDLE;
    
    // After button is held, the next rising edge will call buttonPressed.
    //   Because of this, inside the callbacks need to ensure that edge
    //   does not do anything unintended.
    captureButton.attach_deasserted_held(buttonHeld);
    captureButton.attach_asserted(buttonPressed);
    captureButton.setSampleFrequency();
    captureButton.setSamplesTillHeld(SYNC_HELD_SAMPLES);
    
    VectorInt16 *data;
    while (true) {
        if (State == IDLE){
            OLED_CLEAR();
            OLED_PRINTP("Idling...", 0, 0);
            PC_PRINTLN("Idling...");
        } else if (State == CAPTURE) {
            OLED_PRINTP("Starting capture...", 0, 0);
            OLED_PRINTP("Init SD card...", 0, 10);
            log_init();
            OLED_PRINTP("Init peak detect...", 0, 10);
            process_init();
            OLED_PRINTP("Init data receipt...", 0, 10);
            receive_init();           
            OLED_CLEAR();
            OLED_PRINTP("Capturing data...", 0, 0);
            
            
            while (State == CAPTURE) {
                data = receive_data();
                log_data(data);
                int split = process_data(data->x);
                if (split != UINT16_MAX) {
                    PC_PRINTLNF("Split time: %d", split);
                    OLED_PRINTPF("Split: %d", split, 0, 40);
                    log_data(split);
                }
            }
            
            receive_close();
            process_close();
            log_close();
        } else if (State == SYNC) {
            OLED_PRINTP("Ready to sync...", 0, 0);
//            sync_init();
//            while (State == SYNC)
//                sync();
        }
    }
}