Swimate V2 without RTOS code

Dependencies:   Adafruit_GFX_128x64 DS3231 PinDetect SDFileSystem USBDevice mbed RealtimeMath MODSERIAL

Committer:
ellingjp
Date:
Thu May 08 20:59:29 2014 +0000
Revision:
1:93723df61425
Parent:
0:cd1fe4f0ed39
Initial RTOS integration commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ellingjp 0:cd1fe4f0ed39 1 #include "main.h"
ellingjp 0:cd1fe4f0ed39 2 #include "mbed.h"
ellingjp 1:93723df61425 3 #include "rtos.h"
ellingjp 0:cd1fe4f0ed39 4 #include "USBSerial.h"
ellingjp 0:cd1fe4f0ed39 5 #include "Adafruit_SSD1306.h"
ellingjp 0:cd1fe4f0ed39 6 #include "MPU6050_6Axis_MotionApps20.h"
ellingjp 0:cd1fe4f0ed39 7 #include "SDFileSystem.h"
ellingjp 1:93723df61425 8 #include "output.h"
ellingjp 1:93723df61425 9 #include "threads.h"
ellingjp 1:93723df61425 10 #include "signals.h"
ellingjp 0:cd1fe4f0ed39 11
ellingjp 0:cd1fe4f0ed39 12 // SD Card
ellingjp 0:cd1fe4f0ed39 13 SDFileSystem sd(P0_21, P0_22, P1_15, P1_19, "sd"); // MOSI, MISO, SCLK, SSEL SPI1
ellingjp 0:cd1fe4f0ed39 14
ellingjp 0:cd1fe4f0ed39 15 // LED for debug
ellingjp 0:cd1fe4f0ed39 16 DigitalOut led(LED1);
ellingjp 0:cd1fe4f0ed39 17
ellingjp 0:cd1fe4f0ed39 18 // Timer
ellingjp 0:cd1fe4f0ed39 19 Timer totalTime;
ellingjp 0:cd1fe4f0ed39 20 Timer captureTime;
ellingjp 0:cd1fe4f0ed39 21
ellingjp 0:cd1fe4f0ed39 22 // Switch
ellingjp 0:cd1fe4f0ed39 23 InterruptIn captureSwitch(P0_16);
ellingjp 0:cd1fe4f0ed39 24
ellingjp 1:93723df61425 25 // Main thread reference
ellingjp 1:93723df61425 26 osThreadId mainThreadID;
ellingjp 0:cd1fe4f0ed39 27
ellingjp 0:cd1fe4f0ed39 28 // State
ellingjp 0:cd1fe4f0ed39 29 enum state {IDLE, CAPTURE};
ellingjp 0:cd1fe4f0ed39 30 enum state State;
ellingjp 0:cd1fe4f0ed39 31
ellingjp 0:cd1fe4f0ed39 32 // Forward declarations
ellingjp 0:cd1fe4f0ed39 33 void die(int flash_rate_s);
ellingjp 0:cd1fe4f0ed39 34 bool log_open();
ellingjp 0:cd1fe4f0ed39 35 void log_close();
ellingjp 0:cd1fe4f0ed39 36
ellingjp 0:cd1fe4f0ed39 37 void captureSwitchISR() {
ellingjp 0:cd1fe4f0ed39 38 // used for debouncing
ellingjp 0:cd1fe4f0ed39 39 static int prev_time = 0;
ellingjp 0:cd1fe4f0ed39 40
ellingjp 0:cd1fe4f0ed39 41 if (totalTime.read_ms() - prev_time < 200)
ellingjp 0:cd1fe4f0ed39 42 return;
ellingjp 0:cd1fe4f0ed39 43
ellingjp 1:93723df61425 44 // State = (State == IDLE) ? CAPTURE : IDLE;
ellingjp 1:93723df61425 45 osSignalSet(mainThreadID, SIG_CAPTURE);
ellingjp 0:cd1fe4f0ed39 46 prev_time = totalTime.read_ms();
ellingjp 0:cd1fe4f0ed39 47 }
ellingjp 0:cd1fe4f0ed39 48
ellingjp 0:cd1fe4f0ed39 49 int main(void)
ellingjp 0:cd1fe4f0ed39 50 {
ellingjp 0:cd1fe4f0ed39 51 totalTime.start();
ellingjp 0:cd1fe4f0ed39 52
ellingjp 0:cd1fe4f0ed39 53 captureSwitch.mode(PullUp);
ellingjp 0:cd1fe4f0ed39 54 captureSwitch.rise(captureSwitchISR);
ellingjp 0:cd1fe4f0ed39 55
ellingjp 1:93723df61425 56 mainThreadID = osThreadGetId();
ellingjp 1:93723df61425 57
ellingjp 0:cd1fe4f0ed39 58 while (true) {
ellingjp 1:93723df61425 59 Thread::wait(SIG_CAPTURE);
ellingjp 1:93723df61425 60
ellingjp 1:93723df61425 61 FILE *logfile = log_open();
ellingjp 1:93723df61425 62 Thread get_data_thread(get_data, logfile, osPriority priority=osPriorityHigh, DEFAULT_STACK_SIZE, NULL);
ellingjp 1:93723df61425 63
ellingjp 1:93723df61425 64 Thread::wait(SIG_CAPTURE);
ellingjp 1:93723df61425 65
ellingjp 1:93723df61425 66 get_data.terminate();
ellingjp 0:cd1fe4f0ed39 67 }
ellingjp 0:cd1fe4f0ed39 68 }
ellingjp 0:cd1fe4f0ed39 69
ellingjp 0:cd1fe4f0ed39 70 /* Halts program and flashes LED at specified rate */
ellingjp 0:cd1fe4f0ed39 71 void die(int flash_rate_s) {
ellingjp 0:cd1fe4f0ed39 72 while (1) {
ellingjp 0:cd1fe4f0ed39 73 led = 1;
ellingjp 0:cd1fe4f0ed39 74 wait(flash_rate_s/2);
ellingjp 0:cd1fe4f0ed39 75 led = 0;
ellingjp 0:cd1fe4f0ed39 76 wait(flash_rate_s/2);
ellingjp 0:cd1fe4f0ed39 77 }
ellingjp 0:cd1fe4f0ed39 78 }
ellingjp 0:cd1fe4f0ed39 79
ellingjp 1:93723df61425 80 /* Blocks until logfile is successfully opened
ellingjp 1:93723df61425 81 * There are cases where this will hang forever and need to reset (unavoidable) */
ellingjp 1:93723df61425 82 FILE *log_open() {
ellingjp 1:93723df61425 83 FILE *logFile = fopen(LOG_FILE, "a");
ellingjp 1:93723df61425 84 while (logFile == NULL) {
ellingjp 1:93723df61425 85 PC_PRINTLN("Error opening SD card!");
ellingjp 1:93723df61425 86 OLED_PRINTP("ERROR: SD (retry?)", 0, 50);
ellingjp 0:cd1fe4f0ed39 87 }
ellingjp 1:93723df61425 88 return logFile;
ellingjp 0:cd1fe4f0ed39 89 }
ellingjp 0:cd1fe4f0ed39 90
ellingjp 0:cd1fe4f0ed39 91 void log_close() {
ellingjp 0:cd1fe4f0ed39 92 if (logFile != NULL)
ellingjp 0:cd1fe4f0ed39 93 fclose(logFile);
ellingjp 0:cd1fe4f0ed39 94 }
ellingjp 0:cd1fe4f0ed39 95
ellingjp 0:cd1fe4f0ed39 96 void log_acceleration(VectorInt16 data) {
ellingjp 0:cd1fe4f0ed39 97 fprintf(logFile, "%d, %d,%d,%d\n", captureTime.read_ms(), data.x, data.y, data.z);
ellingjp 0:cd1fe4f0ed39 98 }