TEAMUSB_SLAVE

Dependencies:   USBHost mbed

Fork of USBHostKeyboard_HelloWorld by TEAMUSB

Revision:
11:e12aae7de530
Parent:
10:54ffd94e075c
Child:
12:d32975c44c98
--- a/main.cpp	Fri Apr 03 13:21:03 2015 +0000
+++ b/main.cpp	Tue Apr 07 15:33:25 2015 +0000
@@ -4,11 +4,42 @@
 
 DigitalOut led(LED1);
 SPISlave device(D11, D12, D13, D10);
-uint8_t keyReg = 0x0;
+DigitalOut ir(D9);
+
+uint8_t spi_reply_blocking(uint8_t reply = 0x00, bool sendIr = 0) {
+    device.reply(reply);
+    if(sendIr == 1) { wait_ms(100); ir = 1; ir = 0;}
+    while(!device.receive()) {}
+    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("Key: %x modifier: %x\r\n", key, modifier);
-    keyReg = key; 
+    printf("got key: %x 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, 1);
+        return;
+    }
+        
+    //printf("    got instruction FE. key sent\r\n");
+    uint8_t ack_key = spi_reply_blocking(modifier);
+    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");
+        
+    //printf("    mod sent.\r\n");
+    uint8_t ack_modifier = spi_reply_blocking(); 
+    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");
+
 }
 
 
@@ -27,9 +58,7 @@
 
         printf("connected\r\n");
 
-    
         // when connected, attach handler called on keyboard event
-        //keyboard.attach(onKey);
         keyboard.attach(onKeyMod);
         
         printf("eventhandler attached\r\n");
@@ -47,62 +76,7 @@
 int main() {
     Thread keyboardTask(keyboard_task, NULL, osPriorityNormal, 256 * 4);
     device.frequency(1000000);
-    //device.format(8, 1);
-    device.format(8, 3);
-    
-    //while(1) {
-        //Thread::wait(1000); 
-        //printf("waiting...\r\n");
-    //}
-    
-    int i = 0; 
-        
-    while(1) {
-        
-        //get instruciton
-        printf("first poll\r\n");
-        device.reply(0x00);
-        while(!device.receive()) {
-             if(i++ % 10000 == 0) {
-                 printf(".");
-             }
-        }
-        
-        uint8_t instruction = device.read();
-        
-        if(instruction != 0xFE) {
-            printf("received %x on first poll\r\n", instruction );
-            continue;        
-        }
-        
-        printf("second step\r\n");
-        device.reply(keyReg);        
-        
-        while(!device.receive()) {
-             if(i++ % 10000 == 0) {
-                 printf(".");
-             }
-        }
-        
-        device.read();
-
-        printf("third step\r\n");
-        device.reply(0x00);        
-
-        while(!device.receive()) {
-             if(i++ % 10000 == 0) {
-                 printf(".");
-             }
-        }
-
-        int response = device.read();
-            
-        printf("Sent Keystroke %x over SPI. response was %x\r\n", keyReg, response);
-         
-        if(response == keyReg) {
-            keyReg = 0;
-        }
-        
-    }
+    device.format(8, 1);
+    while(1) {}
     
 }