Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of USBHostKeyboard_HelloWorld by
main.cpp@14:c820ae094728, 2015-05-04 (annotated)
- Committer:
- armdran
- Date:
- Mon May 04 15:01:26 2015 +0000
- Revision:
- 14:c820ae094728
- Parent:
- 13:b05908f05e81
- platform & pin switch; - queue size changed; - auto restart on spi failure implemented
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" |
armdran | 14:c820ae094728 | 3 | #include "time.h" |
armdran | 14:c820ae094728 | 4 | |
armdran | 14:c820ae094728 | 5 | extern "C" void mbed_reset(); |
armdran | 14:c820ae094728 | 6 | |
armdran | 14:c820ae094728 | 7 | clock_t spi_begin = 0; |
armdran | 14:c820ae094728 | 8 | clock_t spi_end = 0; |
gabriel4211 | 12:d32975c44c98 | 9 | |
gabriel4211 | 12:d32975c44c98 | 10 | typedef struct { |
gabriel4211 | 12:d32975c44c98 | 11 | uint8_t key; |
gabriel4211 | 12:d32975c44c98 | 12 | uint8_t modifier; |
gabriel4211 | 12:d32975c44c98 | 13 | } keyPress_t; |
gabriel4211 | 12:d32975c44c98 | 14 | |
samux | 0:88d52e197201 | 15 | DigitalOut led(LED1); |
armdran | 14:c820ae094728 | 16 | SPISlave device(p5, p6, p7, p8); |
armdran | 14:c820ae094728 | 17 | DigitalOut ir(p21); |
armdran | 14:c820ae094728 | 18 | Queue<keyPress_t, 1024> _keyQueue; |
armdran | 14:c820ae094728 | 19 | MemoryPool<keyPress_t, 1024> mpool; |
gabriel4211 | 13:b05908f05e81 | 20 | |
armdran | 11:e12aae7de530 | 21 | uint8_t spi_reply_blocking(uint8_t reply = 0x00, bool sendIr = 0) { |
armdran | 11:e12aae7de530 | 22 | device.reply(reply); |
gabriel4211 | 12:d32975c44c98 | 23 | if(sendIr == 1) { |
gabriel4211 | 12:d32975c44c98 | 24 | ir = 1; ir = 0; |
gabriel4211 | 12:d32975c44c98 | 25 | } |
gabriel4211 | 13:b05908f05e81 | 26 | while(!device.receive()) { |
gabriel4211 | 13:b05908f05e81 | 27 | // busy |
gabriel4211 | 12:d32975c44c98 | 28 | } |
armdran | 11:e12aae7de530 | 29 | uint8_t instruction = device.read(); |
armdran | 11:e12aae7de530 | 30 | return instruction; |
armdran | 11:e12aae7de530 | 31 | } |
armdran | 11:e12aae7de530 | 32 | |
samux | 0:88d52e197201 | 33 | |
armdran | 8:f8122ca2f58f | 34 | void onKeyMod(uint8_t key, uint8_t modifier) { |
gabriel4211 | 12:d32975c44c98 | 35 | keyPress_t *keypress = mpool.alloc(); |
gabriel4211 | 12:d32975c44c98 | 36 | keypress->key = key; |
armdran | 14:c820ae094728 | 37 | keypress->modifier = modifier; |
gabriel4211 | 12:d32975c44c98 | 38 | _keyQueue.put(keypress); |
armdran | 8:f8122ca2f58f | 39 | } |
armdran | 8:f8122ca2f58f | 40 | |
gabriel4211 | 12:d32975c44c98 | 41 | void spi_task(void const *) { |
armdran | 14:c820ae094728 | 42 | |
gabriel4211 | 12:d32975c44c98 | 43 | while(1){ |
gabriel4211 | 12:d32975c44c98 | 44 | osEvent evt = _keyQueue.get(); |
gabriel4211 | 12:d32975c44c98 | 45 | if (evt.status == osEventMessage) { |
armdran | 14:c820ae094728 | 46 | |
armdran | 14:c820ae094728 | 47 | spi_begin = clock(); |
armdran | 14:c820ae094728 | 48 | |
gabriel4211 | 12:d32975c44c98 | 49 | keyPress_t *keypress = (keyPress_t*)evt.value.p; |
gabriel4211 | 12:d32975c44c98 | 50 | uint8_t key = keypress->key; |
gabriel4211 | 12:d32975c44c98 | 51 | uint8_t modifier = keypress->modifier; |
gabriel4211 | 12:d32975c44c98 | 52 | |
gabriel4211 | 13:b05908f05e81 | 53 | printf(" sending key %x and modifier %x \r\n", key, modifier); |
gabriel4211 | 12:d32975c44c98 | 54 | uint8_t instruction = spi_reply_blocking(key, 1); |
gabriel4211 | 12:d32975c44c98 | 55 | |
gabriel4211 | 12:d32975c44c98 | 56 | while(instruction != 0xFE) { |
gabriel4211 | 12:d32975c44c98 | 57 | printf(" out of sync.\r\n"); |
gabriel4211 | 12:d32975c44c98 | 58 | instruction = spi_reply_blocking(key); |
gabriel4211 | 12:d32975c44c98 | 59 | } |
gabriel4211 | 12:d32975c44c98 | 60 | |
gabriel4211 | 12:d32975c44c98 | 61 | uint8_t ack_key = spi_reply_blocking(modifier); |
gabriel4211 | 12:d32975c44c98 | 62 | |
gabriel4211 | 12:d32975c44c98 | 63 | uint8_t ack_keychar = spi_reply_blocking(); |
gabriel4211 | 12:d32975c44c98 | 64 | |
gabriel4211 | 12:d32975c44c98 | 65 | if(ack_key != key) |
gabriel4211 | 12:d32975c44c98 | 66 | printf(" key ack failed (is %x, should be %x)!!!\r\n", ack_key, key); |
gabriel4211 | 12:d32975c44c98 | 67 | else |
gabriel4211 | 12:d32975c44c98 | 68 | printf(" key ack ok\r\n"); |
gabriel4211 | 13:b05908f05e81 | 69 | |
gabriel4211 | 12:d32975c44c98 | 70 | mpool.free(keypress); |
armdran | 14:c820ae094728 | 71 | |
armdran | 14:c820ae094728 | 72 | spi_end = clock(); |
armdran | 14:c820ae094728 | 73 | |
gabriel4211 | 13:b05908f05e81 | 74 | } |
gabriel4211 | 12:d32975c44c98 | 75 | } |
gabriel4211 | 12:d32975c44c98 | 76 | } |
armdran | 8:f8122ca2f58f | 77 | |
samux | 0:88d52e197201 | 78 | void keyboard_task(void const *) { |
samux | 0:88d52e197201 | 79 | |
samux | 0:88d52e197201 | 80 | USBHostKeyboard keyboard; |
samux | 0:88d52e197201 | 81 | |
samux | 0:88d52e197201 | 82 | while(1) { |
armdran | 8:f8122ca2f58f | 83 | |
armdran | 8:f8122ca2f58f | 84 | printf("trying to connect\r\n"); |
armdran | 8:f8122ca2f58f | 85 | |
samux | 0:88d52e197201 | 86 | // try to connect a USB keyboard |
armdran | 8:f8122ca2f58f | 87 | while(!keyboard.connect()) { |
samux | 0:88d52e197201 | 88 | Thread::wait(500); |
armdran | 8:f8122ca2f58f | 89 | } |
armdran | 8:f8122ca2f58f | 90 | printf("connected\r\n"); |
samux | 0:88d52e197201 | 91 | // when connected, attach handler called on keyboard event |
armdran | 8:f8122ca2f58f | 92 | keyboard.attach(onKeyMod); |
armdran | 8:f8122ca2f58f | 93 | printf("eventhandler attached\r\n"); |
samux | 0:88d52e197201 | 94 | // wait until the keyboard is disconnected |
armdran | 8:f8122ca2f58f | 95 | while(keyboard.connected()) { |
armdran | 14:c820ae094728 | 96 | //USBHost::poll(); |
armdran | 14:c820ae094728 | 97 | Thread::wait(500); |
armdran | 8:f8122ca2f58f | 98 | } |
armdran | 8:f8122ca2f58f | 99 | printf("disconnected\r\n"); |
samux | 0:88d52e197201 | 100 | } |
samux | 0:88d52e197201 | 101 | } |
samux | 0:88d52e197201 | 102 | |
armdran | 14:c820ae094728 | 103 | void alive_task(void const *) { |
armdran | 14:c820ae094728 | 104 | while(1) { |
armdran | 14:c820ae094728 | 105 | if(spi_begin > spi_end) { |
armdran | 14:c820ae094728 | 106 | float diff = ((float)clock() - (float)spi_begin) / CLOCKS_PER_SEC; |
armdran | 14:c820ae094728 | 107 | if(diff > .5) { |
armdran | 14:c820ae094728 | 108 | mbed_reset(); |
armdran | 14:c820ae094728 | 109 | } |
armdran | 14:c820ae094728 | 110 | } |
armdran | 14:c820ae094728 | 111 | Thread::wait(2000); |
armdran | 14:c820ae094728 | 112 | } |
armdran | 14:c820ae094728 | 113 | } |
armdran | 14:c820ae094728 | 114 | |
samux | 0:88d52e197201 | 115 | int main() { |
gabriel4211 | 12:d32975c44c98 | 116 | ir = 0; |
samux | 0:88d52e197201 | 117 | Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 256 * 4); |
gabriel4211 | 12:d32975c44c98 | 118 | Thread spiTask(spi_task, NULL, osPriorityNormal, 256 * 4); |
armdran | 14:c820ae094728 | 119 | Thread aliveTask(alive_task, NULL, osPriorityNormal, 256 * 4); |
armdran | 9:8bcd70b26084 | 120 | device.frequency(1000000); |
armdran | 11:e12aae7de530 | 121 | device.format(8, 1); |
armdran | 11:e12aae7de530 | 122 | while(1) {} |
armdran | 9:8bcd70b26084 | 123 | |
samux | 0:88d52e197201 | 124 | } |