BLE switch interface using micro:bit with 3 tact switches or 3 Makey Makey sensors

Dependencies:   microbit

Committer:
masakjm
Date:
Tue Jun 11 18:08:53 2019 +0000
Revision:
3:d8fd4efb63cc
Parent:
1:9d0e2e5b5d25
Change the usage of timer.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
masakjm 1:9d0e2e5b5d25 1 /* mbed Microcontroller Library
masakjm 1:9d0e2e5b5d25 2 * Copyright (c) 2015 ARM Limited
masakjm 1:9d0e2e5b5d25 3 *
masakjm 1:9d0e2e5b5d25 4 * Licensed under the Apache License, Version 2.0 (the "License");
masakjm 1:9d0e2e5b5d25 5 * you may not use this file except in compliance with the License.
masakjm 1:9d0e2e5b5d25 6 * You may obtain a copy of the License at
masakjm 1:9d0e2e5b5d25 7 *
masakjm 1:9d0e2e5b5d25 8 * http://www.apache.org/licenses/LICENSE-2.0
masakjm 1:9d0e2e5b5d25 9 *
masakjm 1:9d0e2e5b5d25 10 * Unless required by applicable law or agreed to in writing, software
masakjm 1:9d0e2e5b5d25 11 * distributed under the License is distributed on an "AS IS" BASIS,
masakjm 1:9d0e2e5b5d25 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
masakjm 1:9d0e2e5b5d25 13 * See the License for the specific language governing permissions and
masakjm 1:9d0e2e5b5d25 14 * limitations under the License.
masakjm 1:9d0e2e5b5d25 15 */
masakjm 1:9d0e2e5b5d25 16
masakjm 1:9d0e2e5b5d25 17 #include "mbed.h"
masakjm 1:9d0e2e5b5d25 18
masakjm 1:9d0e2e5b5d25 19 #include "HIDServiceBase.h"
masakjm 1:9d0e2e5b5d25 20
masakjm 1:9d0e2e5b5d25 21 enum ButtonState
masakjm 1:9d0e2e5b5d25 22 {
masakjm 1:9d0e2e5b5d25 23 BUTTON_UP,
masakjm 1:9d0e2e5b5d25 24 BUTTON_DOWN
masakjm 1:9d0e2e5b5d25 25 };
masakjm 1:9d0e2e5b5d25 26
masakjm 1:9d0e2e5b5d25 27 enum JoystickButton
masakjm 1:9d0e2e5b5d25 28 {
masakjm 1:9d0e2e5b5d25 29 JOYSTICK_BUTTON_1 = 0x1,
masakjm 1:9d0e2e5b5d25 30 JOYSTICK_BUTTON_2 = 0x2,
masakjm 1:9d0e2e5b5d25 31 };
masakjm 1:9d0e2e5b5d25 32
masakjm 1:9d0e2e5b5d25 33 report_map_t JOYSTICK_REPORT_MAP = {
masakjm 1:9d0e2e5b5d25 34 USAGE_PAGE(1), 0x01, // Generic Desktop
masakjm 1:9d0e2e5b5d25 35 USAGE(1), 0x04, // Joystick
masakjm 1:9d0e2e5b5d25 36 COLLECTION(1), 0x01, // Application
masakjm 1:9d0e2e5b5d25 37 COLLECTION(1), 0x00, // Physical
masakjm 1:9d0e2e5b5d25 38 USAGE_PAGE(1), 0x09, // Buttons
masakjm 1:9d0e2e5b5d25 39 USAGE_MINIMUM(1), 0x01,
masakjm 1:9d0e2e5b5d25 40 USAGE_MAXIMUM(1), 0x03,
masakjm 1:9d0e2e5b5d25 41 LOGICAL_MINIMUM(1), 0x00,
masakjm 1:9d0e2e5b5d25 42 LOGICAL_MAXIMUM(1), 0x01,
masakjm 1:9d0e2e5b5d25 43 REPORT_COUNT(1), 0x03, // 2 bits (Buttons)
masakjm 1:9d0e2e5b5d25 44 REPORT_SIZE(1), 0x01,
masakjm 1:9d0e2e5b5d25 45 INPUT(1), 0x02, // Data, Variable, Absolute
masakjm 1:9d0e2e5b5d25 46 REPORT_COUNT(1), 0x01, // 6 bits (Padding)
masakjm 1:9d0e2e5b5d25 47 REPORT_SIZE(1), 0x05,
masakjm 1:9d0e2e5b5d25 48 INPUT(1), 0x01, // Constant
masakjm 1:9d0e2e5b5d25 49 USAGE_PAGE(1), 0x01, // Generic Desktop
masakjm 1:9d0e2e5b5d25 50 USAGE(1), 0x30, // X
masakjm 1:9d0e2e5b5d25 51 USAGE(1), 0x31, // Y
masakjm 1:9d0e2e5b5d25 52 USAGE(1), 0x32, // Z
masakjm 1:9d0e2e5b5d25 53 USAGE(1), 0x33, // Rx
masakjm 1:9d0e2e5b5d25 54 LOGICAL_MINIMUM(1), 0x81, // -127
masakjm 1:9d0e2e5b5d25 55 LOGICAL_MAXIMUM(1), 0x7f, // 127
masakjm 1:9d0e2e5b5d25 56 REPORT_SIZE(1), 0x08, // Three bytes
masakjm 1:9d0e2e5b5d25 57 REPORT_COUNT(1), 0x04,
masakjm 1:9d0e2e5b5d25 58 INPUT(1), 0x02, // Data, Variable, Absolute (unlike mouse)
masakjm 1:9d0e2e5b5d25 59 END_COLLECTION(0),
masakjm 1:9d0e2e5b5d25 60 END_COLLECTION(0),
masakjm 1:9d0e2e5b5d25 61 };
masakjm 1:9d0e2e5b5d25 62
masakjm 1:9d0e2e5b5d25 63 uint8_t report[] = { 0, 0, 0, 0, 0 };
masakjm 1:9d0e2e5b5d25 64
masakjm 1:9d0e2e5b5d25 65 class JoystickService: public HIDServiceBase
masakjm 1:9d0e2e5b5d25 66 {
masakjm 1:9d0e2e5b5d25 67 public:
masakjm 1:9d0e2e5b5d25 68 JoystickService(BLE &_ble) :
masakjm 1:9d0e2e5b5d25 69 HIDServiceBase(_ble,
masakjm 1:9d0e2e5b5d25 70 JOYSTICK_REPORT_MAP, sizeof(JOYSTICK_REPORT_MAP),
masakjm 1:9d0e2e5b5d25 71 inputReport = report,
masakjm 1:9d0e2e5b5d25 72 outputReport = NULL,
masakjm 1:9d0e2e5b5d25 73 featureReport = NULL,
masakjm 1:9d0e2e5b5d25 74 inputReportLength = sizeof(inputReport),
masakjm 1:9d0e2e5b5d25 75 outputReportLength = 0,
masakjm 1:9d0e2e5b5d25 76 featureReportLength = 0,
masakjm 1:9d0e2e5b5d25 77 reportTickerDelay = 20),
masakjm 1:9d0e2e5b5d25 78 buttonsState (0),
masakjm 1:9d0e2e5b5d25 79 failedReports (0)
masakjm 1:9d0e2e5b5d25 80 {
masakjm 1:9d0e2e5b5d25 81 speed[0] = 0;
masakjm 1:9d0e2e5b5d25 82 speed[1] = 0;
masakjm 1:9d0e2e5b5d25 83 speed[2] = 0;
masakjm 1:9d0e2e5b5d25 84 speed[3] = 0;
masakjm 1:9d0e2e5b5d25 85
masakjm 1:9d0e2e5b5d25 86 startReportTicker();
masakjm 1:9d0e2e5b5d25 87 }
masakjm 1:9d0e2e5b5d25 88
masakjm 1:9d0e2e5b5d25 89 int setSpeed(int8_t x, int8_t y, int8_t z)
masakjm 1:9d0e2e5b5d25 90 {
masakjm 1:9d0e2e5b5d25 91 speed[0] = x;
masakjm 1:9d0e2e5b5d25 92 speed[1] = y;
masakjm 1:9d0e2e5b5d25 93 speed[2] = z;
masakjm 1:9d0e2e5b5d25 94
masakjm 1:9d0e2e5b5d25 95 return 0;
masakjm 1:9d0e2e5b5d25 96 }
masakjm 1:9d0e2e5b5d25 97
masakjm 1:9d0e2e5b5d25 98 int setButton(JoystickButton button, ButtonState state)
masakjm 1:9d0e2e5b5d25 99 {
masakjm 1:9d0e2e5b5d25 100 if (state == BUTTON_UP)
masakjm 1:9d0e2e5b5d25 101 buttonsState &= ~(button);
masakjm 1:9d0e2e5b5d25 102 else
masakjm 1:9d0e2e5b5d25 103 buttonsState |= button;
masakjm 1:9d0e2e5b5d25 104
masakjm 1:9d0e2e5b5d25 105 return 0;
masakjm 1:9d0e2e5b5d25 106 }
masakjm 1:9d0e2e5b5d25 107
masakjm 1:9d0e2e5b5d25 108 virtual void sendCallback(void) {
masakjm 1:9d0e2e5b5d25 109 if (!connected)
masakjm 1:9d0e2e5b5d25 110 return;
masakjm 1:9d0e2e5b5d25 111
masakjm 1:9d0e2e5b5d25 112 report[0] = buttonsState & 0x7;
masakjm 1:9d0e2e5b5d25 113 report[1] = speed[0];
masakjm 1:9d0e2e5b5d25 114 report[2] = speed[1];
masakjm 1:9d0e2e5b5d25 115 report[3] = speed[2];
masakjm 1:9d0e2e5b5d25 116 report[4] = speed[3];
masakjm 1:9d0e2e5b5d25 117
masakjm 1:9d0e2e5b5d25 118 if (send(report))
masakjm 1:9d0e2e5b5d25 119 failedReports++;
masakjm 1:9d0e2e5b5d25 120 }
masakjm 1:9d0e2e5b5d25 121
masakjm 1:9d0e2e5b5d25 122 protected:
masakjm 1:9d0e2e5b5d25 123 uint8_t buttonsState;
masakjm 1:9d0e2e5b5d25 124 uint8_t speed[4];
masakjm 1:9d0e2e5b5d25 125
masakjm 1:9d0e2e5b5d25 126 public:
masakjm 1:9d0e2e5b5d25 127 uint32_t failedReports;
masakjm 1:9d0e2e5b5d25 128 };
masakjm 1:9d0e2e5b5d25 129