MouseScroll example from BLE_HID
Dependencies: BLE_API BLE_HID mbed nRF51822
This example is currently copied as-is from the github development repo.
mouse_scroll.cpp@4:8fb54802ba2b, 2016-04-03 (annotated)
- Committer:
- Jean-Philippe Brucker
- Date:
- Sun Apr 03 22:06:47 2016 +0100
- Revision:
- 4:8fb54802ba2b
- Parent:
- 1:0a97297327eb
v0.4 - small change to enable Win10 support
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jean-Philippe Brucker |
1:0a97297327eb | 1 | /* mbed Microcontroller Library |
Jean-Philippe Brucker |
1:0a97297327eb | 2 | * Copyright (c) 2015 ARM Limited |
Jean-Philippe Brucker |
1:0a97297327eb | 3 | * |
Jean-Philippe Brucker |
1:0a97297327eb | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Jean-Philippe Brucker |
1:0a97297327eb | 5 | * you may not use this file except in compliance with the License. |
Jean-Philippe Brucker |
1:0a97297327eb | 6 | * You may obtain a copy of the License at |
Jean-Philippe Brucker |
1:0a97297327eb | 7 | * |
Jean-Philippe Brucker |
1:0a97297327eb | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Jean-Philippe Brucker |
1:0a97297327eb | 9 | * |
Jean-Philippe Brucker |
1:0a97297327eb | 10 | * Unless required by applicable law or agreed to in writing, software |
Jean-Philippe Brucker |
1:0a97297327eb | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Jean-Philippe Brucker |
1:0a97297327eb | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Jean-Philippe Brucker |
1:0a97297327eb | 13 | * See the License for the specific language governing permissions and |
Jean-Philippe Brucker |
1:0a97297327eb | 14 | * limitations under the License. |
Jean-Philippe Brucker |
1:0a97297327eb | 15 | */ |
Jean-Philippe Brucker |
1:0a97297327eb | 16 | |
Jean-Philippe Brucker |
1:0a97297327eb | 17 | #include "mbed.h" |
Jean-Philippe Brucker |
1:0a97297327eb | 18 | |
Jean-Philippe Brucker |
1:0a97297327eb | 19 | #include "ble/BLE.h" |
Jean-Philippe Brucker |
1:0a97297327eb | 20 | #include "MouseService.h" |
Jean-Philippe Brucker |
1:0a97297327eb | 21 | |
Jean-Philippe Brucker |
1:0a97297327eb | 22 | #include "examples_common.h" |
Jean-Philippe Brucker |
1:0a97297327eb | 23 | /* |
Jean-Philippe Brucker |
1:0a97297327eb | 24 | * Simplest use of MouseService: scroll up and down when buttons are pressed |
Jean-Philippe Brucker |
1:0a97297327eb | 25 | * To do that, we change wheel speed in HID reports when a button is pushed, |
Jean-Philippe Brucker |
1:0a97297327eb | 26 | * and reset it to 0 when it is released. |
Jean-Philippe Brucker |
1:0a97297327eb | 27 | */ |
Jean-Philippe Brucker |
1:0a97297327eb | 28 | |
Jean-Philippe Brucker |
1:0a97297327eb | 29 | BLE ble; |
Jean-Philippe Brucker |
1:0a97297327eb | 30 | |
Jean-Philippe Brucker |
1:0a97297327eb | 31 | MouseService *mouseServicePtr; |
Jean-Philippe Brucker |
1:0a97297327eb | 32 | static const char DEVICE_NAME[] = "TrivialMouse"; |
Jean-Philippe Brucker |
1:0a97297327eb | 33 | static const char SHORT_DEVICE_NAME[] = "mouse0"; |
Jean-Philippe Brucker |
1:0a97297327eb | 34 | |
Jean-Philippe Brucker |
1:0a97297327eb | 35 | DigitalOut waiting_led(LED1); |
Jean-Philippe Brucker |
1:0a97297327eb | 36 | DigitalOut connected_led(LED2); |
Jean-Philippe Brucker |
1:0a97297327eb | 37 | |
Jean-Philippe Brucker |
1:0a97297327eb | 38 | InterruptIn button1(BUTTON1); |
Jean-Philippe Brucker |
1:0a97297327eb | 39 | InterruptIn button2(BUTTON2); |
Jean-Philippe Brucker |
1:0a97297327eb | 40 | |
Jean-Philippe Brucker |
1:0a97297327eb | 41 | void button1_down() { |
Jean-Philippe Brucker |
1:0a97297327eb | 42 | mouseServicePtr->setSpeed(0, 0, 1); |
Jean-Philippe Brucker |
1:0a97297327eb | 43 | } |
Jean-Philippe Brucker |
1:0a97297327eb | 44 | |
Jean-Philippe Brucker |
1:0a97297327eb | 45 | void button1_up() { |
Jean-Philippe Brucker |
1:0a97297327eb | 46 | mouseServicePtr->setSpeed(0, 0, 0); |
Jean-Philippe Brucker |
1:0a97297327eb | 47 | } |
Jean-Philippe Brucker |
1:0a97297327eb | 48 | |
Jean-Philippe Brucker |
1:0a97297327eb | 49 | void button2_down() { |
Jean-Philippe Brucker |
1:0a97297327eb | 50 | mouseServicePtr->setSpeed(0, 0, -1); |
Jean-Philippe Brucker |
1:0a97297327eb | 51 | } |
Jean-Philippe Brucker |
1:0a97297327eb | 52 | |
Jean-Philippe Brucker |
1:0a97297327eb | 53 | void button2_up() { |
Jean-Philippe Brucker |
1:0a97297327eb | 54 | mouseServicePtr->setSpeed(0, 0, 0); |
Jean-Philippe Brucker |
1:0a97297327eb | 55 | } |
Jean-Philippe Brucker |
1:0a97297327eb | 56 | |
Jean-Philippe Brucker |
1:0a97297327eb | 57 | |
Jean-Philippe Brucker |
1:0a97297327eb | 58 | static void onDisconnect(const Gap::DisconnectionCallbackParams_t *params) |
Jean-Philippe Brucker |
1:0a97297327eb | 59 | { |
Jean-Philippe Brucker |
1:0a97297327eb | 60 | HID_DEBUG("disconnected\r\n"); |
Jean-Philippe Brucker |
1:0a97297327eb | 61 | connected_led = 0; |
Jean-Philippe Brucker |
1:0a97297327eb | 62 | |
Jean-Philippe Brucker |
1:0a97297327eb | 63 | ble.gap().startAdvertising(); // restart advertising |
Jean-Philippe Brucker |
1:0a97297327eb | 64 | } |
Jean-Philippe Brucker |
1:0a97297327eb | 65 | |
Jean-Philippe Brucker |
1:0a97297327eb | 66 | static void onConnect(const Gap::ConnectionCallbackParams_t *params) |
Jean-Philippe Brucker |
1:0a97297327eb | 67 | { |
Jean-Philippe Brucker |
1:0a97297327eb | 68 | HID_DEBUG("connected\r\n"); |
Jean-Philippe Brucker |
1:0a97297327eb | 69 | waiting_led = 0; |
Jean-Philippe Brucker |
1:0a97297327eb | 70 | } |
Jean-Philippe Brucker |
1:0a97297327eb | 71 | |
Jean-Philippe Brucker |
1:0a97297327eb | 72 | static void waiting() { |
Jean-Philippe Brucker |
1:0a97297327eb | 73 | if (!mouseServicePtr->isConnected()) |
Jean-Philippe Brucker |
1:0a97297327eb | 74 | waiting_led = !waiting_led; |
Jean-Philippe Brucker |
1:0a97297327eb | 75 | else |
Jean-Philippe Brucker |
1:0a97297327eb | 76 | connected_led = !connected_led; |
Jean-Philippe Brucker |
1:0a97297327eb | 77 | } |
Jean-Philippe Brucker |
1:0a97297327eb | 78 | |
Jean-Philippe Brucker |
1:0a97297327eb | 79 | int main() |
Jean-Philippe Brucker |
1:0a97297327eb | 80 | { |
Jean-Philippe Brucker |
1:0a97297327eb | 81 | Ticker heartbeat; |
Jean-Philippe Brucker |
1:0a97297327eb | 82 | |
Jean-Philippe Brucker |
1:0a97297327eb | 83 | button1.rise(button1_up); |
Jean-Philippe Brucker |
1:0a97297327eb | 84 | button1.fall(button1_down); |
Jean-Philippe Brucker |
1:0a97297327eb | 85 | button2.rise(button2_up); |
Jean-Philippe Brucker |
1:0a97297327eb | 86 | button2.fall(button2_down); |
Jean-Philippe Brucker |
1:0a97297327eb | 87 | |
Jean-Philippe Brucker |
1:0a97297327eb | 88 | HID_DEBUG("initialising ticker\r\n"); |
Jean-Philippe Brucker |
1:0a97297327eb | 89 | |
Jean-Philippe Brucker |
1:0a97297327eb | 90 | heartbeat.attach(waiting, 1); |
Jean-Philippe Brucker |
1:0a97297327eb | 91 | |
Jean-Philippe Brucker |
1:0a97297327eb | 92 | HID_DEBUG("initialising ble\r\n"); |
Jean-Philippe Brucker |
1:0a97297327eb | 93 | ble.init(); |
Jean-Philippe Brucker |
1:0a97297327eb | 94 | |
Jean-Philippe Brucker |
1:0a97297327eb | 95 | ble.gap().onDisconnection(onDisconnect); |
Jean-Philippe Brucker |
1:0a97297327eb | 96 | ble.gap().onConnection(onConnect); |
Jean-Philippe Brucker |
1:0a97297327eb | 97 | |
Jean-Philippe Brucker |
1:0a97297327eb | 98 | initializeSecurity(ble); |
Jean-Philippe Brucker |
1:0a97297327eb | 99 | |
Jean-Philippe Brucker |
1:0a97297327eb | 100 | HID_DEBUG("adding hid service\r\n"); |
Jean-Philippe Brucker |
1:0a97297327eb | 101 | |
Jean-Philippe Brucker |
1:0a97297327eb | 102 | MouseService mouseService(ble); |
Jean-Philippe Brucker |
1:0a97297327eb | 103 | mouseServicePtr = &mouseService; |
Jean-Philippe Brucker |
1:0a97297327eb | 104 | |
Jean-Philippe Brucker |
1:0a97297327eb | 105 | HID_DEBUG("adding dev info and battery service\r\n"); |
Jean-Philippe Brucker |
1:0a97297327eb | 106 | initializeHOGP(ble); |
Jean-Philippe Brucker |
1:0a97297327eb | 107 | |
Jean-Philippe Brucker |
1:0a97297327eb | 108 | HID_DEBUG("setting up gap\r\n"); |
Jean-Philippe Brucker |
1:0a97297327eb | 109 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MOUSE); |
Jean-Philippe Brucker |
1:0a97297327eb | 110 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, |
Jean-Philippe Brucker |
4:8fb54802ba2b | 111 | (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
Jean-Philippe Brucker |
1:0a97297327eb | 112 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
Jean-Philippe Brucker |
4:8fb54802ba2b | 113 | (const uint8_t *)SHORT_DEVICE_NAME, sizeof(SHORT_DEVICE_NAME)); |
Jean-Philippe Brucker |
4:8fb54802ba2b | 114 | |
Jean-Philippe Brucker |
4:8fb54802ba2b | 115 | ble.gap().setDeviceName((const uint8_t *)DEVICE_NAME); |
Jean-Philippe Brucker |
1:0a97297327eb | 116 | |
Jean-Philippe Brucker |
1:0a97297327eb | 117 | HID_DEBUG("advertising\r\n"); |
Jean-Philippe Brucker |
1:0a97297327eb | 118 | ble.gap().startAdvertising(); |
Jean-Philippe Brucker |
1:0a97297327eb | 119 | |
Jean-Philippe Brucker |
1:0a97297327eb | 120 | while (true) { |
Jean-Philippe Brucker |
1:0a97297327eb | 121 | ble.waitForEvent(); |
Jean-Philippe Brucker |
1:0a97297327eb | 122 | } |
Jean-Philippe Brucker |
1:0a97297327eb | 123 | } |