Swimate V2 without RTOS code
Dependencies: Adafruit_GFX_128x64 DS3231 PinDetect SDFileSystem USBDevice mbed RealtimeMath MODSERIAL
main.cpp
- Committer:
- ellingjp
- Date:
- 2014-05-08
- Revision:
- 1:93723df61425
- Parent:
- 0:cd1fe4f0ed39
File content as of revision 1:93723df61425:
#include "main.h" #include "mbed.h" #include "rtos.h" #include "USBSerial.h" #include "Adafruit_SSD1306.h" #include "MPU6050_6Axis_MotionApps20.h" #include "SDFileSystem.h" #include "output.h" #include "threads.h" #include "signals.h" // SD Card SDFileSystem sd(P0_21, P0_22, P1_15, P1_19, "sd"); // MOSI, MISO, SCLK, SSEL SPI1 // LED for debug DigitalOut led(LED1); // Timer Timer totalTime; Timer captureTime; // Switch InterruptIn captureSwitch(P0_16); // Main thread reference osThreadId mainThreadID; // State enum state {IDLE, CAPTURE}; enum state State; // Forward declarations void die(int flash_rate_s); bool log_open(); void log_close(); void captureSwitchISR() { // used for debouncing static int prev_time = 0; if (totalTime.read_ms() - prev_time < 200) return; // State = (State == IDLE) ? CAPTURE : IDLE; osSignalSet(mainThreadID, SIG_CAPTURE); prev_time = totalTime.read_ms(); } int main(void) { totalTime.start(); captureSwitch.mode(PullUp); captureSwitch.rise(captureSwitchISR); mainThreadID = osThreadGetId(); while (true) { Thread::wait(SIG_CAPTURE); FILE *logfile = log_open(); Thread get_data_thread(get_data, logfile, osPriority priority=osPriorityHigh, DEFAULT_STACK_SIZE, NULL); Thread::wait(SIG_CAPTURE); get_data.terminate(); } } /* Halts program and flashes LED at specified rate */ void die(int flash_rate_s) { while (1) { led = 1; wait(flash_rate_s/2); led = 0; wait(flash_rate_s/2); } } /* Blocks until logfile is successfully opened * There are cases where this will hang forever and need to reset (unavoidable) */ FILE *log_open() { FILE *logFile = fopen(LOG_FILE, "a"); while (logFile == NULL) { PC_PRINTLN("Error opening SD card!"); OLED_PRINTP("ERROR: SD (retry?)", 0, 50); } return logFile; } void log_close() { if (logFile != NULL) fclose(logFile); } void log_acceleration(VectorInt16 data) { fprintf(logFile, "%d, %d,%d,%d\n", captureTime.read_ms(), data.x, data.y, data.z); }