![](/media/cache/profiles/1d7c91037101cce725efa1fe469232a5.50x50_q85.jpg)
Sample program illustrating the use of the GroveEarbudSensor library
Dependencies: GroveEarbudSensor mbed
main.cpp@0:1358c23ecb2e, 2014-09-25 (annotated)
- Committer:
- ansond
- Date:
- Thu Sep 25 21:35:44 2014 +0000
- Revision:
- 0:1358c23ecb2e
- Child:
- 4:df7309612854
initial checkin
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:1358c23ecb2e | 1 | /* Copyright C2014 ARM, MIT License |
ansond | 0:1358c23ecb2e | 2 | * |
ansond | 0:1358c23ecb2e | 3 | * Author: Doug Anson (doug.anson@arm.com) |
ansond | 0:1358c23ecb2e | 4 | * |
ansond | 0:1358c23ecb2e | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
ansond | 0:1358c23ecb2e | 6 | * and associated documentation files the "Software", to deal in the Software without restriction, |
ansond | 0:1358c23ecb2e | 7 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
ansond | 0:1358c23ecb2e | 8 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
ansond | 0:1358c23ecb2e | 9 | * furnished to do so, subject to the following conditions: |
ansond | 0:1358c23ecb2e | 10 | * |
ansond | 0:1358c23ecb2e | 11 | * The above copyright notice and this permission notice shall be included in all copies or |
ansond | 0:1358c23ecb2e | 12 | * substantial portions of the Software. |
ansond | 0:1358c23ecb2e | 13 | * |
ansond | 0:1358c23ecb2e | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
ansond | 0:1358c23ecb2e | 15 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
ansond | 0:1358c23ecb2e | 16 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
ansond | 0:1358c23ecb2e | 17 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ansond | 0:1358c23ecb2e | 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
ansond | 0:1358c23ecb2e | 19 | */ |
ansond | 0:1358c23ecb2e | 20 | |
ansond | 0:1358c23ecb2e | 21 | #include "mbed.h" |
ansond | 0:1358c23ecb2e | 22 | |
ansond | 0:1358c23ecb2e | 23 | // Console |
ansond | 0:1358c23ecb2e | 24 | Serial pc(USBTX,USBRX); |
ansond | 0:1358c23ecb2e | 25 | |
ansond | 0:1358c23ecb2e | 26 | // Blinky |
ansond | 0:1358c23ecb2e | 27 | DigitalOut led(LED1); |
ansond | 0:1358c23ecb2e | 28 | |
ansond | 0:1358c23ecb2e | 29 | // Our sensor as an InterruptIn |
ansond | 0:1358c23ecb2e | 30 | InterruptIn sensor(D0); |
ansond | 0:1358c23ecb2e | 31 | |
ansond | 0:1358c23ecb2e | 32 | // Grove Earbud Sensor include |
ansond | 0:1358c23ecb2e | 33 | #include "GroveEarbudSensor.h" |
ansond | 0:1358c23ecb2e | 34 | |
ansond | 0:1358c23ecb2e | 35 | // callback for receiving heartrate values |
ansond | 0:1358c23ecb2e | 36 | void heartrateCallback(float heartrate,void *data) { |
ansond | 0:1358c23ecb2e | 37 | pc.printf("Callback: heartrate = %.1f\r\n",heartrate); |
ansond | 0:1358c23ecb2e | 38 | } |
ansond | 0:1358c23ecb2e | 39 | |
ansond | 0:1358c23ecb2e | 40 | int main() |
ansond | 0:1358c23ecb2e | 41 | { |
ansond | 0:1358c23ecb2e | 42 | // announce |
ansond | 0:1358c23ecb2e | 43 | pc.printf("Grove Earbud Sensor Example v1.0.0\r\n"); |
ansond | 0:1358c23ecb2e | 44 | |
ansond | 0:1358c23ecb2e | 45 | // allocate the earbud sensor |
ansond | 0:1358c23ecb2e | 46 | pc.printf("Allocating earbud sensor instance...\r\n"); |
ansond | 0:1358c23ecb2e | 47 | GroveEarbudSensor earbud(&sensor); |
ansond | 0:1358c23ecb2e | 48 | |
ansond | 0:1358c23ecb2e | 49 | // register our callback function |
ansond | 0:1358c23ecb2e | 50 | pc.printf("registering callback...\r\n"); |
ansond | 0:1358c23ecb2e | 51 | earbud.registerCallback(heartrateCallback); |
ansond | 0:1358c23ecb2e | 52 | |
ansond | 0:1358c23ecb2e | 53 | // begin main loop |
ansond | 0:1358c23ecb2e | 54 | pc.printf("Beginning main loop...\r\n"); |
ansond | 0:1358c23ecb2e | 55 | while (true) { |
ansond | 0:1358c23ecb2e | 56 | // blink... |
ansond | 0:1358c23ecb2e | 57 | led = !led; |
ansond | 0:1358c23ecb2e | 58 | wait(0.5); |
ansond | 0:1358c23ecb2e | 59 | |
ansond | 0:1358c23ecb2e | 60 | // we can also call directly |
ansond | 0:1358c23ecb2e | 61 | //pc.printf("Direct: heartrate = %.1f\r\n",earbud.getHeartRate()); |
ansond | 0:1358c23ecb2e | 62 | } |
ansond | 0:1358c23ecb2e | 63 | } |