BLE switch interface with GROVE joystic for micro:bit http://mahoro-ba.net/e2073.html

Dependencies:   microbit

Committer:
masakjm
Date:
Fri Feb 22 21:10:14 2019 +0000
Revision:
7:e2779d2fef9c
Child:
11:8e0b379efcb3
1st release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
masakjm 7:e2779d2fef9c 1 //=================================
masakjm 7:e2779d2fef9c 2 // microbit_switch_if_joy
masakjm 7:e2779d2fef9c 3 //=================================
masakjm 7:e2779d2fef9c 4 // BLE switch interface with GROVE joystic for micro:bit
masakjm 7:e2779d2fef9c 5 // The MIT License (MIT) Copyright (c) 2019 Masatomo Kojima
masakjm 7:e2779d2fef9c 6
masakjm 7:e2779d2fef9c 7 #include "mbed.h"
masakjm 7:e2779d2fef9c 8 #include "MicroBit.h"
masakjm 7:e2779d2fef9c 9 #include "ble/services/BatteryService.h"
masakjm 7:e2779d2fef9c 10 #include "KeyboardService.h"
masakjm 7:e2779d2fef9c 11 #include "Keyboard_types.h"
masakjm 7:e2779d2fef9c 12 #include "HIDServiceBase.h"
masakjm 7:e2779d2fef9c 13 #include "HIDDeviceInformationService.h"
masakjm 7:e2779d2fef9c 14
masakjm 7:e2779d2fef9c 15 //----------------------
masakjm 7:e2779d2fef9c 16 // GROVE JoyStick
masakjm 7:e2779d2fef9c 17 //----------------------
masakjm 7:e2779d2fef9c 18 #define JOY_ERR_THRE 0.4 // JoyStick 異常入力の閾値
masakjm 7:e2779d2fef9c 19 #define JOY_LOW_THRE 0.35 // JoyStick 入力値の低い側の閾値
masakjm 7:e2779d2fef9c 20 #define JOY_HIGH_THRE 0.65 // JoyStick 入力値の高い側の閾値
masakjm 7:e2779d2fef9c 21 #define JOY_CENTER_THRE 0.90 // JoyStick の押しスイッチの閾値
masakjm 7:e2779d2fef9c 22 enum JOY_STATUS {
masakjm 7:e2779d2fef9c 23 JOY_NEUTRAL = -1,
masakjm 7:e2779d2fef9c 24 JOY_CENTER_PRESS , // 中央
masakjm 7:e2779d2fef9c 25 JOY_YHIGH_PRESS, // 左
masakjm 7:e2779d2fef9c 26 JOY_YLOW_PRESS, // 右
masakjm 7:e2779d2fef9c 27 JOY_XHIGH_PRESS, // 上
masakjm 7:e2779d2fef9c 28 JOY_XLOW_PRESS, // 下
masakjm 7:e2779d2fef9c 29 };
masakjm 7:e2779d2fef9c 30
masakjm 7:e2779d2fef9c 31 //----------------------
masakjm 7:e2779d2fef9c 32 // Keybord
masakjm 7:e2779d2fef9c 33 //----------------------
masakjm 7:e2779d2fef9c 34 const int KEY_BS = 8; /* Keyboard Backspace */
masakjm 7:e2779d2fef9c 35 const int KEY_TAB = 9; /* Keyboard Tab */
masakjm 7:e2779d2fef9c 36 const int KEY_ENTER = 10; /* Keyboard Return (Enter) */
masakjm 7:e2779d2fef9c 37 //const int KEY_ESC = 27; /* Keyboard Escape */
masakjm 7:e2779d2fef9c 38 const int KEY_SPACE = 32; /* Keyboard Space */
masakjm 7:e2779d2fef9c 39
masakjm 7:e2779d2fef9c 40 const int KEY_NUMPAD0 = 0x60; /* Numpad 0 */
masakjm 7:e2779d2fef9c 41 const int KEY_NUMPAD1 = 0x61; /* Numpad 1 */
masakjm 7:e2779d2fef9c 42 const int KEY_NUMPAD2 = 0x62; /* Numpad 2 */
masakjm 7:e2779d2fef9c 43 const int KEY_NUMPAD3 = 0x63; /* Numpad 3 */
masakjm 7:e2779d2fef9c 44 const int KEY_NUMPAD4 = 0x64; /* Numpad 4 */
masakjm 7:e2779d2fef9c 45 const int KEY_NUMPAD5 = 0x65; /* Numpad 5 */
masakjm 7:e2779d2fef9c 46 const int KEY_NUMPAD6 = 0x66; /* Numpad 6 */
masakjm 7:e2779d2fef9c 47 const int KEY_NUMPAD7 = 0x67; /* Numpad 7 */
masakjm 7:e2779d2fef9c 48 const int KEY_NUMPAD8 = 0x68; /* Numpad 8 */
masakjm 7:e2779d2fef9c 49 const int KEY_NUMPAD9 = 0x69; /* Numpad 9 */
masakjm 7:e2779d2fef9c 50
masakjm 7:e2779d2fef9c 51 //----------------------
masakjm 7:e2779d2fef9c 52 // Setting
masakjm 7:e2779d2fef9c 53 //----------------------
masakjm 7:e2779d2fef9c 54 #define NUM_GROUP1 4
masakjm 7:e2779d2fef9c 55 #define NUM_G1MEMBER 5
masakjm 7:e2779d2fef9c 56
masakjm 7:e2779d2fef9c 57 #define MODIFY_CTRL 0x100
masakjm 7:e2779d2fef9c 58 #define MODIFY_SHIFT 0x200
masakjm 7:e2779d2fef9c 59 #define MODIFY_OPTION 0x400 /* option or alt */
masakjm 7:e2779d2fef9c 60 #define MODIFY_COMMAND 0x800 /* command or Windows */
masakjm 7:e2779d2fef9c 61
masakjm 7:e2779d2fef9c 62 const int keyCodeGroup0[3] = { // Button A, Button B, Button A&B
masakjm 7:e2779d2fef9c 63 'a',
masakjm 7:e2779d2fef9c 64 'b',
masakjm 7:e2779d2fef9c 65 MODIFY_COMMAND + 'h' // アプリ終了
masakjm 7:e2779d2fef9c 66 };
masakjm 7:e2779d2fef9c 67 const int keyCodeGroup1[NUM_GROUP1][NUM_G1MEMBER] = { // 中央 左 右 上 下
masakjm 7:e2779d2fef9c 68 {KEY_ENTER, KEY_TAB, KEY_SPACE, 0, KEY_ENTER },
masakjm 7:e2779d2fef9c 69 {KEY_HOME, RIGHT_ARROW, LEFT_ARROW, DOWN_ARROW, UP_ARROW },
masakjm 7:e2779d2fef9c 70 {KEY_NUMPAD1, KEY_NUMPAD2, KEY_NUMPAD3, KEY_NUMPAD4, KEY_NUMPAD5},
masakjm 7:e2779d2fef9c 71 {KEY_ENTER, MODIFY_OPTION+MODIFY_SHIFT+KEY_TAB, MODIFY_OPTION+KEY_TAB,
masakjm 7:e2779d2fef9c 72 MODIFY_OPTION+DOWN_ARROW, MODIFY_OPTION+UP_ARROW},
masakjm 7:e2779d2fef9c 73 // {KEY_ENTER, MODIFY_SHIFT+KEY_TAB, KEY_TAB, KEY_SPACE, MODIFY_SHIFT+KEY_SPACE},
masakjm 7:e2779d2fef9c 74 };
masakjm 7:e2779d2fef9c 75
masakjm 7:e2779d2fef9c 76 //in "BLE_HID\Keyboad_types.h"
masakjm 7:e2779d2fef9c 77 //enum FUNCTION_KEY {
masakjm 7:e2779d2fef9c 78 // KEY_F1 = 128, /* F1 key */
masakjm 7:e2779d2fef9c 79 // KEY_F2, /* F2 key */
masakjm 7:e2779d2fef9c 80 // KEY_F3, /* F3 key */
masakjm 7:e2779d2fef9c 81 // KEY_F4, /* F4 key */
masakjm 7:e2779d2fef9c 82 // KEY_F5, /* F5 key */
masakjm 7:e2779d2fef9c 83 // KEY_F6, /* F6 key */
masakjm 7:e2779d2fef9c 84 // KEY_F7, /* F7 key */
masakjm 7:e2779d2fef9c 85 // KEY_F8, /* F8 key */
masakjm 7:e2779d2fef9c 86 // KEY_F9, /* F9 key */
masakjm 7:e2779d2fef9c 87 // KEY_F10, /* F10 key */
masakjm 7:e2779d2fef9c 88 // KEY_F11, /* F11 key */
masakjm 7:e2779d2fef9c 89 // KEY_F12, /* F12 key */
masakjm 7:e2779d2fef9c 90 //
masakjm 7:e2779d2fef9c 91 // KEY_PRINT_SCREEN, /* Print Screen key */
masakjm 7:e2779d2fef9c 92 // KEY_SCROLL_LOCK, /* Scroll lock */
masakjm 7:e2779d2fef9c 93 // KEY_CAPS_LOCK, /* caps lock */
masakjm 7:e2779d2fef9c 94 // KEY_NUM_LOCK, /* num lock */
masakjm 7:e2779d2fef9c 95 // KEY_INSERT, /* Insert key */
masakjm 7:e2779d2fef9c 96 // KEY_HOME, /* Home key */
masakjm 7:e2779d2fef9c 97 // KEY_PAGE_UP, /* Page Up key */
masakjm 7:e2779d2fef9c 98 // KEY_PAGE_DOWN, /* Page Down key */
masakjm 7:e2779d2fef9c 99 //
masakjm 7:e2779d2fef9c 100 // RIGHT_ARROW, /* Right arrow */
masakjm 7:e2779d2fef9c 101 // LEFT_ARROW, /* Left arrow */
masakjm 7:e2779d2fef9c 102 // DOWN_ARROW, /* Down arrow */
masakjm 7:e2779d2fef9c 103 // UP_ARROW, /* Up arrow */
masakjm 7:e2779d2fef9c 104 //};
masakjm 7:e2779d2fef9c 105
masakjm 7:e2779d2fef9c 106 //----------------------
masakjm 7:e2779d2fef9c 107 // Display
masakjm 7:e2779d2fef9c 108 //----------------------
masakjm 7:e2779d2fef9c 109 #define TIME_TURN_OFF 7.0 // 非表示モードに入るまでの時間 (s)
masakjm 7:e2779d2fef9c 110 #define SETTING_DISPLAY_WAIT 1.0 // 設定モード表示時間 (s)
masakjm 7:e2779d2fef9c 111
masakjm 7:e2779d2fef9c 112 //----------------------
masakjm 7:e2779d2fef9c 113 // State
masakjm 7:e2779d2fef9c 114 //----------------------
masakjm 7:e2779d2fef9c 115 enum STATE {
masakjm 7:e2779d2fef9c 116 STATE_DESABLE_INPUT, // 入力無効時
masakjm 7:e2779d2fef9c 117 STATE_SETTING, // パラメータ設定時
masakjm 7:e2779d2fef9c 118 STATE_OPERATING, // 操作時
masakjm 7:e2779d2fef9c 119 };
masakjm 7:e2779d2fef9c 120 //----------------------
masakjm 7:e2779d2fef9c 121 // Disable debug messages by setting NO_DEBUG
masakjm 7:e2779d2fef9c 122 //----------------------
masakjm 7:e2779d2fef9c 123 #ifndef NO_DEBUG
masakjm 7:e2779d2fef9c 124 #define DEBUG(...) printf(__VA_ARGS__)
masakjm 7:e2779d2fef9c 125 #else
masakjm 7:e2779d2fef9c 126 #define DEBUG(...)
masakjm 7:e2779d2fef9c 127 #endif
masakjm 7:e2779d2fef9c 128
masakjm 7:e2779d2fef9c 129 //----------------------
masakjm 7:e2779d2fef9c 130 // BLE & HID
masakjm 7:e2779d2fef9c 131 //----------------------
masakjm 7:e2779d2fef9c 132 /**
masakjm 7:e2779d2fef9c 133 * IO capabilities of the device. During development, you most likely want "JustWorks", which means
masakjm 7:e2779d2fef9c 134 * no IO capabilities.
masakjm 7:e2779d2fef9c 135 * It is also possible to use IO_CAPS_DISPLAY_ONLY to generate and show a pincode on the serial
masakjm 7:e2779d2fef9c 136 * output.
masakjm 7:e2779d2fef9c 137 */
masakjm 7:e2779d2fef9c 138 #ifndef HID_SECURITY_IOCAPS
masakjm 7:e2779d2fef9c 139 #define HID_SECURITY_IOCAPS (SecurityManager::IO_CAPS_NONE)
masakjm 7:e2779d2fef9c 140 #endif
masakjm 7:e2779d2fef9c 141
masakjm 7:e2779d2fef9c 142 /**
masakjm 7:e2779d2fef9c 143 * Security level. MITM disabled forces "Just Works". If you require MITM, HID_SECURITY_IOCAPS must
masakjm 7:e2779d2fef9c 144 * be at least IO_CAPS_DISPLAY_ONLY.
masakjm 7:e2779d2fef9c 145 */
masakjm 7:e2779d2fef9c 146 #ifndef HID_SECURITY_REQUIRE_MITM
masakjm 7:e2779d2fef9c 147 #define HID_SECURITY_REQUIRE_MITM false
masakjm 7:e2779d2fef9c 148 #endif
masakjm 7:e2779d2fef9c 149
masakjm 7:e2779d2fef9c 150 /**
masakjm 7:e2779d2fef9c 151 * Initialize security manager: set callback functions and required security level
masakjm 7:e2779d2fef9c 152 */
masakjm 7:e2779d2fef9c 153 void initializeSecurity(BLE &_ble);
masakjm 7:e2779d2fef9c 154
masakjm 7:e2779d2fef9c 155 /**
masakjm 7:e2779d2fef9c 156 * - Initialize auxiliary services required by the HID-over-GATT Profile.
masakjm 7:e2779d2fef9c 157 * - Initialize common Gap advertisement.
masakjm 7:e2779d2fef9c 158 *
masakjm 7:e2779d2fef9c 159 * Demos only have to set a custom device name and appearance, and their HID
masakjm 7:e2779d2fef9c 160 * service.
masakjm 7:e2779d2fef9c 161 */
masakjm 7:e2779d2fef9c 162 void initializeHOGP(BLE &_ble);
masakjm 7:e2779d2fef9c 163
masakjm 7:e2779d2fef9c 164 enum BLE_MESSAGE {
masakjm 7:e2779d2fef9c 165 BLE_NO_MESSAGE, // メッセージ無し
masakjm 7:e2779d2fef9c 166 BLE_CONNECTED, // BLE接続完了
masakjm 7:e2779d2fef9c 167 BLE_PAIRING_SUCCESS, // ペアリング成功
masakjm 7:e2779d2fef9c 168 BLE_PAIRING_FAILED, // ペアリング失敗
masakjm 7:e2779d2fef9c 169 };
masakjm 7:e2779d2fef9c 170
masakjm 7:e2779d2fef9c 171 const char bleDispChar[]={0, 'C', 'P', 'F'};
masakjm 7:e2779d2fef9c 172