BLE mouse with uBit ( still some issues to resolve )

Dependencies:   BLE_API mbed nRF51822

Fork of microbit_presenter by micro:bit

Committer:
suntopbd
Date:
Sun May 27 13:57:09 2018 +0000
Revision:
3:7036de769c32
ver 0.0.1 beta

Who changed what in which revision?

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