![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
TEAMUSB_SLAVE
Fork of USBHostKeyboard_HelloWorld by
main.cpp@12:d32975c44c98, 2015-04-16 (annotated)
- Committer:
- gabriel4211
- Date:
- Thu Apr 16 13:44:57 2015 +0000
- Revision:
- 12:d32975c44c98
- Parent:
- 11:e12aae7de530
- Child:
- 13:b05908f05e81
working (and messy) state;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 0:88d52e197201 | 1 | #include "mbed.h" |
samux | 0:88d52e197201 | 2 | #include "USBHostKeyboard.h" |
wlangenkamp | 7:be82ac3c4821 | 3 | #include "rtos.h" |
gabriel4211 | 12:d32975c44c98 | 4 | //#include <queue> |
gabriel4211 | 12:d32975c44c98 | 5 | |
gabriel4211 | 12:d32975c44c98 | 6 | typedef struct { |
gabriel4211 | 12:d32975c44c98 | 7 | uint8_t key; |
gabriel4211 | 12:d32975c44c98 | 8 | uint8_t modifier; |
gabriel4211 | 12:d32975c44c98 | 9 | } keyPress_t; |
gabriel4211 | 12:d32975c44c98 | 10 | |
gabriel4211 | 12:d32975c44c98 | 11 | |
gabriel4211 | 12:d32975c44c98 | 12 | static uint8_t keymap[57] = { |
gabriel4211 | 12:d32975c44c98 | 13 | 0, 0, 0, 0, 'a', 'b' /*0x05*/, |
gabriel4211 | 12:d32975c44c98 | 14 | 'c', 'd', 'e', 'f', 'g' /*0x0a*/, |
gabriel4211 | 12:d32975c44c98 | 15 | 'h', 'i', 'j', 'k', 'l'/*0x0f*/, |
gabriel4211 | 12:d32975c44c98 | 16 | 'm', 'n', 'o', 'p', 'q'/*0x14*/, |
gabriel4211 | 12:d32975c44c98 | 17 | 'r', 's', 't', 'u', 'v'/*0x19*/, |
gabriel4211 | 12:d32975c44c98 | 18 | 'w', 'x', 'y', 'z', '1'/*0x1E*/, |
gabriel4211 | 12:d32975c44c98 | 19 | '2', '3', '4', '5', '6'/*0x23*/, |
gabriel4211 | 12:d32975c44c98 | 20 | '7', '8', '9', '0', 0x0A /*enter*/, /*0x28*/ |
gabriel4211 | 12:d32975c44c98 | 21 | 0x1B /*escape*/, 0x08 /*backspace*/, 0x09/*tab*/, 0x20/*space*/, '-', /*0x2d*/ |
gabriel4211 | 12:d32975c44c98 | 22 | '=', '[', ']', '\\', '#', /*0x32*/ |
gabriel4211 | 12:d32975c44c98 | 23 | ';', '\'', 0, ',', '.', /*0x37*/ |
gabriel4211 | 12:d32975c44c98 | 24 | '/'}; |
samux | 0:88d52e197201 | 25 | |
samux | 0:88d52e197201 | 26 | DigitalOut led(LED1); |
armdran | 9:8bcd70b26084 | 27 | SPISlave device(D11, D12, D13, D10); |
armdran | 11:e12aae7de530 | 28 | DigitalOut ir(D9); |
gabriel4211 | 12:d32975c44c98 | 29 | //std::queue<uint8_t> _keyQueue; |
gabriel4211 | 12:d32975c44c98 | 30 | Queue<keyPress_t, 16> _keyQueue; |
gabriel4211 | 12:d32975c44c98 | 31 | MemoryPool<keyPress_t, 16> mpool; |
gabriel4211 | 12:d32975c44c98 | 32 | //Mutex _keyMutex; |
gabriel4211 | 12:d32975c44c98 | 33 | int bla = 0; |
armdran | 11:e12aae7de530 | 34 | uint8_t spi_reply_blocking(uint8_t reply = 0x00, bool sendIr = 0) { |
armdran | 11:e12aae7de530 | 35 | device.reply(reply); |
gabriel4211 | 12:d32975c44c98 | 36 | bla= 0; |
gabriel4211 | 12:d32975c44c98 | 37 | if(sendIr == 1) { |
gabriel4211 | 12:d32975c44c98 | 38 | ir = 1; ir = 0; |
gabriel4211 | 12:d32975c44c98 | 39 | } |
gabriel4211 | 12:d32975c44c98 | 40 | while(!device.receive()) { |
gabriel4211 | 12:d32975c44c98 | 41 | /*bla++; |
gabriel4211 | 12:d32975c44c98 | 42 | if(bla % 100000 == 0){ |
gabriel4211 | 12:d32975c44c98 | 43 | printf("stuck... "); |
gabriel4211 | 12:d32975c44c98 | 44 | }*/ |
gabriel4211 | 12:d32975c44c98 | 45 | } |
armdran | 11:e12aae7de530 | 46 | uint8_t instruction = device.read(); |
armdran | 11:e12aae7de530 | 47 | //printf(" send: %x received: %x\r\n", reply, instruction); |
armdran | 11:e12aae7de530 | 48 | return instruction; |
armdran | 11:e12aae7de530 | 49 | } |
armdran | 11:e12aae7de530 | 50 | |
samux | 0:88d52e197201 | 51 | |
armdran | 8:f8122ca2f58f | 52 | void onKeyMod(uint8_t key, uint8_t modifier) { |
gabriel4211 | 12:d32975c44c98 | 53 | //printf("got key: %x modifier: %x.\r\n", key, modifier); |
gabriel4211 | 12:d32975c44c98 | 54 | //_keyMutex.lock(); |
gabriel4211 | 12:d32975c44c98 | 55 | /* |
gabriel4211 | 12:d32975c44c98 | 56 | _keyQueue.push(key); |
gabriel4211 | 12:d32975c44c98 | 57 | _keyQueue.push(modifier); |
gabriel4211 | 12:d32975c44c98 | 58 | */ |
gabriel4211 | 12:d32975c44c98 | 59 | |
gabriel4211 | 12:d32975c44c98 | 60 | keyPress_t *keypress = mpool.alloc(); |
gabriel4211 | 12:d32975c44c98 | 61 | keypress->key = key; |
gabriel4211 | 12:d32975c44c98 | 62 | keypress->modifier = modifier; |
gabriel4211 | 12:d32975c44c98 | 63 | _keyQueue.put(keypress); |
gabriel4211 | 12:d32975c44c98 | 64 | |
gabriel4211 | 12:d32975c44c98 | 65 | //_keyMutex.unlock(); |
gabriel4211 | 12:d32975c44c98 | 66 | /* |
gabriel4211 | 12:d32975c44c98 | 67 | ir = 1; ir = 0; |
gabriel4211 | 12:d32975c44c98 | 68 | uint8_t sync = 0; |
gabriel4211 | 12:d32975c44c98 | 69 | do{ |
gabriel4211 | 12:d32975c44c98 | 70 | sync = spi_reply_blocking(0xFD); |
gabriel4211 | 12:d32975c44c98 | 71 | }while(sync != 0xFD); |
gabriel4211 | 12:d32975c44c98 | 72 | */ |
gabriel4211 | 12:d32975c44c98 | 73 | /* |
armdran | 11:e12aae7de530 | 74 | uint8_t instruction = spi_reply_blocking(key, 1); |
gabriel4211 | 12:d32975c44c98 | 75 | |
armdran | 11:e12aae7de530 | 76 | while(instruction != 0xFE) { |
armdran | 11:e12aae7de530 | 77 | printf(" out of sync.\r\n"); |
gabriel4211 | 12:d32975c44c98 | 78 | instruction = spi_reply_blocking(key); |
gabriel4211 | 12:d32975c44c98 | 79 | //return; |
armdran | 11:e12aae7de530 | 80 | } |
armdran | 11:e12aae7de530 | 81 | |
armdran | 11:e12aae7de530 | 82 | //printf(" got instruction FE. key sent\r\n"); |
armdran | 11:e12aae7de530 | 83 | uint8_t ack_key = spi_reply_blocking(modifier); |
gabriel4211 | 12:d32975c44c98 | 84 | |
gabriel4211 | 12:d32975c44c98 | 85 | |
gabriel4211 | 12:d32975c44c98 | 86 | |
gabriel4211 | 12:d32975c44c98 | 87 | //printf(" mod sent.\r\n"); |
gabriel4211 | 12:d32975c44c98 | 88 | uint8_t ack_modifier = spi_reply_blocking(); |
gabriel4211 | 12:d32975c44c98 | 89 | |
armdran | 11:e12aae7de530 | 90 | if(ack_key != key) |
armdran | 11:e12aae7de530 | 91 | printf(" key ack failed (is %x, should be %x)!!!\r\n", ack_key, key); |
armdran | 11:e12aae7de530 | 92 | else |
armdran | 11:e12aae7de530 | 93 | printf(" key ack ok\r\n"); |
gabriel4211 | 12:d32975c44c98 | 94 | |
armdran | 11:e12aae7de530 | 95 | if(ack_modifier != modifier) |
armdran | 11:e12aae7de530 | 96 | printf(" mod ack failed(is %x, should be %x)!!!\r\n", ack_modifier, modifier); |
armdran | 11:e12aae7de530 | 97 | else |
armdran | 11:e12aae7de530 | 98 | printf(" mod ack ok\r\n"); |
gabriel4211 | 12:d32975c44c98 | 99 | */ |
armdran | 8:f8122ca2f58f | 100 | } |
armdran | 8:f8122ca2f58f | 101 | |
gabriel4211 | 12:d32975c44c98 | 102 | void spi_task(void const *) { |
gabriel4211 | 12:d32975c44c98 | 103 | while(1){ |
gabriel4211 | 12:d32975c44c98 | 104 | //_keyMutex.lock(); |
gabriel4211 | 12:d32975c44c98 | 105 | osEvent evt = _keyQueue.get(); |
gabriel4211 | 12:d32975c44c98 | 106 | if (evt.status == osEventMessage) { |
gabriel4211 | 12:d32975c44c98 | 107 | keyPress_t *keypress = (keyPress_t*)evt.value.p; |
gabriel4211 | 12:d32975c44c98 | 108 | /* |
gabriel4211 | 12:d32975c44c98 | 109 | uint8_t key = _keyQueue.front(); |
gabriel4211 | 12:d32975c44c98 | 110 | _keyQueue.pop(); |
gabriel4211 | 12:d32975c44c98 | 111 | uint8_t modifier = _keyQueue.front(); |
gabriel4211 | 12:d32975c44c98 | 112 | _keyQueue.pop(); |
gabriel4211 | 12:d32975c44c98 | 113 | */ |
gabriel4211 | 12:d32975c44c98 | 114 | uint8_t key = keypress->key; |
gabriel4211 | 12:d32975c44c98 | 115 | uint8_t modifier = keypress->modifier; |
gabriel4211 | 12:d32975c44c98 | 116 | |
gabriel4211 | 12:d32975c44c98 | 117 | printf(" sending key %x and modifier %x %c \r\n", key, modifier, keymap[key]); |
gabriel4211 | 12:d32975c44c98 | 118 | uint8_t instruction = spi_reply_blocking(key, 1); |
gabriel4211 | 12:d32975c44c98 | 119 | |
gabriel4211 | 12:d32975c44c98 | 120 | while(instruction != 0xFE) { |
gabriel4211 | 12:d32975c44c98 | 121 | printf(" out of sync.\r\n"); |
gabriel4211 | 12:d32975c44c98 | 122 | instruction = spi_reply_blocking(key); |
gabriel4211 | 12:d32975c44c98 | 123 | //return; |
gabriel4211 | 12:d32975c44c98 | 124 | } |
gabriel4211 | 12:d32975c44c98 | 125 | |
gabriel4211 | 12:d32975c44c98 | 126 | uint8_t ack_key = spi_reply_blocking(modifier); |
gabriel4211 | 12:d32975c44c98 | 127 | |
gabriel4211 | 12:d32975c44c98 | 128 | uint8_t ack_modifier = spi_reply_blocking(keymap[key]); |
gabriel4211 | 12:d32975c44c98 | 129 | |
gabriel4211 | 12:d32975c44c98 | 130 | uint8_t ack_keychar = spi_reply_blocking(); |
gabriel4211 | 12:d32975c44c98 | 131 | |
gabriel4211 | 12:d32975c44c98 | 132 | if(ack_key != key) |
gabriel4211 | 12:d32975c44c98 | 133 | printf(" key ack failed (is %x, should be %x)!!!\r\n", ack_key, key); |
gabriel4211 | 12:d32975c44c98 | 134 | else |
gabriel4211 | 12:d32975c44c98 | 135 | printf(" key ack ok\r\n"); |
gabriel4211 | 12:d32975c44c98 | 136 | |
gabriel4211 | 12:d32975c44c98 | 137 | if(ack_modifier != modifier) |
gabriel4211 | 12:d32975c44c98 | 138 | printf(" mod ack failed(is %x, should be %x)!!!\r\n", ack_modifier, modifier); |
gabriel4211 | 12:d32975c44c98 | 139 | else |
gabriel4211 | 12:d32975c44c98 | 140 | printf(" mod ack ok\r\n"); |
gabriel4211 | 12:d32975c44c98 | 141 | |
gabriel4211 | 12:d32975c44c98 | 142 | if(ack_modifier != modifier) |
gabriel4211 | 12:d32975c44c98 | 143 | printf(" keychar failed(is %x, should be %x)!!!\r\n", ack_keychar, keymap[key]); |
gabriel4211 | 12:d32975c44c98 | 144 | else |
gabriel4211 | 12:d32975c44c98 | 145 | printf(" keychar ack ok\r\n"); |
gabriel4211 | 12:d32975c44c98 | 146 | |
gabriel4211 | 12:d32975c44c98 | 147 | mpool.free(keypress); |
gabriel4211 | 12:d32975c44c98 | 148 | |
gabriel4211 | 12:d32975c44c98 | 149 | } |
gabriel4211 | 12:d32975c44c98 | 150 | //wait_ms(1); |
gabriel4211 | 12:d32975c44c98 | 151 | //_keyMutex.unlock(); |
gabriel4211 | 12:d32975c44c98 | 152 | |
gabriel4211 | 12:d32975c44c98 | 153 | } |
gabriel4211 | 12:d32975c44c98 | 154 | } |
armdran | 8:f8122ca2f58f | 155 | |
samux | 0:88d52e197201 | 156 | void keyboard_task(void const *) { |
samux | 0:88d52e197201 | 157 | |
samux | 0:88d52e197201 | 158 | USBHostKeyboard keyboard; |
samux | 0:88d52e197201 | 159 | |
samux | 0:88d52e197201 | 160 | while(1) { |
armdran | 8:f8122ca2f58f | 161 | |
armdran | 8:f8122ca2f58f | 162 | printf("trying to connect\r\n"); |
armdran | 8:f8122ca2f58f | 163 | |
samux | 0:88d52e197201 | 164 | // try to connect a USB keyboard |
armdran | 8:f8122ca2f58f | 165 | while(!keyboard.connect()) { |
samux | 0:88d52e197201 | 166 | Thread::wait(500); |
armdran | 8:f8122ca2f58f | 167 | } |
armdran | 8:f8122ca2f58f | 168 | |
armdran | 8:f8122ca2f58f | 169 | printf("connected\r\n"); |
armdran | 8:f8122ca2f58f | 170 | |
samux | 0:88d52e197201 | 171 | // when connected, attach handler called on keyboard event |
armdran | 8:f8122ca2f58f | 172 | keyboard.attach(onKeyMod); |
armdran | 8:f8122ca2f58f | 173 | |
armdran | 8:f8122ca2f58f | 174 | printf("eventhandler attached\r\n"); |
samux | 0:88d52e197201 | 175 | |
samux | 0:88d52e197201 | 176 | // wait until the keyboard is disconnected |
armdran | 8:f8122ca2f58f | 177 | while(keyboard.connected()) { |
armdran | 8:f8122ca2f58f | 178 | USBHost::poll(); |
gabriel4211 | 12:d32975c44c98 | 179 | //wait_ms(1); |
armdran | 8:f8122ca2f58f | 180 | } |
armdran | 8:f8122ca2f58f | 181 | |
armdran | 8:f8122ca2f58f | 182 | printf("disconnected\r\n"); |
armdran | 8:f8122ca2f58f | 183 | |
samux | 0:88d52e197201 | 184 | } |
samux | 0:88d52e197201 | 185 | } |
samux | 0:88d52e197201 | 186 | |
samux | 0:88d52e197201 | 187 | int main() { |
gabriel4211 | 12:d32975c44c98 | 188 | ir = 0; |
samux | 0:88d52e197201 | 189 | Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 256 * 4); |
gabriel4211 | 12:d32975c44c98 | 190 | Thread spiTask(spi_task, NULL, osPriorityNormal, 256 * 4); |
armdran | 9:8bcd70b26084 | 191 | device.frequency(1000000); |
armdran | 11:e12aae7de530 | 192 | device.format(8, 1); |
armdran | 11:e12aae7de530 | 193 | while(1) {} |
armdran | 9:8bcd70b26084 | 194 | |
samux | 0:88d52e197201 | 195 | } |