Use Tiny BLE as a joystick. The data is got from an external data source via a serial port.
Dependencies: BLE_API BLE_HID eMPL_MPU6050 mbed nRF51822
Fork of Seeed_Tiny_BLE_Get_Started by
examples_common.cpp@4:89f37a17eba6, 2017-06-12 (annotated)
- Committer:
- bowenfeng
- Date:
- Mon Jun 12 04:10:04 2017 +0000
- Revision:
- 4:89f37a17eba6
Use Tiny BLE as a BLE joystick. The direction data is got from an external data source via serial protocol.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bowenfeng | 4:89f37a17eba6 | 1 | /* mbed Microcontroller Library |
bowenfeng | 4:89f37a17eba6 | 2 | * Copyright (c) 2015 ARM Limited |
bowenfeng | 4:89f37a17eba6 | 3 | * |
bowenfeng | 4:89f37a17eba6 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bowenfeng | 4:89f37a17eba6 | 5 | * you may not use this file except in compliance with the License. |
bowenfeng | 4:89f37a17eba6 | 6 | * You may obtain a copy of the License at |
bowenfeng | 4:89f37a17eba6 | 7 | * |
bowenfeng | 4:89f37a17eba6 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bowenfeng | 4:89f37a17eba6 | 9 | * |
bowenfeng | 4:89f37a17eba6 | 10 | * Unless required by applicable law or agreed to in writing, software |
bowenfeng | 4:89f37a17eba6 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bowenfeng | 4:89f37a17eba6 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bowenfeng | 4:89f37a17eba6 | 13 | * See the License for the specific language governing permissions and |
bowenfeng | 4:89f37a17eba6 | 14 | * limitations under the License. |
bowenfeng | 4:89f37a17eba6 | 15 | */ |
bowenfeng | 4:89f37a17eba6 | 16 | |
bowenfeng | 4:89f37a17eba6 | 17 | #include "ble/services/BatteryService.h" |
bowenfeng | 4:89f37a17eba6 | 18 | #include "ble/services/DeviceInformationService.h" |
bowenfeng | 4:89f37a17eba6 | 19 | |
bowenfeng | 4:89f37a17eba6 | 20 | #include "examples_common.h" |
bowenfeng | 4:89f37a17eba6 | 21 | |
bowenfeng | 4:89f37a17eba6 | 22 | static void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey) |
bowenfeng | 4:89f37a17eba6 | 23 | { |
bowenfeng | 4:89f37a17eba6 | 24 | printf("Input passKey: "); |
bowenfeng | 4:89f37a17eba6 | 25 | for (unsigned i = 0; i < Gap::ADDR_LEN; i++) { |
bowenfeng | 4:89f37a17eba6 | 26 | printf("%c", passkey[i]); |
bowenfeng | 4:89f37a17eba6 | 27 | } |
bowenfeng | 4:89f37a17eba6 | 28 | printf("\r\n"); |
bowenfeng | 4:89f37a17eba6 | 29 | } |
bowenfeng | 4:89f37a17eba6 | 30 | |
bowenfeng | 4:89f37a17eba6 | 31 | static void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status) |
bowenfeng | 4:89f37a17eba6 | 32 | { |
bowenfeng | 4:89f37a17eba6 | 33 | if (status == SecurityManager::SEC_STATUS_SUCCESS) { |
bowenfeng | 4:89f37a17eba6 | 34 | printf("Security success %d\r\n", status); |
bowenfeng | 4:89f37a17eba6 | 35 | } else { |
bowenfeng | 4:89f37a17eba6 | 36 | printf("Security failed %d\r\n", status); |
bowenfeng | 4:89f37a17eba6 | 37 | } |
bowenfeng | 4:89f37a17eba6 | 38 | } |
bowenfeng | 4:89f37a17eba6 | 39 | |
bowenfeng | 4:89f37a17eba6 | 40 | static void securitySetupInitiatedCallback(Gap::Handle_t, bool allowBonding, bool requireMITM, SecurityManager::SecurityIOCapabilities_t iocaps) |
bowenfeng | 4:89f37a17eba6 | 41 | { |
bowenfeng | 4:89f37a17eba6 | 42 | printf("Security setup initiated\r\n"); |
bowenfeng | 4:89f37a17eba6 | 43 | } |
bowenfeng | 4:89f37a17eba6 | 44 | |
bowenfeng | 4:89f37a17eba6 | 45 | void initializeSecurity(BLE &ble) |
bowenfeng | 4:89f37a17eba6 | 46 | { |
bowenfeng | 4:89f37a17eba6 | 47 | bool enableBonding = true; |
bowenfeng | 4:89f37a17eba6 | 48 | bool requireMITM = HID_SECURITY_REQUIRE_MITM; |
bowenfeng | 4:89f37a17eba6 | 49 | |
bowenfeng | 4:89f37a17eba6 | 50 | ble.securityManager().onSecuritySetupInitiated(securitySetupInitiatedCallback); |
bowenfeng | 4:89f37a17eba6 | 51 | ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback); |
bowenfeng | 4:89f37a17eba6 | 52 | ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback); |
bowenfeng | 4:89f37a17eba6 | 53 | |
bowenfeng | 4:89f37a17eba6 | 54 | ble.securityManager().init(enableBonding, requireMITM, HID_SECURITY_IOCAPS); |
bowenfeng | 4:89f37a17eba6 | 55 | } |
bowenfeng | 4:89f37a17eba6 | 56 | |
bowenfeng | 4:89f37a17eba6 | 57 | void initializeHOGP(BLE &ble) |
bowenfeng | 4:89f37a17eba6 | 58 | { |
bowenfeng | 4:89f37a17eba6 | 59 | static const uint16_t uuid16_list[] = {GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE, |
bowenfeng | 4:89f37a17eba6 | 60 | GattService::UUID_DEVICE_INFORMATION_SERVICE, |
bowenfeng | 4:89f37a17eba6 | 61 | GattService::UUID_BATTERY_SERVICE}; |
bowenfeng | 4:89f37a17eba6 | 62 | |
bowenfeng | 4:89f37a17eba6 | 63 | DeviceInformationService deviceInfo(ble, "ARM", "m1", "abc", "def", "ghi", "jkl"); |
bowenfeng | 4:89f37a17eba6 | 64 | |
bowenfeng | 4:89f37a17eba6 | 65 | BatteryService batteryInfo(ble, 80); |
bowenfeng | 4:89f37a17eba6 | 66 | |
bowenfeng | 4:89f37a17eba6 | 67 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | |
bowenfeng | 4:89f37a17eba6 | 68 | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
bowenfeng | 4:89f37a17eba6 | 69 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, |
bowenfeng | 4:89f37a17eba6 | 70 | (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
bowenfeng | 4:89f37a17eba6 | 71 | |
bowenfeng | 4:89f37a17eba6 | 72 | // see 5.1.2: HID over GATT Specification (pg. 25) |
bowenfeng | 4:89f37a17eba6 | 73 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
bowenfeng | 4:89f37a17eba6 | 74 | // 30ms to 50ms is recommended (5.1.2) |
bowenfeng | 4:89f37a17eba6 | 75 | ble.gap().setAdvertisingInterval(50); |
bowenfeng | 4:89f37a17eba6 | 76 | } |