for demo on Thursday

Dependencies:   microbit

Committer:
xx316
Date:
Wed Jun 12 15:21:01 2019 +0000
Revision:
1:032b96b05e51
for demo on Thursday;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xx316 1:032b96b05e51 1 /* mbed Microcontroller Library
xx316 1:032b96b05e51 2 * Copyright (c) 2015 ARM Limited
xx316 1:032b96b05e51 3 *
xx316 1:032b96b05e51 4 * Licensed under the Apache License, Version 2.0 (the "License");
xx316 1:032b96b05e51 5 * you may not use this file except in compliance with the License.
xx316 1:032b96b05e51 6 * You may obtain a copy of the License at
xx316 1:032b96b05e51 7 *
xx316 1:032b96b05e51 8 * http://www.apache.org/licenses/LICENSE-2.0
xx316 1:032b96b05e51 9 *
xx316 1:032b96b05e51 10 * Unless required by applicable law or agreed to in writing, software
xx316 1:032b96b05e51 11 * distributed under the License is distributed on an "AS IS" BASIS,
xx316 1:032b96b05e51 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
xx316 1:032b96b05e51 13 * See the License for the specific language governing permissions and
xx316 1:032b96b05e51 14 * limitations under the License.
xx316 1:032b96b05e51 15 */
xx316 1:032b96b05e51 16
xx316 1:032b96b05e51 17 #include "mbed.h"
xx316 1:032b96b05e51 18 #include "ble/BLE.h"
xx316 1:032b96b05e51 19 #include "KeyboardService.h"
xx316 1:032b96b05e51 20 #include "MicroBitPin.h"
xx316 1:032b96b05e51 21 #include "examples_common.h"
xx316 1:032b96b05e51 22 #include "MicroBit.h"
xx316 1:032b96b05e51 23 /**
xx316 1:032b96b05e51 24 * This program implements a complete HID-over-Gatt Profile:
xx316 1:032b96b05e51 25 * - HID is provided by KeyboardService
xx316 1:032b96b05e51 26 * - Battery Service
xx316 1:032b96b05e51 27 * - Device Information Service
xx316 1:032b96b05e51 28 *
xx316 1:032b96b05e51 29 * Complete strings can be sent over BLE using printf. Please note, however, than a 12char string
xx316 1:032b96b05e51 30 * will take about 500ms to transmit, principally because of the limited notification rate in BLE.
xx316 1:032b96b05e51 31 * KeyboardService uses a circular buffer to store the strings to send, and calls to putc will fail
xx316 1:032b96b05e51 32 * once this buffer is full. This will result in partial strings being sent to the client.
xx316 1:032b96b05e51 33 */
xx316 1:032b96b05e51 34
xx316 1:032b96b05e51 35 //The micro:bit has a matrixed display, this is a simple way to use some LEDs on it
xx316 1:032b96b05e51 36 DigitalOut col9(P0_12, 0);
xx316 1:032b96b05e51 37 DigitalOut waiting_led(P0_13);
xx316 1:032b96b05e51 38 DigitalOut connected_led(P0_15);
xx316 1:032b96b05e51 39
xx316 1:032b96b05e51 40 InterruptIn button1(BUTTON_A);
xx316 1:032b96b05e51 41 InterruptIn button2(BUTTON_B);
xx316 1:032b96b05e51 42
xx316 1:032b96b05e51 43 //InterruptIn key1(MICROBIT_PIN_P0);
xx316 1:032b96b05e51 44 // Try to use external signal, rather than button a&b as keys
xx316 1:032b96b05e51 45
xx316 1:032b96b05e51 46 BLE ble;
xx316 1:032b96b05e51 47 KeyboardService *kbdServicePtr;
xx316 1:032b96b05e51 48
xx316 1:032b96b05e51 49 static const char DEVICE_NAME[] = "bit:board wire loop";
xx316 1:032b96b05e51 50 static const char SHORT_DEVICE_NAME[] = "kbd blue";
xx316 1:032b96b05e51 51
xx316 1:032b96b05e51 52 static void onDisconnect(const Gap::DisconnectionCallbackParams_t *params)
xx316 1:032b96b05e51 53 {
xx316 1:032b96b05e51 54 HID_DEBUG("disconnected\r\n");
xx316 1:032b96b05e51 55 connected_led = 0;
xx316 1:032b96b05e51 56
xx316 1:032b96b05e51 57 ble.gap().startAdvertising(); // restart advertising
xx316 1:032b96b05e51 58 }
xx316 1:032b96b05e51 59
xx316 1:032b96b05e51 60 static void onConnect(const Gap::ConnectionCallbackParams_t *params)
xx316 1:032b96b05e51 61 {
xx316 1:032b96b05e51 62 HID_DEBUG("connected\r\n");
xx316 1:032b96b05e51 63 waiting_led = false;
xx316 1:032b96b05e51 64 }
xx316 1:032b96b05e51 65
xx316 1:032b96b05e51 66 static void waiting() {
xx316 1:032b96b05e51 67 if (!kbdServicePtr->isConnected())
xx316 1:032b96b05e51 68 waiting_led = !waiting_led;
xx316 1:032b96b05e51 69 else
xx316 1:032b96b05e51 70 connected_led = !connected_led;
xx316 1:032b96b05e51 71 }
xx316 1:032b96b05e51 72
xx316 1:032b96b05e51 73 void send_string(const char * c) {
xx316 1:032b96b05e51 74 if (!kbdServicePtr)
xx316 1:032b96b05e51 75 return;
xx316 1:032b96b05e51 76
xx316 1:032b96b05e51 77 if (!kbdServicePtr->isConnected()) {
xx316 1:032b96b05e51 78 HID_DEBUG("we haven't connected yet...");
xx316 1:032b96b05e51 79 } else {
xx316 1:032b96b05e51 80 int len = strlen(c);
xx316 1:032b96b05e51 81 kbdServicePtr->printf(c);
xx316 1:032b96b05e51 82 HID_DEBUG("sending %d chars\r\n", len);
xx316 1:032b96b05e51 83 }
xx316 1:032b96b05e51 84 }
xx316 1:032b96b05e51 85
xx316 1:032b96b05e51 86 void send_stuff() {send_string("n");wait(0.1);}
xx316 1:032b96b05e51 87
xx316 1:032b96b05e51 88 void send_more_stuff() {send_string("p"); wait(0.1);}
xx316 1:032b96b05e51 89
xx316 1:032b96b05e51 90 void send_one(){
xx316 1:032b96b05e51 91 inputReportData[0] = 0;
xx316 1:032b96b05e51 92 inputReportData[2] = 0x1e;
xx316 1:032b96b05e51 93 kbdServicePtr->send(inputReportData); //key down event?
xx316 1:032b96b05e51 94 kbdServicePtr->send(emptyInputReportData); // key up event?
xx316 1:032b96b05e51 95 wait(0.1);
xx316 1:032b96b05e51 96 }
xx316 1:032b96b05e51 97
xx316 1:032b96b05e51 98 void send_zero(){// testing input from pins
xx316 1:032b96b05e51 99 inputReportData[0] = 0;
xx316 1:032b96b05e51 100 inputReportData[2] = 0x27;
xx316 1:032b96b05e51 101 kbdServicePtr->send(inputReportData); //key down event?
xx316 1:032b96b05e51 102 kbdServicePtr->send(emptyInputReportData); // key up event?
xx316 1:032b96b05e51 103 wait(0.1);
xx316 1:032b96b05e51 104 }
xx316 1:032b96b05e51 105
xx316 1:032b96b05e51 106 void send_up(){// testing input from pins
xx316 1:032b96b05e51 107 inputReportData[0] = 0;
xx316 1:032b96b05e51 108 inputReportData[2] = 0x52;
xx316 1:032b96b05e51 109 kbdServicePtr->send(inputReportData); //key down event?
xx316 1:032b96b05e51 110 kbdServicePtr->send(emptyInputReportData); // key up event?
xx316 1:032b96b05e51 111 wait(0.1);
xx316 1:032b96b05e51 112 }
xx316 1:032b96b05e51 113
xx316 1:032b96b05e51 114 void send_down(){// testing input from pins
xx316 1:032b96b05e51 115 // kbdServicePtr->putc(DownArrow);
xx316 1:032b96b05e51 116 inputReportData[0] = 0;
xx316 1:032b96b05e51 117 inputReportData[2] = 0x51;
xx316 1:032b96b05e51 118 kbdServicePtr->send(inputReportData);
xx316 1:032b96b05e51 119 kbdServicePtr->send(emptyInputReportData);
xx316 1:032b96b05e51 120 wait(0.1);
xx316 1:032b96b05e51 121 }
xx316 1:032b96b05e51 122
xx316 1:032b96b05e51 123 void send_left(){// testing input from pins
xx316 1:032b96b05e51 124 inputReportData[0] = 0;
xx316 1:032b96b05e51 125 inputReportData[2] = 0x50;
xx316 1:032b96b05e51 126 kbdServicePtr->send(inputReportData);
xx316 1:032b96b05e51 127 kbdServicePtr->send(emptyInputReportData);
xx316 1:032b96b05e51 128 wait(0.1);
xx316 1:032b96b05e51 129 }
xx316 1:032b96b05e51 130
xx316 1:032b96b05e51 131 void send_right(){// testing input from pins
xx316 1:032b96b05e51 132 inputReportData[0] = 0;
xx316 1:032b96b05e51 133 inputReportData[2] = 0x4f;
xx316 1:032b96b05e51 134 kbdServicePtr->send(inputReportData);
xx316 1:032b96b05e51 135 kbdServicePtr->send(emptyInputReportData);
xx316 1:032b96b05e51 136 wait(0.1);
xx316 1:032b96b05e51 137 }
xx316 1:032b96b05e51 138
xx316 1:032b96b05e51 139 void send_space(){// testing input from pins
xx316 1:032b96b05e51 140 inputReportData[0] = 0;
xx316 1:032b96b05e51 141 inputReportData[2] = 0x2c;
xx316 1:032b96b05e51 142 kbdServicePtr->send(inputReportData);
xx316 1:032b96b05e51 143 kbdServicePtr->send(emptyInputReportData);
xx316 1:032b96b05e51 144 wait(0.1);
xx316 1:032b96b05e51 145 }
xx316 1:032b96b05e51 146
xx316 1:032b96b05e51 147 void send_delete(){// testing input from pins
xx316 1:032b96b05e51 148 inputReportData[0] = 0;
xx316 1:032b96b05e51 149 inputReportData[2] = 0x2a;
xx316 1:032b96b05e51 150 kbdServicePtr->send(inputReportData);
xx316 1:032b96b05e51 151 kbdServicePtr->send(emptyInputReportData);
xx316 1:032b96b05e51 152 wait(0.1);
xx316 1:032b96b05e51 153 }
xx316 1:032b96b05e51 154 void send_tab(){// testing input from pins
xx316 1:032b96b05e51 155 inputReportData[0] = 0;
xx316 1:032b96b05e51 156 inputReportData[2] = 0x2b;
xx316 1:032b96b05e51 157 kbdServicePtr->send(inputReportData);
xx316 1:032b96b05e51 158 kbdServicePtr->send(emptyInputReportData);
xx316 1:032b96b05e51 159 wait(0.1);
xx316 1:032b96b05e51 160 }
xx316 1:032b96b05e51 161
xx316 1:032b96b05e51 162 void send_return(){// testing input from pins
xx316 1:032b96b05e51 163 inputReportData[0] = 0;
xx316 1:032b96b05e51 164 inputReportData[2] = 0x28;
xx316 1:032b96b05e51 165 kbdServicePtr->send(inputReportData);
xx316 1:032b96b05e51 166 kbdServicePtr->send(emptyInputReportData);
xx316 1:032b96b05e51 167 wait(0.1);
xx316 1:032b96b05e51 168 }
xx316 1:032b96b05e51 169
xx316 1:032b96b05e51 170 void send_release(){
xx316 1:032b96b05e51 171 kbdServicePtr->send(emptyInputReportData);
xx316 1:032b96b05e51 172 wait(0.1);
xx316 1:032b96b05e51 173 }
xx316 1:032b96b05e51 174
xx316 1:032b96b05e51 175
xx316 1:032b96b05e51 176 DigitalIn touch1(MICROBIT_PIN_P13); // for the normal 4
xx316 1:032b96b05e51 177 DigitalIn touch2(MICROBIT_PIN_P14);
xx316 1:032b96b05e51 178 DigitalIn touch3(MICROBIT_PIN_P15);
xx316 1:032b96b05e51 179 DigitalIn touch4(MICROBIT_PIN_P16); // disable for wire loop example?
xx316 1:032b96b05e51 180 InterruptIn touch0(MICROBIT_PIN_P8);//
xx316 1:032b96b05e51 181 //DigitalIn touch0(MICROBIT_PIN_P8); // for wire loop example only
xx316 1:032b96b05e51 182 AnalogIn x_axis(MICROBIT_PIN_P1);
xx316 1:032b96b05e51 183 AnalogIn y_axis(MICROBIT_PIN_P2);
xx316 1:032b96b05e51 184 AnalogIn potentiometer(MICROBIT_PIN_P0);
xx316 1:032b96b05e51 185
xx316 1:032b96b05e51 186 void JoystickControl(){
xx316 1:032b96b05e51 187 float x,y;
xx316 1:032b96b05e51 188
xx316 1:032b96b05e51 189 x = x_axis.read();
xx316 1:032b96b05e51 190 if (x < 0.05f) send_left();
xx316 1:032b96b05e51 191 if (x > 0.95f) send_right();
xx316 1:032b96b05e51 192
xx316 1:032b96b05e51 193 y = y_axis.read();
xx316 1:032b96b05e51 194 if (y < 0.05f) send_down();
xx316 1:032b96b05e51 195 if (y > 0.95f) send_up();
xx316 1:032b96b05e51 196
xx316 1:032b96b05e51 197 wait(0.05);
xx316 1:032b96b05e51 198 }
xx316 1:032b96b05e51 199
xx316 1:032b96b05e51 200 void crossy_road_touch(){
xx316 1:032b96b05e51 201 if (touch1 == 1) send_space();
xx316 1:032b96b05e51 202 wait(0.05);
xx316 1:032b96b05e51 203 }
xx316 1:032b96b05e51 204
xx316 1:032b96b05e51 205 enum mode{empty,arithmetic, alphebet, number};
xx316 1:032b96b05e51 206
xx316 1:032b96b05e51 207 void potentiometer_control(){
xx316 1:032b96b05e51 208 mode current_mode = empty;
xx316 1:032b96b05e51 209 float value = potentiometer.read();
xx316 1:032b96b05e51 210
xx316 1:032b96b05e51 211 if (value < (float) 229/1024 ) {current_mode = empty;}
xx316 1:032b96b05e51 212 else if (value < (float) 307/1024) { current_mode = arithmetic;}
xx316 1:032b96b05e51 213 else if (value < (float) 522/1024) { current_mode = number;}
xx316 1:032b96b05e51 214 else {current_mode = alphebet;}
xx316 1:032b96b05e51 215
xx316 1:032b96b05e51 216 // need to update mode, depending on value
xx316 1:032b96b05e51 217
xx316 1:032b96b05e51 218 switch(current_mode){
xx316 1:032b96b05e51 219 case empty:
xx316 1:032b96b05e51 220 if (touch1 == 1) send_space();
xx316 1:032b96b05e51 221 if (touch2 == 1) send_delete();
xx316 1:032b96b05e51 222 if (touch3 == 1) send_tab();
xx316 1:032b96b05e51 223 if (touch4 == 1) send_return();
xx316 1:032b96b05e51 224 wait(0.05);
xx316 1:032b96b05e51 225 break;
xx316 1:032b96b05e51 226 case arithmetic:
xx316 1:032b96b05e51 227 if (touch1 == 1) {send_string("+");wait(0.1);}
xx316 1:032b96b05e51 228 if (touch2 == 1) {send_string("-");wait(0.1);}
xx316 1:032b96b05e51 229 if (touch3 == 1) {send_string("*");wait(0.1);}
xx316 1:032b96b05e51 230 if (touch4 == 1) {send_string("/");wait(0.1);}
xx316 1:032b96b05e51 231 wait(0.05);
xx316 1:032b96b05e51 232 break;
xx316 1:032b96b05e51 233 case alphebet:
xx316 1:032b96b05e51 234 if (touch1 == 1) {send_string("A");wait(0.1);}
xx316 1:032b96b05e51 235 if (touch2 == 1) {send_string("B");wait(0.1);}
xx316 1:032b96b05e51 236 if (touch3 == 1) {send_string("C");wait(0.1);}
xx316 1:032b96b05e51 237 if (touch4 == 1) {send_string("D");wait(0.1);}
xx316 1:032b96b05e51 238 wait(0.05);
xx316 1:032b96b05e51 239 break;
xx316 1:032b96b05e51 240 case number:
xx316 1:032b96b05e51 241 if (touch1 == 1) {send_string("1");wait(0.1);}
xx316 1:032b96b05e51 242 if (touch2 == 1) {send_string("2");wait(0.1);}
xx316 1:032b96b05e51 243 if (touch3 == 1) {send_string("3");wait(0.1);}
xx316 1:032b96b05e51 244 if (touch4 == 1) {send_string("4");wait(0.1);}
xx316 1:032b96b05e51 245 wait(0.05);
xx316 1:032b96b05e51 246 break;
xx316 1:032b96b05e51 247 }
xx316 1:032b96b05e51 248 }
xx316 1:032b96b05e51 249
xx316 1:032b96b05e51 250 void wire_loop_control(){
xx316 1:032b96b05e51 251 // if (touch0 == 1) {send_string("w");wait(0.1);} // touch, need to stop timer as well
xx316 1:032b96b05e51 252 // ???somehow pin8 is active high, need to pull down????
xx316 1:032b96b05e51 253 if (touch1 == 1) {send_string("x");wait(0.1);} // start timer
xx316 1:032b96b05e51 254 if (touch2 == 1) {send_string("y");wait(0.1);} // stop timer
xx316 1:032b96b05e51 255 if (touch3 == 1) {send_string("z");wait(0.1);} // reset timer
xx316 1:032b96b05e51 256 // if (touch4 == 1) {send_string("w");wait(0.1);}
xx316 1:032b96b05e51 257 /// try to use interrup
xx316 1:032b96b05e51 258 }
xx316 1:032b96b05e51 259
xx316 1:032b96b05e51 260 void touched(){send_string("w");wait(0.1);}
xx316 1:032b96b05e51 261
xx316 1:032b96b05e51 262 int main(){
xx316 1:032b96b05e51 263 //create_fiber(JoystickControl);
xx316 1:032b96b05e51 264 // create_fiber(BLE_fiber);
xx316 1:032b96b05e51 265 Ticker heartbeat;
xx316 1:032b96b05e51 266
xx316 1:032b96b05e51 267 touch0.fall(touched);
xx316 1:032b96b05e51 268 //button1.rise(send_one);
xx316 1:032b96b05e51 269 // button2.rise(send_zero);
xx316 1:032b96b05e51 270 button1.rise(send_stuff);
xx316 1:032b96b05e51 271 button2.rise(send_more_stuff);
xx316 1:032b96b05e51 272
xx316 1:032b96b05e51 273 HID_DEBUG("initialising ticker\r\n");
xx316 1:032b96b05e51 274
xx316 1:032b96b05e51 275 heartbeat.attach(waiting, 1);
xx316 1:032b96b05e51 276
xx316 1:032b96b05e51 277 HID_DEBUG("initialising ble\r\n");
xx316 1:032b96b05e51 278 ble.init();
xx316 1:032b96b05e51 279
xx316 1:032b96b05e51 280 ble.gap().onDisconnection(onDisconnect);
xx316 1:032b96b05e51 281 ble.gap().onConnection(onConnect);
xx316 1:032b96b05e51 282
xx316 1:032b96b05e51 283 initializeSecurity(ble);
xx316 1:032b96b05e51 284
xx316 1:032b96b05e51 285 HID_DEBUG("adding hid service\r\n");
xx316 1:032b96b05e51 286 KeyboardService kbdService(ble);
xx316 1:032b96b05e51 287 kbdServicePtr = &kbdService;
xx316 1:032b96b05e51 288
xx316 1:032b96b05e51 289 HID_DEBUG("adding device info and battery service\r\n");
xx316 1:032b96b05e51 290 initializeHOGP(ble);
xx316 1:032b96b05e51 291
xx316 1:032b96b05e51 292 HID_DEBUG("setting up gap\r\n");
xx316 1:032b96b05e51 293 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::KEYBOARD);
xx316 1:032b96b05e51 294 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,
xx316 1:032b96b05e51 295 (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
xx316 1:032b96b05e51 296 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
xx316 1:032b96b05e51 297 (uint8_t *)SHORT_DEVICE_NAME, sizeof(SHORT_DEVICE_NAME));
xx316 1:032b96b05e51 298 ble.gap().setDeviceName((const uint8_t *)DEVICE_NAME);
xx316 1:032b96b05e51 299
xx316 1:032b96b05e51 300 HID_DEBUG("advertising\r\n");
xx316 1:032b96b05e51 301 ble.gap().startAdvertising();
xx316 1:032b96b05e51 302
xx316 1:032b96b05e51 303 while (true) {
xx316 1:032b96b05e51 304 ble.waitForEvent();
xx316 1:032b96b05e51 305
xx316 1:032b96b05e51 306 //JoystickControl();
xx316 1:032b96b05e51 307
xx316 1:032b96b05e51 308 // normally, only realize one example: crossy road, potentiometer, or wire loop
xx316 1:032b96b05e51 309 // crossy_road_touch();
xx316 1:032b96b05e51 310 // potentiometer_control();
xx316 1:032b96b05e51 311 wire_loop_control();
xx316 1:032b96b05e51 312 }
xx316 1:032b96b05e51 313 // release_fiber();
xx316 1:032b96b05e51 314 }
xx316 1:032b96b05e51 315
xx316 1:032b96b05e51 316