Swimate V2 without RTOS code
Dependencies: Adafruit_GFX_128x64 DS3231 PinDetect SDFileSystem USBDevice mbed RealtimeMath MODSERIAL
log_data.cpp@21:2fa676f214fe, 2014-06-07 (annotated)
- Committer:
- ellingjp
- Date:
- Sat Jun 07 07:12:25 2014 +0000
- Revision:
- 21:2fa676f214fe
- Parent:
- 15:002bac432234
- Child:
- 22:9350752f5414
Working sync
Who changed what in which revision?
User | Revision | Line number | New 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" |
ellingjp | 21:2fa676f214fe | 7 | #include "pins.h" |
ellingjp | 8:8430a5c0914c | 8 | |
ellingjp | 8:8430a5c0914c | 9 | // SD Card |
ellingjp | 8:8430a5c0914c | 10 | SDFileSystem sd(P0_21, P0_22, P1_15, P1_19, "sd"); // MOSI, MISO, SCLK, SSEL SPI1 |
ellingjp | 8:8430a5c0914c | 11 | |
ellingjp | 15:002bac432234 | 12 | // DS3231 rtc(I2C_SDA, I2C_SCL); |
ellingjp | 12:bf282a100fbc | 13 | |
ellingjp | 8:8430a5c0914c | 14 | // Logging vars |
ellingjp | 9:a711b5b34d73 | 15 | FILE *accelFile; |
ellingjp | 15:002bac432234 | 16 | FILE *peakFile; |
ellingjp | 8:8430a5c0914c | 17 | |
ellingjp | 8:8430a5c0914c | 18 | /* Returns true if logging was successfully initialized, false otherwise */ |
ellingjp | 8:8430a5c0914c | 19 | bool log_init() { |
ellingjp | 21:2fa676f214fe | 20 | PC_PRINTLN("Initializing logging..."); |
ellingjp | 21:2fa676f214fe | 21 | |
ellingjp | 9:a711b5b34d73 | 22 | accelFile = fopen(ACCEL_LOG, "a"); |
ellingjp | 9:a711b5b34d73 | 23 | if (accelFile == NULL) { |
ellingjp | 9:a711b5b34d73 | 24 | PC_PRINTLNF("SD card initialization error: Failed to open %s", ACCEL_LOG); |
ellingjp | 8:8430a5c0914c | 25 | DIE(SD_ERROR_RATE); |
ellingjp | 8:8430a5c0914c | 26 | return false; |
ellingjp | 8:8430a5c0914c | 27 | } |
ellingjp | 9:a711b5b34d73 | 28 | |
ellingjp | 15:002bac432234 | 29 | peakFile = fopen(PEAK_LOG, "a"); |
ellingjp | 15:002bac432234 | 30 | if (peakFile == NULL) { |
ellingjp | 9:a711b5b34d73 | 31 | PC_PRINTLNF("SD card initialization error: Failed to open %s", SPLIT_LOG); |
ellingjp | 9:a711b5b34d73 | 32 | DIE(SD_ERROR_RATE); |
ellingjp | 9:a711b5b34d73 | 33 | return false; |
ellingjp | 9:a711b5b34d73 | 34 | } |
ellingjp | 21:2fa676f214fe | 35 | |
ellingjp | 21:2fa676f214fe | 36 | // fprintf(accelFile, "---- BEGIN NEW DATASET ----\n"); |
ellingjp | 21:2fa676f214fe | 37 | // fprintf(peakFile, "---- BEGIN NEW DATASET ----\n"); |
ellingjp | 8:8430a5c0914c | 38 | return true; |
ellingjp | 8:8430a5c0914c | 39 | } |
ellingjp | 8:8430a5c0914c | 40 | |
ellingjp | 8:8430a5c0914c | 41 | /* Returns true if data was successfully logged, false otherwise |
ellingjp | 8:8430a5c0914c | 42 | Used for logging acceleration data */ |
ellingjp | 21:2fa676f214fe | 43 | bool log_data(int time_ms, VectorInt16 *data) { |
ellingjp | 9:a711b5b34d73 | 44 | if (accelFile != NULL) { |
ellingjp | 21:2fa676f214fe | 45 | // fprintf(accelFile, "%d, %d, %d, %d\n", time_ms, data->x, data->y, data->z); |
ellingjp | 21:2fa676f214fe | 46 | fwrite(&time_ms, sizeof(int), 1, accelFile); |
ellingjp | 21:2fa676f214fe | 47 | fwrite(data, sizeof(VectorInt16), 1, accelFile); |
ellingjp | 8:8430a5c0914c | 48 | return true; |
ellingjp | 8:8430a5c0914c | 49 | } |
ellingjp | 8:8430a5c0914c | 50 | |
ellingjp | 8:8430a5c0914c | 51 | return false; |
ellingjp | 8:8430a5c0914c | 52 | } |
ellingjp | 8:8430a5c0914c | 53 | |
ellingjp | 8:8430a5c0914c | 54 | /* Returns true if data was successfully logged, false otherwise |
ellingjp | 8:8430a5c0914c | 55 | Used for logging split times */ |
ellingjp | 21:2fa676f214fe | 56 | bool log_data(int time_ms, int split) { |
ellingjp | 15:002bac432234 | 57 | if (peakFile != NULL) { |
ellingjp | 21:2fa676f214fe | 58 | // fprintf(peakFile, "%d, %d\n", time_ms, split); |
ellingjp | 21:2fa676f214fe | 59 | fwrite( (void*) &time_ms, sizeof(int), 1, peakFile); |
ellingjp | 21:2fa676f214fe | 60 | fwrite( (void*) &split, sizeof(int), 1, peakFile); |
ellingjp | 9:a711b5b34d73 | 61 | return true; |
ellingjp | 9:a711b5b34d73 | 62 | } |
ellingjp | 8:8430a5c0914c | 63 | |
ellingjp | 9:a711b5b34d73 | 64 | return false; |
ellingjp | 8:8430a5c0914c | 65 | } |
ellingjp | 8:8430a5c0914c | 66 | |
ellingjp | 8:8430a5c0914c | 67 | /* Returns true if logging was successfully closed, false otherwise */ |
ellingjp | 8:8430a5c0914c | 68 | bool log_close() { |
ellingjp | 9:a711b5b34d73 | 69 | // if (accelFile != NULL && splitFile != NULL) |
ellingjp | 9:a711b5b34d73 | 70 | // return ( (fclose(accelFile) == 0) && (fclose(splitFile) == 0) ); |
ellingjp | 8:8430a5c0914c | 71 | |
ellingjp | 9:a711b5b34d73 | 72 | fclose(accelFile); |
ellingjp | 15:002bac432234 | 73 | fclose(peakFile); |
ellingjp | 9:a711b5b34d73 | 74 | |
ellingjp | 9:a711b5b34d73 | 75 | return true; |
ellingjp | 8:8430a5c0914c | 76 | } |