TEAMUSB_SLAVE

Dependencies:   USBHost mbed

Fork of USBHostKeyboard_HelloWorld by TEAMUSB

main.cpp

Committer:
gabriel4211
Date:
2015-04-16
Revision:
12:d32975c44c98
Parent:
11:e12aae7de530
Child:
13:b05908f05e81

File content as of revision 12:d32975c44c98:

#include "mbed.h"
#include "USBHostKeyboard.h"
#include "rtos.h"
//#include <queue>   

typedef struct {
    uint8_t key;        
    uint8_t modifier;
} keyPress_t;


static uint8_t keymap[57] = { 
     0, 0, 0, 0, 'a', 'b' /*0x05*/,
      'c', 'd', 'e', 'f', 'g' /*0x0a*/,
      'h', 'i', 'j', 'k', 'l'/*0x0f*/,
      'm', 'n', 'o', 'p', 'q'/*0x14*/,
      'r', 's', 't', 'u', 'v'/*0x19*/,
      'w', 'x', 'y', 'z', '1'/*0x1E*/,
      '2', '3', '4', '5', '6'/*0x23*/,
      '7', '8', '9', '0', 0x0A /*enter*/, /*0x28*/
      0x1B /*escape*/, 0x08 /*backspace*/, 0x09/*tab*/, 0x20/*space*/, '-', /*0x2d*/
      '=', '[', ']', '\\', '#', /*0x32*/
      ';', '\'', 0, ',', '.', /*0x37*/
      '/'};

DigitalOut led(LED1);
SPISlave device(D11, D12, D13, D10);
DigitalOut ir(D9);
//std::queue<uint8_t> _keyQueue;
Queue<keyPress_t, 16> _keyQueue;
MemoryPool<keyPress_t, 16> mpool;
//Mutex _keyMutex;
int bla = 0;
uint8_t spi_reply_blocking(uint8_t reply = 0x00, bool sendIr = 0) {
    device.reply(reply);
    bla= 0;
    if(sendIr == 1) { 
        ir = 1; ir = 0;
    }
    while(!device.receive()) {
        /*bla++;
        if(bla % 100000 == 0){
            printf("stuck... ");
        }*/
    }
    uint8_t instruction = device.read();
    //printf("    send: %x received: %x\r\n", reply, instruction);
    return instruction;  
}


void onKeyMod(uint8_t key, uint8_t modifier) {
    //printf("got key: %x modifier: %x.\r\n", key, modifier);
    //_keyMutex.lock();
    /*
    _keyQueue.push(key);
    _keyQueue.push(modifier);
    */
    
    keyPress_t *keypress = mpool.alloc();
    keypress->key = key;
    keypress->modifier = modifier;    
    _keyQueue.put(keypress);
    
    //_keyMutex.unlock();
    /*
    ir = 1; ir = 0;
    uint8_t sync = 0;
    do{
        sync = spi_reply_blocking(0xFD);
    }while(sync != 0xFD);    
      */  
    /*    
    uint8_t instruction = spi_reply_blocking(key, 1);
    
    while(instruction != 0xFE) {
        printf("    out of sync.\r\n");
        instruction = spi_reply_blocking(key);
        //return;
    }
        
    //printf("    got instruction FE. key sent\r\n");
    uint8_t ack_key = spi_reply_blocking(modifier);
    
    
        
    //printf("    mod sent.\r\n");
    uint8_t ack_modifier = 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");
    
    if(ack_modifier != modifier) 
        printf("    mod ack failed(is %x, should be %x)!!!\r\n", ack_modifier, modifier); 
    else
        printf("    mod ack ok\r\n");
    */
}

void spi_task(void const *) {
    while(1){
        //_keyMutex.lock();
        osEvent evt = _keyQueue.get();
        if (evt.status == osEventMessage) {
            keyPress_t *keypress = (keyPress_t*)evt.value.p;
            /*
            uint8_t key = _keyQueue.front();
            _keyQueue.pop();
            uint8_t modifier = _keyQueue.front();
            _keyQueue.pop();
            */
            uint8_t key = keypress->key;
            uint8_t modifier = keypress->modifier;
            
            printf("    sending key %x and modifier %x %c \r\n", key, modifier, keymap[key]);
            uint8_t instruction = spi_reply_blocking(key, 1);
    
            while(instruction != 0xFE) {
                printf("    out of sync.\r\n");
                instruction = spi_reply_blocking(key);
                //return;
            }
         
            uint8_t ack_key = spi_reply_blocking(modifier);
       
            uint8_t ack_modifier = spi_reply_blocking(keymap[key]); 
            
            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");
            
            if(ack_modifier != modifier) 
                printf("    mod ack failed(is %x, should be %x)!!!\r\n", ack_modifier, modifier); 
            else
                printf("    mod ack ok\r\n");
                
            if(ack_modifier != modifier) 
                printf("     keychar failed(is %x, should be %x)!!!\r\n", ack_keychar, keymap[key]); 
            else
                printf("    keychar ack ok\r\n");
                
            mpool.free(keypress);
                
        }
        //wait_ms(1);
        //_keyMutex.unlock();
            
    }
}

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();
            //wait_ms(1);
        }
            
        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) {}
    
}