Run on TY51822r3 board with ACC sensor (LIS3DH or BMC050)
Dependencies: BLE_API LIS3DH mbed nRF51822 BMC050 nRF51_LowPwr nRF51_Vdd
Fork of BLE_EddystoneBeacon_Service by
main.cpp@16:a7d07ea94b31, 2015-07-22 (annotated)
- Committer:
- mbedAustin
- Date:
- Wed Jul 22 18:01:20 2015 +0000
- Revision:
- 16:a7d07ea94b31
- Parent:
- 15:af8c24f34a9f
- Child:
- 18:91c36071aafb
[[DEBUG]] commiting for help with Debug. Currently hardfaulting when trying to hotswap frames.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 0:c04d932e96c9 | 1 | /* mbed Microcontroller Library |
screamer | 0:c04d932e96c9 | 2 | * Copyright (c) 2006-2013 ARM Limited |
screamer | 0:c04d932e96c9 | 3 | * |
screamer | 0:c04d932e96c9 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
screamer | 0:c04d932e96c9 | 5 | * you may not use this file except in compliance with the License. |
screamer | 0:c04d932e96c9 | 6 | * You may obtain a copy of the License at |
screamer | 0:c04d932e96c9 | 7 | * |
screamer | 0:c04d932e96c9 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
screamer | 0:c04d932e96c9 | 9 | * |
screamer | 0:c04d932e96c9 | 10 | * Unless required by applicable law or agreed to in writing, software |
screamer | 0:c04d932e96c9 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
screamer | 0:c04d932e96c9 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
screamer | 0:c04d932e96c9 | 13 | * See the License for the specific language governing permissions and |
screamer | 0:c04d932e96c9 | 14 | * limitations under the License. |
screamer | 0:c04d932e96c9 | 15 | */ |
screamer | 0:c04d932e96c9 | 16 | |
screamer | 0:c04d932e96c9 | 17 | #include "mbed.h" |
rgrover1 | 7:e9800c45e065 | 18 | #include "BLE.h" |
screamer | 0:c04d932e96c9 | 19 | #include "ZipBeaconConfigService.h" |
screamer | 0:c04d932e96c9 | 20 | #include "DFUService.h" |
screamer | 0:c04d932e96c9 | 21 | #include "DeviceInformationService.h" |
rgrover1 | 2:8020d6d4455a | 22 | #include "ConfigParamsPersistence.h" |
screamer | 0:c04d932e96c9 | 23 | |
rgrover1 | 8:1a21308e5008 | 24 | BLE ble; |
screamer | 0:c04d932e96c9 | 25 | ZipBeaconConfigService *zipBeaconConfig; |
mbedAustin | 15:af8c24f34a9f | 26 | //InterruptIn button(p17); |
screamer | 0:c04d932e96c9 | 27 | |
screamer | 0:c04d932e96c9 | 28 | /** |
screamer | 0:c04d932e96c9 | 29 | * URIBeaconConfig service can operate in two modes: a configuration mode which |
screamer | 0:c04d932e96c9 | 30 | * allows a user to update settings over a connection; and normal URIBeacon mode |
screamer | 0:c04d932e96c9 | 31 | * which involves advertising a URI. Constructing an object from URIBeaconConfig |
screamer | 0:c04d932e96c9 | 32 | * service sets up advertisements for the configuration mode. It is then up to |
screamer | 0:c04d932e96c9 | 33 | * the application to switch to URIBeacon mode based on some timeout. |
screamer | 0:c04d932e96c9 | 34 | * |
screamer | 0:c04d932e96c9 | 35 | * The following help with this switch. |
screamer | 0:c04d932e96c9 | 36 | */ |
mbedAustin | 15:af8c24f34a9f | 37 | static const int CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS = 5; // Duration after power-on that config service is available. |
screamer | 0:c04d932e96c9 | 38 | Ticker configAdvertisementTimeoutTicker; |
screamer | 0:c04d932e96c9 | 39 | |
mbedAustin | 14:5a2a104a21a8 | 40 | // |
mbedAustin | 14:5a2a104a21a8 | 41 | // Have to use this to add a interrupt, because C++ is stupid. |
mbedAustin | 14:5a2a104a21a8 | 42 | // |
mbedAustin | 16:a7d07ea94b31 | 43 | //void stupidWrapperFn(){ |
mbedAustin | 16:a7d07ea94b31 | 44 | // zipBeaconConfig->radioNotificationCallback(); |
mbedAustin | 16:a7d07ea94b31 | 45 | // } |
mbedAustin | 14:5a2a104a21a8 | 46 | |
screamer | 0:c04d932e96c9 | 47 | /** |
screamer | 0:c04d932e96c9 | 48 | * Stop advertising the UriBeaconConfig Service after a delay; and switch to normal URIBeacon. |
screamer | 0:c04d932e96c9 | 49 | */ |
screamer | 0:c04d932e96c9 | 50 | void timeout(void) |
screamer | 0:c04d932e96c9 | 51 | { |
screamer | 0:c04d932e96c9 | 52 | Gap::GapState_t state; |
screamer | 0:c04d932e96c9 | 53 | state = ble.getGapState(); |
screamer | 0:c04d932e96c9 | 54 | if (!state.connected) { /* don't switch if we're in a connected state. */ |
screamer | 0:c04d932e96c9 | 55 | zipBeaconConfig->setupZipBeaconAdvertisements(); |
rgrover1 | 8:1a21308e5008 | 56 | ble.gap().startAdvertising(); |
screamer | 0:c04d932e96c9 | 57 | |
screamer | 0:c04d932e96c9 | 58 | configAdvertisementTimeoutTicker.detach(); /* disable the callback from the timeout Ticker. */ |
mbedAustin | 15:af8c24f34a9f | 59 | printf("removing config service\r\n"); |
screamer | 0:c04d932e96c9 | 60 | } |
screamer | 0:c04d932e96c9 | 61 | } |
screamer | 0:c04d932e96c9 | 62 | |
screamer | 0:c04d932e96c9 | 63 | /** |
screamer | 0:c04d932e96c9 | 64 | * Callback triggered upon a disconnection event. Needs to re-enable advertisements. |
screamer | 0:c04d932e96c9 | 65 | */ |
screamer | 0:c04d932e96c9 | 66 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
screamer | 0:c04d932e96c9 | 67 | { |
rgrover1 | 8:1a21308e5008 | 68 | ble.gap().startAdvertising(); |
mbedAustin | 15:af8c24f34a9f | 69 | printf("disconnect CB\r\n"); |
screamer | 0:c04d932e96c9 | 70 | } |
screamer | 0:c04d932e96c9 | 71 | |
screamer | 0:c04d932e96c9 | 72 | int main(void) |
screamer | 0:c04d932e96c9 | 73 | { |
mbedAustin | 15:af8c24f34a9f | 74 | printf("Starting Example\r\n"); |
screamer | 0:c04d932e96c9 | 75 | ble.init(); |
rgrover1 | 8:1a21308e5008 | 76 | ble.gap().onDisconnection(disconnectionCallback); |
screamer | 0:c04d932e96c9 | 77 | |
screamer | 0:c04d932e96c9 | 78 | /* |
screamer | 0:c04d932e96c9 | 79 | * Load parameters from (platform specific) persistent storage. Parameters |
screamer | 0:c04d932e96c9 | 80 | * can be set to non-default values while the URIBeacon is in configuration |
screamer | 0:c04d932e96c9 | 81 | * mode (within the first 60 seconds of power-up). Thereafter, parameters |
screamer | 0:c04d932e96c9 | 82 | * get copied out to persistent storage before switching to normal URIBeacon |
screamer | 0:c04d932e96c9 | 83 | * operation. |
screamer | 0:c04d932e96c9 | 84 | */ |
screamer | 0:c04d932e96c9 | 85 | ZipBeaconConfigService::Params_t params; |
rgrover1 | 2:8020d6d4455a | 86 | bool fetchedFromPersistentStorage = loadURIBeaconConfigParams(¶ms); |
screamer | 0:c04d932e96c9 | 87 | |
screamer | 0:c04d932e96c9 | 88 | /* Initialize a zipBeaconConfig service providing config params, default URI, and power levels. */ |
screamer | 0:c04d932e96c9 | 89 | static ZipBeaconConfigService::PowerLevels_t defaultAdvPowerLevels = {-20, -4, 0, 10}; // Values for ADV packets related to firmware levels |
mbedAustin | 15:af8c24f34a9f | 90 | zipBeaconConfig = new ZipBeaconConfigService(ble, params, !fetchedFromPersistentStorage, "http://mbed.org", defaultAdvPowerLevels); |
screamer | 0:c04d932e96c9 | 91 | if (!zipBeaconConfig->configuredSuccessfully()) { |
screamer | 0:c04d932e96c9 | 92 | error("failed to accommodate URI"); |
screamer | 0:c04d932e96c9 | 93 | } |
screamer | 0:c04d932e96c9 | 94 | configAdvertisementTimeoutTicker.attach(timeout, CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS); |
screamer | 0:c04d932e96c9 | 95 | |
screamer | 0:c04d932e96c9 | 96 | // Setup auxiliary services to allow over-the-air firmware updates, etc |
screamer | 0:c04d932e96c9 | 97 | DFUService dfu(ble); |
screamer | 0:c04d932e96c9 | 98 | DeviceInformationService deviceInfo(ble, "ARM", "UriBeacon", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); |
screamer | 0:c04d932e96c9 | 99 | |
rgrover1 | 8:1a21308e5008 | 100 | ble.gap().startAdvertising(); /* Set the whole thing in motion. After this call a GAP central can scan the zipBeaconConfig |
rgrover1 | 8:1a21308e5008 | 101 | * service. This can then be switched to the normal URIBeacon functionality after a timeout. */ |
mbedAustin | 15:af8c24f34a9f | 102 | printf("Running...\r\n"); |
mbedAustin | 15:af8c24f34a9f | 103 | //button.rise(&stupidWrapperFn); |
screamer | 0:c04d932e96c9 | 104 | while (true) { |
screamer | 0:c04d932e96c9 | 105 | ble.waitForEvent(); |
screamer | 0:c04d932e96c9 | 106 | } |
screamer | 0:c04d932e96c9 | 107 | } |