Based on the example USB HID keyboard https://developer.mbed.org/users/jbru/code/BLE_HID_KeyboardStreamDemo/

Dependencies:   BLE_API mbed nRF51822

Fork of HID-kb by Microbug

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