mbed code for UberVest health monitoring
Dependencies: BLE_API mbed nRF51822
main.cpp@3:183823979708, 2015-10-22 (annotated)
- Committer:
- haydenball
- Date:
- Thu Oct 22 10:22:59 2015 +0000
- Revision:
- 3:183823979708
- Parent:
- 2:9b977e86e2d5
- Child:
- 4:851d3f52c344
Use AnalogIn rather than BUS in
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
haydenball | 0:b6fae6eb2bfe | 1 | /* mbed Microcontroller Library |
haydenball | 0:b6fae6eb2bfe | 2 | * Copyright (c) 2006-2013 ARM Limited |
haydenball | 0:b6fae6eb2bfe | 3 | * |
haydenball | 0:b6fae6eb2bfe | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
haydenball | 0:b6fae6eb2bfe | 5 | * you may not use this file except in compliance with the License. |
haydenball | 0:b6fae6eb2bfe | 6 | * You may obtain a copy of the License at |
haydenball | 0:b6fae6eb2bfe | 7 | * |
haydenball | 0:b6fae6eb2bfe | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
haydenball | 0:b6fae6eb2bfe | 9 | * |
haydenball | 0:b6fae6eb2bfe | 10 | * Unless required by applicable law or agreed to in writing, software |
haydenball | 0:b6fae6eb2bfe | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
haydenball | 0:b6fae6eb2bfe | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
haydenball | 0:b6fae6eb2bfe | 13 | * See the License for the specific language governing permissions and |
haydenball | 0:b6fae6eb2bfe | 14 | * limitations under the License. |
haydenball | 0:b6fae6eb2bfe | 15 | */ |
haydenball | 0:b6fae6eb2bfe | 16 | |
haydenball | 0:b6fae6eb2bfe | 17 | #include "mbed.h" |
haydenball | 0:b6fae6eb2bfe | 18 | #include "BLE.h" |
haydenball | 0:b6fae6eb2bfe | 19 | #include "UberVestService.h" |
haydenball | 0:b6fae6eb2bfe | 20 | |
haydenball | 0:b6fae6eb2bfe | 21 | BLE ble; |
haydenball | 2:9b977e86e2d5 | 22 | DigitalOut connectionLed(LED1); |
haydenball | 0:b6fae6eb2bfe | 23 | InterruptIn button(BUTTON1); |
haydenball | 3:183823979708 | 24 | AnalogIn ecg(p1); |
haydenball | 0:b6fae6eb2bfe | 25 | |
haydenball | 0:b6fae6eb2bfe | 26 | const static char DEVICE_NAME[] = "UberVest"; |
haydenball | 0:b6fae6eb2bfe | 27 | static const uint16_t uuid16_list[] = {UberVestService::UBER_VEST_SERVICE_UUID}; |
haydenball | 0:b6fae6eb2bfe | 28 | |
haydenball | 0:b6fae6eb2bfe | 29 | enum { |
haydenball | 0:b6fae6eb2bfe | 30 | RELEASED = 0, |
haydenball | 0:b6fae6eb2bfe | 31 | PRESSED, |
haydenball | 0:b6fae6eb2bfe | 32 | IDLE |
haydenball | 0:b6fae6eb2bfe | 33 | }; |
haydenball | 0:b6fae6eb2bfe | 34 | static uint8_t buttonState = IDLE; |
haydenball | 0:b6fae6eb2bfe | 35 | static int8_t ecgValue; |
haydenball | 0:b6fae6eb2bfe | 36 | |
haydenball | 0:b6fae6eb2bfe | 37 | UberVestService *uberVestServicePtr; |
haydenball | 0:b6fae6eb2bfe | 38 | |
haydenball | 0:b6fae6eb2bfe | 39 | void buttonPressedCallback(void) |
haydenball | 0:b6fae6eb2bfe | 40 | { |
haydenball | 0:b6fae6eb2bfe | 41 | /* Note that the buttonPressedCallback() executes in interrupt context, so it is safer to access |
haydenball | 0:b6fae6eb2bfe | 42 | * BLE device API from the main thread. */ |
haydenball | 0:b6fae6eb2bfe | 43 | buttonState = PRESSED; |
haydenball | 0:b6fae6eb2bfe | 44 | } |
haydenball | 0:b6fae6eb2bfe | 45 | |
haydenball | 0:b6fae6eb2bfe | 46 | void buttonReleasedCallback(void) |
haydenball | 0:b6fae6eb2bfe | 47 | { |
haydenball | 0:b6fae6eb2bfe | 48 | /* Note that the buttonReleasedCallback() executes in interrupt context, so it is safer to access |
haydenball | 0:b6fae6eb2bfe | 49 | * BLE device API from the main thread. */ |
haydenball | 0:b6fae6eb2bfe | 50 | buttonState = RELEASED; |
haydenball | 0:b6fae6eb2bfe | 51 | } |
haydenball | 0:b6fae6eb2bfe | 52 | |
haydenball | 2:9b977e86e2d5 | 53 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) |
haydenball | 2:9b977e86e2d5 | 54 | { |
haydenball | 2:9b977e86e2d5 | 55 | connectionLed = 1; |
haydenball | 2:9b977e86e2d5 | 56 | } |
haydenball | 2:9b977e86e2d5 | 57 | |
haydenball | 0:b6fae6eb2bfe | 58 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
haydenball | 0:b6fae6eb2bfe | 59 | { |
haydenball | 0:b6fae6eb2bfe | 60 | ble.gap().startAdvertising(); |
haydenball | 2:9b977e86e2d5 | 61 | connectionLed = 0; |
haydenball | 0:b6fae6eb2bfe | 62 | } |
haydenball | 0:b6fae6eb2bfe | 63 | |
haydenball | 0:b6fae6eb2bfe | 64 | int main(void) |
haydenball | 0:b6fae6eb2bfe | 65 | { |
haydenball | 0:b6fae6eb2bfe | 66 | button.fall(buttonPressedCallback); |
haydenball | 0:b6fae6eb2bfe | 67 | button.rise(buttonReleasedCallback); |
haydenball | 0:b6fae6eb2bfe | 68 | |
haydenball | 0:b6fae6eb2bfe | 69 | ble.init(); |
haydenball | 2:9b977e86e2d5 | 70 | ble.gap().onConnection(connectionCallback); |
haydenball | 0:b6fae6eb2bfe | 71 | ble.gap().onDisconnection(disconnectionCallback); |
haydenball | 0:b6fae6eb2bfe | 72 | |
haydenball | 0:b6fae6eb2bfe | 73 | UberVestService uberVestService(ble, false /* initial value for button pressed */, 0); |
haydenball | 0:b6fae6eb2bfe | 74 | uberVestServicePtr = &uberVestService; |
haydenball | 0:b6fae6eb2bfe | 75 | |
haydenball | 0:b6fae6eb2bfe | 76 | /* setup advertising */ |
haydenball | 0:b6fae6eb2bfe | 77 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
haydenball | 0:b6fae6eb2bfe | 78 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
haydenball | 0:b6fae6eb2bfe | 79 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
haydenball | 0:b6fae6eb2bfe | 80 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
haydenball | 0:b6fae6eb2bfe | 81 | ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ |
haydenball | 0:b6fae6eb2bfe | 82 | ble.gap().startAdvertising(); |
haydenball | 0:b6fae6eb2bfe | 83 | |
haydenball | 0:b6fae6eb2bfe | 84 | while (true) { |
haydenball | 0:b6fae6eb2bfe | 85 | if (buttonState!=IDLE) { |
haydenball | 0:b6fae6eb2bfe | 86 | uberVestServicePtr->updateButtonState(buttonState); |
haydenball | 0:b6fae6eb2bfe | 87 | buttonState = IDLE; |
haydenball | 0:b6fae6eb2bfe | 88 | } |
haydenball | 0:b6fae6eb2bfe | 89 | |
haydenball | 3:183823979708 | 90 | ecgValue = (ecg.read() * 1024); |
haydenball | 0:b6fae6eb2bfe | 91 | uberVestServicePtr->updateEcg(ecgValue); |
haydenball | 0:b6fae6eb2bfe | 92 | |
haydenball | 0:b6fae6eb2bfe | 93 | ble.waitForEvent(); |
haydenball | 0:b6fae6eb2bfe | 94 | } |
haydenball | 0:b6fae6eb2bfe | 95 | } |