CSE477 / swimate_v2

Dependencies:   Adafruit_GFX_128x64 DS3231 PinDetect SDFileSystem USBDevice mbed RealtimeMath MODSERIAL

Committer:
briggsa
Date:
Thu Jun 05 19:09:07 2014 +0000
Revision:
18:06b718f8e6fd
Parent:
13:227a6cfd2097
Child:
19:4f4f7bc4a3fb
Bluetooth essentially working, but requires not using USBSerial for some reason (or not using OLED :( )

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