interrupt handling

Dependencies:  

Committer:
rwclough
Date:
Wed Feb 25 21:06:50 2015 +0000
Revision:
0:5622c60e9d3a
Child:
1:1eb96189824d
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rwclough 0:5622c60e9d3a 1 /* mbed Microcontroller Library
rwclough 0:5622c60e9d3a 2 * Copyright (c) 2006-2013 ARM Limited
rwclough 0:5622c60e9d3a 3 *
rwclough 0:5622c60e9d3a 4 * Licensed under the Apache License, Version 2.0 (the "License");
rwclough 0:5622c60e9d3a 5 * you may not use this file except in compliance with the License.
rwclough 0:5622c60e9d3a 6 * You may obtain a copy of the License at
rwclough 0:5622c60e9d3a 7 *
rwclough 0:5622c60e9d3a 8 * http://www.apache.org/licenses/LICENSE-2.0
rwclough 0:5622c60e9d3a 9 *
rwclough 0:5622c60e9d3a 10 * Unless required by applicable law or agreed to in writing, software
rwclough 0:5622c60e9d3a 11 * distributed under the License is distributed on an "AS IS" BASIS,
rwclough 0:5622c60e9d3a 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rwclough 0:5622c60e9d3a 13 * See the License for the specific language governing permissions and
rwclough 0:5622c60e9d3a 14 * limitations under the License.
rwclough 0:5622c60e9d3a 15 */
rwclough 0:5622c60e9d3a 16
rwclough 0:5622c60e9d3a 17 #include "mbed.h"
rwclough 0:5622c60e9d3a 18 #include "BLEDevice.h"
rwclough 0:5622c60e9d3a 19 #include "HeartRateService.h"
rwclough 0:5622c60e9d3a 20 #include "DeviceInformationService.h"
rwclough 0:5622c60e9d3a 21
rwclough 0:5622c60e9d3a 22 /* Enable the following if you need to throttle the connection interval. This has
rwclough 0:5622c60e9d3a 23 * the effect of reducing energy consumption after a connection is made;
rwclough 0:5622c60e9d3a 24 * particularly for applications where the central may want a fast connection
rwclough 0:5622c60e9d3a 25 * interval.*/
rwclough 0:5622c60e9d3a 26 #define UPDATE_PARAMS_FOR_LONGER_CONNECTION_INTERVAL 0
rwclough 0:5622c60e9d3a 27
rwclough 0:5622c60e9d3a 28 BLEDevice ble;
rwclough 0:5622c60e9d3a 29 DigitalOut led1(LED1);
rwclough 0:5622c60e9d3a 30 Serial pc(USBTX, USBRX); // tx, rx
rwclough 0:5622c60e9d3a 31
rwclough 0:5622c60e9d3a 32 const static char DEVICE_NAME[] = "HRM1";
rwclough 0:5622c60e9d3a 33 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE,
rwclough 0:5622c60e9d3a 34 GattService::UUID_DEVICE_INFORMATION_SERVICE};
rwclough 0:5622c60e9d3a 35 static volatile bool triggerSensorPolling = false;
rwclough 0:5622c60e9d3a 36
rwclough 0:5622c60e9d3a 37 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
rwclough 0:5622c60e9d3a 38 {
rwclough 0:5622c60e9d3a 39 ble.startAdvertising(); // restart advertising
rwclough 0:5622c60e9d3a 40 }
rwclough 0:5622c60e9d3a 41
rwclough 0:5622c60e9d3a 42 void periodicCallback(void)
rwclough 0:5622c60e9d3a 43 {
rwclough 0:5622c60e9d3a 44 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
rwclough 0:5622c60e9d3a 45
rwclough 0:5622c60e9d3a 46 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
rwclough 0:5622c60e9d3a 47 * heavy-weight sensor polling from the main thread. */
rwclough 0:5622c60e9d3a 48 triggerSensorPolling = true;
rwclough 0:5622c60e9d3a 49 }
rwclough 0:5622c60e9d3a 50
rwclough 0:5622c60e9d3a 51 int main(void)
rwclough 0:5622c60e9d3a 52 {
rwclough 0:5622c60e9d3a 53 pc.baud(115200);
rwclough 0:5622c60e9d3a 54 for(int i = 0; i < 100; i ++)
rwclough 0:5622c60e9d3a 55 {
rwclough 0:5622c60e9d3a 56 printf("Hello Ron. Index is %d\r\n", i);
rwclough 0:5622c60e9d3a 57 wait_ms(100);
rwclough 0:5622c60e9d3a 58 }
rwclough 0:5622c60e9d3a 59 led1 = 1;
rwclough 0:5622c60e9d3a 60 Ticker ticker;
rwclough 0:5622c60e9d3a 61 ticker.attach(periodicCallback, 1); // blink LED every second
rwclough 0:5622c60e9d3a 62
rwclough 0:5622c60e9d3a 63 ble.init();
rwclough 0:5622c60e9d3a 64 ble.onDisconnection(disconnectionCallback);
rwclough 0:5622c60e9d3a 65
rwclough 0:5622c60e9d3a 66 /* Setup primary service. */
rwclough 0:5622c60e9d3a 67 uint8_t hrmCounter = 100; // init HRM to 100bps
rwclough 0:5622c60e9d3a 68 HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
rwclough 0:5622c60e9d3a 69
rwclough 0:5622c60e9d3a 70 /* Setup auxiliary service. */
rwclough 0:5622c60e9d3a 71 DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
rwclough 0:5622c60e9d3a 72
rwclough 0:5622c60e9d3a 73 /* Setup advertising. */
rwclough 0:5622c60e9d3a 74 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rwclough 0:5622c60e9d3a 75 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
rwclough 0:5622c60e9d3a 76 ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
rwclough 0:5622c60e9d3a 77 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rwclough 0:5622c60e9d3a 78 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
rwclough 0:5622c60e9d3a 79 ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000));
rwclough 0:5622c60e9d3a 80 ble.startAdvertising();
rwclough 0:5622c60e9d3a 81
rwclough 0:5622c60e9d3a 82 // infinite loop
rwclough 0:5622c60e9d3a 83 while (1) {
rwclough 0:5622c60e9d3a 84 // check for trigger from periodicCallback()
rwclough 0:5622c60e9d3a 85 if (triggerSensorPolling && ble.getGapState().connected) {
rwclough 0:5622c60e9d3a 86 triggerSensorPolling = false;
rwclough 0:5622c60e9d3a 87
rwclough 0:5622c60e9d3a 88 // Do blocking calls or whatever is necessary for sensor polling.
rwclough 0:5622c60e9d3a 89 // In our case, we simply update the HRM measurement.
rwclough 0:5622c60e9d3a 90 hrmCounter++;
rwclough 0:5622c60e9d3a 91
rwclough 0:5622c60e9d3a 92 // 100 <= HRM bps <=175
rwclough 0:5622c60e9d3a 93 if (hrmCounter == 175) {
rwclough 0:5622c60e9d3a 94 hrmCounter = 100;
rwclough 0:5622c60e9d3a 95 }
rwclough 0:5622c60e9d3a 96
rwclough 0:5622c60e9d3a 97 // update bps
rwclough 0:5622c60e9d3a 98 hrService.updateHeartRate(hrmCounter);
rwclough 0:5622c60e9d3a 99 } else {
rwclough 0:5622c60e9d3a 100 ble.waitForEvent(); // low power wait for event
rwclough 0:5622c60e9d3a 101 }
rwclough 0:5622c60e9d3a 102 }
rwclough 0:5622c60e9d3a 103 }