BLE mouse with uBit ( still some issues to resolve )
Dependencies: BLE_API mbed nRF51822
Fork of microbit_presenter by
keyboard_stream.cpp@1:9e174f8fd9e9, 2016-08-31 (annotated)
- Committer:
- JonnyA
- Date:
- Wed Aug 31 18:59:58 2016 +0000
- Revision:
- 1:9e174f8fd9e9
- Parent:
- 0:cb1939018833
Add fix from v0.4, while we work out how to properly keep GitHub in sync;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JonnyA | 0:cb1939018833 | 1 | /* mbed Microcontroller Library |
JonnyA | 0:cb1939018833 | 2 | * Copyright (c) 2015 ARM Limited |
JonnyA | 0:cb1939018833 | 3 | * |
JonnyA | 0:cb1939018833 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
JonnyA | 0:cb1939018833 | 5 | * you may not use this file except in compliance with the License. |
JonnyA | 0:cb1939018833 | 6 | * You may obtain a copy of the License at |
JonnyA | 0:cb1939018833 | 7 | * |
JonnyA | 0:cb1939018833 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
JonnyA | 0:cb1939018833 | 9 | * |
JonnyA | 0:cb1939018833 | 10 | * Unless required by applicable law or agreed to in writing, software |
JonnyA | 0:cb1939018833 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
JonnyA | 0:cb1939018833 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
JonnyA | 0:cb1939018833 | 13 | * See the License for the specific language governing permissions and |
JonnyA | 0:cb1939018833 | 14 | * limitations under the License. |
JonnyA | 0:cb1939018833 | 15 | */ |
JonnyA | 0:cb1939018833 | 16 | |
JonnyA | 0:cb1939018833 | 17 | #include "mbed.h" |
JonnyA | 0:cb1939018833 | 18 | |
JonnyA | 0:cb1939018833 | 19 | #include "ble/BLE.h" |
JonnyA | 0:cb1939018833 | 20 | #include "KeyboardService.h" |
JonnyA | 0:cb1939018833 | 21 | |
JonnyA | 0:cb1939018833 | 22 | #include "examples_common.h" |
JonnyA | 0:cb1939018833 | 23 | |
JonnyA | 0:cb1939018833 | 24 | /** |
JonnyA | 0:cb1939018833 | 25 | * This program implements a complete HID-over-Gatt Profile: |
JonnyA | 0:cb1939018833 | 26 | * - HID is provided by KeyboardService |
JonnyA | 0:cb1939018833 | 27 | * - Battery Service |
JonnyA | 0:cb1939018833 | 28 | * - Device Information Service |
JonnyA | 0:cb1939018833 | 29 | * |
JonnyA | 0:cb1939018833 | 30 | * Complete strings can be sent over BLE using printf. Please note, however, than a 12char string |
JonnyA | 0:cb1939018833 | 31 | * will take about 500ms to transmit, principally because of the limited notification rate in BLE. |
JonnyA | 0:cb1939018833 | 32 | * KeyboardService uses a circular buffer to store the strings to send, and calls to putc will fail |
JonnyA | 0:cb1939018833 | 33 | * once this buffer is full. This will result in partial strings being sent to the client. |
JonnyA | 0:cb1939018833 | 34 | */ |
JonnyA | 1:9e174f8fd9e9 | 35 | |
JonnyA | 1:9e174f8fd9e9 | 36 | //The micro:bit has a matrixed display, this is a simple way to use some LEDs on it |
JonnyA | 1:9e174f8fd9e9 | 37 | DigitalOut col9(P0_12, 0); |
JonnyA | 0:cb1939018833 | 38 | |
JonnyA | 1:9e174f8fd9e9 | 39 | DigitalOut waiting_led(P0_13); |
JonnyA | 1:9e174f8fd9e9 | 40 | DigitalOut connected_led(P0_15); |
JonnyA | 0:cb1939018833 | 41 | |
JonnyA | 0:cb1939018833 | 42 | InterruptIn button1(BUTTON_A); |
JonnyA | 0:cb1939018833 | 43 | InterruptIn button2(BUTTON_B); |
JonnyA | 0:cb1939018833 | 44 | |
JonnyA | 0:cb1939018833 | 45 | BLE ble; |
JonnyA | 0:cb1939018833 | 46 | KeyboardService *kbdServicePtr; |
JonnyA | 0:cb1939018833 | 47 | |
JonnyA | 1:9e174f8fd9e9 | 48 | static const char DEVICE_NAME[] = "micro:bit Presenter"; |
JonnyA | 0:cb1939018833 | 49 | static const char SHORT_DEVICE_NAME[] = "kbd1"; |
JonnyA | 0:cb1939018833 | 50 | |
JonnyA | 0:cb1939018833 | 51 | static void onDisconnect(const Gap::DisconnectionCallbackParams_t *params) |
JonnyA | 0:cb1939018833 | 52 | { |
JonnyA | 0:cb1939018833 | 53 | HID_DEBUG("disconnected\r\n"); |
JonnyA | 0:cb1939018833 | 54 | connected_led = 0; |
JonnyA | 0:cb1939018833 | 55 | |
JonnyA | 0:cb1939018833 | 56 | ble.gap().startAdvertising(); // restart advertising |
JonnyA | 0:cb1939018833 | 57 | } |
JonnyA | 0:cb1939018833 | 58 | |
JonnyA | 0:cb1939018833 | 59 | static void onConnect(const Gap::ConnectionCallbackParams_t *params) |
JonnyA | 0:cb1939018833 | 60 | { |
JonnyA | 0:cb1939018833 | 61 | HID_DEBUG("connected\r\n"); |
JonnyA | 0:cb1939018833 | 62 | waiting_led = false; |
JonnyA | 0:cb1939018833 | 63 | } |
JonnyA | 0:cb1939018833 | 64 | |
JonnyA | 0:cb1939018833 | 65 | static void waiting() { |
JonnyA | 0:cb1939018833 | 66 | if (!kbdServicePtr->isConnected()) |
JonnyA | 0:cb1939018833 | 67 | waiting_led = !waiting_led; |
JonnyA | 0:cb1939018833 | 68 | else |
JonnyA | 0:cb1939018833 | 69 | connected_led = !connected_led; |
JonnyA | 0:cb1939018833 | 70 | } |
JonnyA | 0:cb1939018833 | 71 | |
JonnyA | 0:cb1939018833 | 72 | void send_string(const char * c) { |
JonnyA | 0:cb1939018833 | 73 | if (!kbdServicePtr) |
JonnyA | 0:cb1939018833 | 74 | return; |
JonnyA | 0:cb1939018833 | 75 | |
JonnyA | 0:cb1939018833 | 76 | if (!kbdServicePtr->isConnected()) { |
JonnyA | 0:cb1939018833 | 77 | HID_DEBUG("we haven't connected yet..."); |
JonnyA | 0:cb1939018833 | 78 | } else { |
JonnyA | 0:cb1939018833 | 79 | int len = strlen(c); |
JonnyA | 0:cb1939018833 | 80 | kbdServicePtr->printf(c); |
JonnyA | 0:cb1939018833 | 81 | HID_DEBUG("sending %d chars\r\n", len); |
JonnyA | 0:cb1939018833 | 82 | } |
JonnyA | 0:cb1939018833 | 83 | } |
JonnyA | 0:cb1939018833 | 84 | |
JonnyA | 0:cb1939018833 | 85 | void send_stuff() { |
JonnyA | 0:cb1939018833 | 86 | send_string("n"); |
JonnyA | 0:cb1939018833 | 87 | } |
JonnyA | 0:cb1939018833 | 88 | |
JonnyA | 0:cb1939018833 | 89 | void send_more_stuff() { |
JonnyA | 0:cb1939018833 | 90 | send_string("p"); |
JonnyA | 0:cb1939018833 | 91 | } |
JonnyA | 0:cb1939018833 | 92 | |
JonnyA | 0:cb1939018833 | 93 | int main() |
JonnyA | 0:cb1939018833 | 94 | { |
JonnyA | 0:cb1939018833 | 95 | Ticker heartbeat; |
JonnyA | 0:cb1939018833 | 96 | |
JonnyA | 0:cb1939018833 | 97 | button1.rise(send_stuff); |
JonnyA | 0:cb1939018833 | 98 | button2.rise(send_more_stuff); |
JonnyA | 0:cb1939018833 | 99 | |
JonnyA | 0:cb1939018833 | 100 | HID_DEBUG("initialising ticker\r\n"); |
JonnyA | 0:cb1939018833 | 101 | |
JonnyA | 0:cb1939018833 | 102 | heartbeat.attach(waiting, 1); |
JonnyA | 0:cb1939018833 | 103 | |
JonnyA | 0:cb1939018833 | 104 | HID_DEBUG("initialising ble\r\n"); |
JonnyA | 0:cb1939018833 | 105 | ble.init(); |
JonnyA | 0:cb1939018833 | 106 | |
JonnyA | 0:cb1939018833 | 107 | ble.gap().onDisconnection(onDisconnect); |
JonnyA | 0:cb1939018833 | 108 | ble.gap().onConnection(onConnect); |
JonnyA | 0:cb1939018833 | 109 | |
JonnyA | 0:cb1939018833 | 110 | initializeSecurity(ble); |
JonnyA | 0:cb1939018833 | 111 | |
JonnyA | 0:cb1939018833 | 112 | HID_DEBUG("adding hid service\r\n"); |
JonnyA | 0:cb1939018833 | 113 | KeyboardService kbdService(ble); |
JonnyA | 0:cb1939018833 | 114 | kbdServicePtr = &kbdService; |
JonnyA | 0:cb1939018833 | 115 | |
JonnyA | 0:cb1939018833 | 116 | HID_DEBUG("adding device info and battery service\r\n"); |
JonnyA | 0:cb1939018833 | 117 | initializeHOGP(ble); |
JonnyA | 0:cb1939018833 | 118 | |
JonnyA | 0:cb1939018833 | 119 | HID_DEBUG("setting up gap\r\n"); |
JonnyA | 0:cb1939018833 | 120 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::KEYBOARD); |
JonnyA | 0:cb1939018833 | 121 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, |
JonnyA | 0:cb1939018833 | 122 | (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
JonnyA | 0:cb1939018833 | 123 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
JonnyA | 0:cb1939018833 | 124 | (uint8_t *)SHORT_DEVICE_NAME, sizeof(SHORT_DEVICE_NAME)); |
JonnyA | 1:9e174f8fd9e9 | 125 | ble.gap().setDeviceName((const uint8_t *)DEVICE_NAME); |
JonnyA | 0:cb1939018833 | 126 | |
JonnyA | 0:cb1939018833 | 127 | HID_DEBUG("advertising\r\n"); |
JonnyA | 0:cb1939018833 | 128 | ble.gap().startAdvertising(); |
JonnyA | 0:cb1939018833 | 129 | |
JonnyA | 0:cb1939018833 | 130 | while (true) { |
JonnyA | 0:cb1939018833 | 131 | ble.waitForEvent(); |
JonnyA | 0:cb1939018833 | 132 | } |
JonnyA | 0:cb1939018833 | 133 | } |
JonnyA | 0:cb1939018833 | 134 |