Swimate V2 without RTOS code
Dependencies: Adafruit_GFX_128x64 DS3231 PinDetect SDFileSystem USBDevice mbed RealtimeMath MODSERIAL
main.cpp@7:33a74adff0ff, 2014-05-17 (annotated)
- Committer:
- ellingjp
- Date:
- Sat May 17 01:20:31 2014 +0000
- Revision:
- 7:33a74adff0ff
- Parent:
- 4:b962f5a783a1
- Child:
- 8:8430a5c0914c
Refactor -- introduced bug that causes garbage to be received from mpu
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ellingjp | 0:cd1fe4f0ed39 | 1 | #include "main.h" |
ellingjp | 0:cd1fe4f0ed39 | 2 | #include "mbed.h" |
ellingjp | 0:cd1fe4f0ed39 | 3 | #include "USBSerial.h" |
ellingjp | 0:cd1fe4f0ed39 | 4 | #include "Adafruit_SSD1306.h" |
ellingjp | 0:cd1fe4f0ed39 | 5 | #include "SDFileSystem.h" |
ellingjp | 7:33a74adff0ff | 6 | #include "receive_data.h" |
ellingjp | 7:33a74adff0ff | 7 | #include "debug.h" |
ellingjp | 0:cd1fe4f0ed39 | 8 | |
ellingjp | 0:cd1fe4f0ed39 | 9 | // Display |
ellingjp | 0:cd1fe4f0ed39 | 10 | #ifdef OLED_DEBUG |
ellingjp | 0:cd1fe4f0ed39 | 11 | SPI spi0(P0_9, NC, P0_10); // mosi, miso, sclk |
ellingjp | 4:b962f5a783a1 | 12 | Adafruit_SSD1306 oled(spi0, P0_11, P0_12, P0_13); // DC, RST, CS |
ellingjp | 0:cd1fe4f0ed39 | 13 | #endif |
ellingjp | 0:cd1fe4f0ed39 | 14 | |
ellingjp | 0:cd1fe4f0ed39 | 15 | // SD Card |
ellingjp | 0:cd1fe4f0ed39 | 16 | SDFileSystem sd(P0_21, P0_22, P1_15, P1_19, "sd"); // MOSI, MISO, SCLK, SSEL SPI1 |
ellingjp | 0:cd1fe4f0ed39 | 17 | |
ellingjp | 0:cd1fe4f0ed39 | 18 | // Logging vars |
ellingjp | 0:cd1fe4f0ed39 | 19 | FILE *logFile; |
ellingjp | 0:cd1fe4f0ed39 | 20 | |
ellingjp | 0:cd1fe4f0ed39 | 21 | // Timer |
ellingjp | 0:cd1fe4f0ed39 | 22 | Timer totalTime; |
ellingjp | 0:cd1fe4f0ed39 | 23 | Timer captureTime; |
ellingjp | 0:cd1fe4f0ed39 | 24 | |
ellingjp | 0:cd1fe4f0ed39 | 25 | // Switch |
ellingjp | 0:cd1fe4f0ed39 | 26 | InterruptIn captureSwitch(P0_16); |
ellingjp | 0:cd1fe4f0ed39 | 27 | |
ellingjp | 0:cd1fe4f0ed39 | 28 | |
ellingjp | 0:cd1fe4f0ed39 | 29 | // State |
ellingjp | 0:cd1fe4f0ed39 | 30 | enum state {IDLE, CAPTURE}; |
ellingjp | 0:cd1fe4f0ed39 | 31 | enum state State; |
ellingjp | 0:cd1fe4f0ed39 | 32 | |
ellingjp | 0:cd1fe4f0ed39 | 33 | void captureSwitchISR() { |
ellingjp | 0:cd1fe4f0ed39 | 34 | // used for debouncing |
ellingjp | 0:cd1fe4f0ed39 | 35 | static int prev_time = 0; |
ellingjp | 4:b962f5a783a1 | 36 | int curr_time = totalTime.read_ms(); |
ellingjp | 4:b962f5a783a1 | 37 | |
ellingjp | 4:b962f5a783a1 | 38 | // Only change state after an amount of time |
ellingjp | 4:b962f5a783a1 | 39 | // Note: abs value is necessary in case of |
ellingjp | 4:b962f5a783a1 | 40 | // overflows |
ellingjp | 4:b962f5a783a1 | 41 | if (abs(curr_time - prev_time) > 200) |
ellingjp | 4:b962f5a783a1 | 42 | State = (State == IDLE) ? CAPTURE : IDLE; |
ellingjp | 0:cd1fe4f0ed39 | 43 | |
ellingjp | 4:b962f5a783a1 | 44 | prev_time = curr_time; |
ellingjp | 0:cd1fe4f0ed39 | 45 | } |
ellingjp | 0:cd1fe4f0ed39 | 46 | |
ellingjp | 0:cd1fe4f0ed39 | 47 | int main(void) |
ellingjp | 0:cd1fe4f0ed39 | 48 | { |
ellingjp | 0:cd1fe4f0ed39 | 49 | totalTime.start(); |
ellingjp | 0:cd1fe4f0ed39 | 50 | |
ellingjp | 0:cd1fe4f0ed39 | 51 | State = IDLE; |
ellingjp | 0:cd1fe4f0ed39 | 52 | captureSwitch.mode(PullUp); |
ellingjp | 0:cd1fe4f0ed39 | 53 | captureSwitch.rise(captureSwitchISR); |
ellingjp | 0:cd1fe4f0ed39 | 54 | |
ellingjp | 0:cd1fe4f0ed39 | 55 | while (true) { |
ellingjp | 7:33a74adff0ff | 56 | if (State == IDLE){ |
ellingjp | 7:33a74adff0ff | 57 | PC_PRINT("Idling\r"); |
ellingjp | 7:33a74adff0ff | 58 | } else if (State == CAPTURE) { |
ellingjp | 7:33a74adff0ff | 59 | receive_init(); |
ellingjp | 7:33a74adff0ff | 60 | while (State == CAPTURE) { |
ellingjp | 7:33a74adff0ff | 61 | __disable_irq(); |
ellingjp | 7:33a74adff0ff | 62 | receive_data(); |
ellingjp | 7:33a74adff0ff | 63 | __enable_irq(); |
ellingjp | 0:cd1fe4f0ed39 | 64 | } |
ellingjp | 7:33a74adff0ff | 65 | // data = receive_data(); |
ellingjp | 7:33a74adff0ff | 66 | // log_data(data); |
ellingjp | 7:33a74adff0ff | 67 | // split = get_split(data); |
ellingjp | 7:33a74adff0ff | 68 | // display_split; |
ellingjp | 7:33a74adff0ff | 69 | } |
ellingjp | 7:33a74adff0ff | 70 | // else if (State == SYNC) { |
ellingjp | 7:33a74adff0ff | 71 | // begin_sync(); |
ellingjp | 7:33a74adff0ff | 72 | // } |
ellingjp | 0:cd1fe4f0ed39 | 73 | } |
ellingjp | 0:cd1fe4f0ed39 | 74 | } |
ellingjp | 0:cd1fe4f0ed39 | 75 | |
ellingjp | 0:cd1fe4f0ed39 | 76 | |
ellingjp | 0:cd1fe4f0ed39 | 77 | |
ellingjp | 7:33a74adff0ff | 78 | /* Returns false on failure, true otherwise */ |
ellingjp | 7:33a74adff0ff | 79 | //bool log_open() { |
ellingjp | 7:33a74adff0ff | 80 | // logFile = fopen(LOG_FILE, "a"); |
ellingjp | 7:33a74adff0ff | 81 | // if (logFile == NULL) { |
ellingjp | 7:33a74adff0ff | 82 | // PC_PRINTLNF("SD card initialization error: Failed to open %s", LOG_FILE); |
ellingjp | 7:33a74adff0ff | 83 | // return false; |
ellingjp | 7:33a74adff0ff | 84 | // } |
ellingjp | 7:33a74adff0ff | 85 | // fprintf(logFile, "---- BEGIN NEW DATASET ----\n"); |
ellingjp | 7:33a74adff0ff | 86 | // return true; |
ellingjp | 7:33a74adff0ff | 87 | //} |