![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
TEAMUSB_SLAVE
Fork of USBHostKeyboard_HelloWorld by
main.cpp@13:b05908f05e81, 2015-04-16 (annotated)
- Committer:
- gabriel4211
- Date:
- Thu Apr 16 14:43:44 2015 +0000
- Revision:
- 13:b05908f05e81
- Parent:
- 12:d32975c44c98
- Child:
- 14:c820ae094728
tidied up the code
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 | |
samux | 0:88d52e197201 | 11 | DigitalOut led(LED1); |
armdran | 9:8bcd70b26084 | 12 | SPISlave device(D11, D12, D13, D10); |
armdran | 11:e12aae7de530 | 13 | DigitalOut ir(D9); |
gabriel4211 | 12:d32975c44c98 | 14 | Queue<keyPress_t, 16> _keyQueue; |
gabriel4211 | 12:d32975c44c98 | 15 | MemoryPool<keyPress_t, 16> mpool; |
gabriel4211 | 13:b05908f05e81 | 16 | |
armdran | 11:e12aae7de530 | 17 | uint8_t spi_reply_blocking(uint8_t reply = 0x00, bool sendIr = 0) { |
armdran | 11:e12aae7de530 | 18 | device.reply(reply); |
gabriel4211 | 12:d32975c44c98 | 19 | if(sendIr == 1) { |
gabriel4211 | 12:d32975c44c98 | 20 | ir = 1; ir = 0; |
gabriel4211 | 12:d32975c44c98 | 21 | } |
gabriel4211 | 13:b05908f05e81 | 22 | while(!device.receive()) { |
gabriel4211 | 13:b05908f05e81 | 23 | // busy |
gabriel4211 | 12:d32975c44c98 | 24 | } |
armdran | 11:e12aae7de530 | 25 | uint8_t instruction = device.read(); |
armdran | 11:e12aae7de530 | 26 | return instruction; |
armdran | 11:e12aae7de530 | 27 | } |
armdran | 11:e12aae7de530 | 28 | |
samux | 0:88d52e197201 | 29 | |
armdran | 8:f8122ca2f58f | 30 | void onKeyMod(uint8_t key, uint8_t modifier) { |
gabriel4211 | 12:d32975c44c98 | 31 | keyPress_t *keypress = mpool.alloc(); |
gabriel4211 | 12:d32975c44c98 | 32 | keypress->key = key; |
gabriel4211 | 12:d32975c44c98 | 33 | keypress->modifier = modifier; |
gabriel4211 | 12:d32975c44c98 | 34 | _keyQueue.put(keypress); |
armdran | 8:f8122ca2f58f | 35 | } |
armdran | 8:f8122ca2f58f | 36 | |
gabriel4211 | 12:d32975c44c98 | 37 | void spi_task(void const *) { |
gabriel4211 | 12:d32975c44c98 | 38 | while(1){ |
gabriel4211 | 12:d32975c44c98 | 39 | osEvent evt = _keyQueue.get(); |
gabriel4211 | 12:d32975c44c98 | 40 | if (evt.status == osEventMessage) { |
gabriel4211 | 12:d32975c44c98 | 41 | keyPress_t *keypress = (keyPress_t*)evt.value.p; |
gabriel4211 | 12:d32975c44c98 | 42 | uint8_t key = keypress->key; |
gabriel4211 | 12:d32975c44c98 | 43 | uint8_t modifier = keypress->modifier; |
gabriel4211 | 12:d32975c44c98 | 44 | |
gabriel4211 | 13:b05908f05e81 | 45 | printf(" sending key %x and modifier %x \r\n", key, modifier); |
gabriel4211 | 12:d32975c44c98 | 46 | uint8_t instruction = spi_reply_blocking(key, 1); |
gabriel4211 | 12:d32975c44c98 | 47 | |
gabriel4211 | 12:d32975c44c98 | 48 | while(instruction != 0xFE) { |
gabriel4211 | 12:d32975c44c98 | 49 | printf(" out of sync.\r\n"); |
gabriel4211 | 12:d32975c44c98 | 50 | instruction = spi_reply_blocking(key); |
gabriel4211 | 12:d32975c44c98 | 51 | } |
gabriel4211 | 12:d32975c44c98 | 52 | |
gabriel4211 | 12:d32975c44c98 | 53 | uint8_t ack_key = spi_reply_blocking(modifier); |
gabriel4211 | 12:d32975c44c98 | 54 | |
gabriel4211 | 12:d32975c44c98 | 55 | uint8_t ack_keychar = spi_reply_blocking(); |
gabriel4211 | 12:d32975c44c98 | 56 | |
gabriel4211 | 12:d32975c44c98 | 57 | if(ack_key != key) |
gabriel4211 | 12:d32975c44c98 | 58 | printf(" key ack failed (is %x, should be %x)!!!\r\n", ack_key, key); |
gabriel4211 | 12:d32975c44c98 | 59 | else |
gabriel4211 | 12:d32975c44c98 | 60 | printf(" key ack ok\r\n"); |
gabriel4211 | 13:b05908f05e81 | 61 | |
gabriel4211 | 12:d32975c44c98 | 62 | mpool.free(keypress); |
gabriel4211 | 12:d32975c44c98 | 63 | |
gabriel4211 | 13:b05908f05e81 | 64 | } |
gabriel4211 | 12:d32975c44c98 | 65 | } |
gabriel4211 | 12:d32975c44c98 | 66 | } |
armdran | 8:f8122ca2f58f | 67 | |
samux | 0:88d52e197201 | 68 | void keyboard_task(void const *) { |
samux | 0:88d52e197201 | 69 | |
samux | 0:88d52e197201 | 70 | USBHostKeyboard keyboard; |
samux | 0:88d52e197201 | 71 | |
samux | 0:88d52e197201 | 72 | while(1) { |
armdran | 8:f8122ca2f58f | 73 | |
armdran | 8:f8122ca2f58f | 74 | printf("trying to connect\r\n"); |
armdran | 8:f8122ca2f58f | 75 | |
samux | 0:88d52e197201 | 76 | // try to connect a USB keyboard |
armdran | 8:f8122ca2f58f | 77 | while(!keyboard.connect()) { |
samux | 0:88d52e197201 | 78 | Thread::wait(500); |
armdran | 8:f8122ca2f58f | 79 | } |
armdran | 8:f8122ca2f58f | 80 | printf("connected\r\n"); |
samux | 0:88d52e197201 | 81 | // when connected, attach handler called on keyboard event |
armdran | 8:f8122ca2f58f | 82 | keyboard.attach(onKeyMod); |
armdran | 8:f8122ca2f58f | 83 | printf("eventhandler attached\r\n"); |
samux | 0:88d52e197201 | 84 | // wait until the keyboard is disconnected |
armdran | 8:f8122ca2f58f | 85 | while(keyboard.connected()) { |
armdran | 8:f8122ca2f58f | 86 | USBHost::poll(); |
armdran | 8:f8122ca2f58f | 87 | } |
armdran | 8:f8122ca2f58f | 88 | printf("disconnected\r\n"); |
samux | 0:88d52e197201 | 89 | } |
samux | 0:88d52e197201 | 90 | } |
samux | 0:88d52e197201 | 91 | |
samux | 0:88d52e197201 | 92 | int main() { |
gabriel4211 | 12:d32975c44c98 | 93 | ir = 0; |
samux | 0:88d52e197201 | 94 | Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 256 * 4); |
gabriel4211 | 12:d32975c44c98 | 95 | Thread spiTask(spi_task, NULL, osPriorityNormal, 256 * 4); |
armdran | 9:8bcd70b26084 | 96 | device.frequency(1000000); |
armdran | 11:e12aae7de530 | 97 | device.format(8, 1); |
armdran | 11:e12aae7de530 | 98 | while(1) {} |
armdran | 9:8bcd70b26084 | 99 | |
samux | 0:88d52e197201 | 100 | } |