Example code to create a heartbeat sensor using the Si1146 on Silicon Lab's Biometrics expansion board for the Wonder Gecko
Dependencies: EFM32_SegmentLCD Si114x mbed
main.cpp@0:f3705ec14cf0, 2015-08-23 (annotated)
- Committer:
- Sissors
- Date:
- Sun Aug 23 17:00:09 2015 +0000
- Revision:
- 0:f3705ec14cf0
- Child:
- 1:b2b2924c3c48
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 0:f3705ec14cf0 | 1 | /* |
Sissors | 0:f3705ec14cf0 | 2 | Heart rate example for the SiLabs biometrics board using the Si1146 |
Sissors | 0:f3705ec14cf0 | 3 | |
Sissors | 0:f3705ec14cf0 | 4 | Since their own example code is all hidden behind precompiled libraries, here an |
Sissors | 0:f3705ec14cf0 | 5 | example which is actually useful :D. Take into account this is an example, I just screwed |
Sissors | 0:f3705ec14cf0 | 6 | around a bit and observed some output waveforms to make it work reasonable, there are |
Sissors | 0:f3705ec14cf0 | 7 | for sure better ways to implement this. |
Sissors | 0:f3705ec14cf0 | 8 | |
Sissors | 0:f3705ec14cf0 | 9 | User manual: |
Sissors | 0:f3705ec14cf0 | 10 | If the Gecko symbol turns on on the LCD it is connected to the sensor |
Sissors | 0:f3705ec14cf0 | 11 | Place your finger on the sensor. In the biometrics board manual it is explained how, |
Sissors | 0:f3705ec14cf0 | 12 | but you can also try until it works properly. You don't want to use the extreme tip of your |
Sissors | 0:f3705ec14cf0 | 13 | finger, but a bit further back. |
Sissors | 0:f3705ec14cf0 | 14 | The battery indicator will flash briefly when a beat is detected. |
Sissors | 0:f3705ec14cf0 | 15 | |
Sissors | 0:f3705ec14cf0 | 16 | Every beat it re-calculates your heartbeat using the time of the previous beat, since no |
Sissors | 0:f3705ec14cf0 | 17 | filtering happens here, it can jump around a bit, but this should be limitted. |
Sissors | 0:f3705ec14cf0 | 18 | |
Sissors | 0:f3705ec14cf0 | 19 | And just to be sure since we can never overestimate human stupidity: This is not a medical tool... |
Sissors | 0:f3705ec14cf0 | 20 | |
Sissors | 0:f3705ec14cf0 | 21 | |
Sissors | 0:f3705ec14cf0 | 22 | Todo: |
Sissors | 0:f3705ec14cf0 | 23 | 1. Get rid of my hardcoded value used as threshold |
Sissors | 0:f3705ec14cf0 | 24 | 2. Measure PO2 |
Sissors | 0:f3705ec14cf0 | 25 | */ |
Sissors | 0:f3705ec14cf0 | 26 | |
Sissors | 0:f3705ec14cf0 | 27 | |
Sissors | 0:f3705ec14cf0 | 28 | #include "mbed.h" |
Sissors | 0:f3705ec14cf0 | 29 | #include "Si114x.h" |
Sissors | 0:f3705ec14cf0 | 30 | #include "EFM32_SegmentLCD.h" |
Sissors | 0:f3705ec14cf0 | 31 | |
Sissors | 0:f3705ec14cf0 | 32 | |
Sissors | 0:f3705ec14cf0 | 33 | DigitalOut myled(LED1); |
Sissors | 0:f3705ec14cf0 | 34 | silabs::EFM32_SegmentLCD segmentDisplay; |
Sissors | 0:f3705ec14cf0 | 35 | |
Sissors | 0:f3705ec14cf0 | 36 | |
Sissors | 0:f3705ec14cf0 | 37 | int main() { |
Sissors | 0:f3705ec14cf0 | 38 | segmentDisplay.AllOff(); |
Sissors | 0:f3705ec14cf0 | 39 | Si114x sensor(PD6, PD7); |
Sissors | 0:f3705ec14cf0 | 40 | while(sensor.verifyConnection() != 1); |
Sissors | 0:f3705ec14cf0 | 41 | segmentDisplay.Symbol(LCD_SYMBOL_GECKO, 1); |
Sissors | 0:f3705ec14cf0 | 42 | |
Sissors | 0:f3705ec14cf0 | 43 | int curval; |
Sissors | 0:f3705ec14cf0 | 44 | int prevval = 0; |
Sissors | 0:f3705ec14cf0 | 45 | int mvingavg[3] = {0}; |
Sissors | 0:f3705ec14cf0 | 46 | int sum; |
Sissors | 0:f3705ec14cf0 | 47 | int prevtime = 0; |
Sissors | 0:f3705ec14cf0 | 48 | int timesincelast; |
Sissors | 0:f3705ec14cf0 | 49 | |
Sissors | 0:f3705ec14cf0 | 50 | Timer timey; |
Sissors | 0:f3705ec14cf0 | 51 | timey.start(); |
Sissors | 0:f3705ec14cf0 | 52 | while(1) { |
Sissors | 0:f3705ec14cf0 | 53 | curval=sensor.getProximity(2); //We use visible light, seems to work alot better than IR |
Sissors | 0:f3705ec14cf0 | 54 | |
Sissors | 0:f3705ec14cf0 | 55 | //Three times moving average of the diff of the output |
Sissors | 0:f3705ec14cf0 | 56 | mvingavg[2] = mvingavg[1]; |
Sissors | 0:f3705ec14cf0 | 57 | mvingavg[1] = mvingavg[0]; |
Sissors | 0:f3705ec14cf0 | 58 | mvingavg[0] = curval - prevval; |
Sissors | 0:f3705ec14cf0 | 59 | sum = mvingavg[2] + mvingavg[1] + mvingavg[0]; |
Sissors | 0:f3705ec14cf0 | 60 | |
Sissors | 0:f3705ec14cf0 | 61 | //Magic number for minimum threshold before it sees something as a heartbeat, adjust if my fingers are special :) |
Sissors | 0:f3705ec14cf0 | 62 | if (sum < -50) { |
Sissors | 0:f3705ec14cf0 | 63 | segmentDisplay.Battery(4); |
Sissors | 0:f3705ec14cf0 | 64 | timesincelast = timey.read_ms() - prevtime; |
Sissors | 0:f3705ec14cf0 | 65 | if (timesincelast > 250) { |
Sissors | 0:f3705ec14cf0 | 66 | uint32_t hearthrate = 60000 / timesincelast; |
Sissors | 0:f3705ec14cf0 | 67 | segmentDisplay.Number(hearthrate); |
Sissors | 0:f3705ec14cf0 | 68 | prevtime = timey.read_ms(); |
Sissors | 0:f3705ec14cf0 | 69 | } |
Sissors | 0:f3705ec14cf0 | 70 | } |
Sissors | 0:f3705ec14cf0 | 71 | else { |
Sissors | 0:f3705ec14cf0 | 72 | segmentDisplay.Battery(0); |
Sissors | 0:f3705ec14cf0 | 73 | } |
Sissors | 0:f3705ec14cf0 | 74 | |
Sissors | 0:f3705ec14cf0 | 75 | prevval = curval; |
Sissors | 0:f3705ec14cf0 | 76 | |
Sissors | 0:f3705ec14cf0 | 77 | } |
Sissors | 0:f3705ec14cf0 | 78 | |
Sissors | 0:f3705ec14cf0 | 79 | } |