KeyboardStream example from BLE_HID

Dependencies:   BLE_API BLE_HID mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers keyboard_stream.cpp Source File

keyboard_stream.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(BUTTON1);
00040 InterruptIn button2(BUTTON2);
00041 
00042 BLE ble;
00043 KeyboardService *kbdServicePtr;
00044 
00045 static const char DEVICE_NAME[] = "uKbd";
00046 static const char SHORT_DEVICE_NAME[] = "kbd1";
00047 
00048 static void onDisconnect(const Gap::DisconnectionCallbackParams_t *params)
00049 {
00050     HID_DEBUG("disconnected\r\n");
00051     connected_led = 0;
00052 
00053     ble.gap().startAdvertising(); // restart advertising
00054 }
00055 
00056 static void onConnect(const Gap::ConnectionCallbackParams_t *params)
00057 {
00058     HID_DEBUG("connected\r\n");
00059     waiting_led = false;
00060 }
00061 
00062 static void waiting() {
00063     if (!kbdServicePtr->isConnected())
00064         waiting_led = !waiting_led;
00065     else
00066         connected_led = !connected_led;
00067 }
00068 
00069 void send_string(const char * c) {
00070     if (!kbdServicePtr)
00071         return;
00072 
00073     if (!kbdServicePtr->isConnected()) {
00074         HID_DEBUG("we haven't connected yet...");
00075     } else {
00076         int len = strlen(c);
00077         kbdServicePtr->printf(c);
00078         HID_DEBUG("sending %d chars\r\n", len);
00079     }
00080 }
00081 
00082 void send_stuff() {
00083     send_string("hello world!\n");
00084 }
00085 
00086 void send_more_stuff() {
00087     send_string("All work and no play makes Jack a dull boy\n");
00088 }
00089 
00090 int main()
00091 {
00092     Ticker heartbeat;
00093 
00094     button1.rise(send_stuff);
00095     button2.rise(send_more_stuff);
00096 
00097     HID_DEBUG("initialising ticker\r\n");
00098 
00099     heartbeat.attach(waiting, 1);
00100 
00101     HID_DEBUG("initialising ble\r\n");
00102     ble.init();
00103 
00104     ble.gap().onDisconnection(onDisconnect);
00105     ble.gap().onConnection(onConnect);
00106 
00107     initializeSecurity(ble);
00108 
00109     HID_DEBUG("adding hid service\r\n");
00110     KeyboardService kbdService(ble);
00111     kbdServicePtr = &kbdService;
00112 
00113     HID_DEBUG("adding device info and battery service\r\n");
00114     initializeHOGP(ble);
00115 
00116     HID_DEBUG("setting up gap\r\n");
00117     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::KEYBOARD);
00118     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,
00119                                            (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00120     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00121                                            (const uint8_t *)SHORT_DEVICE_NAME, sizeof(SHORT_DEVICE_NAME));
00122 
00123     ble.gap().setDeviceName((const uint8_t *)DEVICE_NAME);
00124 
00125     HID_DEBUG("advertising\r\n");
00126     ble.gap().startAdvertising();
00127 
00128     while (true) {
00129         ble.waitForEvent();
00130     }
00131 }