This is mandatory
Dependencies: BLE_API mbed nRF51822 SHT21_ncleee
main.cpp@0:7f951f57dbd2, 2015-11-09 (annotated)
- Committer:
- PostaL
- Date:
- Mon Nov 09 18:57:46 2015 +0000
- Revision:
- 0:7f951f57dbd2
- Child:
- 1:f7fe82a00d4e
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
PostaL | 0:7f951f57dbd2 | 1 | /* mbed Microcontroller Library |
PostaL | 0:7f951f57dbd2 | 2 | * Copyright (c) 2006-2013 ARM Limited |
PostaL | 0:7f951f57dbd2 | 3 | * |
PostaL | 0:7f951f57dbd2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
PostaL | 0:7f951f57dbd2 | 5 | * you may not use this file except in compliance with the License. |
PostaL | 0:7f951f57dbd2 | 6 | * You may obtain a copy of the License at |
PostaL | 0:7f951f57dbd2 | 7 | * |
PostaL | 0:7f951f57dbd2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
PostaL | 0:7f951f57dbd2 | 9 | * |
PostaL | 0:7f951f57dbd2 | 10 | * Unless required by applicable law or agreed to in writing, software |
PostaL | 0:7f951f57dbd2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
PostaL | 0:7f951f57dbd2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
PostaL | 0:7f951f57dbd2 | 13 | * See the License for the specific language governing permissions and |
PostaL | 0:7f951f57dbd2 | 14 | * limitations under the License. |
PostaL | 0:7f951f57dbd2 | 15 | */ |
PostaL | 0:7f951f57dbd2 | 16 | |
PostaL | 0:7f951f57dbd2 | 17 | #include "mbed.h" |
PostaL | 0:7f951f57dbd2 | 18 | #include "BLE.h" |
PostaL | 0:7f951f57dbd2 | 19 | #include "HealthThermometerService.h" |
PostaL | 0:7f951f57dbd2 | 20 | #include "EnvSense.h" |
PostaL | 0:7f951f57dbd2 | 21 | |
PostaL | 0:7f951f57dbd2 | 22 | BLE ble; |
PostaL | 0:7f951f57dbd2 | 23 | DigitalOut led1(LED1); |
PostaL | 0:7f951f57dbd2 | 24 | |
PostaL | 0:7f951f57dbd2 | 25 | const static char DEVICE_NAME[] = "Weather Station"; |
PostaL | 0:7f951f57dbd2 | 26 | static const uint16_t uuid16_list[] = {EnvironmentalSensingService::SERVICE_UUID}; |
PostaL | 0:7f951f57dbd2 | 27 | static volatile bool triggerSensorPolling = false; |
PostaL | 0:7f951f57dbd2 | 28 | |
PostaL | 0:7f951f57dbd2 | 29 | /* Restart Advertising on disconnection*/ |
PostaL | 0:7f951f57dbd2 | 30 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
PostaL | 0:7f951f57dbd2 | 31 | { |
PostaL | 0:7f951f57dbd2 | 32 | ble.gap().startAdvertising(); |
PostaL | 0:7f951f57dbd2 | 33 | } |
PostaL | 0:7f951f57dbd2 | 34 | |
PostaL | 0:7f951f57dbd2 | 35 | void periodicCallback(void) |
PostaL | 0:7f951f57dbd2 | 36 | { |
PostaL | 0:7f951f57dbd2 | 37 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
PostaL | 0:7f951f57dbd2 | 38 | |
PostaL | 0:7f951f57dbd2 | 39 | /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
PostaL | 0:7f951f57dbd2 | 40 | * heavy-weight sensor polling from the main thread. */ |
PostaL | 0:7f951f57dbd2 | 41 | triggerSensorPolling = true; |
PostaL | 0:7f951f57dbd2 | 42 | } |
PostaL | 0:7f951f57dbd2 | 43 | |
PostaL | 0:7f951f57dbd2 | 44 | int main(void) |
PostaL | 0:7f951f57dbd2 | 45 | { |
PostaL | 0:7f951f57dbd2 | 46 | led1 = 1; |
PostaL | 0:7f951f57dbd2 | 47 | Ticker ticker; |
PostaL | 0:7f951f57dbd2 | 48 | ticker.attach(periodicCallback, 1); |
PostaL | 0:7f951f57dbd2 | 49 | |
PostaL | 0:7f951f57dbd2 | 50 | ble.init(); |
PostaL | 0:7f951f57dbd2 | 51 | ble.gap().onDisconnection(disconnectionCallback); |
PostaL | 0:7f951f57dbd2 | 52 | |
PostaL | 0:7f951f57dbd2 | 53 | /* Setup primary service. */ |
PostaL | 0:7f951f57dbd2 | 54 | uint8_t currentTemperature = 42; |
PostaL | 0:7f951f57dbd2 | 55 | EnvironmentalSensingService thermometerService(ble, currentTemperature); |
PostaL | 0:7f951f57dbd2 | 56 | |
PostaL | 0:7f951f57dbd2 | 57 | /* setup advertising */ |
PostaL | 0:7f951f57dbd2 | 58 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
PostaL | 0:7f951f57dbd2 | 59 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
PostaL | 0:7f951f57dbd2 | 60 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::THERMOMETER_EAR); |
PostaL | 0:7f951f57dbd2 | 61 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
PostaL | 0:7f951f57dbd2 | 62 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
PostaL | 0:7f951f57dbd2 | 63 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
PostaL | 0:7f951f57dbd2 | 64 | ble.gap().startAdvertising(); |
PostaL | 0:7f951f57dbd2 | 65 | |
PostaL | 0:7f951f57dbd2 | 66 | while (true) { |
PostaL | 0:7f951f57dbd2 | 67 | if (triggerSensorPolling && ble.getGapState().connected) { |
PostaL | 0:7f951f57dbd2 | 68 | triggerSensorPolling = false; |
PostaL | 0:7f951f57dbd2 | 69 | |
PostaL | 0:7f951f57dbd2 | 70 | /* Do blocking calls or whatever is necessary for sensor polling. */ |
PostaL | 0:7f951f57dbd2 | 71 | // error = sensor.readData(); |
PostaL | 0:7f951f57dbd2 | 72 | // if (!error) { |
PostaL | 0:7f951f57dbd2 | 73 | // thermometerService.updateTemperature(c); |
PostaL | 0:7f951f57dbd2 | 74 | // } |
PostaL | 0:7f951f57dbd2 | 75 | |
PostaL | 0:7f951f57dbd2 | 76 | /* In our case, we simply update the dummy temperature measurement. */ |
PostaL | 0:7f951f57dbd2 | 77 | currentTemperature += 0.1; |
PostaL | 0:7f951f57dbd2 | 78 | thermometerService.updateTemperature(22); |
PostaL | 0:7f951f57dbd2 | 79 | } else { |
PostaL | 0:7f951f57dbd2 | 80 | ble.waitForEvent(); |
PostaL | 0:7f951f57dbd2 | 81 | } |
PostaL | 0:7f951f57dbd2 | 82 | } |
PostaL | 0:7f951f57dbd2 | 83 | } |