added simple keycode->char lookup functionality
authortheStack <sebastian.falbesoner@gmail.com>
Thu, 24 Sep 2009 01:44:38 +0000 (03:44 +0200)
committertheStack <sebastian.falbesoner@gmail.com>
Thu, 24 Sep 2009 01:44:38 +0000 (03:44 +0200)
and tried to make it a bit more nice in main.c :)
don't know if it works though, hopefully it does...

main.c
usb/drivers/class/hid.c
usb/drivers/class/hid.h

diff --git a/main.c b/main.c
index a058cfe93d7efd138d2fa467af90e84bdb0372c0..2bc956f7c3532bddb1197fcb49090216e1bbb7b9 100644 (file)
--- a/main.c
+++ b/main.c
@@ -134,43 +134,66 @@ int main(void)
        }
        while(!usb_hidkb_inuse());
 
+#define FONT_WIDTH  13
+#define FONT_HEIGHT 15
+#define STDOUT_BORDER_LEFT 20
+#define STDOUT_BORDER_RIGHT 650
+#define STDOUT_BORDER_TOP 20
+#define STDOUT_BORDER_BOTTOM 550
+#define TABSIZE 4
        /* you are welcome to make this nice :) */
        char str[7];
-       u16 i, j, y=20, x=20;
+       u16 i, j, y=STDOUT_BORDER_TOP, x=STDOUT_BORDER_LEFT;
+       u16 old_x, old_y;
        struct kbrep *k;
 
        while(1) {
                memset(str, '\0', 8);
                j=0;
                k = usb_hidkb_getChars();
+               old_x = x; /* save actual x and y position for printing after the loop */
+               old_y = y;
                for(i=0; k->keys[i]>0; i++) {
-                       if(x>650) {
-                               x = 20;
-                               y += 15;
+                       unsigned char key = usb_hidkb_get_char_from_keycode(k->keys[i],
+                                       (k->mod & MOD_lshift) || (k->mod & MOD_rshift));
+                       /* no key or key not relevant? next, please. */
+                       if (key == 0)
+                               continue;
+
+                       /* RETURN pressed? */
+                       if (key == '\n') {
+                               x = STDOUT_BORDER_LEFT;
+                               y += FONT_HEIGHT;
+                       /* TAB pressed? */
+                       } else if (key == '\t') {
+                               x += (TABSIZE*FONT_WIDTH);
+
+                       /* BACKSPACE pressed? */
+                       } else if (key == '\r') {
+                               /* TODO */
+
+                       /* now we have only printable characters left */
+                       } else {
+                               x += FONT_WIDTH;
+                               str[j] = key;
+                               j++;
                        }
-                       if(y>440) {
-                               y=20;
-                       }
-                       if((k->keys[i] >= 4) && k->keys[i] <= 4+'z'-'a') {
-                               str[j] = k->keys[i] - 4 + (k->mod & MOD_lshift || k->mod & MOD_rshift ? 'A' : 'a');
-                       } else if ((k->keys[i] >= 0x1e) && (k->keys[i] <= 0x26)) {
-                               str[j] = k->keys[i] - 0x1e + '1';
-                       } else if (k->keys[i] == 0x27) {
-                               str[j] = '0';
+
+                       /* line full? break! */
+                       if(x > (STDOUT_BORDER_RIGHT-FONT_WIDTH)) {
+                               x = STDOUT_BORDER_LEFT;
+                               y += FONT_HEIGHT;
                        }
-                       else if (k->keys[i] == 0x28) {
-                               y += 15;
-                               x = 20;
+                       /* screen full? start again at top */
+                       if(y > (STDOUT_BORDER_BOTTOM-FONT_HEIGHT)) {
+                               y = STDOUT_BORDER_TOP;
                        }
-                       j++;
                }
-               if(j > 0) {
-                       print_str_noscroll(x, y, str);
+               if(j > 0) { /* when there was any printable stuff, show it */
+                       print_str_noscroll(old_x, old_y, str);
                        printf("y: %d\n", y);
                }
-               while(j--) {
-                       x += 13;
-               }
+
        }
 
 #if 0
index fe5a17c2fecfa5e8600a452b1cb2ac8eeb2cae3b..c8c9dfe1e2d10eb3d93a7f54df2a420e7f2c13fe 100644 (file)
@@ -24,6 +24,28 @@ struct usb_driver hidkb = {
        .data     = NULL
 };
 
+/*
+ * just using two very simple US layout code translation tables that
+ * are sufficient for providing a getc() C standard library call;
+ * the only non-printable character here in this table is ESCAPE which
+ * has index 0x29 and is zero
+ */
+unsigned char code_translation_table[2][57] = {
+       { /* unshifted */
+        0,   0,   0,   0,  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
+       'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2',
+       '3', '4', '5', '6', '7', '8', '9', '0', '\n', 0,  '\r','\t',' ', '-', '=', '[',
+       ']', '\\','\\',';', '\'','`', ',', '.', '/'
+       },
+       { /* shifted */
+        0,   0,   0,   0,  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
+       'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '@',
+       '#', '$', '%', '^', '&', '*', '(', ')', '\n', 0,  '\r','\t',' ', '_', '+', '{',
+       '}', '|', '|', ':', '\"','~', '<', '>', '?'
+       }
+};
+
+
 void usb_hidkb_init()
 {
        usb_register_driver(&hidkb);
@@ -72,3 +94,10 @@ struct kbrep *usb_hidkb_getChars() {
        return ret;
 }
 
+unsigned char usb_hidkb_get_char_from_keycode(u8 keycode, int shifted)
+{
+       unsigned char result = 0;
+       if (keycode < 57)
+               result = code_translation_table[!!shifted][keycode];
+       return result;
+}
index 347a0db69d899662843d8e72abb68456ade737d2..85ce3a836398b3c31702b85107c1ac8bb4ae334f 100644 (file)
@@ -34,6 +34,7 @@ void usb_hidkb_init();
 u8 usb_hidkb_inuse();
 
 struct kbrep *usb_hidkb_getChars();
+unsigned char usb_hidkb_get_char_from_keycode(u8 keycode, int shifted);
 
 #endif /* __HID_H */