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:
23:80083138d609
Using RTC filenames

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ellingjp 8:8430a5c0914c 1 #include "mbed.h"
ellingjp 8:8430a5c0914c 2 #include "log_data.h"
ellingjp 8:8430a5c0914c 3 #include "SDFileSystem.h"
ellingjp 8:8430a5c0914c 4 #include "helper_3dmath.h"
ellingjp 8:8430a5c0914c 5 #include "debug.h"
ellingjp 21:2fa676f214fe 6 #include "SystemTime.h"
paulbartell 13:227a6cfd2097 7 #include "pins.h"
ellingjp 24:f2503d1256ad 8 #include "main.h"
ellingjp 8:8430a5c0914c 9
ellingjp 8:8430a5c0914c 10 // SD Card
ellingjp 23:80083138d609 11 SDFileSystem sd(P0_9, P0_8, P0_10, P1_19, "sd"); // MOSI, MISO, SCLK, SSEL SPI1
ellingjp 8:8430a5c0914c 12
ellingjp 15:002bac432234 13 // DS3231 rtc(I2C_SDA, I2C_SCL);
ellingjp 12:bf282a100fbc 14
ellingjp 8:8430a5c0914c 15 // Logging vars
ellingjp 9:a711b5b34d73 16 FILE *accelFile;
ellingjp 24:f2503d1256ad 17 //FILE *peakFile;
ellingjp 8:8430a5c0914c 18
ellingjp 8:8430a5c0914c 19 /* Returns true if logging was successfully initialized, false otherwise */
ellingjp 20:294eaeaf2ebb 20 bool log_init() {
ellingjp 20:294eaeaf2ebb 21 PC_PRINTLN("Initializing logging...");
ellingjp 20:294eaeaf2ebb 22
ellingjp 24:f2503d1256ad 23 char filename[32];
ellingjp 24:f2503d1256ad 24 // year_month_day_hour_min_sec.log
ellingjp 24:f2503d1256ad 25 int year, month, day, hour, min, sec;
ellingjp 24:f2503d1256ad 26 rtc.readDate(&day, &month, &year);
ellingjp 24:f2503d1256ad 27 rtc.readTime(&hour, &min, &sec);
ellingjp 24:f2503d1256ad 28 sprintf(filename, "/sd/%04d_%02d_%02d_%02d_%02d_%02d.log", year, month, day, hour, min, sec);
ellingjp 24:f2503d1256ad 29
ellingjp 24:f2503d1256ad 30 PC_PRINTLNF("Trying to open %s...", filename);
ellingjp 24:f2503d1256ad 31
ellingjp 24:f2503d1256ad 32 accelFile = fopen(filename, "w");
ellingjp 9:a711b5b34d73 33 if (accelFile == NULL) {
ellingjp 24:f2503d1256ad 34 PC_PRINTLNF("SD card initialization error: Failed to open %s", filename);
ellingjp 8:8430a5c0914c 35 DIE(SD_ERROR_RATE);
ellingjp 8:8430a5c0914c 36 return false;
ellingjp 8:8430a5c0914c 37 }
ellingjp 9:a711b5b34d73 38
ellingjp 8:8430a5c0914c 39 return true;
ellingjp 8:8430a5c0914c 40 }
ellingjp 8:8430a5c0914c 41
ellingjp 8:8430a5c0914c 42 /* Returns true if data was successfully logged, false otherwise
ellingjp 8:8430a5c0914c 43 Used for logging acceleration data */
ellingjp 21:2fa676f214fe 44 bool log_data(int time_ms, VectorInt16 *data) {
ellingjp 9:a711b5b34d73 45 if (accelFile != NULL) {
ellingjp 21:2fa676f214fe 46 // fprintf(accelFile, "%d, %d, %d, %d\n", time_ms, data->x, data->y, data->z);
ellingjp 21:2fa676f214fe 47 fwrite(&time_ms, sizeof(int), 1, accelFile);
ellingjp 21:2fa676f214fe 48 fwrite(data, sizeof(VectorInt16), 1, accelFile);
ellingjp 8:8430a5c0914c 49 return true;
ellingjp 8:8430a5c0914c 50 }
ellingjp 8:8430a5c0914c 51
ellingjp 8:8430a5c0914c 52 return false;
ellingjp 8:8430a5c0914c 53 }
ellingjp 8:8430a5c0914c 54
ellingjp 24:f2503d1256ad 55 ///* Returns true if data was successfully logged, false otherwise
ellingjp 24:f2503d1256ad 56 // Used for logging split times */
ellingjp 24:f2503d1256ad 57 //bool log_data(int time_ms, int split) {
ellingjp 24:f2503d1256ad 58 // if (peakFile != NULL) {
ellingjp 24:f2503d1256ad 59 //// fprintf(peakFile, "%d, %d\n", time_ms, split);
ellingjp 24:f2503d1256ad 60 // fwrite( (void*) &time_ms, sizeof(int), 1, peakFile);
ellingjp 24:f2503d1256ad 61 // fwrite( (void*) &split, sizeof(int), 1, peakFile);
ellingjp 24:f2503d1256ad 62 // return true;
ellingjp 24:f2503d1256ad 63 // }
ellingjp 24:f2503d1256ad 64 //
ellingjp 24:f2503d1256ad 65 // return false;
ellingjp 24:f2503d1256ad 66 //}
ellingjp 8:8430a5c0914c 67
ellingjp 8:8430a5c0914c 68 /* Returns true if logging was successfully closed, false otherwise */
ellingjp 8:8430a5c0914c 69 bool log_close() {
ellingjp 9:a711b5b34d73 70 // if (accelFile != NULL && splitFile != NULL)
ellingjp 9:a711b5b34d73 71 // return ( (fclose(accelFile) == 0) && (fclose(splitFile) == 0) );
ellingjp 8:8430a5c0914c 72
ellingjp 9:a711b5b34d73 73 fclose(accelFile);
ellingjp 24:f2503d1256ad 74 // fclose(peakFile);
ellingjp 9:a711b5b34d73 75
ellingjp 9:a711b5b34d73 76 return true;
ellingjp 8:8430a5c0914c 77 }