![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
TEAMUSB_SLAVE
Fork of USBHostKeyboard_HelloWorld by
main.cpp@9:8bcd70b26084, 2015-04-01 (annotated)
- Committer:
- armdran
- Date:
- Wed Apr 01 15:50:28 2015 +0000
- Revision:
- 9:8bcd70b26084
- Parent:
- 8:f8122ca2f58f
- Child:
- 10:54ffd94e075c
added send routine for spi
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" |
samux | 0:88d52e197201 | 4 | |
samux | 0:88d52e197201 | 5 | DigitalOut led(LED1); |
armdran | 9:8bcd70b26084 | 6 | SPISlave device(D11, D12, D13, D10); |
armdran | 9:8bcd70b26084 | 7 | uint8_t keyReg = 0x0; |
samux | 0:88d52e197201 | 8 | |
armdran | 8:f8122ca2f58f | 9 | void onKeyMod(uint8_t key, uint8_t modifier) { |
armdran | 9:8bcd70b26084 | 10 | printf("Key: %x modifier: %x\r\n", key, modifier); |
armdran | 9:8bcd70b26084 | 11 | keyReg = key; |
armdran | 8:f8122ca2f58f | 12 | } |
armdran | 8:f8122ca2f58f | 13 | |
armdran | 8:f8122ca2f58f | 14 | |
samux | 0:88d52e197201 | 15 | void keyboard_task(void const *) { |
samux | 0:88d52e197201 | 16 | |
samux | 0:88d52e197201 | 17 | USBHostKeyboard keyboard; |
samux | 0:88d52e197201 | 18 | |
samux | 0:88d52e197201 | 19 | while(1) { |
armdran | 8:f8122ca2f58f | 20 | |
armdran | 8:f8122ca2f58f | 21 | printf("trying to connect\r\n"); |
armdran | 8:f8122ca2f58f | 22 | |
samux | 0:88d52e197201 | 23 | // try to connect a USB keyboard |
armdran | 8:f8122ca2f58f | 24 | while(!keyboard.connect()) { |
samux | 0:88d52e197201 | 25 | Thread::wait(500); |
armdran | 8:f8122ca2f58f | 26 | } |
armdran | 8:f8122ca2f58f | 27 | |
armdran | 8:f8122ca2f58f | 28 | printf("connected\r\n"); |
armdran | 8:f8122ca2f58f | 29 | |
samux | 0:88d52e197201 | 30 | |
samux | 0:88d52e197201 | 31 | // when connected, attach handler called on keyboard event |
armdran | 9:8bcd70b26084 | 32 | //keyboard.attach(onKey); |
armdran | 8:f8122ca2f58f | 33 | keyboard.attach(onKeyMod); |
armdran | 8:f8122ca2f58f | 34 | |
armdran | 8:f8122ca2f58f | 35 | printf("eventhandler attached\r\n"); |
samux | 0:88d52e197201 | 36 | |
samux | 0:88d52e197201 | 37 | // wait until the keyboard is disconnected |
armdran | 8:f8122ca2f58f | 38 | while(keyboard.connected()) { |
armdran | 8:f8122ca2f58f | 39 | USBHost::poll(); |
armdran | 8:f8122ca2f58f | 40 | } |
armdran | 8:f8122ca2f58f | 41 | |
armdran | 8:f8122ca2f58f | 42 | printf("disconnected\r\n"); |
armdran | 8:f8122ca2f58f | 43 | |
samux | 0:88d52e197201 | 44 | } |
samux | 0:88d52e197201 | 45 | } |
samux | 0:88d52e197201 | 46 | |
samux | 0:88d52e197201 | 47 | int main() { |
samux | 0:88d52e197201 | 48 | Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 256 * 4); |
armdran | 9:8bcd70b26084 | 49 | device.frequency(1000000); |
armdran | 9:8bcd70b26084 | 50 | device.format(8, 1); |
armdran | 8:f8122ca2f58f | 51 | |
armdran | 9:8bcd70b26084 | 52 | //while(1) { |
armdran | 9:8bcd70b26084 | 53 | //Thread::wait(1000); |
armdran | 9:8bcd70b26084 | 54 | //printf("waiting...\r\n"); |
armdran | 9:8bcd70b26084 | 55 | //} |
armdran | 9:8bcd70b26084 | 56 | |
armdran | 9:8bcd70b26084 | 57 | int i = 0; |
armdran | 9:8bcd70b26084 | 58 | |
samux | 0:88d52e197201 | 59 | while(1) { |
armdran | 9:8bcd70b26084 | 60 | |
armdran | 9:8bcd70b26084 | 61 | //get instruciton |
armdran | 9:8bcd70b26084 | 62 | device.reply(0x00); |
armdran | 9:8bcd70b26084 | 63 | while(!device.receive()) { |
armdran | 9:8bcd70b26084 | 64 | if(i++ % 10000 == 0) { |
armdran | 9:8bcd70b26084 | 65 | printf("."); |
armdran | 9:8bcd70b26084 | 66 | } |
armdran | 9:8bcd70b26084 | 67 | } |
armdran | 9:8bcd70b26084 | 68 | |
armdran | 9:8bcd70b26084 | 69 | uint8_t instruction = device.read(); |
armdran | 9:8bcd70b26084 | 70 | |
armdran | 9:8bcd70b26084 | 71 | if(instruction != 0xFE) continue; |
armdran | 9:8bcd70b26084 | 72 | |
armdran | 9:8bcd70b26084 | 73 | device.reply(keyReg); |
armdran | 9:8bcd70b26084 | 74 | |
armdran | 9:8bcd70b26084 | 75 | while(!device.receive()) { |
armdran | 9:8bcd70b26084 | 76 | if(i++ % 10000 == 0) { |
armdran | 9:8bcd70b26084 | 77 | printf("."); |
armdran | 9:8bcd70b26084 | 78 | } |
armdran | 9:8bcd70b26084 | 79 | } |
armdran | 9:8bcd70b26084 | 80 | |
armdran | 9:8bcd70b26084 | 81 | device.read(); |
armdran | 9:8bcd70b26084 | 82 | |
armdran | 9:8bcd70b26084 | 83 | device.reply(0x00); |
armdran | 9:8bcd70b26084 | 84 | |
armdran | 9:8bcd70b26084 | 85 | while(!device.receive()) { |
armdran | 9:8bcd70b26084 | 86 | if(i++ % 10000 == 0) { |
armdran | 9:8bcd70b26084 | 87 | printf("."); |
armdran | 9:8bcd70b26084 | 88 | } |
armdran | 9:8bcd70b26084 | 89 | } |
armdran | 9:8bcd70b26084 | 90 | |
armdran | 9:8bcd70b26084 | 91 | int response = device.read(); |
armdran | 9:8bcd70b26084 | 92 | |
armdran | 9:8bcd70b26084 | 93 | //printf("Sent Keystroke %x over SPI. response was %x\r\n", keyReg, response); |
armdran | 9:8bcd70b26084 | 94 | |
armdran | 9:8bcd70b26084 | 95 | if(response == keyReg) { |
armdran | 9:8bcd70b26084 | 96 | keyReg = 0; |
armdran | 9:8bcd70b26084 | 97 | } |
armdran | 9:8bcd70b26084 | 98 | |
samux | 0:88d52e197201 | 99 | } |
armdran | 9:8bcd70b26084 | 100 | |
samux | 0:88d52e197201 | 101 | } |