Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed BLE_API nRF51822
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2015 ARM Limited 00003 * Modifications copyright (C) 2017 Ozan ALTINKAYA 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 #include "mbed.h" 00019 00020 #include "ble/BLE.h" 00021 #include "KeyboardService.h" 00022 #include "GamepadService.h" 00023 #include "examples_common.h" 00024 00025 /** 00026 * This program implements a complete HID-over-Gatt Profile: 00027 * - HID is provided by KeyboardService 00028 * - Battery Service 00029 * - Device Information Service 00030 * 00031 * Complete strings can be sent over BLE using printf. Please note, however, than a 12char string 00032 * will take about 500ms to transmit, principally because of the limited notification rate in BLE. 00033 * KeyboardService uses a circular buffer to store the strings to send, and calls to putc will fail 00034 * once this buffer is full. This will result in partial strings being sent to the client. 00035 */ 00036 00037 DigitalOut waiting_led(P0_2); 00038 DigitalOut connected_led(P0_19); 00039 00040 DigitalIn axisXp(P0_19); 00041 DigitalIn axisXn(P0_20); 00042 DigitalIn axisYp(P0_18); 00043 DigitalIn axisYn(P0_17); 00044 00045 DigitalIn buttonA(P0_22); 00046 DigitalIn buttonB(P0_24); 00047 DigitalIn buttonC(P0_5); 00048 DigitalIn buttonD(P0_28); 00049 DigitalIn buttonE(P0_29); 00050 DigitalIn buttonF(P0_13); 00051 DigitalIn buttonG(P0_25); 00052 DigitalIn buttonH(P0_23); 00053 00054 //Serial pc(USBTX, USBRX); // tx, rx 00055 00056 BLE ble; 00057 GamepadService *kbdServicePtr; 00058 00059 00060 static const char DEVICE_NAME[] = "ArcadeGamepad"; 00061 static const char SHORT_DEVICE_NAME[] = "gpd1"; 00062 00063 static void onDisconnect(const Gap::DisconnectionCallbackParams_t *params) 00064 { 00065 // pc.printf("disconnected\r\n"); 00066 connected_led = 0; 00067 00068 ble.gap().startAdvertising(); // restart advertising 00069 } 00070 00071 static void onConnect(const Gap::ConnectionCallbackParams_t *params) 00072 { 00073 // pc.printf("connected\r\n"); 00074 waiting_led = false; 00075 } 00076 00077 static void waiting() { 00078 if (!kbdServicePtr->isConnected()) 00079 waiting_led = !waiting_led; 00080 else 00081 waiting_led = 1; 00082 } 00083 00084 00085 void update(){ 00086 if(buttonA){ 00087 kbdServicePtr->setButton(GAMEPAD_BUTTON_A, BUTTON_DOWN); 00088 } 00089 00090 else{ 00091 kbdServicePtr->setButton(GAMEPAD_BUTTON_A, BUTTON_UP); 00092 } 00093 00094 if(buttonB){ 00095 kbdServicePtr->setButton(GAMEPAD_BUTTON_B, BUTTON_DOWN); 00096 } 00097 00098 else{ 00099 kbdServicePtr->setButton(GAMEPAD_BUTTON_B, BUTTON_UP); 00100 } 00101 00102 if(buttonC){ 00103 kbdServicePtr->setButton(GAMEPAD_BUTTON_C, BUTTON_DOWN); 00104 } 00105 00106 else{ 00107 kbdServicePtr->setButton(GAMEPAD_BUTTON_C, BUTTON_UP); 00108 } 00109 00110 if(buttonD){ 00111 kbdServicePtr->setButton(GAMEPAD_BUTTON_D, BUTTON_DOWN); 00112 } 00113 00114 else{ 00115 kbdServicePtr->setButton(GAMEPAD_BUTTON_D, BUTTON_UP); 00116 } 00117 00118 if(buttonE){ 00119 kbdServicePtr->setButton(GAMEPAD_BUTTON_E, BUTTON_DOWN); 00120 } 00121 00122 else{ 00123 kbdServicePtr->setButton(GAMEPAD_BUTTON_E, BUTTON_UP); 00124 } 00125 00126 if(buttonF){ 00127 kbdServicePtr->setButton(GAMEPAD_BUTTON_F, BUTTON_DOWN); 00128 } 00129 00130 else{ 00131 kbdServicePtr->setButton(GAMEPAD_BUTTON_F, BUTTON_UP); 00132 } 00133 00134 if(buttonG){ 00135 kbdServicePtr->setButton(GAMEPAD_BUTTON_G, BUTTON_DOWN); 00136 } 00137 00138 else{ 00139 kbdServicePtr->setButton(GAMEPAD_BUTTON_G, BUTTON_UP); 00140 } 00141 00142 if(buttonH){ 00143 kbdServicePtr->setButton(GAMEPAD_BUTTON_H, BUTTON_DOWN); 00144 } 00145 00146 else{ 00147 kbdServicePtr->setButton(GAMEPAD_BUTTON_H, BUTTON_UP); 00148 } 00149 00150 00151 00152 00153 // AXIS CHECK 00154 00155 00156 00157 00158 if((axisXp) || (axisXn)){ 00159 if(axisXp) 00160 kbdServicePtr->setXAxis(GAMEPAD_BUTTON_AXIS_Xp, BUTTON_DOWN); 00161 if(axisXn) 00162 kbdServicePtr->setXAxis(GAMEPAD_BUTTON_AXIS_Xn, BUTTON_DOWN); 00163 } 00164 00165 else{ 00166 kbdServicePtr->setXAxis(GAMEPAD_BUTTON_AXIS_Xp, BUTTON_UP); 00167 } 00168 00169 if((axisYp)||(axisYn)){ 00170 if(axisYp) 00171 kbdServicePtr->setYAxis(GAMEPAD_BUTTON_AXIS_Yp, BUTTON_DOWN); 00172 if(axisYn) 00173 kbdServicePtr->setYAxis(GAMEPAD_BUTTON_AXIS_Yn, BUTTON_DOWN); 00174 } 00175 00176 else{ 00177 kbdServicePtr->setYAxis(GAMEPAD_BUTTON_AXIS_Yp, BUTTON_UP); 00178 } 00179 } 00180 00181 00182 int main() 00183 { 00184 Ticker heartbeat; 00185 Ticker updater; 00186 00187 // pc.printf("initialising ticker\r\n"); 00188 00189 heartbeat.attach(waiting, 1); 00190 updater.attach(update, 0.006); 00191 00192 // pc.printf("initialising ble\r\n"); 00193 ble.init(); 00194 00195 ble.gap().onDisconnection(onDisconnect); 00196 ble.gap().onConnection(onConnect); 00197 00198 initializeSecurity(ble); 00199 00200 // pc.printf("adding hid service\r\n"); 00201 GamepadService kbdService(ble); 00202 kbdServicePtr = &kbdService; 00203 00204 // pc.printf("adding device info and battery service\r\n"); 00205 initializeHOGP(ble); 00206 00207 // pc.printf("setting up gap\r\n"); 00208 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GAMEPAD); 00209 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, 00210 (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00211 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, 00212 (const uint8_t *)SHORT_DEVICE_NAME, sizeof(SHORT_DEVICE_NAME)); 00213 00214 ble.gap().setDeviceName((const uint8_t *)DEVICE_NAME); 00215 00216 // pc.printf("advertising\r\n"); 00217 ble.gap().startAdvertising(); 00218 00219 while (true) { 00220 ble.waitForEvent(); 00221 } 00222 }
Generated on Sun Jul 17 2022 12:11:34 by
1.7.2