back up of work during May 2019
Dependencies: microbit
bitboard.cpp@6:f372773ad32f, 2019-06-05 (annotated)
- Committer:
- tht216
- Date:
- Wed Jun 05 15:21:14 2019 +0000
- Branch:
- class_implmentation
- Revision:
- 6:f372773ad32f
TODO:; 1. multi keypresses; 2. integration
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tht216 | 6:f372773ad32f | 1 | #include "bitboard.h" |
tht216 | 6:f372773ad32f | 2 | |
tht216 | 6:f372773ad32f | 3 | bitboard::bitboard(/**MicroBit &_uBit,**/ BLE &_ble, KeyboardService* _kbdServicePtr, const char* _DEVICE_NAME, const char* _SHORT_DEVICE_NAME): /**uBit(_uBit),**/ ble(_ble), kbdServicePtr(_kbdServicePtr), DEVICE_NAME(_DEVICE_NAME), SHORT_DEVICE_NAME(_SHORT_DEVICE_NAME){ |
tht216 | 6:f372773ad32f | 4 | Buffer = 0; //might change data type to allow for multi keypress |
tht216 | 6:f372773ad32f | 5 | } |
tht216 | 6:f372773ad32f | 6 | |
tht216 | 6:f372773ad32f | 7 | bitboard::~bitboard(){ |
tht216 | 6:f372773ad32f | 8 | delete DEVICE_NAME; |
tht216 | 6:f372773ad32f | 9 | delete SHORT_DEVICE_NAME; |
tht216 | 6:f372773ad32f | 10 | } |
tht216 | 6:f372773ad32f | 11 | |
tht216 | 6:f372773ad32f | 12 | void bitboard::init(){ |
tht216 | 6:f372773ad32f | 13 | // uBit.init(); |
tht216 | 6:f372773ad32f | 14 | // uBit.serial.send("uBit init...\r\n"); |
tht216 | 6:f372773ad32f | 15 | // uBit.serial.send("BLE init...\r\n"); |
tht216 | 6:f372773ad32f | 16 | ble.init(); |
tht216 | 6:f372773ad32f | 17 | |
tht216 | 6:f372773ad32f | 18 | ble.gap().onDisconnection(this, &bitboard::onDisconnect); |
tht216 | 6:f372773ad32f | 19 | ble.gap().onConnection(this, &bitboard::onConnect); |
tht216 | 6:f372773ad32f | 20 | // uBit.serial.send("Security init...\r\n"); |
tht216 | 6:f372773ad32f | 21 | initializeSecurity(ble); |
tht216 | 6:f372773ad32f | 22 | |
tht216 | 6:f372773ad32f | 23 | KeyboardService kbdService(ble); |
tht216 | 6:f372773ad32f | 24 | kbdServicePtr = &kbdService; |
tht216 | 6:f372773ad32f | 25 | // uBit.serial.send("HOGP init...\r\n"); |
tht216 | 6:f372773ad32f | 26 | initializeHOGP(ble); |
tht216 | 6:f372773ad32f | 27 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::KEYBOARD); |
tht216 | 6:f372773ad32f | 28 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
tht216 | 6:f372773ad32f | 29 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (uint8_t *)SHORT_DEVICE_NAME, sizeof(SHORT_DEVICE_NAME)); |
tht216 | 6:f372773ad32f | 30 | ble.gap().setDeviceName((const uint8_t *)DEVICE_NAME); |
tht216 | 6:f372773ad32f | 31 | // uBit.serial.send("advertising...\r\n"); |
tht216 | 6:f372773ad32f | 32 | ble.gap().startAdvertising(); |
tht216 | 6:f372773ad32f | 33 | } |
tht216 | 6:f372773ad32f | 34 | |
tht216 | 6:f372773ad32f | 35 | void bitboard::onDisconnect(const Gap::DisconnectionCallbackParams_t *params){ |
tht216 | 6:f372773ad32f | 36 | ble.gap().startAdvertising(); // restart advertising |
tht216 | 6:f372773ad32f | 37 | // uBit.serial.send("Disconnected, advertising...\r\n"); |
tht216 | 6:f372773ad32f | 38 | } |
tht216 | 6:f372773ad32f | 39 | |
tht216 | 6:f372773ad32f | 40 | void bitboard::onConnect(const Gap::ConnectionCallbackParams_t *params){ |
tht216 | 6:f372773ad32f | 41 | // uBit.serial.send("Connected...\r\n"); |
tht216 | 6:f372773ad32f | 42 | } |
tht216 | 6:f372773ad32f | 43 | |
tht216 | 6:f372773ad32f | 44 | void bitboard::onButtonA(MicroBitEvent e){ |
tht216 | 6:f372773ad32f | 45 | // uBit.display.print("A"); |
tht216 | 6:f372773ad32f | 46 | // uBit.serial.send("Button A pressed"); |
tht216 | 6:f372773ad32f | 47 | // uBit.serial.send(e.value); |
tht216 | 6:f372773ad32f | 48 | } |
tht216 | 6:f372773ad32f | 49 | |
tht216 | 6:f372773ad32f | 50 | void bitboard::send_keypress(){ |
tht216 | 6:f372773ad32f | 51 | inputReportData[0] = 0; |
tht216 | 6:f372773ad32f | 52 | inputReportData[2] = Buffer; |
tht216 | 6:f372773ad32f | 53 | kbdServicePtr->send(inputReportData); |
tht216 | 6:f372773ad32f | 54 | kbdServicePtr->send(emptyInputReportData); |
tht216 | 6:f372773ad32f | 55 | wait(0.1); |
tht216 | 6:f372773ad32f | 56 | } |
tht216 | 6:f372773ad32f | 57 | uint8_t bitboard::get_keycode(char c){ |
tht216 | 6:f372773ad32f | 58 | switch (c){ |
tht216 | 6:f372773ad32f | 59 | case 'A': |
tht216 | 6:f372773ad32f | 60 | case 'a': |
tht216 | 6:f372773ad32f | 61 | return 0x04; |
tht216 | 6:f372773ad32f | 62 | case 'B': |
tht216 | 6:f372773ad32f | 63 | case 'b': |
tht216 | 6:f372773ad32f | 64 | return 0x05; |
tht216 | 6:f372773ad32f | 65 | case 'C': |
tht216 | 6:f372773ad32f | 66 | case 'c': |
tht216 | 6:f372773ad32f | 67 | return 0x06; |
tht216 | 6:f372773ad32f | 68 | case 'D': |
tht216 | 6:f372773ad32f | 69 | case 'd': |
tht216 | 6:f372773ad32f | 70 | return 0x07; |
tht216 | 6:f372773ad32f | 71 | case 'E': |
tht216 | 6:f372773ad32f | 72 | case 'e': |
tht216 | 6:f372773ad32f | 73 | return 0x08; |
tht216 | 6:f372773ad32f | 74 | case 'F': |
tht216 | 6:f372773ad32f | 75 | case 'f': |
tht216 | 6:f372773ad32f | 76 | return 0x09; |
tht216 | 6:f372773ad32f | 77 | case 'G': |
tht216 | 6:f372773ad32f | 78 | case 'g': |
tht216 | 6:f372773ad32f | 79 | return 0x0a; |
tht216 | 6:f372773ad32f | 80 | case 'H': |
tht216 | 6:f372773ad32f | 81 | case 'h': |
tht216 | 6:f372773ad32f | 82 | return 0x0b; |
tht216 | 6:f372773ad32f | 83 | case 'I': |
tht216 | 6:f372773ad32f | 84 | case 'i': |
tht216 | 6:f372773ad32f | 85 | return 0x0c; |
tht216 | 6:f372773ad32f | 86 | case 'J': |
tht216 | 6:f372773ad32f | 87 | case 'j': |
tht216 | 6:f372773ad32f | 88 | return 0x0d; |
tht216 | 6:f372773ad32f | 89 | case 'K': |
tht216 | 6:f372773ad32f | 90 | case 'k': |
tht216 | 6:f372773ad32f | 91 | return 0x0e; |
tht216 | 6:f372773ad32f | 92 | case 'L': |
tht216 | 6:f372773ad32f | 93 | case 'l': |
tht216 | 6:f372773ad32f | 94 | return 0x0f; |
tht216 | 6:f372773ad32f | 95 | case 'M': |
tht216 | 6:f372773ad32f | 96 | case 'm': |
tht216 | 6:f372773ad32f | 97 | return 0x10; |
tht216 | 6:f372773ad32f | 98 | case 'N': |
tht216 | 6:f372773ad32f | 99 | case 'n': |
tht216 | 6:f372773ad32f | 100 | return 0x11; |
tht216 | 6:f372773ad32f | 101 | case 'O': |
tht216 | 6:f372773ad32f | 102 | case 'o': |
tht216 | 6:f372773ad32f | 103 | return 0x12; |
tht216 | 6:f372773ad32f | 104 | case 'P': |
tht216 | 6:f372773ad32f | 105 | case 'p': |
tht216 | 6:f372773ad32f | 106 | return 0x13; |
tht216 | 6:f372773ad32f | 107 | case 'Q': |
tht216 | 6:f372773ad32f | 108 | case 'q': |
tht216 | 6:f372773ad32f | 109 | return 0x14; |
tht216 | 6:f372773ad32f | 110 | case 'R': |
tht216 | 6:f372773ad32f | 111 | case 'r': |
tht216 | 6:f372773ad32f | 112 | return 0x15; |
tht216 | 6:f372773ad32f | 113 | case 'S': |
tht216 | 6:f372773ad32f | 114 | case 's': |
tht216 | 6:f372773ad32f | 115 | return 0x16; |
tht216 | 6:f372773ad32f | 116 | case 'T': |
tht216 | 6:f372773ad32f | 117 | case 't': |
tht216 | 6:f372773ad32f | 118 | return 0x17; |
tht216 | 6:f372773ad32f | 119 | case 'U': |
tht216 | 6:f372773ad32f | 120 | case 'u': |
tht216 | 6:f372773ad32f | 121 | return 0x18; |
tht216 | 6:f372773ad32f | 122 | case 'V': |
tht216 | 6:f372773ad32f | 123 | case 'v': |
tht216 | 6:f372773ad32f | 124 | return 0x19; |
tht216 | 6:f372773ad32f | 125 | case 'W': |
tht216 | 6:f372773ad32f | 126 | case 'w': |
tht216 | 6:f372773ad32f | 127 | return 0x1a; |
tht216 | 6:f372773ad32f | 128 | case 'X': |
tht216 | 6:f372773ad32f | 129 | case 'x': |
tht216 | 6:f372773ad32f | 130 | return 0x1b; |
tht216 | 6:f372773ad32f | 131 | case 'Y': |
tht216 | 6:f372773ad32f | 132 | case 'y': |
tht216 | 6:f372773ad32f | 133 | return 0x1c; |
tht216 | 6:f372773ad32f | 134 | case 'Z': |
tht216 | 6:f372773ad32f | 135 | case 'z': |
tht216 | 6:f372773ad32f | 136 | return 0x1d; |
tht216 | 6:f372773ad32f | 137 | case '!': |
tht216 | 6:f372773ad32f | 138 | case '1': |
tht216 | 6:f372773ad32f | 139 | return 0x1e; |
tht216 | 6:f372773ad32f | 140 | case '@': |
tht216 | 6:f372773ad32f | 141 | case '2': |
tht216 | 6:f372773ad32f | 142 | return 0x1f; |
tht216 | 6:f372773ad32f | 143 | case '#': |
tht216 | 6:f372773ad32f | 144 | case '3': |
tht216 | 6:f372773ad32f | 145 | return 0x20; |
tht216 | 6:f372773ad32f | 146 | case '$': |
tht216 | 6:f372773ad32f | 147 | case '4': |
tht216 | 6:f372773ad32f | 148 | return 0x21; |
tht216 | 6:f372773ad32f | 149 | case '%': |
tht216 | 6:f372773ad32f | 150 | case '5': |
tht216 | 6:f372773ad32f | 151 | return 0x22; |
tht216 | 6:f372773ad32f | 152 | case '^': |
tht216 | 6:f372773ad32f | 153 | case '6': |
tht216 | 6:f372773ad32f | 154 | return 0x23; |
tht216 | 6:f372773ad32f | 155 | case '&': |
tht216 | 6:f372773ad32f | 156 | case '7': |
tht216 | 6:f372773ad32f | 157 | return 0x24; |
tht216 | 6:f372773ad32f | 158 | case '*': |
tht216 | 6:f372773ad32f | 159 | case '8': |
tht216 | 6:f372773ad32f | 160 | return 0x25; |
tht216 | 6:f372773ad32f | 161 | case '(': |
tht216 | 6:f372773ad32f | 162 | case '9': |
tht216 | 6:f372773ad32f | 163 | return 0x26; |
tht216 | 6:f372773ad32f | 164 | case ')': |
tht216 | 6:f372773ad32f | 165 | case '0': |
tht216 | 6:f372773ad32f | 166 | return 0x27; |
tht216 | 6:f372773ad32f | 167 | case '\n': // LF |
tht216 | 6:f372773ad32f | 168 | return 0x28; |
tht216 | 6:f372773ad32f | 169 | case '\b': // BS |
tht216 | 6:f372773ad32f | 170 | return 0x2a; |
tht216 | 6:f372773ad32f | 171 | case '\t': // TAB |
tht216 | 6:f372773ad32f | 172 | return 0x2b; |
tht216 | 6:f372773ad32f | 173 | case ' ': |
tht216 | 6:f372773ad32f | 174 | return 0x2c; |
tht216 | 6:f372773ad32f | 175 | case '_': |
tht216 | 6:f372773ad32f | 176 | case '-': |
tht216 | 6:f372773ad32f | 177 | return 0x2d; |
tht216 | 6:f372773ad32f | 178 | case '+': |
tht216 | 6:f372773ad32f | 179 | case '=': |
tht216 | 6:f372773ad32f | 180 | return 0x2e; |
tht216 | 6:f372773ad32f | 181 | case '{': |
tht216 | 6:f372773ad32f | 182 | case '[': |
tht216 | 6:f372773ad32f | 183 | return 0x2f; |
tht216 | 6:f372773ad32f | 184 | case '}': |
tht216 | 6:f372773ad32f | 185 | case ']': |
tht216 | 6:f372773ad32f | 186 | return 0x30; |
tht216 | 6:f372773ad32f | 187 | case '|': |
tht216 | 6:f372773ad32f | 188 | case '\\': |
tht216 | 6:f372773ad32f | 189 | return 0x31; |
tht216 | 6:f372773ad32f | 190 | case ':': |
tht216 | 6:f372773ad32f | 191 | case ';': |
tht216 | 6:f372773ad32f | 192 | return 0x33; |
tht216 | 6:f372773ad32f | 193 | case '"': |
tht216 | 6:f372773ad32f | 194 | case '\'': |
tht216 | 6:f372773ad32f | 195 | return 0x34; |
tht216 | 6:f372773ad32f | 196 | case '~': |
tht216 | 6:f372773ad32f | 197 | case '`': |
tht216 | 6:f372773ad32f | 198 | return 0x35; |
tht216 | 6:f372773ad32f | 199 | case '<': |
tht216 | 6:f372773ad32f | 200 | case ',': |
tht216 | 6:f372773ad32f | 201 | return 0x36; |
tht216 | 6:f372773ad32f | 202 | case '>': |
tht216 | 6:f372773ad32f | 203 | case '.': |
tht216 | 6:f372773ad32f | 204 | return 0x37; |
tht216 | 6:f372773ad32f | 205 | case '?': |
tht216 | 6:f372773ad32f | 206 | case '/': |
tht216 | 6:f372773ad32f | 207 | return 0x38; |
tht216 | 6:f372773ad32f | 208 | default: |
tht216 | 6:f372773ad32f | 209 | return 0; |
tht216 | 6:f372773ad32f | 210 | } |
tht216 | 6:f372773ad32f | 211 | } |