wei simin / Mbed 2 deprecated BLE_GATT_GAMEPAD

Dependencies:   mbed BLE_API nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GamepadService.h Source File

GamepadService.h

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 "HIDServiceBase.h"
00021 
00022 enum ButtonState
00023 {
00024     BUTTON_UP,
00025     BUTTON_DOWN
00026 };
00027 
00028 enum GamepadButton
00029 {
00030     GAMEPAD_BUTTON_A       = 0x1,
00031     GAMEPAD_BUTTON_B       = 0x2,
00032     GAMEPAD_BUTTON_C       = 0x4,
00033     GAMEPAD_BUTTON_D       = 0x8,
00034     GAMEPAD_BUTTON_E       = 0x10,
00035     GAMEPAD_BUTTON_F       = 0x20,
00036     GAMEPAD_BUTTON_G       = 0x40,
00037     GAMEPAD_BUTTON_H       = 0x80,
00038     
00039     GAMEPAD_BUTTON_AXIS_Xp       = 0x01,
00040     GAMEPAD_BUTTON_AXIS_Xn       = 0x03,
00041     GAMEPAD_BUTTON_AXIS_Yp       = 0x04,
00042     GAMEPAD_BUTTON_AXIS_Yn       = 0x0C,
00043     
00044 };
00045 
00046 report_map_t GAMEPAD_REPORT_MAP = {
00047     USAGE_PAGE(1),      0x01,         // Generic Desktop
00048     USAGE(1),           0x05,         // Gamepad
00049     COLLECTION(1),      0x01,         // Application
00050     USAGE(1),           0x01,         // Pointer
00051     COLLECTION(1),      0x00,         // Physical
00052     USAGE(1),           0x30,          // X Axis
00053     USAGE(1),           0x31,         // Y Axis
00054     LOGICAL_MINIMUM(1), 0xFF,         // -1
00055     LOGICAL_MAXIMUM(1), 0x01,         // 1
00056     REPORT_COUNT(1),    0x02,         // 2 Fields, X-Y
00057     REPORT_SIZE(1),     0x02,         // 2 bits per Axis
00058     INPUT(1),           0x02,         // Data, Variable, Absolute
00059     END_COLLECTION(0),
00060     
00061     REPORT_COUNT(1),    0x04,         // 4 Bit Padding
00062     REPORT_SIZE(1),     0x01,         // 4 Bit Padding
00063     INPUT(1),           0x03,         // Constant, Variable, Absolute
00064     
00065     USAGE_PAGE(1),      0x09,         //   Buttons
00066     USAGE_MINIMUM(1),   0x01,
00067     USAGE_MAXIMUM(1),   0x08,
00068     LOGICAL_MINIMUM(1), 0x00,
00069     LOGICAL_MAXIMUM(1), 0x01,
00070     REPORT_COUNT(1),    0x08,        // 2 bits (Buttons)
00071     REPORT_SIZE(1),     0x01,
00072     INPUT(1),           0x02,         // Data, Variable, Absolute
00073     
00074 //    REPORT_COUNT(1),    0x02,         // 2 Bit Padding
00075 //    REPORT_SIZE(1),     0x01,         // 2 Bit Padding
00076 //    INPUT(1),           0x03,         // Constant, Variable, Absolute
00077     END_COLLECTION(0),
00078 };
00079 
00080 uint8_t report[] = { 0, 0 };
00081 
00082 class GamepadService: public HIDServiceBase
00083 {
00084 public:
00085     GamepadService(BLE &_ble) :
00086         HIDServiceBase(_ble,
00087                        GAMEPAD_REPORT_MAP, sizeof(GAMEPAD_REPORT_MAP),
00088                        inputReport          = report,
00089                        outputReport         = NULL,
00090                        featureReport        = NULL,
00091                        inputReportLength    = sizeof(inputReport),
00092                        outputReportLength   = 0,
00093                        featureReportLength  = 0,
00094                        reportTickerDelay    = 6),
00095         buttonsState (0),
00096         axisState (0),
00097         failedReports (0)
00098     {
00099 
00100         startReportTicker();
00101     }
00102 
00103     int setXAxis(GamepadButton button, ButtonState state)
00104     {
00105         if (state == BUTTON_DOWN)
00106             XaxisState = button;
00107         else
00108         XaxisState = 0x00;
00109 
00110         return 0;
00111     }
00112     
00113     int setYAxis(GamepadButton button, ButtonState state)
00114     {
00115         if (state == BUTTON_DOWN)
00116             YaxisState = button;
00117         else
00118         YaxisState = 0x00;
00119 
00120         return 0;
00121     }
00122 
00123     int setButton(GamepadButton button, ButtonState state)
00124     {
00125         if (state == BUTTON_UP)
00126             buttonsState &= ~(button);
00127         else
00128             buttonsState |= button;
00129 
00130         return 0;
00131     }
00132     
00133 
00134     virtual void sendCallback(void) {
00135         if (!connected)
00136             return;
00137 
00138         report[1] = buttonsState;
00139         report[0] = YaxisState | XaxisState;
00140 
00141 
00142         if (send(report))
00143             failedReports++;
00144     }
00145 
00146 protected:
00147     uint8_t buttonsState;
00148     uint8_t axisState;
00149     uint8_t XaxisState;
00150     uint8_t YaxisState;
00151     
00152 
00153 public:
00154     uint32_t failedReports;
00155 };