Swimate V2 without RTOS code

Dependencies:   Adafruit_GFX_128x64 DS3231 PinDetect SDFileSystem USBDevice mbed RealtimeMath MODSERIAL

Committer:
ellingjp
Date:
Mon Jun 09 04:55:16 2014 +0000
Revision:
24:f2503d1256ad
Parent:
22:9350752f5414
Using RTC filenames

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ellingjp 22:9350752f5414 1 #include "mbed.h"
ellingjp 22:9350752f5414 2 #include "log_data.h"
ellingjp 22:9350752f5414 3 #include "SDFileSystem.h"
ellingjp 22:9350752f5414 4 #include "helper_3dmath.h"
ellingjp 22:9350752f5414 5 #include "debug.h"
ellingjp 22:9350752f5414 6 #include "SystemTime.h"
ellingjp 22:9350752f5414 7 #include "pins.h"
ellingjp 22:9350752f5414 8
ellingjp 22:9350752f5414 9 // SD Card
ellingjp 22:9350752f5414 10 SDFileSystem sd(P0_21, P0_22, P1_15, P1_19, "sd"); // MOSI, MISO, SCLK, SSEL SPI1
ellingjp 22:9350752f5414 11
ellingjp 22:9350752f5414 12 // DS3231 rtc(I2C_SDA, I2C_SCL);
ellingjp 22:9350752f5414 13
ellingjp 22:9350752f5414 14 // Logging vars
ellingjp 22:9350752f5414 15 FILE *accelFile;
ellingjp 22:9350752f5414 16 FILE *peakFile;
ellingjp 22:9350752f5414 17
ellingjp 22:9350752f5414 18 /* Returns true if logging was successfully initialized, false otherwise */
ellingjp 22:9350752f5414 19 bool log_init() {
ellingjp 22:9350752f5414 20 PC_PRINTLN("Initializing logging...");
ellingjp 22:9350752f5414 21
ellingjp 22:9350752f5414 22 accelFile = fopen(ACCEL_LOG, "a");
ellingjp 22:9350752f5414 23 if (accelFile == NULL) {
ellingjp 22:9350752f5414 24 PC_PRINTLNF("SD card initialization error: Failed to open %s", ACCEL_LOG);
ellingjp 22:9350752f5414 25 DIE(SD_ERROR_RATE);
ellingjp 22:9350752f5414 26 return false;
ellingjp 22:9350752f5414 27 }
ellingjp 22:9350752f5414 28
ellingjp 22:9350752f5414 29 peakFile = fopen(PEAK_LOG, "a");
ellingjp 22:9350752f5414 30 if (peakFile == NULL) {
ellingjp 22:9350752f5414 31 PC_PRINTLNF("SD card initialization error: Failed to open %s", SPLIT_LOG);
ellingjp 22:9350752f5414 32 DIE(SD_ERROR_RATE);
ellingjp 22:9350752f5414 33 return false;
ellingjp 22:9350752f5414 34 }
ellingjp 22:9350752f5414 35
ellingjp 22:9350752f5414 36 // fprintf(accelFile, "---- BEGIN NEW DATASET ----\n");
ellingjp 22:9350752f5414 37 // fprintf(peakFile, "---- BEGIN NEW DATASET ----\n");
ellingjp 22:9350752f5414 38 return true;
ellingjp 22:9350752f5414 39 }
ellingjp 22:9350752f5414 40
ellingjp 22:9350752f5414 41 /* Returns true if data was successfully logged, false otherwise
ellingjp 22:9350752f5414 42 Used for logging acceleration data */
ellingjp 22:9350752f5414 43 bool log_data(int time_ms, VectorInt16 *data) {
ellingjp 22:9350752f5414 44 if (accelFile != NULL) {
ellingjp 22:9350752f5414 45 // fprintf(accelFile, "%d, %d, %d, %d\n", time_ms, data->x, data->y, data->z);
ellingjp 22:9350752f5414 46 fwrite(&time_ms, sizeof(int), 1, accelFile);
ellingjp 22:9350752f5414 47 fwrite(data, sizeof(VectorInt16), 1, accelFile);
ellingjp 22:9350752f5414 48 return true;
ellingjp 22:9350752f5414 49 }
ellingjp 22:9350752f5414 50
ellingjp 22:9350752f5414 51 return false;
ellingjp 22:9350752f5414 52 }
ellingjp 22:9350752f5414 53
ellingjp 22:9350752f5414 54 /* Returns true if data was successfully logged, false otherwise
ellingjp 22:9350752f5414 55 Used for logging split times */
ellingjp 22:9350752f5414 56 bool log_data(int time_ms, int split) {
ellingjp 22:9350752f5414 57 if (peakFile != NULL) {
ellingjp 22:9350752f5414 58 // fprintf(peakFile, "%d, %d\n", time_ms, split);
ellingjp 22:9350752f5414 59 fwrite( (void*) &time_ms, sizeof(int), 1, peakFile);
ellingjp 22:9350752f5414 60 fwrite( (void*) &split, sizeof(int), 1, peakFile);
ellingjp 22:9350752f5414 61 return true;
ellingjp 22:9350752f5414 62 }
ellingjp 22:9350752f5414 63
ellingjp 22:9350752f5414 64 return false;
ellingjp 22:9350752f5414 65 }
ellingjp 22:9350752f5414 66
ellingjp 22:9350752f5414 67 /* Returns true if logging was successfully closed, false otherwise */
ellingjp 22:9350752f5414 68 bool log_close() {
ellingjp 22:9350752f5414 69 // if (accelFile != NULL && splitFile != NULL)
ellingjp 22:9350752f5414 70 // return ( (fclose(accelFile) == 0) && (fclose(splitFile) == 0) );
ellingjp 22:9350752f5414 71
ellingjp 22:9350752f5414 72 fclose(accelFile);
ellingjp 22:9350752f5414 73 fclose(peakFile);
ellingjp 22:9350752f5414 74
ellingjp 22:9350752f5414 75 return true;
ellingjp 22:9350752f5414 76 }