Locator mobile version
Dependencies: BLE_API mbed nRF51822
Fork of WeatherStation by
main.cpp@1:f7fe82a00d4e, 2015-11-09 (annotated)
- Committer:
- PostaL
- Date:
- Mon Nov 09 20:58:25 2015 +0000
- Revision:
- 1:f7fe82a00d4e
- Parent:
- 0:7f951f57dbd2
- Child:
- 2:654ee4b3950f
Using env service from the new ble api
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 | 1:f7fe82a00d4e | 19 | #include "EnvironmentalService.h" |
PostaL | 0:7f951f57dbd2 | 20 | |
PostaL | 0:7f951f57dbd2 | 21 | BLE ble; |
PostaL | 0:7f951f57dbd2 | 22 | DigitalOut led1(LED1); |
PostaL | 0:7f951f57dbd2 | 23 | |
PostaL | 0:7f951f57dbd2 | 24 | const static char DEVICE_NAME[] = "Weather Station"; |
PostaL | 1:f7fe82a00d4e | 25 | static const uint16_t uuid16_list[] = {GattService::UUID_ENVIRONMENTAL_SERVICE}; |
PostaL | 0:7f951f57dbd2 | 26 | static volatile bool triggerSensorPolling = false; |
PostaL | 0:7f951f57dbd2 | 27 | |
PostaL | 0:7f951f57dbd2 | 28 | /* Restart Advertising on disconnection*/ |
PostaL | 0:7f951f57dbd2 | 29 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
PostaL | 0:7f951f57dbd2 | 30 | { |
PostaL | 0:7f951f57dbd2 | 31 | ble.gap().startAdvertising(); |
PostaL | 0:7f951f57dbd2 | 32 | } |
PostaL | 0:7f951f57dbd2 | 33 | |
PostaL | 1:f7fe82a00d4e | 34 | void blink(void) |
PostaL | 0:7f951f57dbd2 | 35 | { |
PostaL | 0:7f951f57dbd2 | 36 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
PostaL | 0:7f951f57dbd2 | 37 | triggerSensorPolling = true; |
PostaL | 0:7f951f57dbd2 | 38 | } |
PostaL | 0:7f951f57dbd2 | 39 | |
PostaL | 0:7f951f57dbd2 | 40 | int main(void) |
PostaL | 0:7f951f57dbd2 | 41 | { |
PostaL | 0:7f951f57dbd2 | 42 | led1 = 1; |
PostaL | 0:7f951f57dbd2 | 43 | Ticker ticker; |
PostaL | 1:f7fe82a00d4e | 44 | ticker.attach(blink, 1); |
PostaL | 0:7f951f57dbd2 | 45 | |
PostaL | 0:7f951f57dbd2 | 46 | ble.init(); |
PostaL | 0:7f951f57dbd2 | 47 | ble.gap().onDisconnection(disconnectionCallback); |
PostaL | 0:7f951f57dbd2 | 48 | |
PostaL | 0:7f951f57dbd2 | 49 | /* Setup primary service. */ |
PostaL | 1:f7fe82a00d4e | 50 | float currentTemperature = 42.22; |
PostaL | 1:f7fe82a00d4e | 51 | EnvironmentalService envService(ble); |
PostaL | 0:7f951f57dbd2 | 52 | |
PostaL | 0:7f951f57dbd2 | 53 | /* setup advertising */ |
PostaL | 0:7f951f57dbd2 | 54 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
PostaL | 0:7f951f57dbd2 | 55 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
PostaL | 0:7f951f57dbd2 | 56 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::THERMOMETER_EAR); |
PostaL | 0:7f951f57dbd2 | 57 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
PostaL | 0:7f951f57dbd2 | 58 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
PostaL | 0:7f951f57dbd2 | 59 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
PostaL | 0:7f951f57dbd2 | 60 | ble.gap().startAdvertising(); |
PostaL | 0:7f951f57dbd2 | 61 | |
PostaL | 0:7f951f57dbd2 | 62 | while (true) { |
PostaL | 0:7f951f57dbd2 | 63 | if (triggerSensorPolling && ble.getGapState().connected) { |
PostaL | 0:7f951f57dbd2 | 64 | triggerSensorPolling = false; |
PostaL | 0:7f951f57dbd2 | 65 | |
PostaL | 0:7f951f57dbd2 | 66 | /* Do blocking calls or whatever is necessary for sensor polling. */ |
PostaL | 0:7f951f57dbd2 | 67 | // error = sensor.readData(); |
PostaL | 0:7f951f57dbd2 | 68 | // if (!error) { |
PostaL | 0:7f951f57dbd2 | 69 | // thermometerService.updateTemperature(c); |
PostaL | 0:7f951f57dbd2 | 70 | // } |
PostaL | 0:7f951f57dbd2 | 71 | |
PostaL | 0:7f951f57dbd2 | 72 | /* In our case, we simply update the dummy temperature measurement. */ |
PostaL | 1:f7fe82a00d4e | 73 | // currentTemperature += 0.1; |
PostaL | 1:f7fe82a00d4e | 74 | envService.updateTemperature(currentTemperature); |
PostaL | 0:7f951f57dbd2 | 75 | } else { |
PostaL | 0:7f951f57dbd2 | 76 | ble.waitForEvent(); |
PostaL | 0:7f951f57dbd2 | 77 | } |
PostaL | 0:7f951f57dbd2 | 78 | } |
PostaL | 0:7f951f57dbd2 | 79 | } |