Yuuichi Akagawa / Mbed 2 deprecated BLENano_HID

Dependencies:   BLE_API BLE_HID mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016  
00017 #include "mbed.h"
00018  
00019 #include "ble/BLE.h"
00020 #include "KeyboardService.h"
00021  
00022 #include "examples_common.h"
00023  
00024 /**
00025  * This program implements a complete HID-over-Gatt Profile:
00026  *  - HID is provided by KeyboardService
00027  *  - Battery Service
00028  *  - Device Information Service
00029  *
00030  * Complete strings can be sent over BLE using printf. Please note, however, than a 12char string
00031  * will take about 500ms to transmit, principally because of the limited notification rate in BLE.
00032  * KeyboardService uses a circular buffer to store the strings to send, and calls to putc will fail
00033  * once this buffer is full. This will result in partial strings being sent to the client.
00034  */
00035  
00036 DigitalOut waiting_led(LED1);
00037 DigitalOut connected_led(LED2);
00038  
00039 InterruptIn button1(D2);
00040  
00041 BLE ble;
00042 KeyboardService *kbdServicePtr;
00043  
00044 static const char DEVICE_NAME[] = "uKbd";
00045 static const char SHORT_DEVICE_NAME[] = "kbd1";
00046  
00047 static void onDisconnect(const Gap::DisconnectionCallbackParams_t *params)
00048 {
00049     HID_DEBUG("disconnected\r\n");
00050     connected_led = 0;
00051  
00052     ble.gap().startAdvertising(); // restart advertising
00053 }
00054  
00055 static void onConnect(const Gap::ConnectionCallbackParams_t *params)
00056 {
00057     HID_DEBUG("connected\r\n");
00058     waiting_led = false;
00059 }
00060  
00061 static void waiting() {
00062     if (!kbdServicePtr->isConnected())
00063         waiting_led = !waiting_led;
00064     else
00065         connected_led = !connected_led;
00066 }
00067  
00068 void send_string(const char * c) {
00069     if (!kbdServicePtr)
00070         return;
00071  
00072     if (!kbdServicePtr->isConnected()) {
00073         HID_DEBUG("we haven't connected yet...");
00074     } else {
00075         int len = strlen(c);
00076         kbdServicePtr->printf(c);
00077         HID_DEBUG("sending %d chars\r\n", len);
00078     }
00079 }
00080  
00081 void send_stuff() {
00082     send_string("hello world!\n");
00083 }
00084  
00085 int main()
00086 {
00087     Ticker heartbeat;
00088  
00089     button1.rise(send_stuff);
00090  
00091     HID_DEBUG("initialising ticker\r\n");
00092  
00093     heartbeat.attach(waiting, 1);
00094  
00095     HID_DEBUG("initialising ble\r\n");
00096     ble.init();
00097  
00098     ble.gap().onDisconnection(onDisconnect);
00099     ble.gap().onConnection(onConnect);
00100  
00101     initializeSecurity(ble);
00102  
00103     HID_DEBUG("adding hid service\r\n");
00104     KeyboardService kbdService(ble);
00105     kbdServicePtr = &kbdService;
00106  
00107     HID_DEBUG("adding device info and battery service\r\n");
00108     initializeHOGP(ble);
00109  
00110     HID_DEBUG("setting up gap\r\n");
00111     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::KEYBOARD);
00112     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,
00113                                            (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00114     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00115                                            (uint8_t *)SHORT_DEVICE_NAME, sizeof(SHORT_DEVICE_NAME));
00116  
00117     HID_DEBUG("advertising\r\n");
00118     ble.gap().startAdvertising();
00119  
00120     while (true) {
00121         ble.waitForEvent();
00122     }
00123 }