A BLE HID controller implementation with communication over SPI

Dependencies:   BLE_API BLE_HID mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ControllerService.h Source File

ControllerService.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 
00019 #include "HIDServiceBase.h"
00020 
00021 /**
00022  * Report descriptor for a standard 3 buttons + wheel mouse with relative X/Y
00023  * moves
00024  */
00025 report_map_t CONTROLLER_REPORT_MAP = {
00026         USAGE_PAGE(1),      0x01,       // Genric Desktop
00027         USAGE(1),           0x05,       // Gamepad
00028         COLLECTION(1),      0x01,       // Application
00029         COLLECTION(1),      0x00,       // Physical
00030         
00031         REPORT_ID(1),       0x03,       // Report ID 3
00032         USAGE_PAGE(1),      0x09,       // Buttons
00033         USAGE_MINIMUM(1),       0x01,   // Minimum of 1
00034         USAGE_MAXIMUM(1),       0x10,   // Maximum of 16
00035         LOGICAL_MINIMUM(1),     0x00,   // Logical Minimum of 0
00036         LOGICAL_MAXIMUM(1),     0x01,   // Logical Maximum of 1
00037         REPORT_SIZE(1),     0x01,       // Each report is 1 bit
00038         REPORT_COUNT(1),    0x10,       // All in all 16 * 1 bit
00039         INPUT(1),           0x02,       // Input (Data, Var, Abs)
00040         
00041         USAGE_PAGE(1),      0x01,       // Generic Desktop
00042         USAGE(1),           0x30,       // Usage X
00043         USAGE(1),           0x31,       // Usage Y
00044         USAGE(1),           0x32,       // Usage Z
00045         USAGE(1),           0x33,       // Usage Rx
00046         LOGICAL_MINIMUM(1), 0x00,       // Logical Minimum: 0
00047         LOGICAL_MAXIMUM(1), 0xFE,       // Logical maximum: 254
00048         REPORT_SIZE(1),     0x08,       // Each report is 8 bits
00049         REPORT_COUNT(1),    0x04,       // 4 reports * 8 bits
00050         INPUT(1),           0x02,        // Input (Data, Var, Abs)
00051         END_COLLECTION(0),
00052         END_COLLECTION(0)
00053 };
00054 
00055 uint8_t report[7] = {0x03, 0, 0, 0, 0, 0, 0};
00056 
00057 //bool toggle = true;
00058 //uint8_t onReport[7] = {0x03, 0xFF, 0xFF, 0x80, 0x80, 0x80, 0x80};
00059 //uint8_t offReport[7] = {0x03, 0x00, 0x00, 0x20, 0x20, 0x20, 0x20};
00060 
00061 class ControllerService: public HIDServiceBase
00062 {
00063 public:
00064     ControllerService(BLE &_ble) :
00065         HIDServiceBase(_ble,
00066                        CONTROLLER_REPORT_MAP, sizeof(CONTROLLER_REPORT_MAP),
00067                        inputReport          = report,
00068                        outputReport         = NULL,
00069                        featureReport        = NULL,
00070                        inputReportLength    = 7,
00071                        outputReportLength   = 0,
00072                        featureReportLength  = 0,
00073                        reportTickerDelay    = 250)
00074     {
00075         failedReports = 0;
00076         startReportTicker();
00077     }
00078 
00079     virtual void onConnection(const Gap::ConnectionCallbackParams_t *params)
00080     {
00081         HIDServiceBase::onConnection(params);
00082         startReportTicker();
00083     }
00084 
00085     virtual void onDisconnection(const Gap::DisconnectionCallbackParams_t *params)
00086     {
00087         stopReportTicker();
00088         HIDServiceBase::onDisconnection(params);
00089     }
00090     
00091     void setReport(uint8_t *r) {
00092 //        for (int i = 0; i < 6; i++) {
00093 //            report[i+1] = r[i];
00094 //        }
00095         memcpy(&report[1], r, 6);
00096     }
00097 
00098     /**
00099      * Called by the report ticker
00100      */
00101 
00102     virtual void sendCallback(void) {
00103 //        if (toggle) {
00104 //            send(onReport);
00105 //        } else {
00106 //            send(offReport);
00107 //        }
00108 //        toggle = !toggle;
00109         
00110         if (send(report)) {
00111             failedReports++;
00112         }   
00113     }
00114     
00115     uint32_t failedReports;
00116 };