Jean-Philippe Brucker / Mbed 2 deprecated BLE_HID_MouseScrollDemo

Dependencies:   BLE_API BLE_HID mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mouse_scroll.cpp Source File

mouse_scroll.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 "MouseService.h"
00021 
00022 #include "examples_common.h"
00023 /*
00024  * Simplest use of MouseService: scroll up and down when buttons are pressed
00025  * To do that, we change wheel speed in HID reports when a button is pushed,
00026  * and reset it to 0 when it is released.
00027  */
00028 
00029 BLE ble;
00030 
00031 MouseService *mouseServicePtr;
00032 static const char DEVICE_NAME[] = "TrivialMouse";
00033 static const char SHORT_DEVICE_NAME[] = "mouse0";
00034 
00035 DigitalOut waiting_led(LED1);
00036 DigitalOut connected_led(LED2);
00037 
00038 InterruptIn button1(BUTTON1);
00039 InterruptIn button2(BUTTON2);
00040 
00041 void button1_down() {
00042     mouseServicePtr->setSpeed(0, 0, 1);
00043 }
00044 
00045 void button1_up() {
00046     mouseServicePtr->setSpeed(0, 0, 0);
00047 }
00048 
00049 void button2_down() {
00050     mouseServicePtr->setSpeed(0, 0, -1);
00051 }
00052 
00053 void button2_up() {
00054     mouseServicePtr->setSpeed(0, 0, 0);
00055 }
00056 
00057 
00058 static void onDisconnect(const Gap::DisconnectionCallbackParams_t *params)
00059 {
00060     HID_DEBUG("disconnected\r\n");
00061     connected_led = 0;
00062 
00063     ble.gap().startAdvertising(); // restart advertising
00064 }
00065 
00066 static void onConnect(const Gap::ConnectionCallbackParams_t *params)
00067 {
00068     HID_DEBUG("connected\r\n");
00069     waiting_led = 0;
00070 }
00071 
00072 static void waiting() {
00073     if (!mouseServicePtr->isConnected())
00074         waiting_led = !waiting_led;
00075     else
00076         connected_led = !connected_led;
00077 }
00078 
00079 int main()
00080 {
00081     Ticker heartbeat;
00082 
00083     button1.rise(button1_up);
00084     button1.fall(button1_down);
00085     button2.rise(button2_up);
00086     button2.fall(button2_down);
00087 
00088     HID_DEBUG("initialising ticker\r\n");
00089 
00090     heartbeat.attach(waiting, 1);
00091 
00092     HID_DEBUG("initialising ble\r\n");
00093     ble.init();
00094 
00095     ble.gap().onDisconnection(onDisconnect);
00096     ble.gap().onConnection(onConnect);
00097 
00098     initializeSecurity(ble);
00099 
00100     HID_DEBUG("adding hid service\r\n");
00101 
00102     MouseService mouseService(ble);
00103     mouseServicePtr = &mouseService;
00104 
00105     HID_DEBUG("adding dev info and battery service\r\n");
00106     initializeHOGP(ble);
00107 
00108     HID_DEBUG("setting up gap\r\n");
00109     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MOUSE);
00110     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,
00111                                            (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00112     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00113                                            (const uint8_t *)SHORT_DEVICE_NAME, sizeof(SHORT_DEVICE_NAME));
00114 
00115     ble.gap().setDeviceName((const uint8_t *)DEVICE_NAME);
00116 
00117     HID_DEBUG("advertising\r\n");
00118     ble.gap().startAdvertising();
00119 
00120     while (true) {
00121         ble.waitForEvent();
00122     }
00123 }