TEAMUSB
/
TEAMUSB_SLAVE
TEAMUSB_SLAVE
Fork of USBHostKeyboard_HelloWorld by
main.cpp
- Committer:
- gabriel4211
- Date:
- 2015-04-16
- Revision:
- 13:b05908f05e81
- Parent:
- 12:d32975c44c98
- Child:
- 14:c820ae094728
File content as of revision 13:b05908f05e81:
#include "mbed.h" #include "USBHostKeyboard.h" #include "rtos.h" //#include <queue> typedef struct { uint8_t key; uint8_t modifier; } keyPress_t; DigitalOut led(LED1); SPISlave device(D11, D12, D13, D10); DigitalOut ir(D9); Queue<keyPress_t, 16> _keyQueue; MemoryPool<keyPress_t, 16> mpool; uint8_t spi_reply_blocking(uint8_t reply = 0x00, bool sendIr = 0) { device.reply(reply); if(sendIr == 1) { ir = 1; ir = 0; } while(!device.receive()) { // busy } uint8_t instruction = device.read(); return instruction; } void onKeyMod(uint8_t key, uint8_t modifier) { keyPress_t *keypress = mpool.alloc(); keypress->key = key; keypress->modifier = modifier; _keyQueue.put(keypress); } void spi_task(void const *) { while(1){ osEvent evt = _keyQueue.get(); if (evt.status == osEventMessage) { keyPress_t *keypress = (keyPress_t*)evt.value.p; uint8_t key = keypress->key; uint8_t modifier = keypress->modifier; printf(" sending key %x and modifier %x \r\n", key, modifier); uint8_t instruction = spi_reply_blocking(key, 1); while(instruction != 0xFE) { printf(" out of sync.\r\n"); instruction = spi_reply_blocking(key); } uint8_t ack_key = spi_reply_blocking(modifier); uint8_t ack_keychar = spi_reply_blocking(); if(ack_key != key) printf(" key ack failed (is %x, should be %x)!!!\r\n", ack_key, key); else printf(" key ack ok\r\n"); mpool.free(keypress); } } } void keyboard_task(void const *) { USBHostKeyboard keyboard; while(1) { printf("trying to connect\r\n"); // try to connect a USB keyboard while(!keyboard.connect()) { Thread::wait(500); } printf("connected\r\n"); // when connected, attach handler called on keyboard event keyboard.attach(onKeyMod); printf("eventhandler attached\r\n"); // wait until the keyboard is disconnected while(keyboard.connected()) { USBHost::poll(); } printf("disconnected\r\n"); } } int main() { ir = 0; Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 256 * 4); Thread spiTask(spi_task, NULL, osPriorityNormal, 256 * 4); device.frequency(1000000); device.format(8, 1); while(1) {} }