Initial release
microbit_switch_if_wear.h@6:e2074d02065b, 2018-12-14 (annotated)
- Committer:
- masakjm
- Date:
- Fri Dec 14 18:36:40 2018 +0000
- Revision:
- 6:e2074d02065b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
masakjm | 6:e2074d02065b | 1 | //================================= |
masakjm | 6:e2074d02065b | 2 | // microbit_switch_if_wear |
masakjm | 6:e2074d02065b | 3 | //================================= |
masakjm | 6:e2074d02065b | 4 | // A wearable BLE switch interface using micro bits |
masakjm | 6:e2074d02065b | 5 | // The MIT License (MIT) Copyright (c) 2018 Masatomo Kojima |
masakjm | 6:e2074d02065b | 6 | |
masakjm | 6:e2074d02065b | 7 | #include "mbed.h" |
masakjm | 6:e2074d02065b | 8 | #include "MicroBit.h" |
masakjm | 6:e2074d02065b | 9 | #include "ble/services/BatteryService.h" |
masakjm | 6:e2074d02065b | 10 | #include "KeyboardService.h" |
masakjm | 6:e2074d02065b | 11 | #include "Keyboard_types.h" |
masakjm | 6:e2074d02065b | 12 | #include "HIDServiceBase.h" |
masakjm | 6:e2074d02065b | 13 | #include "HIDDeviceInformationService.h" |
masakjm | 6:e2074d02065b | 14 | |
masakjm | 6:e2074d02065b | 15 | //---------------------- |
masakjm | 6:e2074d02065b | 16 | // Keybord |
masakjm | 6:e2074d02065b | 17 | //---------------------- |
masakjm | 6:e2074d02065b | 18 | const int KEY_BS = 8; /* Keyboard Backspace */ |
masakjm | 6:e2074d02065b | 19 | const int KEY_TAB = 9; /* Keyboard Tab */ |
masakjm | 6:e2074d02065b | 20 | const int KEY_ENTER = 10; /* Keyboard Return (Enter) */ |
masakjm | 6:e2074d02065b | 21 | //const int KEY_ESC = 27; /* Keyboard Escape */ |
masakjm | 6:e2074d02065b | 22 | const int KEY_SPACE = 32; /* Keyboard Space */ |
masakjm | 6:e2074d02065b | 23 | |
masakjm | 6:e2074d02065b | 24 | //---------------------- |
masakjm | 6:e2074d02065b | 25 | // Setting |
masakjm | 6:e2074d02065b | 26 | //---------------------- |
masakjm | 6:e2074d02065b | 27 | #define NUM_GROUP1 6 |
masakjm | 6:e2074d02065b | 28 | #define NUM_GROUP2 6 |
masakjm | 6:e2074d02065b | 29 | |
masakjm | 6:e2074d02065b | 30 | #define MODIFY_CTRL 0x100 |
masakjm | 6:e2074d02065b | 31 | #define MODIFY_SHIFT 0x200 |
masakjm | 6:e2074d02065b | 32 | #define MODIFY_OPTION 0x400 /* option or alt */ |
masakjm | 6:e2074d02065b | 33 | #define MODIFY_COMMAND 0x800 /* command or Windows */ |
masakjm | 6:e2074d02065b | 34 | |
masakjm | 6:e2074d02065b | 35 | const int keyCodeGroup1[NUM_GROUP1][3] = { // Switch 1 2 |
masakjm | 6:e2074d02065b | 36 | {KEY_SPACE, KEY_ENTER, MODIFY_COMMAND + 'h'}, |
masakjm | 6:e2074d02065b | 37 | {RIGHT_ARROW, LEFT_ARROW, MODIFY_COMMAND + 'h'}, |
masakjm | 6:e2074d02065b | 38 | {DOWN_ARROW, UP_ARROW, MODIFY_COMMAND + 'h'}, |
masakjm | 6:e2074d02065b | 39 | {MODIFY_OPTION + DOWN_ARROW, MODIFY_OPTION + UP_ARROW, MODIFY_COMMAND + 'h'}, |
masakjm | 6:e2074d02065b | 40 | {'1', '3', MODIFY_COMMAND + 'h'}, |
masakjm | 6:e2074d02065b | 41 | {'h', 'j', MODIFY_COMMAND + 'h'} |
masakjm | 6:e2074d02065b | 42 | }; |
masakjm | 6:e2074d02065b | 43 | |
masakjm | 6:e2074d02065b | 44 | //in "BLE_HID\Keyboad_types.h" |
masakjm | 6:e2074d02065b | 45 | //enum FUNCTION_KEY { |
masakjm | 6:e2074d02065b | 46 | // KEY_F1 = 128, /* F1 key */ |
masakjm | 6:e2074d02065b | 47 | // KEY_F2, /* F2 key */ |
masakjm | 6:e2074d02065b | 48 | // KEY_F3, /* F3 key */ |
masakjm | 6:e2074d02065b | 49 | // KEY_F4, /* F4 key */ |
masakjm | 6:e2074d02065b | 50 | // KEY_F5, /* F5 key */ |
masakjm | 6:e2074d02065b | 51 | // KEY_F6, /* F6 key */ |
masakjm | 6:e2074d02065b | 52 | // KEY_F7, /* F7 key */ |
masakjm | 6:e2074d02065b | 53 | // KEY_F8, /* F8 key */ |
masakjm | 6:e2074d02065b | 54 | // KEY_F9, /* F9 key */ |
masakjm | 6:e2074d02065b | 55 | // KEY_F10, /* F10 key */ |
masakjm | 6:e2074d02065b | 56 | // KEY_F11, /* F11 key */ |
masakjm | 6:e2074d02065b | 57 | // KEY_F12, /* F12 key */ |
masakjm | 6:e2074d02065b | 58 | // |
masakjm | 6:e2074d02065b | 59 | // KEY_PRINT_SCREEN, /* Print Screen key */ |
masakjm | 6:e2074d02065b | 60 | // KEY_SCROLL_LOCK, /* Scroll lock */ |
masakjm | 6:e2074d02065b | 61 | // KEY_CAPS_LOCK, /* caps lock */ |
masakjm | 6:e2074d02065b | 62 | // KEY_NUM_LOCK, /* num lock */ |
masakjm | 6:e2074d02065b | 63 | // KEY_INSERT, /* Insert key */ |
masakjm | 6:e2074d02065b | 64 | // KEY_HOME, /* Home key */ |
masakjm | 6:e2074d02065b | 65 | // KEY_PAGE_UP, /* Page Up key */ |
masakjm | 6:e2074d02065b | 66 | // KEY_PAGE_DOWN, /* Page Down key */ |
masakjm | 6:e2074d02065b | 67 | // |
masakjm | 6:e2074d02065b | 68 | // RIGHT_ARROW, /* Right arrow */ |
masakjm | 6:e2074d02065b | 69 | // LEFT_ARROW, /* Left arrow */ |
masakjm | 6:e2074d02065b | 70 | // DOWN_ARROW, /* Down arrow */ |
masakjm | 6:e2074d02065b | 71 | // UP_ARROW, /* Up arrow */ |
masakjm | 6:e2074d02065b | 72 | //}; |
masakjm | 6:e2074d02065b | 73 | |
masakjm | 6:e2074d02065b | 74 | //---------------------- |
masakjm | 6:e2074d02065b | 75 | // Display |
masakjm | 6:e2074d02065b | 76 | //---------------------- |
masakjm | 6:e2074d02065b | 77 | #define TIME_TURN_OFF 7.0 // 非表示モードに入るまでの時間 (s) |
masakjm | 6:e2074d02065b | 78 | #define SETTING_DISPLAY_WAIT 1.0 // 設定モード表示時間 (s) |
masakjm | 6:e2074d02065b | 79 | |
masakjm | 6:e2074d02065b | 80 | //---------------------- |
masakjm | 6:e2074d02065b | 81 | // State |
masakjm | 6:e2074d02065b | 82 | //---------------------- |
masakjm | 6:e2074d02065b | 83 | enum STATE { |
masakjm | 6:e2074d02065b | 84 | STATE_DESABLE_INPUT, // 入力無効時 |
masakjm | 6:e2074d02065b | 85 | STATE_SETTING, // パラメータ設定時 |
masakjm | 6:e2074d02065b | 86 | STATE_OPERATING, // 操作時 |
masakjm | 6:e2074d02065b | 87 | }; |
masakjm | 6:e2074d02065b | 88 | //---------------------- |
masakjm | 6:e2074d02065b | 89 | // Disable debug messages by setting NO_DEBUG |
masakjm | 6:e2074d02065b | 90 | //---------------------- |
masakjm | 6:e2074d02065b | 91 | #ifndef NO_DEBUG |
masakjm | 6:e2074d02065b | 92 | #define DEBUG(...) printf(__VA_ARGS__) |
masakjm | 6:e2074d02065b | 93 | #else |
masakjm | 6:e2074d02065b | 94 | #define DEBUG(...) |
masakjm | 6:e2074d02065b | 95 | #endif |
masakjm | 6:e2074d02065b | 96 | |
masakjm | 6:e2074d02065b | 97 | //---------------------- |
masakjm | 6:e2074d02065b | 98 | // BLE & HID |
masakjm | 6:e2074d02065b | 99 | //---------------------- |
masakjm | 6:e2074d02065b | 100 | /** |
masakjm | 6:e2074d02065b | 101 | * IO capabilities of the device. During development, you most likely want "JustWorks", which means |
masakjm | 6:e2074d02065b | 102 | * no IO capabilities. |
masakjm | 6:e2074d02065b | 103 | * It is also possible to use IO_CAPS_DISPLAY_ONLY to generate and show a pincode on the serial |
masakjm | 6:e2074d02065b | 104 | * output. |
masakjm | 6:e2074d02065b | 105 | */ |
masakjm | 6:e2074d02065b | 106 | #ifndef HID_SECURITY_IOCAPS |
masakjm | 6:e2074d02065b | 107 | #define HID_SECURITY_IOCAPS (SecurityManager::IO_CAPS_NONE) |
masakjm | 6:e2074d02065b | 108 | #endif |
masakjm | 6:e2074d02065b | 109 | |
masakjm | 6:e2074d02065b | 110 | /** |
masakjm | 6:e2074d02065b | 111 | * Security level. MITM disabled forces "Just Works". If you require MITM, HID_SECURITY_IOCAPS must |
masakjm | 6:e2074d02065b | 112 | * be at least IO_CAPS_DISPLAY_ONLY. |
masakjm | 6:e2074d02065b | 113 | */ |
masakjm | 6:e2074d02065b | 114 | #ifndef HID_SECURITY_REQUIRE_MITM |
masakjm | 6:e2074d02065b | 115 | #define HID_SECURITY_REQUIRE_MITM false |
masakjm | 6:e2074d02065b | 116 | #endif |
masakjm | 6:e2074d02065b | 117 | |
masakjm | 6:e2074d02065b | 118 | /** |
masakjm | 6:e2074d02065b | 119 | * Initialize security manager: set callback functions and required security level |
masakjm | 6:e2074d02065b | 120 | */ |
masakjm | 6:e2074d02065b | 121 | void initializeSecurity(BLE &_ble); |
masakjm | 6:e2074d02065b | 122 | |
masakjm | 6:e2074d02065b | 123 | /** |
masakjm | 6:e2074d02065b | 124 | * - Initialize auxiliary services required by the HID-over-GATT Profile. |
masakjm | 6:e2074d02065b | 125 | * - Initialize common Gap advertisement. |
masakjm | 6:e2074d02065b | 126 | * |
masakjm | 6:e2074d02065b | 127 | * Demos only have to set a custom device name and appearance, and their HID |
masakjm | 6:e2074d02065b | 128 | * service. |
masakjm | 6:e2074d02065b | 129 | */ |
masakjm | 6:e2074d02065b | 130 | void initializeHOGP(BLE &_ble); |
masakjm | 6:e2074d02065b | 131 | |
masakjm | 6:e2074d02065b | 132 | enum BLE_MESSAGE { |
masakjm | 6:e2074d02065b | 133 | BLE_NO_MESSAGE, // メッセージ無し |
masakjm | 6:e2074d02065b | 134 | BLE_CONNECTED, // BLE接続完了 |
masakjm | 6:e2074d02065b | 135 | BLE_PAIRING_SUCCESS, // ペアリング成功 |
masakjm | 6:e2074d02065b | 136 | BLE_PAIRING_FAILED, // ペアリング失敗 |
masakjm | 6:e2074d02065b | 137 | }; |
masakjm | 6:e2074d02065b | 138 | |
masakjm | 6:e2074d02065b | 139 | const char bleDispChar[]={0, 'C', 'P', 'F'}; |
masakjm | 6:e2074d02065b | 140 |