BLE switch interface with GROVE joystic for micro:bit http://mahoro-ba.net/e2073.html
microbit_switch_if_joy.h@19:4354c463c36c, 2019-03-30 (annotated)
- Committer:
- masakjm
- Date:
- Sat Mar 30 00:11:35 2019 +0000
- Revision:
- 19:4354c463c36c
- Parent:
- 14:469243e10ce5
Add key assignment of S = 5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
masakjm | 7:e2779d2fef9c | 1 | //================================= |
masakjm | 7:e2779d2fef9c | 2 | // microbit_switch_if_joy |
masakjm | 7:e2779d2fef9c | 3 | //================================= |
masakjm | 12:7fb193872bec | 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 | 13:39ecc149d163 | 34 | #define MODIFY_CTRL 0x100 |
masakjm | 13:39ecc149d163 | 35 | #define MODIFY_SHIFT 0x200 |
masakjm | 13:39ecc149d163 | 36 | #define MODIFY_OPTION 0x400 /* option or alt */ |
masakjm | 13:39ecc149d163 | 37 | #define MODIFY_COMMAND 0x800 /* command or Windows */ |
masakjm | 13:39ecc149d163 | 38 | |
masakjm | 7:e2779d2fef9c | 39 | const int KEY_BS = 8; /* Keyboard Backspace */ |
masakjm | 7:e2779d2fef9c | 40 | const int KEY_TAB = 9; /* Keyboard Tab */ |
masakjm | 7:e2779d2fef9c | 41 | const int KEY_ENTER = 10; /* Keyboard Return (Enter) */ |
masakjm | 7:e2779d2fef9c | 42 | //const int KEY_ESC = 27; /* Keyboard Escape */ |
masakjm | 7:e2779d2fef9c | 43 | const int KEY_SPACE = 32; /* Keyboard Space */ |
masakjm | 7:e2779d2fef9c | 44 | |
masakjm | 13:39ecc149d163 | 45 | const int KEY_EQUAL = 0x2b; /* = */ |
masakjm | 13:39ecc149d163 | 46 | const int KEY_COMMA = 0x2c; /* , */ |
masakjm | 13:39ecc149d163 | 47 | const int KEY_MINUS = 0x2d; /* - */ |
masakjm | 13:39ecc149d163 | 48 | const int KEY_PIRIOD = 0x2e; /* . */ |
masakjm | 13:39ecc149d163 | 49 | const int KEY_SLASH = 0x2f; /* / */ |
masakjm | 13:39ecc149d163 | 50 | const int KEY_QUOT = 0x40; /* ' */ |
masakjm | 13:39ecc149d163 | 51 | |
masakjm | 13:39ecc149d163 | 52 | const int KEY_PLUS = MODIFY_SHIFT +0x2b; /* + */ |
masakjm | 13:39ecc149d163 | 53 | const int KEY_LT = MODIFY_SHIFT +0x2c; /* < */ |
masakjm | 13:39ecc149d163 | 54 | const int KEY_UNDER = MODIFY_SHIFT +0x2d; /* _ */ |
masakjm | 13:39ecc149d163 | 55 | const int KEY_GT = MODIFY_SHIFT +0x2e; /* > */ |
masakjm | 13:39ecc149d163 | 56 | const int KEY_QUESTION = MODIFY_SHIFT +0x2f; /* ? */ |
masakjm | 13:39ecc149d163 | 57 | const int KEY_RBRACKET = MODIFY_SHIFT +0x30; /* ) shif+0 */ |
masakjm | 13:39ecc149d163 | 58 | const int KEY_EXCLA = MODIFY_SHIFT +0x31; /* ! shif+1 */ |
masakjm | 13:39ecc149d163 | 59 | const int KEY_AT = MODIFY_SHIFT +0x32; /* @ shif+2 */ |
masakjm | 13:39ecc149d163 | 60 | const int KEY_NUMBER = MODIFY_SHIFT +0x33; /* # shif+3 */ |
masakjm | 13:39ecc149d163 | 61 | const int KEY_DOLL = MODIFY_SHIFT +0x34; /* $ shif+4 */ |
masakjm | 13:39ecc149d163 | 62 | const int KEY_PERCENT = MODIFY_SHIFT +0x35; /* % shif+5 */ |
masakjm | 13:39ecc149d163 | 63 | const int KEY_CARET = MODIFY_SHIFT +0x36; /* ^ shif+6 */ |
masakjm | 13:39ecc149d163 | 64 | const int KEY_AMP = MODIFY_SHIFT +0x37; /* & shif+7 */ |
masakjm | 13:39ecc149d163 | 65 | const int KEY_ASTERISK = MODIFY_SHIFT +0x38; /* * shif+8 */ |
masakjm | 13:39ecc149d163 | 66 | const int KEY_LBRACKET = MODIFY_SHIFT +0x39; /* ( shif+9 */ |
masakjm | 13:39ecc149d163 | 67 | const int KEY_DBLQUOT = MODIFY_SHIFT +0x40; /* " */ |
masakjm | 7:e2779d2fef9c | 68 | |
masakjm | 7:e2779d2fef9c | 69 | //---------------------- |
masakjm | 7:e2779d2fef9c | 70 | // Setting |
masakjm | 7:e2779d2fef9c | 71 | //---------------------- |
masakjm | 19:4354c463c36c | 72 | #define NUM_GROUP1 5 |
masakjm | 7:e2779d2fef9c | 73 | #define NUM_G1MEMBER 5 |
masakjm | 7:e2779d2fef9c | 74 | |
masakjm | 13:39ecc149d163 | 75 | |
masakjm | 7:e2779d2fef9c | 76 | |
masakjm | 7:e2779d2fef9c | 77 | const int keyCodeGroup0[3] = { // Button A, Button B, Button A&B |
masakjm | 7:e2779d2fef9c | 78 | 'a', |
masakjm | 7:e2779d2fef9c | 79 | 'b', |
masakjm | 7:e2779d2fef9c | 80 | MODIFY_COMMAND + 'h' // アプリ終了 |
masakjm | 7:e2779d2fef9c | 81 | }; |
masakjm | 7:e2779d2fef9c | 82 | const int keyCodeGroup1[NUM_GROUP1][NUM_G1MEMBER] = { // 中央 左 右 上 下 |
masakjm | 19:4354c463c36c | 83 | {0, KEY_TAB, KEY_SPACE, 0, KEY_ENTER }, |
masakjm | 7:e2779d2fef9c | 84 | {KEY_HOME, RIGHT_ARROW, LEFT_ARROW, DOWN_ARROW, UP_ARROW }, |
masakjm | 14:469243e10ce5 | 85 | {KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5}, |
masakjm | 13:39ecc149d163 | 86 | {MODIFY_COMMAND+'r', 0, 0, MODIFY_OPTION+DOWN_ARROW, MODIFY_OPTION+UP_ARROW}, |
masakjm | 19:4354c463c36c | 87 | {KEY_ENTER, KEY_TAB, KEY_SPACE, 0, 0 }, |
masakjm | 7:e2779d2fef9c | 88 | }; |
masakjm | 7:e2779d2fef9c | 89 | |
masakjm | 7:e2779d2fef9c | 90 | //in "BLE_HID\Keyboad_types.h" |
masakjm | 7:e2779d2fef9c | 91 | //enum FUNCTION_KEY { |
masakjm | 7:e2779d2fef9c | 92 | // KEY_F1 = 128, /* F1 key */ |
masakjm | 7:e2779d2fef9c | 93 | // KEY_F2, /* F2 key */ |
masakjm | 7:e2779d2fef9c | 94 | // KEY_F3, /* F3 key */ |
masakjm | 7:e2779d2fef9c | 95 | // KEY_F4, /* F4 key */ |
masakjm | 7:e2779d2fef9c | 96 | // KEY_F5, /* F5 key */ |
masakjm | 7:e2779d2fef9c | 97 | // KEY_F6, /* F6 key */ |
masakjm | 7:e2779d2fef9c | 98 | // KEY_F7, /* F7 key */ |
masakjm | 7:e2779d2fef9c | 99 | // KEY_F8, /* F8 key */ |
masakjm | 7:e2779d2fef9c | 100 | // KEY_F9, /* F9 key */ |
masakjm | 7:e2779d2fef9c | 101 | // KEY_F10, /* F10 key */ |
masakjm | 7:e2779d2fef9c | 102 | // KEY_F11, /* F11 key */ |
masakjm | 7:e2779d2fef9c | 103 | // KEY_F12, /* F12 key */ |
masakjm | 7:e2779d2fef9c | 104 | // |
masakjm | 7:e2779d2fef9c | 105 | // KEY_PRINT_SCREEN, /* Print Screen key */ |
masakjm | 7:e2779d2fef9c | 106 | // KEY_SCROLL_LOCK, /* Scroll lock */ |
masakjm | 7:e2779d2fef9c | 107 | // KEY_CAPS_LOCK, /* caps lock */ |
masakjm | 7:e2779d2fef9c | 108 | // KEY_NUM_LOCK, /* num lock */ |
masakjm | 7:e2779d2fef9c | 109 | // KEY_INSERT, /* Insert key */ |
masakjm | 7:e2779d2fef9c | 110 | // KEY_HOME, /* Home key */ |
masakjm | 7:e2779d2fef9c | 111 | // KEY_PAGE_UP, /* Page Up key */ |
masakjm | 7:e2779d2fef9c | 112 | // KEY_PAGE_DOWN, /* Page Down key */ |
masakjm | 7:e2779d2fef9c | 113 | // |
masakjm | 7:e2779d2fef9c | 114 | // RIGHT_ARROW, /* Right arrow */ |
masakjm | 7:e2779d2fef9c | 115 | // LEFT_ARROW, /* Left arrow */ |
masakjm | 7:e2779d2fef9c | 116 | // DOWN_ARROW, /* Down arrow */ |
masakjm | 7:e2779d2fef9c | 117 | // UP_ARROW, /* Up arrow */ |
masakjm | 7:e2779d2fef9c | 118 | //}; |
masakjm | 7:e2779d2fef9c | 119 | |
masakjm | 7:e2779d2fef9c | 120 | //---------------------- |
masakjm | 7:e2779d2fef9c | 121 | // Display |
masakjm | 7:e2779d2fef9c | 122 | //---------------------- |
masakjm | 7:e2779d2fef9c | 123 | #define TIME_TURN_OFF 7.0 // 非表示モードに入るまでの時間 (s) |
masakjm | 7:e2779d2fef9c | 124 | #define SETTING_DISPLAY_WAIT 1.0 // 設定モード表示時間 (s) |
masakjm | 7:e2779d2fef9c | 125 | |
masakjm | 7:e2779d2fef9c | 126 | //---------------------- |
masakjm | 7:e2779d2fef9c | 127 | // State |
masakjm | 7:e2779d2fef9c | 128 | //---------------------- |
masakjm | 7:e2779d2fef9c | 129 | enum STATE { |
masakjm | 7:e2779d2fef9c | 130 | STATE_DESABLE_INPUT, // 入力無効時 |
masakjm | 7:e2779d2fef9c | 131 | STATE_SETTING, // パラメータ設定時 |
masakjm | 7:e2779d2fef9c | 132 | STATE_OPERATING, // 操作時 |
masakjm | 7:e2779d2fef9c | 133 | }; |
masakjm | 7:e2779d2fef9c | 134 | //---------------------- |
masakjm | 7:e2779d2fef9c | 135 | // Disable debug messages by setting NO_DEBUG |
masakjm | 7:e2779d2fef9c | 136 | //---------------------- |
masakjm | 7:e2779d2fef9c | 137 | #ifndef NO_DEBUG |
masakjm | 7:e2779d2fef9c | 138 | #define DEBUG(...) printf(__VA_ARGS__) |
masakjm | 7:e2779d2fef9c | 139 | #else |
masakjm | 7:e2779d2fef9c | 140 | #define DEBUG(...) |
masakjm | 7:e2779d2fef9c | 141 | #endif |
masakjm | 7:e2779d2fef9c | 142 | |
masakjm | 7:e2779d2fef9c | 143 | //---------------------- |
masakjm | 7:e2779d2fef9c | 144 | // BLE & HID |
masakjm | 7:e2779d2fef9c | 145 | //---------------------- |
masakjm | 7:e2779d2fef9c | 146 | /** |
masakjm | 7:e2779d2fef9c | 147 | * IO capabilities of the device. During development, you most likely want "JustWorks", which means |
masakjm | 7:e2779d2fef9c | 148 | * no IO capabilities. |
masakjm | 7:e2779d2fef9c | 149 | * It is also possible to use IO_CAPS_DISPLAY_ONLY to generate and show a pincode on the serial |
masakjm | 7:e2779d2fef9c | 150 | * output. |
masakjm | 7:e2779d2fef9c | 151 | */ |
masakjm | 7:e2779d2fef9c | 152 | #ifndef HID_SECURITY_IOCAPS |
masakjm | 7:e2779d2fef9c | 153 | #define HID_SECURITY_IOCAPS (SecurityManager::IO_CAPS_NONE) |
masakjm | 7:e2779d2fef9c | 154 | #endif |
masakjm | 7:e2779d2fef9c | 155 | |
masakjm | 7:e2779d2fef9c | 156 | /** |
masakjm | 7:e2779d2fef9c | 157 | * Security level. MITM disabled forces "Just Works". If you require MITM, HID_SECURITY_IOCAPS must |
masakjm | 7:e2779d2fef9c | 158 | * be at least IO_CAPS_DISPLAY_ONLY. |
masakjm | 7:e2779d2fef9c | 159 | */ |
masakjm | 7:e2779d2fef9c | 160 | #ifndef HID_SECURITY_REQUIRE_MITM |
masakjm | 7:e2779d2fef9c | 161 | #define HID_SECURITY_REQUIRE_MITM false |
masakjm | 7:e2779d2fef9c | 162 | #endif |
masakjm | 7:e2779d2fef9c | 163 | |
masakjm | 7:e2779d2fef9c | 164 | /** |
masakjm | 7:e2779d2fef9c | 165 | * Initialize security manager: set callback functions and required security level |
masakjm | 7:e2779d2fef9c | 166 | */ |
masakjm | 7:e2779d2fef9c | 167 | void initializeSecurity(BLE &_ble); |
masakjm | 7:e2779d2fef9c | 168 | |
masakjm | 7:e2779d2fef9c | 169 | /** |
masakjm | 7:e2779d2fef9c | 170 | * - Initialize auxiliary services required by the HID-over-GATT Profile. |
masakjm | 7:e2779d2fef9c | 171 | * - Initialize common Gap advertisement. |
masakjm | 7:e2779d2fef9c | 172 | * |
masakjm | 7:e2779d2fef9c | 173 | * Demos only have to set a custom device name and appearance, and their HID |
masakjm | 7:e2779d2fef9c | 174 | * service. |
masakjm | 7:e2779d2fef9c | 175 | */ |
masakjm | 7:e2779d2fef9c | 176 | void initializeHOGP(BLE &_ble); |
masakjm | 7:e2779d2fef9c | 177 | |
masakjm | 7:e2779d2fef9c | 178 | enum BLE_MESSAGE { |
masakjm | 7:e2779d2fef9c | 179 | BLE_NO_MESSAGE, // メッセージ無し |
masakjm | 7:e2779d2fef9c | 180 | BLE_CONNECTED, // BLE接続完了 |
masakjm | 7:e2779d2fef9c | 181 | BLE_PAIRING_SUCCESS, // ペアリング成功 |
masakjm | 7:e2779d2fef9c | 182 | BLE_PAIRING_FAILED, // ペアリング失敗 |
masakjm | 7:e2779d2fef9c | 183 | }; |
masakjm | 7:e2779d2fef9c | 184 | |
masakjm | 7:e2779d2fef9c | 185 | const char bleDispChar[]={0, 'C', 'P', 'F'}; |
masakjm | 7:e2779d2fef9c | 186 |