![](/media/cache/profiles/happy_luffy.jpg.50x50_q85.jpg)
BLE mouse with uBit ( still some issues to resolve )
Dependencies: BLE_API microbit_ble_mouse mbed nRF51822
Fork of microbit_mouse_BLE by
main.cpp@5:c1e87a1869cd, 2018-05-27 (annotated)
- Committer:
- suntopbd
- Date:
- Sun May 27 20:29:53 2018 +0000
- Revision:
- 5:c1e87a1869cd
- Parent:
- 4:b58633511f96
connects but does not work !
Who changed what in which revision?
User | Revision | Line number | New 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 | 5:c1e87a1869cd | 19 | #include "HIDServiceBase.h" |
suntopbd | 3:7036de769c32 | 20 | //#include "KeyboardService.h" |
suntopbd | 3:7036de769c32 | 21 | #include "MouseService.h" |
suntopbd | 3:7036de769c32 | 22 | #include "examples_common.h" |
suntopbd | 4:b58633511f96 | 23 | #include "MMA8652.h" // accelerometer |
suntopbd | 5:c1e87a1869cd | 24 | //Serial pc(USBTX, USBRX); |
suntopbd | 3:7036de769c32 | 25 | /** |
suntopbd | 5:c1e87a1869cd | 26 | * This program implements a complete //////////////////////////HID-over-Gatt Profile: |
suntopbd | 5:c1e87a1869cd | 27 | * - //////HID is provided by MouseService |
suntopbd | 3:7036de769c32 | 28 | * - Battery Service |
suntopbd | 3:7036de769c32 | 29 | * - Device Information Service |
suntopbd | 3:7036de769c32 | 30 | */ |
suntopbd | 3:7036de769c32 | 31 | |
suntopbd | 5:c1e87a1869cd | 32 | //The micro:bit has a matrixed display, this is a simple way to use some LEDs on it |
suntopbd | 5:c1e87a1869cd | 33 | // turn off LED saves 1 mA current |
suntopbd | 4:b58633511f96 | 34 | |
suntopbd | 5:c1e87a1869cd | 35 | DigitalOut col9(P0_12, 0); |
suntopbd | 3:7036de769c32 | 36 | //DigitalOut waiting_led(P0_13); |
suntopbd | 3:7036de769c32 | 37 | //DigitalOut connected_led(P0_15); |
suntopbd | 3:7036de769c32 | 38 | |
suntopbd | 5:c1e87a1869cd | 39 | MMA8652 accelerometer(P0_20, P0_19); // i2c pin assign |
suntopbd | 5:c1e87a1869cd | 40 | //I2C i2c(P0_19, P0_20); |
suntopbd | 5:c1e87a1869cd | 41 | //const int accel_addr = 0x3A; |
suntopbd | 5:c1e87a1869cd | 42 | |
suntopbd | 5:c1e87a1869cd | 43 | ButtonState click; |
suntopbd | 5:c1e87a1869cd | 44 | MouseButton LR; |
suntopbd | 5:c1e87a1869cd | 45 | |
suntopbd | 5:c1e87a1869cd | 46 | int Xpos = 0;int Ypos = 0; |
suntopbd | 5:c1e87a1869cd | 47 | int OldXpos = 0; int OldYpos = 0; |
suntopbd | 5:c1e87a1869cd | 48 | int dX = 0; int dY = 0; |
suntopbd | 5:c1e87a1869cd | 49 | |
suntopbd | 4:b58633511f96 | 50 | float accelerometer_data[3]; |
suntopbd | 4:b58633511f96 | 51 | float x,y,z; |
suntopbd | 5:c1e87a1869cd | 52 | Ticker movement,timeout ; |
suntopbd | 5:c1e87a1869cd | 53 | |
suntopbd | 4:b58633511f96 | 54 | |
suntopbd | 3:7036de769c32 | 55 | InterruptIn button1(BUTTON_A); |
suntopbd | 3:7036de769c32 | 56 | InterruptIn button2(BUTTON_B); |
suntopbd | 3:7036de769c32 | 57 | |
suntopbd | 3:7036de769c32 | 58 | BLE ble; |
suntopbd | 3:7036de769c32 | 59 | //KeyboardService *kbdServicePtr; |
suntopbd | 5:c1e87a1869cd | 60 | //MouseService musServicePtr(ble); |
suntopbd | 3:7036de769c32 | 61 | MouseService *musServicePtr; |
suntopbd | 5:c1e87a1869cd | 62 | static const char DEVICE_NAME[] = "micro:bit_BLE_Mouse"; |
suntopbd | 5:c1e87a1869cd | 63 | static const char SHORT_DEVICE_NAME[] = "uBit-mUs"; |
suntopbd | 3:7036de769c32 | 64 | |
suntopbd | 3:7036de769c32 | 65 | static void onDisconnect(const Gap::DisconnectionCallbackParams_t *params) |
suntopbd | 3:7036de769c32 | 66 | { |
suntopbd | 5:c1e87a1869cd | 67 | //////////HID_DEBUG("disconnected\r\n"); |
suntopbd | 3:7036de769c32 | 68 | // connected_led = 0; |
suntopbd | 3:7036de769c32 | 69 | ble.gap().startAdvertising(); // restart advertising |
suntopbd | 3:7036de769c32 | 70 | } |
suntopbd | 3:7036de769c32 | 71 | |
suntopbd | 3:7036de769c32 | 72 | static void onConnect(const Gap::ConnectionCallbackParams_t *params) |
suntopbd | 3:7036de769c32 | 73 | { |
suntopbd | 5:c1e87a1869cd | 74 | ////HID_DEBUG("connected\r\n"); |
suntopbd | 3:7036de769c32 | 75 | // waiting_led = false; |
suntopbd | 3:7036de769c32 | 76 | } |
suntopbd | 3:7036de769c32 | 77 | |
suntopbd | 5:c1e87a1869cd | 78 | static void waiting() |
suntopbd | 5:c1e87a1869cd | 79 | { |
suntopbd | 5:c1e87a1869cd | 80 | // if (!musServicePtr->isConnected()) |
suntopbd | 3:7036de769c32 | 81 | {}// waiting_led = !waiting_led; |
suntopbd | 5:c1e87a1869cd | 82 | // else |
suntopbd | 3:7036de769c32 | 83 | {}// connected_led = !connected_led; |
suntopbd | 3:7036de769c32 | 84 | } |
suntopbd | 3:7036de769c32 | 85 | |
suntopbd | 5:c1e87a1869cd | 86 | |
suntopbd | 5:c1e87a1869cd | 87 | void stop_mouse_move(void) |
suntopbd | 5:c1e87a1869cd | 88 | { |
suntopbd | 5:c1e87a1869cd | 89 | // Set mouse state to immobile |
suntopbd | 5:c1e87a1869cd | 90 | // musServicePtr->.setButton(MOUSE_BUTTON_LEFT, MOUSE_UP); |
suntopbd | 5:c1e87a1869cd | 91 | if (!musServicePtr) |
suntopbd | 5:c1e87a1869cd | 92 | return; |
suntopbd | 5:c1e87a1869cd | 93 | if (!musServicePtr->isConnected()) |
suntopbd | 5:c1e87a1869cd | 94 | { |
suntopbd | 5:c1e87a1869cd | 95 | //////HID_DEBUG("we haven't connected yet...\n"); |
suntopbd | 5:c1e87a1869cd | 96 | } else { |
suntopbd | 5:c1e87a1869cd | 97 | musServicePtr->setSpeed(0, 0, 0); |
suntopbd | 5:c1e87a1869cd | 98 | } |
suntopbd | 5:c1e87a1869cd | 99 | } |
suntopbd | 5:c1e87a1869cd | 100 | |
suntopbd | 5:c1e87a1869cd | 101 | void start_mouse_move(void) |
suntopbd | 5:c1e87a1869cd | 102 | { |
suntopbd | 5:c1e87a1869cd | 103 | // Move left with a left button down. If the focus is on a drawing |
suntopbd | 5:c1e87a1869cd | 104 | // software, for instance, this should draw a line. |
suntopbd | 5:c1e87a1869cd | 105 | // mouse.setButton(MOUSE_BUTTON_LEFT, MOUSE_DOWN); |
suntopbd | 5:c1e87a1869cd | 106 | musServicePtr->setSpeed(0, 0, 0); |
suntopbd | 5:c1e87a1869cd | 107 | // timeout.attach(stop_mouse_move, 0.2); |
suntopbd | 5:c1e87a1869cd | 108 | } |
suntopbd | 5:c1e87a1869cd | 109 | |
suntopbd | 5:c1e87a1869cd | 110 | |
suntopbd | 5:c1e87a1869cd | 111 | void register_Lclick() |
suntopbd | 5:c1e87a1869cd | 112 | { |
suntopbd | 5:c1e87a1869cd | 113 | // send click left |
suntopbd | 3:7036de769c32 | 114 | if (!musServicePtr) |
suntopbd | 3:7036de769c32 | 115 | return; |
suntopbd | 5:c1e87a1869cd | 116 | if (!musServicePtr->isConnected()) |
suntopbd | 5:c1e87a1869cd | 117 | { |
suntopbd | 5:c1e87a1869cd | 118 | //////HID_DEBUG("we haven't connected yet...\n"); |
suntopbd | 5:c1e87a1869cd | 119 | } else { |
suntopbd | 5:c1e87a1869cd | 120 | |
suntopbd | 5:c1e87a1869cd | 121 | LR = MOUSE_BUTTON_LEFT; |
suntopbd | 5:c1e87a1869cd | 122 | click = BUTTON_DOWN; |
suntopbd | 5:c1e87a1869cd | 123 | musServicePtr->setButton(LR,click); |
suntopbd | 5:c1e87a1869cd | 124 | } |
suntopbd | 5:c1e87a1869cd | 125 | } |
suntopbd | 3:7036de769c32 | 126 | |
suntopbd | 5:c1e87a1869cd | 127 | void register_Rclick() |
suntopbd | 5:c1e87a1869cd | 128 | { |
suntopbd | 5:c1e87a1869cd | 129 | // send click rightLR = MOUSE_BUTTON_LEFT; |
suntopbd | 5:c1e87a1869cd | 130 | if (!musServicePtr) |
suntopbd | 5:c1e87a1869cd | 131 | return; |
suntopbd | 5:c1e87a1869cd | 132 | if (!musServicePtr->isConnected()) |
suntopbd | 5:c1e87a1869cd | 133 | { |
suntopbd | 5:c1e87a1869cd | 134 | //////HID_DEBUG("we haven't connected yet...\n"); |
suntopbd | 3:7036de769c32 | 135 | } else { |
suntopbd | 5:c1e87a1869cd | 136 | |
suntopbd | 5:c1e87a1869cd | 137 | LR = MOUSE_BUTTON_RIGHT; |
suntopbd | 5:c1e87a1869cd | 138 | click = BUTTON_DOWN; |
suntopbd | 5:c1e87a1869cd | 139 | musServicePtr->setButton(LR,click); |
suntopbd | 5:c1e87a1869cd | 140 | } |
suntopbd | 5:c1e87a1869cd | 141 | } |
suntopbd | 5:c1e87a1869cd | 142 | |
suntopbd | 5:c1e87a1869cd | 143 | void register_Lclick_release() |
suntopbd | 5:c1e87a1869cd | 144 | { |
suntopbd | 5:c1e87a1869cd | 145 | // send click left_release |
suntopbd | 5:c1e87a1869cd | 146 | if (!musServicePtr) |
suntopbd | 5:c1e87a1869cd | 147 | return; |
suntopbd | 5:c1e87a1869cd | 148 | if (!musServicePtr->isConnected()) |
suntopbd | 5:c1e87a1869cd | 149 | { |
suntopbd | 5:c1e87a1869cd | 150 | //////HID_DEBUG("we haven't connected yet...\n"); |
suntopbd | 5:c1e87a1869cd | 151 | } else { |
suntopbd | 5:c1e87a1869cd | 152 | |
suntopbd | 5:c1e87a1869cd | 153 | LR = MOUSE_BUTTON_LEFT; |
suntopbd | 5:c1e87a1869cd | 154 | click = BUTTON_UP; |
suntopbd | 5:c1e87a1869cd | 155 | musServicePtr->setButton(LR,click); |
suntopbd | 5:c1e87a1869cd | 156 | } |
suntopbd | 3:7036de769c32 | 157 | } |
suntopbd | 3:7036de769c32 | 158 | |
suntopbd | 5:c1e87a1869cd | 159 | void register_Rclick_release() |
suntopbd | 5:c1e87a1869cd | 160 | { |
suntopbd | 5:c1e87a1869cd | 161 | // send click right_release |
suntopbd | 5:c1e87a1869cd | 162 | if (!musServicePtr) |
suntopbd | 5:c1e87a1869cd | 163 | return; |
suntopbd | 5:c1e87a1869cd | 164 | if (!musServicePtr->isConnected()) |
suntopbd | 5:c1e87a1869cd | 165 | { |
suntopbd | 5:c1e87a1869cd | 166 | //////HID_DEBUG("we haven't connected yet...\n"); |
suntopbd | 5:c1e87a1869cd | 167 | } else { |
suntopbd | 5:c1e87a1869cd | 168 | LR = MOUSE_BUTTON_RIGHT; |
suntopbd | 5:c1e87a1869cd | 169 | click = BUTTON_UP; |
suntopbd | 5:c1e87a1869cd | 170 | musServicePtr->setButton(LR,click); |
suntopbd | 5:c1e87a1869cd | 171 | } |
suntopbd | 5:c1e87a1869cd | 172 | } |
suntopbd | 5:c1e87a1869cd | 173 | |
suntopbd | 5:c1e87a1869cd | 174 | void stop_mouse_movement() |
suntopbd | 5:c1e87a1869cd | 175 | { |
suntopbd | 5:c1e87a1869cd | 176 | musServicePtr->setSpeed(0, 0, 0); |
suntopbd | 3:7036de769c32 | 177 | } |
suntopbd | 3:7036de769c32 | 178 | |
suntopbd | 5:c1e87a1869cd | 179 | void register_mouse_movement() |
suntopbd | 5:c1e87a1869cd | 180 | { |
suntopbd | 5:c1e87a1869cd | 181 | if (!musServicePtr) |
suntopbd | 5:c1e87a1869cd | 182 | return; |
suntopbd | 5:c1e87a1869cd | 183 | if (!musServicePtr->isConnected()) |
suntopbd | 5:c1e87a1869cd | 184 | { |
suntopbd | 5:c1e87a1869cd | 185 | //////HID_DEBUG("we haven't connected yet...\n"); |
suntopbd | 5:c1e87a1869cd | 186 | } else { |
suntopbd | 5:c1e87a1869cd | 187 | |
suntopbd | 5:c1e87a1869cd | 188 | accelerometer.ReadXYZ(accelerometer_data); |
suntopbd | 5:c1e87a1869cd | 189 | x = accelerometer_data[0]; |
suntopbd | 5:c1e87a1869cd | 190 | y = accelerometer_data[1]; |
suntopbd | 5:c1e87a1869cd | 191 | z = accelerometer_data[2]; |
suntopbd | 5:c1e87a1869cd | 192 | // do some math to adjust movement |
suntopbd | 5:c1e87a1869cd | 193 | |
suntopbd | 5:c1e87a1869cd | 194 | Xpos = x*50.0; |
suntopbd | 5:c1e87a1869cd | 195 | Ypos = y*50.0; // float to int conv |
suntopbd | 5:c1e87a1869cd | 196 | dX = abs(Xpos - OldXpos); dY = abs(Ypos - OldYpos); |
suntopbd | 5:c1e87a1869cd | 197 | OldXpos = Xpos; OldYpos = Ypos; |
suntopbd | 5:c1e87a1869cd | 198 | musServicePtr->setSpeed(dX, dY, 0); |
suntopbd | 5:c1e87a1869cd | 199 | dX=0;dY=0; |
suntopbd | 5:c1e87a1869cd | 200 | timeout.attach(stop_mouse_movement, 0.001); |
suntopbd | 5:c1e87a1869cd | 201 | } |
suntopbd | 3:7036de769c32 | 202 | } |
suntopbd | 3:7036de769c32 | 203 | |
suntopbd | 3:7036de769c32 | 204 | int main() |
suntopbd | 3:7036de769c32 | 205 | { |
suntopbd | 3:7036de769c32 | 206 | Ticker heartbeat; |
suntopbd | 5:c1e87a1869cd | 207 | |
suntopbd | 5:c1e87a1869cd | 208 | button1.rise(register_Lclick); |
suntopbd | 5:c1e87a1869cd | 209 | button2.rise(register_Rclick); |
suntopbd | 3:7036de769c32 | 210 | |
suntopbd | 5:c1e87a1869cd | 211 | button1.fall(register_Lclick_release); |
suntopbd | 5:c1e87a1869cd | 212 | button2.fall(register_Rclick_release); |
suntopbd | 5:c1e87a1869cd | 213 | |
suntopbd | 3:7036de769c32 | 214 | heartbeat.attach(waiting, 1); |
suntopbd | 5:c1e87a1869cd | 215 | |
suntopbd | 3:7036de769c32 | 216 | ble.init(); |
suntopbd | 3:7036de769c32 | 217 | ble.gap().onDisconnection(onDisconnect); |
suntopbd | 3:7036de769c32 | 218 | ble.gap().onConnection(onConnect); |
suntopbd | 3:7036de769c32 | 219 | initializeSecurity(ble); |
suntopbd | 3:7036de769c32 | 220 | initializeHOGP(ble); |
suntopbd | 3:7036de769c32 | 221 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MOUSE); |
suntopbd | 3:7036de769c32 | 222 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, |
suntopbd | 3:7036de769c32 | 223 | (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
suntopbd | 3:7036de769c32 | 224 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
suntopbd | 3:7036de769c32 | 225 | (uint8_t *)SHORT_DEVICE_NAME, sizeof(SHORT_DEVICE_NAME)); |
suntopbd | 3:7036de769c32 | 226 | ble.gap().setDeviceName((const uint8_t *)DEVICE_NAME); |
suntopbd | 3:7036de769c32 | 227 | ble.gap().startAdvertising(); |
suntopbd | 5:c1e87a1869cd | 228 | |
suntopbd | 5:c1e87a1869cd | 229 | wait(10); |
suntopbd | 5:c1e87a1869cd | 230 | // movement.attach(®ister_mouse_movement, 0.05); |
suntopbd | 3:7036de769c32 | 231 | |
suntopbd | 3:7036de769c32 | 232 | while (true) |
suntopbd | 3:7036de769c32 | 233 | { |
suntopbd | 5:c1e87a1869cd | 234 | register_mouse_movement(); |
suntopbd | 3:7036de769c32 | 235 | ble.waitForEvent(); |
suntopbd | 4:b58633511f96 | 236 | |
suntopbd | 4:b58633511f96 | 237 | /// make the following a separate periodic ticker |
suntopbd | 5:c1e87a1869cd | 238 | //accelerometer.ReadXYZ(accelerometer_data); |
suntopbd | 5:c1e87a1869cd | 239 | // x = accelerometer_data[0]; |
suntopbd | 5:c1e87a1869cd | 240 | // y = accelerometer_data[1]; |
suntopbd | 5:c1e87a1869cd | 241 | // z = accelerometer_data[2]; |
suntopbd | 4:b58633511f96 | 242 | |
suntopbd | 3:7036de769c32 | 243 | } |
suntopbd | 3:7036de769c32 | 244 | } |
suntopbd | 3:7036de769c32 | 245 |