Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Adafruit_GFX_128x64 DS3231 PinDetect SDFileSystem USBDevice mbed RealtimeMath MODSERIAL
main.cpp@22:9350752f5414, 2014-06-07 (annotated)
- Committer:
- ellingjp
- Date:
- Sat Jun 07 07:46:38 2014 +0000
- Revision:
- 22:9350752f5414
- Parent:
- 21:2fa676f214fe
- Parent:
- 20:294eaeaf2ebb
- Child:
- 23:80083138d609
Another merge;
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 | 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" |
| ellingjp | 20:294eaeaf2ebb | 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 | 21:2fa676f214fe | 21 | SPI spi0(P0_9, NC, P0_10); // mosi, miso, sclk |
| ellingjp | 21:2fa676f214fe | 22 | Adafruit_SSD1306 oled(spi0, P0_11, P0_12, P0_13); // DC, RST, CS |
| ellingjp | 0:cd1fe4f0ed39 | 23 | #endif |
| ellingjp | 0:cd1fe4f0ed39 | 24 | |
| ellingjp | 20:294eaeaf2ebb | 25 | // DS3231 rtc(I2C_SDA, I2C_SCL); |
| paulbartell | 13:227a6cfd2097 | 26 | |
| ellingjp | 8:8430a5c0914c | 27 | // Mode button |
| ellingjp | 8:8430a5c0914c | 28 | PinDetect captureButton(P0_16, PullUp); |
| ellingjp | 0:cd1fe4f0ed39 | 29 | |
| ellingjp | 0:cd1fe4f0ed39 | 30 | // State |
| ellingjp | 8:8430a5c0914c | 31 | enum state {IDLE, CAPTURE, SYNC}; |
| ellingjp | 0:cd1fe4f0ed39 | 32 | enum state State; |
| ellingjp | 0:cd1fe4f0ed39 | 33 | |
| ellingjp | 21:2fa676f214fe | 34 | Timer captureTimer; |
| ellingjp | 21:2fa676f214fe | 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); |
| ellingjp | 20:294eaeaf2ebb | 84 | log_init(); |
| 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 | 21:2fa676f214fe | 92 | captureTimer.start(); |
| ellingjp | 7:33a74adff0ff | 93 | while (State == CAPTURE) { |
| ellingjp | 8:8430a5c0914c | 94 | data = receive_data(); |
| ellingjp | 21:2fa676f214fe | 95 | log_data(captureTimer.read_ms(), data); |
| briggsa | 19:4f4f7bc4a3fb | 96 | |
| ellingjp | 15:002bac432234 | 97 | int split; |
| ellingjp | 21:2fa676f214fe | 98 | if (process_data((int) (data->x), (int) (data->y), &split)) { |
| ellingjp | 21:2fa676f214fe | 99 | PC_PRINTLNF("Peak time: %d", split); |
| ellingjp | 15:002bac432234 | 100 | |
| ellingjp | 15:002bac432234 | 101 | int min = split / 60000; |
| ellingjp | 15:002bac432234 | 102 | int sec = (split / 1000) % 60; |
| ellingjp | 15:002bac432234 | 103 | int hund = (split / 10) % 100; |
| ellingjp | 15:002bac432234 | 104 | |
| ellingjp | 21:2fa676f214fe | 105 | // OLED_PRINTPF("%1d", min, 0, 40); |
| ellingjp | 21:2fa676f214fe | 106 | // OLED_PRINTPF("%02d", sec, 5, 40); |
| ellingjp | 21:2fa676f214fe | 107 | // OLED_DRAWPIXEL(14, 44, 0x1); |
| ellingjp | 21:2fa676f214fe | 108 | // OLED_PRINTPF("%02d", hund, 15, 40); |
| ellingjp | 21:2fa676f214fe | 109 | log_data(captureTimer.read_ms(), split); |
| ellingjp | 8:8430a5c0914c | 110 | } |
| ellingjp | 0:cd1fe4f0ed39 | 111 | } |
| ellingjp | 21:2fa676f214fe | 112 | captureTimer.stop(); |
| ellingjp | 21:2fa676f214fe | 113 | captureTimer.reset(); |
| ellingjp | 8:8430a5c0914c | 114 | receive_close(); |
| ellingjp | 8:8430a5c0914c | 115 | process_close(); |
| ellingjp | 8:8430a5c0914c | 116 | log_close(); |
| ellingjp | 8:8430a5c0914c | 117 | } else if (State == SYNC) { |
| ellingjp | 8:8430a5c0914c | 118 | OLED_PRINTP("Ready to sync...", 0, 0); |
| ellingjp | 15:002bac432234 | 119 | sync_init(); |
| briggsa | 18:06b718f8e6fd | 120 | sync(); |
| ellingjp | 8:8430a5c0914c | 121 | } |
| ellingjp | 0:cd1fe4f0ed39 | 122 | } |
| ellingjp | 0:cd1fe4f0ed39 | 123 | } |