wei simin / Mbed 2 deprecated BLE_GATT_GAMEPAD

Dependencies:   mbed BLE_API nRF51822

Committer:
weisimin
Date:
Tue Jul 20 09:23:54 2021 +0000
Revision:
0:706cf6936c11
BLE GATT GAMEPAD NO TEST

Who changed what in which revision?

UserRevisionLine numberNew contents of line
weisimin 0:706cf6936c11 1 /* mbed Microcontroller Library
weisimin 0:706cf6936c11 2 * Copyright (c) 2015 ARM Limited
weisimin 0:706cf6936c11 3 * Modifications copyright (C) 2017 Ozan ALTINKAYA
weisimin 0:706cf6936c11 4 *
weisimin 0:706cf6936c11 5 * Licensed under the Apache License, Version 2.0 (the "License");
weisimin 0:706cf6936c11 6 * you may not use this file except in compliance with the License.
weisimin 0:706cf6936c11 7 * You may obtain a copy of the License at
weisimin 0:706cf6936c11 8 *
weisimin 0:706cf6936c11 9 * http://www.apache.org/licenses/LICENSE-2.0
weisimin 0:706cf6936c11 10 *
weisimin 0:706cf6936c11 11 * Unless required by applicable law or agreed to in writing, software
weisimin 0:706cf6936c11 12 * distributed under the License is distributed on an "AS IS" BASIS,
weisimin 0:706cf6936c11 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
weisimin 0:706cf6936c11 14 * See the License for the specific language governing permissions and
weisimin 0:706cf6936c11 15 * limitations under the License.
weisimin 0:706cf6936c11 16 */
weisimin 0:706cf6936c11 17
weisimin 0:706cf6936c11 18 #include "mbed.h"
weisimin 0:706cf6936c11 19
weisimin 0:706cf6936c11 20 #include "ble/BLE.h"
weisimin 0:706cf6936c11 21 #include "KeyboardService.h"
weisimin 0:706cf6936c11 22 #include "GamepadService.h"
weisimin 0:706cf6936c11 23 #include "examples_common.h"
weisimin 0:706cf6936c11 24
weisimin 0:706cf6936c11 25 /**
weisimin 0:706cf6936c11 26 * This program implements a complete HID-over-Gatt Profile:
weisimin 0:706cf6936c11 27 * - HID is provided by KeyboardService
weisimin 0:706cf6936c11 28 * - Battery Service
weisimin 0:706cf6936c11 29 * - Device Information Service
weisimin 0:706cf6936c11 30 *
weisimin 0:706cf6936c11 31 * Complete strings can be sent over BLE using printf. Please note, however, than a 12char string
weisimin 0:706cf6936c11 32 * will take about 500ms to transmit, principally because of the limited notification rate in BLE.
weisimin 0:706cf6936c11 33 * KeyboardService uses a circular buffer to store the strings to send, and calls to putc will fail
weisimin 0:706cf6936c11 34 * once this buffer is full. This will result in partial strings being sent to the client.
weisimin 0:706cf6936c11 35 */
weisimin 0:706cf6936c11 36
weisimin 0:706cf6936c11 37 DigitalOut waiting_led(P0_2);
weisimin 0:706cf6936c11 38 DigitalOut connected_led(P0_19);
weisimin 0:706cf6936c11 39
weisimin 0:706cf6936c11 40 DigitalIn axisXp(P0_19);
weisimin 0:706cf6936c11 41 DigitalIn axisXn(P0_20);
weisimin 0:706cf6936c11 42 DigitalIn axisYp(P0_18);
weisimin 0:706cf6936c11 43 DigitalIn axisYn(P0_17);
weisimin 0:706cf6936c11 44
weisimin 0:706cf6936c11 45 DigitalIn buttonA(P0_22);
weisimin 0:706cf6936c11 46 DigitalIn buttonB(P0_24);
weisimin 0:706cf6936c11 47 DigitalIn buttonC(P0_5);
weisimin 0:706cf6936c11 48 DigitalIn buttonD(P0_28);
weisimin 0:706cf6936c11 49 DigitalIn buttonE(P0_29);
weisimin 0:706cf6936c11 50 DigitalIn buttonF(P0_13);
weisimin 0:706cf6936c11 51 DigitalIn buttonG(P0_25);
weisimin 0:706cf6936c11 52 DigitalIn buttonH(P0_23);
weisimin 0:706cf6936c11 53
weisimin 0:706cf6936c11 54 //Serial pc(USBTX, USBRX); // tx, rx
weisimin 0:706cf6936c11 55
weisimin 0:706cf6936c11 56 BLE ble;
weisimin 0:706cf6936c11 57 GamepadService *kbdServicePtr;
weisimin 0:706cf6936c11 58
weisimin 0:706cf6936c11 59
weisimin 0:706cf6936c11 60 static const char DEVICE_NAME[] = "ArcadeGamepad";
weisimin 0:706cf6936c11 61 static const char SHORT_DEVICE_NAME[] = "gpd1";
weisimin 0:706cf6936c11 62
weisimin 0:706cf6936c11 63 static void onDisconnect(const Gap::DisconnectionCallbackParams_t *params)
weisimin 0:706cf6936c11 64 {
weisimin 0:706cf6936c11 65 // pc.printf("disconnected\r\n");
weisimin 0:706cf6936c11 66 connected_led = 0;
weisimin 0:706cf6936c11 67
weisimin 0:706cf6936c11 68 ble.gap().startAdvertising(); // restart advertising
weisimin 0:706cf6936c11 69 }
weisimin 0:706cf6936c11 70
weisimin 0:706cf6936c11 71 static void onConnect(const Gap::ConnectionCallbackParams_t *params)
weisimin 0:706cf6936c11 72 {
weisimin 0:706cf6936c11 73 // pc.printf("connected\r\n");
weisimin 0:706cf6936c11 74 waiting_led = false;
weisimin 0:706cf6936c11 75 }
weisimin 0:706cf6936c11 76
weisimin 0:706cf6936c11 77 static void waiting() {
weisimin 0:706cf6936c11 78 if (!kbdServicePtr->isConnected())
weisimin 0:706cf6936c11 79 waiting_led = !waiting_led;
weisimin 0:706cf6936c11 80 else
weisimin 0:706cf6936c11 81 waiting_led = 1;
weisimin 0:706cf6936c11 82 }
weisimin 0:706cf6936c11 83
weisimin 0:706cf6936c11 84
weisimin 0:706cf6936c11 85 void update(){
weisimin 0:706cf6936c11 86 if(buttonA){
weisimin 0:706cf6936c11 87 kbdServicePtr->setButton(GAMEPAD_BUTTON_A, BUTTON_DOWN);
weisimin 0:706cf6936c11 88 }
weisimin 0:706cf6936c11 89
weisimin 0:706cf6936c11 90 else{
weisimin 0:706cf6936c11 91 kbdServicePtr->setButton(GAMEPAD_BUTTON_A, BUTTON_UP);
weisimin 0:706cf6936c11 92 }
weisimin 0:706cf6936c11 93
weisimin 0:706cf6936c11 94 if(buttonB){
weisimin 0:706cf6936c11 95 kbdServicePtr->setButton(GAMEPAD_BUTTON_B, BUTTON_DOWN);
weisimin 0:706cf6936c11 96 }
weisimin 0:706cf6936c11 97
weisimin 0:706cf6936c11 98 else{
weisimin 0:706cf6936c11 99 kbdServicePtr->setButton(GAMEPAD_BUTTON_B, BUTTON_UP);
weisimin 0:706cf6936c11 100 }
weisimin 0:706cf6936c11 101
weisimin 0:706cf6936c11 102 if(buttonC){
weisimin 0:706cf6936c11 103 kbdServicePtr->setButton(GAMEPAD_BUTTON_C, BUTTON_DOWN);
weisimin 0:706cf6936c11 104 }
weisimin 0:706cf6936c11 105
weisimin 0:706cf6936c11 106 else{
weisimin 0:706cf6936c11 107 kbdServicePtr->setButton(GAMEPAD_BUTTON_C, BUTTON_UP);
weisimin 0:706cf6936c11 108 }
weisimin 0:706cf6936c11 109
weisimin 0:706cf6936c11 110 if(buttonD){
weisimin 0:706cf6936c11 111 kbdServicePtr->setButton(GAMEPAD_BUTTON_D, BUTTON_DOWN);
weisimin 0:706cf6936c11 112 }
weisimin 0:706cf6936c11 113
weisimin 0:706cf6936c11 114 else{
weisimin 0:706cf6936c11 115 kbdServicePtr->setButton(GAMEPAD_BUTTON_D, BUTTON_UP);
weisimin 0:706cf6936c11 116 }
weisimin 0:706cf6936c11 117
weisimin 0:706cf6936c11 118 if(buttonE){
weisimin 0:706cf6936c11 119 kbdServicePtr->setButton(GAMEPAD_BUTTON_E, BUTTON_DOWN);
weisimin 0:706cf6936c11 120 }
weisimin 0:706cf6936c11 121
weisimin 0:706cf6936c11 122 else{
weisimin 0:706cf6936c11 123 kbdServicePtr->setButton(GAMEPAD_BUTTON_E, BUTTON_UP);
weisimin 0:706cf6936c11 124 }
weisimin 0:706cf6936c11 125
weisimin 0:706cf6936c11 126 if(buttonF){
weisimin 0:706cf6936c11 127 kbdServicePtr->setButton(GAMEPAD_BUTTON_F, BUTTON_DOWN);
weisimin 0:706cf6936c11 128 }
weisimin 0:706cf6936c11 129
weisimin 0:706cf6936c11 130 else{
weisimin 0:706cf6936c11 131 kbdServicePtr->setButton(GAMEPAD_BUTTON_F, BUTTON_UP);
weisimin 0:706cf6936c11 132 }
weisimin 0:706cf6936c11 133
weisimin 0:706cf6936c11 134 if(buttonG){
weisimin 0:706cf6936c11 135 kbdServicePtr->setButton(GAMEPAD_BUTTON_G, BUTTON_DOWN);
weisimin 0:706cf6936c11 136 }
weisimin 0:706cf6936c11 137
weisimin 0:706cf6936c11 138 else{
weisimin 0:706cf6936c11 139 kbdServicePtr->setButton(GAMEPAD_BUTTON_G, BUTTON_UP);
weisimin 0:706cf6936c11 140 }
weisimin 0:706cf6936c11 141
weisimin 0:706cf6936c11 142 if(buttonH){
weisimin 0:706cf6936c11 143 kbdServicePtr->setButton(GAMEPAD_BUTTON_H, BUTTON_DOWN);
weisimin 0:706cf6936c11 144 }
weisimin 0:706cf6936c11 145
weisimin 0:706cf6936c11 146 else{
weisimin 0:706cf6936c11 147 kbdServicePtr->setButton(GAMEPAD_BUTTON_H, BUTTON_UP);
weisimin 0:706cf6936c11 148 }
weisimin 0:706cf6936c11 149
weisimin 0:706cf6936c11 150
weisimin 0:706cf6936c11 151
weisimin 0:706cf6936c11 152
weisimin 0:706cf6936c11 153 // AXIS CHECK
weisimin 0:706cf6936c11 154
weisimin 0:706cf6936c11 155
weisimin 0:706cf6936c11 156
weisimin 0:706cf6936c11 157
weisimin 0:706cf6936c11 158 if((axisXp) || (axisXn)){
weisimin 0:706cf6936c11 159 if(axisXp)
weisimin 0:706cf6936c11 160 kbdServicePtr->setXAxis(GAMEPAD_BUTTON_AXIS_Xp, BUTTON_DOWN);
weisimin 0:706cf6936c11 161 if(axisXn)
weisimin 0:706cf6936c11 162 kbdServicePtr->setXAxis(GAMEPAD_BUTTON_AXIS_Xn, BUTTON_DOWN);
weisimin 0:706cf6936c11 163 }
weisimin 0:706cf6936c11 164
weisimin 0:706cf6936c11 165 else{
weisimin 0:706cf6936c11 166 kbdServicePtr->setXAxis(GAMEPAD_BUTTON_AXIS_Xp, BUTTON_UP);
weisimin 0:706cf6936c11 167 }
weisimin 0:706cf6936c11 168
weisimin 0:706cf6936c11 169 if((axisYp)||(axisYn)){
weisimin 0:706cf6936c11 170 if(axisYp)
weisimin 0:706cf6936c11 171 kbdServicePtr->setYAxis(GAMEPAD_BUTTON_AXIS_Yp, BUTTON_DOWN);
weisimin 0:706cf6936c11 172 if(axisYn)
weisimin 0:706cf6936c11 173 kbdServicePtr->setYAxis(GAMEPAD_BUTTON_AXIS_Yn, BUTTON_DOWN);
weisimin 0:706cf6936c11 174 }
weisimin 0:706cf6936c11 175
weisimin 0:706cf6936c11 176 else{
weisimin 0:706cf6936c11 177 kbdServicePtr->setYAxis(GAMEPAD_BUTTON_AXIS_Yp, BUTTON_UP);
weisimin 0:706cf6936c11 178 }
weisimin 0:706cf6936c11 179 }
weisimin 0:706cf6936c11 180
weisimin 0:706cf6936c11 181
weisimin 0:706cf6936c11 182 int main()
weisimin 0:706cf6936c11 183 {
weisimin 0:706cf6936c11 184 Ticker heartbeat;
weisimin 0:706cf6936c11 185 Ticker updater;
weisimin 0:706cf6936c11 186
weisimin 0:706cf6936c11 187 // pc.printf("initialising ticker\r\n");
weisimin 0:706cf6936c11 188
weisimin 0:706cf6936c11 189 heartbeat.attach(waiting, 1);
weisimin 0:706cf6936c11 190 updater.attach(update, 0.006);
weisimin 0:706cf6936c11 191
weisimin 0:706cf6936c11 192 // pc.printf("initialising ble\r\n");
weisimin 0:706cf6936c11 193 ble.init();
weisimin 0:706cf6936c11 194
weisimin 0:706cf6936c11 195 ble.gap().onDisconnection(onDisconnect);
weisimin 0:706cf6936c11 196 ble.gap().onConnection(onConnect);
weisimin 0:706cf6936c11 197
weisimin 0:706cf6936c11 198 initializeSecurity(ble);
weisimin 0:706cf6936c11 199
weisimin 0:706cf6936c11 200 // pc.printf("adding hid service\r\n");
weisimin 0:706cf6936c11 201 GamepadService kbdService(ble);
weisimin 0:706cf6936c11 202 kbdServicePtr = &kbdService;
weisimin 0:706cf6936c11 203
weisimin 0:706cf6936c11 204 // pc.printf("adding device info and battery service\r\n");
weisimin 0:706cf6936c11 205 initializeHOGP(ble);
weisimin 0:706cf6936c11 206
weisimin 0:706cf6936c11 207 // pc.printf("setting up gap\r\n");
weisimin 0:706cf6936c11 208 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GAMEPAD);
weisimin 0:706cf6936c11 209 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,
weisimin 0:706cf6936c11 210 (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
weisimin 0:706cf6936c11 211 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
weisimin 0:706cf6936c11 212 (const uint8_t *)SHORT_DEVICE_NAME, sizeof(SHORT_DEVICE_NAME));
weisimin 0:706cf6936c11 213
weisimin 0:706cf6936c11 214 ble.gap().setDeviceName((const uint8_t *)DEVICE_NAME);
weisimin 0:706cf6936c11 215
weisimin 0:706cf6936c11 216 // pc.printf("advertising\r\n");
weisimin 0:706cf6936c11 217 ble.gap().startAdvertising();
weisimin 0:706cf6936c11 218
weisimin 0:706cf6936c11 219 while (true) {
weisimin 0:706cf6936c11 220 ble.waitForEvent();
weisimin 0:706cf6936c11 221 }
weisimin 0:706cf6936c11 222 }