CSE477 / swimate_v2

Dependencies:   Adafruit_GFX_128x64 DS3231 PinDetect SDFileSystem USBDevice mbed RealtimeMath MODSERIAL

Committer:
ellingjp
Date:
Sat Jun 07 07:12:25 2014 +0000
Revision:
21:2fa676f214fe
Parent:
15:002bac432234
Child:
24:f2503d1256ad
Working sync

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ellingjp 8:8430a5c0914c 1 #include "mbed.h"
ellingjp 8:8430a5c0914c 2 #include "process_data.h"
ellingjp 8:8430a5c0914c 3 #include "SystemTime.h"
ellingjp 8:8430a5c0914c 4 #include "debug.h"
ellingjp 15:002bac432234 5 #include "MovingAverage.h"
ellingjp 15:002bac432234 6 #include "PeakDetector.h"
ellingjp 15:002bac432234 7 #include "FloatingThresholdPeakDetector.h"
ellingjp 15:002bac432234 8 #include "SimplePeakDetector.h"
ellingjp 21:2fa676f214fe 9 #include "main.h"
ellingjp 9:a711b5b34d73 10
ellingjp 21:2fa676f214fe 11 PeakDetector::PeakDetector *peakDetector;
ellingjp 21:2fa676f214fe 12 PeakDetector::PeakDetector *startDetector;
ellingjp 8:8430a5c0914c 13 Timer split_timer;
ellingjp 8:8430a5c0914c 14
ellingjp 21:2fa676f214fe 15 enum SwimState {WAITING, TIMING};
ellingjp 21:2fa676f214fe 16 SwimState swimState;
ellingjp 21:2fa676f214fe 17
ellingjp 15:002bac432234 18 /* Creates a new peak detector */
ellingjp 8:8430a5c0914c 19 bool process_init()
ellingjp 8:8430a5c0914c 20 {
ellingjp 21:2fa676f214fe 21 swimState = WAITING;
ellingjp 21:2fa676f214fe 22
ellingjp 21:2fa676f214fe 23 peakDetector = new FloatingThresholdPeakDetector();
ellingjp 21:2fa676f214fe 24 startDetector = new SimplePeakDetector(30, 4000);
ellingjp 8:8430a5c0914c 25
ellingjp 21:2fa676f214fe 26 split_timer.reset();
ellingjp 8:8430a5c0914c 27 return true;
ellingjp 8:8430a5c0914c 28 }
ellingjp 8:8430a5c0914c 29
ellingjp 15:002bac432234 30 /* If true, loads latest split time into split */
ellingjp 21:2fa676f214fe 31 bool process_data(int xdata, int ydata, int *split)
ellingjp 8:8430a5c0914c 32 {
ellingjp 21:2fa676f214fe 33 static int start_time;
ellingjp 21:2fa676f214fe 34 if (swimState == WAITING) {
ellingjp 21:2fa676f214fe 35 if (startDetector->onPeak(xdata)) {
ellingjp 21:2fa676f214fe 36 swimState = TIMING;
ellingjp 21:2fa676f214fe 37 split_timer.start();
ellingjp 21:2fa676f214fe 38 start_time = SystemTime::read_ms();
ellingjp 21:2fa676f214fe 39 OLED_PRINTPF("xpeak - %d ", start_time, 0, 30);
ellingjp 21:2fa676f214fe 40 }
ellingjp 21:2fa676f214fe 41 } else if (swimState == TIMING) {
ellingjp 21:2fa676f214fe 42 if (peakDetector->onPeak(ydata) && SystemTime::read_ms() - start_time > 5000 ) {
ellingjp 21:2fa676f214fe 43 *split = split_timer.read_ms();
ellingjp 21:2fa676f214fe 44 OLED_PRINTPF("ypeak - %d ", *split, 0, 40);
ellingjp 21:2fa676f214fe 45 return true;
ellingjp 21:2fa676f214fe 46 }
ellingjp 8:8430a5c0914c 47 }
ellingjp 15:002bac432234 48
ellingjp 8:8430a5c0914c 49 return false;
ellingjp 8:8430a5c0914c 50 }
ellingjp 8:8430a5c0914c 51
ellingjp 8:8430a5c0914c 52 bool process_close() {
ellingjp 8:8430a5c0914c 53 split_timer.reset();
ellingjp 8:8430a5c0914c 54 split_timer.stop();
ellingjp 15:002bac432234 55
ellingjp 21:2fa676f214fe 56 delete peakDetector;
ellingjp 21:2fa676f214fe 57 delete startDetector;
ellingjp 21:2fa676f214fe 58
ellingjp 8:8430a5c0914c 59 return true;
ellingjp 8:8430a5c0914c 60 }