ble nano hid over gatt

Dependencies:   BLE_API mbed-dev nRF51822

Committer:
cho45
Date:
Thu Sep 15 09:31:05 2016 +0900
Revision:
86:e0fab77e669d
Parent:
48:d6938de02f62
support consumer keys

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cho45 5:65d4e94735b6 1 #include "keyboard.h"
cho45 5:65d4e94735b6 2 #include "keyboard--short-names.h"
cho45 5:65d4e94735b6 3
cho45 47:5bf2ae8cc710 4 class Keymap;
cho45 47:5bf2ae8cc710 5 struct keyfunc_t {
cho45 47:5bf2ae8cc710 6 int8_t row;
cho45 47:5bf2ae8cc710 7 int8_t col;
cho45 47:5bf2ae8cc710 8 void (*func)(Keymap& keymap, bool pressed);
cho45 47:5bf2ae8cc710 9 };
cho45 47:5bf2ae8cc710 10
cho45 47:5bf2ae8cc710 11
cho45 5:65d4e94735b6 12 static const uint8_t ROWS = 8;
cho45 5:65d4e94735b6 13 static const uint8_t COLS = 16;
cho45 47:5bf2ae8cc710 14 static const uint8_t LAYERS = 2;
cho45 47:5bf2ae8cc710 15
cho45 47:5bf2ae8cc710 16 class Keymap {
cho45 47:5bf2ae8cc710 17 static const uint8_t KEYMAP_DEFINITION[LAYERS][ROWS][COLS];
cho45 47:5bf2ae8cc710 18 static const keyfunc_t KEYMAP_FUNCTIONS[];
cho45 47:5bf2ae8cc710 19
cho45 47:5bf2ae8cc710 20 public:
cho45 47:5bf2ae8cc710 21 int8_t layer;
cho45 47:5bf2ae8cc710 22
cho45 47:5bf2ae8cc710 23 Keymap() : layer(0) {
cho45 47:5bf2ae8cc710 24 }
cho45 47:5bf2ae8cc710 25
cho45 48:d6938de02f62 26 static void switch_layer(Keymap& keymap, const bool pressed) {
cho45 47:5bf2ae8cc710 27 if (pressed) {
cho45 47:5bf2ae8cc710 28 keymap.layer++;
cho45 47:5bf2ae8cc710 29 if (keymap.layer > LAYERS) {
cho45 47:5bf2ae8cc710 30 keymap.layer = LAYERS - 1;
cho45 47:5bf2ae8cc710 31 }
cho45 47:5bf2ae8cc710 32 } else {
cho45 47:5bf2ae8cc710 33 keymap.layer--;
cho45 47:5bf2ae8cc710 34 if (keymap.layer < 0) {
cho45 47:5bf2ae8cc710 35 keymap.layer = 0;
cho45 47:5bf2ae8cc710 36 }
cho45 47:5bf2ae8cc710 37 }
cho45 47:5bf2ae8cc710 38 DEBUG_PRINTF_KEYEVENT("LAYER->%d\r\n", keymap.layer);
cho45 47:5bf2ae8cc710 39 }
cho45 47:5bf2ae8cc710 40
cho45 86:e0fab77e669d 41 static void consumer_play_pause(Keymap& keymap, const bool pressed) {
cho45 86:e0fab77e669d 42 if (pressed) {
cho45 86:e0fab77e669d 43 HIDController::pressConsumerKey(0x00cd);
cho45 86:e0fab77e669d 44 } else {
cho45 86:e0fab77e669d 45 HIDController::releaseConsumerKey();
cho45 86:e0fab77e669d 46 }
cho45 86:e0fab77e669d 47 }
cho45 86:e0fab77e669d 48
cho45 48:d6938de02f62 49 void execute(const int row, const int col, const bool pressed) {
cho45 47:5bf2ae8cc710 50 for (int i = 0; ; i++) {
cho45 47:5bf2ae8cc710 51 const keyfunc_t& keyfunc = KEYMAP_FUNCTIONS[i];
cho45 47:5bf2ae8cc710 52 if (keyfunc.row == -1) break;
cho45 47:5bf2ae8cc710 53 if (keyfunc.col == col && keyfunc.row == row) {
cho45 47:5bf2ae8cc710 54 DEBUG_PRINTF_KEYEVENT("& %d:%d\r\n", keyfunc.row, keyfunc.row);
cho45 47:5bf2ae8cc710 55 keyfunc.func(*this, pressed);
cho45 47:5bf2ae8cc710 56 return;
cho45 47:5bf2ae8cc710 57 }
cho45 47:5bf2ae8cc710 58 }
cho45 47:5bf2ae8cc710 59
cho45 47:5bf2ae8cc710 60 if (pressed) {
cho45 47:5bf2ae8cc710 61 uint8_t key = KEYMAP_DEFINITION[layer][row][col];
cho45 47:5bf2ae8cc710 62 if (key) {
cho45 47:5bf2ae8cc710 63 DEBUG_PRINTF_KEYEVENT("D%d %x\r\n", layer, key);
cho45 47:5bf2ae8cc710 64 HIDController::appendReportData(key);
cho45 47:5bf2ae8cc710 65 }
cho45 47:5bf2ae8cc710 66 } else {
cho45 47:5bf2ae8cc710 67 // ensure delete all keys on layers
cho45 47:5bf2ae8cc710 68 for (int i = 0; i < LAYERS; i++) {
cho45 47:5bf2ae8cc710 69 uint8_t key = KEYMAP_DEFINITION[i][row][col];
cho45 47:5bf2ae8cc710 70 DEBUG_PRINTF_KEYEVENT("U%d %x\r\n", layer, key);
cho45 47:5bf2ae8cc710 71 HIDController::deleteReportData(key);
cho45 47:5bf2ae8cc710 72 }
cho45 47:5bf2ae8cc710 73 }
cho45 47:5bf2ae8cc710 74 }
cho45 47:5bf2ae8cc710 75 };
cho45 5:65d4e94735b6 76
cho45 5:65d4e94735b6 77 // unimplemented in hardware is __
cho45 5:65d4e94735b6 78 #define __________ 0
cho45 5:65d4e94735b6 79 // unimplemented in firmware is _undef
cho45 5:65d4e94735b6 80 #define _undef 0
cho45 5:65d4e94735b6 81
cho45 47:5bf2ae8cc710 82 const uint8_t Keymap::KEYMAP_DEFINITION[LAYERS][ROWS][COLS] = {
cho45 47:5bf2ae8cc710 83 // layer 0
cho45 47:5bf2ae8cc710 84 {
cho45 47:5bf2ae8cc710 85 /* { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 } */
cho45 47:5bf2ae8cc710 86 /*0*/{ _esc , _F1 , _F2 , _F3 , _F4 , _F5 , _F6 , __________ , __________ , _F7 , _F8 , _F9 , _F10 , _F11 , _F12 , _undef } ,
cho45 47:5bf2ae8cc710 87 /*1*/{ _esc , _1 , _2 , _3 , _4 , _5 , _6 , _7 , _6 , _7 , _8 , _9 , _0 , _dash , _equal , _backslash } ,
cho45 47:5bf2ae8cc710 88 /*2*/{ _tab , _Q , _W , _E , _R , _T , _Y , __________ , _T , _Y , _U , _I , _O , _P , _bracketL , _grave } ,
cho45 47:5bf2ae8cc710 89 /*3*/{ _ctrlL , _A , _S , _D , _F , _G , _H , __________ , _G , _H , _J , _K , _L , _semicolon , _quote , _bracketR } ,
cho45 47:5bf2ae8cc710 90 /*4*/{ _shiftL , _Z , _X , _C , _V , _B , _N , __________ , _B , _N , _M , _comma , _period , _slash , _shiftR , _bs } ,
cho45 47:5bf2ae8cc710 91 /*5*/{ _altL , _guiL , _space , __________ , __________ , _undef , __________ , __________ , __________ , _arrowU , _space , __________ , _guiR , _altR , _undef , _enter } ,
cho45 47:5bf2ae8cc710 92 /*6*/{ __________ , __________ , __________ , __________ , __________ , __________ , __________ , __________ , _arrowL , _arrowD , _arrowR , __________ , __________ , __________ , __________ , __________ } ,
cho45 47:5bf2ae8cc710 93 /*7*/{ __________ , __________ , __________ , __________ , __________ , __________ , __________ , __________ , __________ , __________ , __________ , __________ , __________ , __________ , __________ , __________ } ,
cho45 47:5bf2ae8cc710 94 },
cho45 47:5bf2ae8cc710 95 // layer 1
cho45 47:5bf2ae8cc710 96 {
cho45 47:5bf2ae8cc710 97 /* { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 } */
cho45 47:5bf2ae8cc710 98 /*0*/{ _esc , _F1 , _F2 , _F3 , _F4 , _F5 , _F6 , __________ , __________ , _F7 , _F8 , _F9 , _F10 , _F11 , _F12 , _undef } ,
cho45 47:5bf2ae8cc710 99 /*1*/{ _esc , _1 , _2 , _3 , _4 , _5 , _6 , _7 , _6 , _7 , _8 , _9 , _0 , _dash , _equal , _backslash } ,
cho45 47:5bf2ae8cc710 100 /*2*/{ _tab , _Q , _W , _E , _R , _T , _Y , __________ , _T , _Y , _U , _I , _O , _P , _arrowU , _del } ,
cho45 47:5bf2ae8cc710 101 /*3*/{ _ctrlL , _A , _S , _D , _F , _G , _H , __________ , _G , _H , _J , _K , _L , _arrowL , _arrowR , _bracketR } ,
cho45 47:5bf2ae8cc710 102 /*4*/{ _shiftL , _Z , _X , _C , _V , _B , _N , __________ , _B , _N , _M , _comma , _period , _arrowD , _shiftR , _bs } ,
cho45 47:5bf2ae8cc710 103 /*5*/{ _altL , _guiL , _space , __________ , __________ , _undef , __________ , __________ , __________ , _arrowU , _space , __________ , _guiR , _altR , _undef , _enter } ,
cho45 47:5bf2ae8cc710 104 /*6*/{ __________ , __________ , __________ , __________ , __________ , __________ , __________ , __________ , _arrowL , _arrowD , _arrowR , __________ , __________ , __________ , __________ , __________ } ,
cho45 47:5bf2ae8cc710 105 /*7*/{ __________ , __________ , __________ , __________ , __________ , __________ , __________ , __________ , __________ , __________ , __________ , __________ , __________ , __________ , __________ , __________ } ,
cho45 47:5bf2ae8cc710 106 },
cho45 47:5bf2ae8cc710 107 };
cho45 47:5bf2ae8cc710 108
cho45 47:5bf2ae8cc710 109 const keyfunc_t Keymap::KEYMAP_FUNCTIONS[] = {
cho45 47:5bf2ae8cc710 110 { 5, 14, &Keymap::switch_layer },
cho45 86:e0fab77e669d 111 { 5, 5, &Keymap::consumer_play_pause },
cho45 47:5bf2ae8cc710 112 { -1, -1, 0 } /* for iteration */
cho45 5:65d4e94735b6 113 };
cho45 5:65d4e94735b6 114
cho45 5:65d4e94735b6 115 #undef __________
cho45 5:65d4e94735b6 116 #undef _undef
cho45 5:65d4e94735b6 117