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 0:cd1fe4f0ed39 1 #include "main.h"
ellingjp 0:cd1fe4f0ed39 2 #include "mbed.h"
ellingjp 8:8430a5c0914c 3 #include "PinDetect.h"
ellingjp 23:80083138d609 4 //#include "USBSerial.h"
ellingjp 0:cd1fe4f0ed39 5 #include "Adafruit_SSD1306.h"
ellingjp 0:cd1fe4f0ed39 6 #include "SDFileSystem.h"
ellingjp 7:33a74adff0ff 7 #include "receive_data.h"
ellingjp 8:8430a5c0914c 8 #include "log_data.h"
ellingjp 8:8430a5c0914c 9 #include "process_data.h"
ellingjp 7:33a74adff0ff 10 #include "debug.h"
ellingjp 8:8430a5c0914c 11 #include "SystemTime.h"
ellingjp 9:a711b5b34d73 12 #include "sync.h"
paulbartell 13:227a6cfd2097 13 #include "pins.h"
ellingjp 23:80083138d609 14 #include "DS3231.h"
ellingjp 23:80083138d609 15
ellingjp 23:80083138d609 16 #define X_POS(x) (32 + x%64)
ellingjp 23:80083138d609 17 #define Y_POS(y) (16 + y%48)
paulbartell 13:227a6cfd2097 18
ellingjp 8:8430a5c0914c 19 // Capture button stuff
ellingjp 8:8430a5c0914c 20 #define PIN_DETECT_SAMPLE_PERIOD_uS 20000 // 20 ms sample period
ellingjp 8:8430a5c0914c 21 #define SYNC_HELD_SAMPLES (SYNC_HOLD_TIME_MS * 1000 / PIN_DETECT_SAMPLE_PERIOD_uS)
ellingjp 8:8430a5c0914c 22
ellingjp 23:80083138d609 23
ellingjp 8:8430a5c0914c 24 #ifdef USE_OLED
ellingjp 23:80083138d609 25 SPI spi0(P0_9, P0_8, P0_10); // mosi, miso, sclk
ellingjp 21:2fa676f214fe 26 Adafruit_SSD1306 oled(spi0, P0_11, P0_12, P0_13); // DC, RST, CS
ellingjp 0:cd1fe4f0ed39 27 #endif
ellingjp 0:cd1fe4f0ed39 28
ellingjp 23:80083138d609 29 DS3231 rtc(I2C_SDA, I2C_SCL);
paulbartell 13:227a6cfd2097 30
ellingjp 8:8430a5c0914c 31 // Mode button
ellingjp 8:8430a5c0914c 32 PinDetect captureButton(P0_16, PullUp);
ellingjp 0:cd1fe4f0ed39 33
ellingjp 0:cd1fe4f0ed39 34 // State
ellingjp 8:8430a5c0914c 35 enum state {IDLE, CAPTURE, SYNC};
ellingjp 0:cd1fe4f0ed39 36 enum state State;
ellingjp 0:cd1fe4f0ed39 37
ellingjp 21:2fa676f214fe 38 Timer captureTimer;
ellingjp 21:2fa676f214fe 39
ellingjp 8:8430a5c0914c 40 // This is used to ensure the rising edge after a hold is ignored
ellingjp 8:8430a5c0914c 41 bool ignore_edge = false;
ellingjp 8:8430a5c0914c 42 void buttonHeld() {
ellingjp 8:8430a5c0914c 43 if (State == IDLE)
ellingjp 8:8430a5c0914c 44 State = SYNC;
ellingjp 8:8430a5c0914c 45 else
ellingjp 8:8430a5c0914c 46 State = IDLE;
ellingjp 4:b962f5a783a1 47
ellingjp 8:8430a5c0914c 48 // Button was held down, so don't count the next assert
ellingjp 8:8430a5c0914c 49 ignore_edge = true;
ellingjp 8:8430a5c0914c 50 }
ellingjp 8:8430a5c0914c 51
ellingjp 8:8430a5c0914c 52 void buttonPressed() {
ellingjp 8:8430a5c0914c 53 // Don't ignore subsequent edges
ellingjp 8:8430a5c0914c 54 if (ignore_edge) {
ellingjp 8:8430a5c0914c 55 ignore_edge = false;
ellingjp 8:8430a5c0914c 56 return;
ellingjp 8:8430a5c0914c 57 }
ellingjp 8:8430a5c0914c 58
ellingjp 8:8430a5c0914c 59 if (State == IDLE)
ellingjp 8:8430a5c0914c 60 State = CAPTURE;
ellingjp 8:8430a5c0914c 61 else if (State == CAPTURE)
ellingjp 8:8430a5c0914c 62 State = IDLE;
ellingjp 0:cd1fe4f0ed39 63 }
ellingjp 0:cd1fe4f0ed39 64
ellingjp 24:f2503d1256ad 65 void printSplit(int split)
ellingjp 24:f2503d1256ad 66 {
ellingjp 24:f2503d1256ad 67 int min = split / 60000;
ellingjp 24:f2503d1256ad 68 int sec = (split / 1000) % 60;
ellingjp 24:f2503d1256ad 69 int hund = (split / 10) % 100;
ellingjp 24:f2503d1256ad 70
ellingjp 24:f2503d1256ad 71 OLED_PRINTPF("%1d", min, X_POS(0), Y_POS(20));
ellingjp 24:f2503d1256ad 72 OLED_PRINTPF("%02d", sec, X_POS(5), Y_POS(20));
ellingjp 24:f2503d1256ad 73 OLED_DRAWPIXEL(X_POS(14), Y_POS(24), 0x1);
ellingjp 24:f2503d1256ad 74 OLED_PRINTPF("%02d", hund, X_POS(15), Y_POS(20));
ellingjp 24:f2503d1256ad 75 }
ellingjp 24:f2503d1256ad 76
ellingjp 0:cd1fe4f0ed39 77 int main(void)
ellingjp 0:cd1fe4f0ed39 78 {
ellingjp 24:f2503d1256ad 79
ellingjp 8:8430a5c0914c 80 SystemTime::start();
ellingjp 9:a711b5b34d73 81
ellingjp 0:cd1fe4f0ed39 82 State = IDLE;
ellingjp 0:cd1fe4f0ed39 83
ellingjp 8:8430a5c0914c 84 // After button is held, the next rising edge will call buttonPressed.
ellingjp 8:8430a5c0914c 85 // Because of this, inside the callbacks need to ensure that edge
ellingjp 8:8430a5c0914c 86 // does not do anything unintended.
ellingjp 8:8430a5c0914c 87 captureButton.attach_deasserted_held(buttonHeld);
ellingjp 8:8430a5c0914c 88 captureButton.attach_asserted(buttonPressed);
ellingjp 8:8430a5c0914c 89 captureButton.setSampleFrequency();
ellingjp 8:8430a5c0914c 90 captureButton.setSamplesTillHeld(SYNC_HELD_SAMPLES);
ellingjp 8:8430a5c0914c 91
ellingjp 8:8430a5c0914c 92 VectorInt16 *data;
ellingjp 0:cd1fe4f0ed39 93 while (true) {
ellingjp 7:33a74adff0ff 94 if (State == IDLE){
ellingjp 24:f2503d1256ad 95 PC_PRINTLN("Idling...");
ellingjp 23:80083138d609 96 OLED_PRINTP("Idling...", X_POS(0), Y_POS(20));
ellingjp 24:f2503d1256ad 97 OLED_CLEAR();
ellingjp 7:33a74adff0ff 98 } else if (State == CAPTURE) {
ellingjp 24:f2503d1256ad 99 PC_PRINTLN("Starting capture...");
ellingjp 24:f2503d1256ad 100 PC_PRINTLN("Init SD card...");
ellingjp 24:f2503d1256ad 101 log_init();
ellingjp 24:f2503d1256ad 102 PC_PRINTLN("Init peak detect...");
ellingjp 24:f2503d1256ad 103 process_init();
ellingjp 24:f2503d1256ad 104 PC_PRINTLN("Init data receipt...");
ellingjp 24:f2503d1256ad 105 receive_init();
ellingjp 24:f2503d1256ad 106 PC_PRINTLN("Capturing data...");
ellingjp 8:8430a5c0914c 107
ellingjp 21:2fa676f214fe 108 captureTimer.start();
ellingjp 24:f2503d1256ad 109 int split = 0;
ellingjp 24:f2503d1256ad 110 printSplit(split);
ellingjp 7:33a74adff0ff 111 while (State == CAPTURE) {
ellingjp 8:8430a5c0914c 112 data = receive_data();
ellingjp 24:f2503d1256ad 113 // PC_PRINTLNF("x: %d ", data->x);
ellingjp 24:f2503d1256ad 114 // PC_PRINTLNF("y: %d ", data->y);
ellingjp 24:f2503d1256ad 115 // PC_PRINTLNF("z: %d ", data->z);
ellingjp 24:f2503d1256ad 116 log_data(captureTimer.read_ms(), data);
briggsa 19:4f4f7bc4a3fb 117
ellingjp 21:2fa676f214fe 118 if (process_data((int) (data->x), (int) (data->y), &split)) {
ellingjp 24:f2503d1256ad 119 PC_PRINTLNF("Split time: %d", split);
ellingjp 24:f2503d1256ad 120 printSplit(split);
ellingjp 24:f2503d1256ad 121
ellingjp 23:80083138d609 122 // log_data(captureTimer.read_ms(), split);
ellingjp 8:8430a5c0914c 123 }
ellingjp 0:cd1fe4f0ed39 124 }
ellingjp 21:2fa676f214fe 125 captureTimer.stop();
ellingjp 21:2fa676f214fe 126 captureTimer.reset();
ellingjp 8:8430a5c0914c 127 receive_close();
ellingjp 8:8430a5c0914c 128 process_close();
ellingjp 24:f2503d1256ad 129 log_close();
ellingjp 8:8430a5c0914c 130 } else if (State == SYNC) {
ellingjp 8:8430a5c0914c 131 OLED_PRINTP("Ready to sync...", 0, 0);
ellingjp 15:002bac432234 132 sync_init();
briggsa 18:06b718f8e6fd 133 sync();
ellingjp 8:8430a5c0914c 134 }
ellingjp 0:cd1fe4f0ed39 135 }
ellingjp 0:cd1fe4f0ed39 136 }