Swimate V2 without RTOS code

Dependencies:   Adafruit_GFX_128x64 DS3231 PinDetect SDFileSystem USBDevice mbed RealtimeMath MODSERIAL

Committer:
ellingjp
Date:
Thu Jun 05 19:07:49 2014 +0000
Revision:
17:fb8415091770
Parent:
15:002bac432234
Parent:
13:227a6cfd2097
Child:
19:4f4f7bc4a3fb
Merged with Paul's published sync code

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