Swimate V2 without RTOS code

Dependencies:   Adafruit_GFX_128x64 DS3231 PinDetect SDFileSystem USBDevice mbed RealtimeMath MODSERIAL

Committer:
paulbartell
Date:
Wed May 28 22:56:25 2014 +0000
Revision:
13:227a6cfd2097
Parent:
9:a711b5b34d73
Child:
17:fb8415091770
Child:
18:06b718f8e6fd
Started bluetooth functionality.

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 9:a711b5b34d73 61 DigitalOut led(LED1);
ellingjp 0:cd1fe4f0ed39 62 int main(void)
ellingjp 0:cd1fe4f0ed39 63 {
ellingjp 8:8430a5c0914c 64 SystemTime::start();
ellingjp 9:a711b5b34d73 65
ellingjp 0:cd1fe4f0ed39 66 State = IDLE;
ellingjp 0:cd1fe4f0ed39 67
ellingjp 8:8430a5c0914c 68 // After button is held, the next rising edge will call buttonPressed.
ellingjp 8:8430a5c0914c 69 // Because of this, inside the callbacks need to ensure that edge
ellingjp 8:8430a5c0914c 70 // does not do anything unintended.
ellingjp 8:8430a5c0914c 71 captureButton.attach_deasserted_held(buttonHeld);
ellingjp 8:8430a5c0914c 72 captureButton.attach_asserted(buttonPressed);
ellingjp 8:8430a5c0914c 73 captureButton.setSampleFrequency();
ellingjp 8:8430a5c0914c 74 captureButton.setSamplesTillHeld(SYNC_HELD_SAMPLES);
ellingjp 8:8430a5c0914c 75
ellingjp 8:8430a5c0914c 76 VectorInt16 *data;
ellingjp 0:cd1fe4f0ed39 77 while (true) {
ellingjp 7:33a74adff0ff 78 if (State == IDLE){
ellingjp 8:8430a5c0914c 79 OLED_CLEAR();
ellingjp 8:8430a5c0914c 80 OLED_PRINTP("Idling...", 0, 0);
ellingjp 8:8430a5c0914c 81 PC_PRINTLN("Idling...");
ellingjp 7:33a74adff0ff 82 } else if (State == CAPTURE) {
ellingjp 8:8430a5c0914c 83 OLED_PRINTP("Starting capture...", 0, 0);
ellingjp 8:8430a5c0914c 84 OLED_PRINTP("Init SD card...", 0, 10);
paulbartell 13:227a6cfd2097 85 log_init(rtc.getTimestamp());
ellingjp 8:8430a5c0914c 86 OLED_PRINTP("Init peak detect...", 0, 10);
ellingjp 8:8430a5c0914c 87 process_init();
ellingjp 8:8430a5c0914c 88 OLED_PRINTP("Init data receipt...", 0, 10);
ellingjp 8:8430a5c0914c 89 receive_init();
ellingjp 8:8430a5c0914c 90 OLED_CLEAR();
ellingjp 8:8430a5c0914c 91 OLED_PRINTP("Capturing data...", 0, 0);
ellingjp 8:8430a5c0914c 92
ellingjp 8:8430a5c0914c 93
ellingjp 7:33a74adff0ff 94 while (State == CAPTURE) {
ellingjp 8:8430a5c0914c 95 data = receive_data();
ellingjp 8:8430a5c0914c 96 log_data(data);
ellingjp 8:8430a5c0914c 97 int split = process_data(data->x);
ellingjp 8:8430a5c0914c 98 if (split != UINT16_MAX) {
ellingjp 8:8430a5c0914c 99 PC_PRINTLNF("Split time: %d", split);
ellingjp 8:8430a5c0914c 100 OLED_PRINTPF("Split: %d", split, 0, 40);
ellingjp 9:a711b5b34d73 101 log_data(split);
ellingjp 8:8430a5c0914c 102 }
ellingjp 0:cd1fe4f0ed39 103 }
ellingjp 8:8430a5c0914c 104
ellingjp 8:8430a5c0914c 105 receive_close();
ellingjp 8:8430a5c0914c 106 process_close();
ellingjp 8:8430a5c0914c 107 log_close();
ellingjp 8:8430a5c0914c 108 } else if (State == SYNC) {
ellingjp 8:8430a5c0914c 109 OLED_PRINTP("Ready to sync...", 0, 0);
ellingjp 9:a711b5b34d73 110 // sync_init();
ellingjp 9:a711b5b34d73 111 // while (State == SYNC)
ellingjp 9:a711b5b34d73 112 // sync();
ellingjp 8:8430a5c0914c 113 }
ellingjp 0:cd1fe4f0ed39 114 }
ellingjp 0:cd1fe4f0ed39 115 }