Testing USBKeyboard

Dependencies:   DebounceIn PinDetect_KL25Z USBDevice mbed

Fork of idd_fall15_hw2_chiller_morse by IDD Fall 2015

Files at this revision

API Documentation at this revision

Comitter:
adithya_murali
Date:
Sun Sep 20 05:35:34 2015 +0000
Parent:
0:4b4ae443bf49
Commit message:
Testing USBKeyboard with basic Pindetect buttons

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 4b4ae443bf49 -r 11e89d389dd4 main.cpp
--- a/main.cpp	Sun Sep 13 21:47:15 2015 +0000
+++ b/main.cpp	Sun Sep 20 05:35:34 2015 +0000
@@ -1,221 +1,96 @@
 #include "mbed.h"
 #include "USBKeyboard.h"
-#include "DebounceIn.h"
 #include "PinDetect.h"
 
-DigitalOut redled(LED1);
-DigitalOut greenled(LED2);
-DigitalOut blueled(LED3);
+USBKeyboard keyboard;
 
+PinDetect button1(D4); // Left
+PinDetect button2(D5); // Right
+PinDetect button3(D6); // Space
+PinDetect button4(D7); // Shift
+
+DigitalOut myled(LED1);
+
+int state[4];
 
-DebounceIn dit(D2);
-DebounceIn dah(D3);
-DebounceIn space(D4);
-PinDetect enter(D5);
-PinDetect backspace(D6);
-PinDetect capsLock(D7);
-
-DigitalOut capsled(D8);
+void button1_pressed() {
+    state[0] = 1;
+}
+void button2_pressed() {
+    state[1] = 1;
+}
+void button3_pressed() {
+    state[2] = 1;
+}
+void button4_pressed() {
+    state[3] = 1;
+}
 
-Serial pc(USBTX, USBRX); // tx, rx
+void button1_released() {
+    state[0] = 0;
+}
+void button2_released() {
+    state[1] = 0;
+}
+void button3_released() {
+    state[2] = 0;
+}
+void button4_released() {
+    state[3] = 0;
+}
 
-USBKeyboard keyboard;
-Timer t;
-struct Node {
-    char character;
-    char modifier;
-    bool display;
-    Node *left, *right;
-    Node(char nodeChar, char nodeMod, bool disp, Node *leftN, Node *rightN):
-        character(nodeChar), modifier(nodeMod), display(disp), left(leftN), right(rightN) {}
-};
-
-Node *head, *superHead;
-double timerTime;
+void exec_keycode(int state[], USBKeyboard keyboard) {
+    if (state[0] == 1) {
+        keyboard.keyCode('a');
+    }
+    if (state[1] == 1) {
+        keyboard.keyCode('d');
+    }
+    if (state[2] == 1) {
+        keyboard.keyCode(32);
+    }
+    if (state[3] == 1) {
+        keyboard.keyCode(16);
+    }
+}
 
 int main() {
-    dit.mode(PullUp);
-    dah.mode(PullUp);
-    
-    space.mode(PullUp);
-    enter.mode(PullUp);
-    backspace.mode(PullUp);
-    capsLock.mode(PullUp);
-    
-    // Delay for initial pullup to take effect
-    wait(.001);
-    
-    bool ditActive=false;
-    bool dahActive=false;
-    bool spaceActive = false;
-    bool enterActive = false;
-    bool backspaceActive = false;
-    bool capsLockActive = false;
-    
-    
+
+    /*Add callback functions for events when buttons are pressed. */
+    button1.attach_asserted(&button1_pressed);
+    button2.attach_asserted(&button2_pressed);
+    button3.attach_asserted(&button3_pressed);
+    button4.attach_asserted(&button4_pressed);
+
+    /*Add callback functions for events when buttons are released. */
+    button1.attach_deasserted(&button1_released);
+    button2.attach_deasserted(&button2_released);
+    button3.attach_deasserted(&button3_released);
+    button4.attach_deasserted(&button4_released);
+
+    button1.setAssertValue(0);
+    button2.setAssertValue(0);
+    button3.setAssertValue(0);
+    button4.setAssertValue(0);
 
-    Node *head = new Node(' ',0, false,  
-    new Node('e',0,true, 
-        new Node('i',0, true, 
-            new Node('s',0, true, 
-                new Node('h',0, true, 
-                    new Node('5',0, true, NULL, NULL), 
-                    new Node('4',0, true, NULL, NULL)), 
-                new Node('v',0, true, 
-                    new Node(' ',0, false, 
-                        new Node(' ',0, false, 
-                            NULL, 
-                            new Node('4',KEY_SHIFT, true, NULL, NULL)), 
-                        NULL), 
-                    new Node('3',0, true, NULL, NULL))), 
-            new Node('u',0, true, 
-                new Node('f',0, true, NULL, NULL), 
-                new Node(' ',0, false, 
-                    new Node(' ',0, false, 
-                        new Node('?',KEY_SHIFT, true, NULL, NULL), 
-                        new Node('_',KEY_SHIFT, true, NULL, NULL)), 
-                    new Node('2',0, true, NULL, NULL)))), 
-        new Node('a',0, true, 
-            new Node('r',0, true, 
-                new Node('l',0, true, 
-                    new Node('7',KEY_SHIFT, true, NULL, NULL), /* & */ 
-                    new Node(' ',0, false, 
-                        new Node('\'',KEY_SHIFT, true, NULL, NULL), /* " */
-                        NULL)), 
-                new Node(' ',0, false, 
-                    new Node('+',KEY_SHIFT, true, 
-                        NULL, 
-                        new Node('.',0, true, NULL, NULL)),   
-                    NULL)), 
-            new Node('w',0, true, 
-                new Node('p',0, true, 
-                    NULL, 
-                    new Node(' ',0, false, 
-                        new Node(34,KEY_SHIFT, true, NULL, NULL), /*@*/
-                        NULL)), 
-                new Node('j',0, true, 
-                    NULL, 
-                    new Node('1',0, true, 
-                        new Node('\'',0, true, NULL, NULL), 
-                        NULL))))), 
-    new Node('t',0, true,  
-        new Node('n',0, true, 
-            new Node('d',0, true, 
-                new Node('b',0, true, 
-                    new Node('6',0, true, 
-                        NULL, 
-                        new Node('-',0, true, NULL, NULL)), 
-                    new Node('=',0, true, NULL, NULL)), 
-                new Node('x',0, true, 
-                    new Node('/',0, true, NULL, NULL), 
-                    NULL)), 
-            new Node('k',0, true, 
-                new Node('c',0, true, 
-                    NULL, 
-                    new Node(' ',0, false, 
-                        new Node(';',0, true, NULL, NULL), 
-                        new Node('1',KEY_SHIFT, true, NULL, NULL))), 
-                new Node('y',0, true, 
-                    new Node('9',KEY_SHIFT, true, 
-                        NULL, 
-                        new Node('0',KEY_SHIFT, true, NULL, NULL)), 
-                    NULL))), 
-        new Node('m',0, true, 
-            new Node('g',0, true, 
-                new Node('z',0, true, 
-                    new Node('7',0, true, NULL, NULL), 
-                    new Node(' ',0, false, 
-                        NULL, 
-                        new Node(',',0, true, NULL, NULL))), 
-                new Node('q',0, true, NULL, NULL)), 
-            new Node('o',0, true, 
-                new Node(' ',0, false, 
-                    new Node('8',0, true, 
-                        new Node(';',KEY_SHIFT, true, NULL, NULL), 
-                        NULL), 
-                    NULL), 
-                new Node(' ',0, false, 
-                    new Node('9',0, true, NULL, NULL), 
-                    new Node('0',0, true, NULL, NULL))))));
-
+    button1.setSampleFrequency();
+    button2.setSampleFrequency();
+    button3.setSampleFrequency();
+    button4.setSampleFrequency();
     
-    superHead = head;
-    redled = 0;
-    greenled = 1;
-    blueled = 1;
-    t.start(); 
-    timerTime = t.read();
-    float tempTime;
-    while(1) {
-        // Non-character buttons, tried with interrupts but would send multiple keys, not sure why
-        if (space == 1) {
-            spaceActive = false;
-        } else if ((space == 0) && !spaceActive) {
-            spaceActive = true;
-            keyboard.keyCode(' ');
-            head = superHead;
-            timerTime = t.read();
-            continue;
+    for (;;) {
+        if (state[0] == 1) {
+            keyboard.keyCode('a');
         }
-        
-        if (enter == 1) {
-            enterActive = false;
-        } else if ((enter == 0) && !enterActive) {
-            enterActive = true;
-            keyboard.printf("\r\n");
-            head = superHead;
-            timerTime = t.read();
-            continue;
-        }
-        if (backspace == 1) {
-            backspaceActive = false;
-        } else if ((backspace == 0) && !backspaceActive) {
-            backspaceActive = true;
-            keyboard.keyCode(8);
-            head = superHead;
-            timerTime = t.read();
-            continue;
+        if (state[1] == 1) {
+            keyboard.keyCode('d');
         }
-        if (capsLock == 1) {
-            capsLockActive = false;
-        } else if ((capsLock == 0) && !capsLockActive) {
-            capsLockActive = true;
-            keyboard.keyCode(KEY_CAPS_LOCK);
-            head = superHead;
-            timerTime = t.read();
-            blueled = 1 - blueled;
-            capsled = 1- capsled;
-            continue;
+        if (state[2] == 1) {
+            keyboard.keyCode(32);
         }
-
-
-        // press a button, advance the tree state
-        if ((dit == 0) && !ditActive){
-            ditActive = true;
-            head = head->left;
-            timerTime = t.read();
-        } else if ((dah == 0) && !dahActive) {
-            dahActive = true;
-            head = head->right;
-            timerTime = t.read();
-        } else {
-            if (dit == 1) {
-                ditActive = false;
-            }
-            if (dah == 1) {
-                dahActive = false;
-            }
+        if (state[3] == 1) {
+            keyboard.keyCode(16);
         }
-        // after a certain delay, send the key, reset the tree
-        tempTime = t.read();
-        if ((tempTime - timerTime) > 0.3f) {
-            if (head->display) {
-                keyboard.keyCode(head->character, head->modifier);
-                redled = 1-redled;
-                greenled = 1-greenled;
-            }
-            timerTime = t.read();
-            head = superHead;    
-        }
+        wait(0.1);
     }
-}
+}
\ No newline at end of file