A version of the PS/2 library customized for MbedConsole. Also includes a few things that make it's behavior easier to control and a few bug fixes.

Dependents:   MbedConsole

Fork of PS2 by Shinichiro Nakamura

Revision:
2:a57bbbec16b1
Parent:
1:823c2798e398
--- a/PS2Mouse.cpp	Wed Sep 29 14:11:44 2010 +0000
+++ b/PS2Mouse.cpp	Wed Sep 29 22:15:03 2010 +0000
@@ -16,40 +16,43 @@
 
 bool PS2Mouse::processing(mouse_event_t *p) {
     bool emit = false;
-    const int c = ps2ms.getc();
-    if (0 <= c) {
-        switch (cnt % 4) {
-            case 0:
-                mi.byte1.byte = c;
-                break;
-            case 1:
-                mi.byte2.byte = c;
-                break;
-            case 2:
-                mi.byte3.byte = c;
-                break;
-            case 3:
-                mi.byte4.byte = c;
-#if 0
-                printf("[%c:%c:%c] - (%4d,%4d) - <%d:%3d>\n",
-                       mi.byte1.bit.btnLeft ? 'o' : ' ',
-                       mi.byte1.bit.btnMiddle ? 'o' : ' ',
-                       mi.byte1.bit.btnRight ? 'o' : ' ',
-                       mi.byte1.bit.signX ? (-256 + mi.byte2.byte) : mi.byte2.byte,
-                       mi.byte1.bit.signY ? (-256 + mi.byte3.byte) : mi.byte3.byte,
-                       mi.byte4.bit.signZ,
-                       mi.byte4.bit.signZ ? (-128 + mi.byte4.bit.value) : mi.byte4.bit.value);
-#endif
-                p->left = mi.byte1.bit.btnLeft ? true : false;
-                p->center = mi.byte1.bit.btnCenter ? true : false;
-                p->right = mi.byte1.bit.btnRight ? true : false;
-                p->x = mi.byte1.bit.signX ? (-256 + mi.byte2.byte) : mi.byte2.byte;
-                p->y = mi.byte1.bit.signY ? (-256 + mi.byte3.byte) : mi.byte3.byte;
-                p->z = mi.byte4.bit.signZ ? (-128 + mi.byte4.bit.value) : mi.byte4.bit.value;
-                emit = true;
-                break;
+    for (int i = 0; i < 4; i++) {
+        const int c = ps2ms.getc();
+        if (0 <= c) {
+            switch (cnt % 4) {
+                case 0:
+                    mi.byte1.byte = c;
+                    /*
+                     * Check and reset a buffer if state is wrong.
+                     */
+                    if (mi.byte1.bit.always1 == 0) {
+                        cnt = 0;
+                        while (0 <= ps2ms.getc()) {
+                        }
+                    }
+                    break;
+                case 1:
+                    mi.byte2.byte = c;
+                    break;
+                case 2:
+                    mi.byte3.byte = c;
+                    break;
+                case 3:
+                    mi.byte4.byte = c;
+                    /*
+                     * Store a event data.
+                     */
+                    p->left = mi.byte1.bit.btnLeft ? true : false;
+                    p->center = mi.byte1.bit.btnCenter ? true : false;
+                    p->right = mi.byte1.bit.btnRight ? true : false;
+                    p->x = mi.byte1.bit.signX ? (-256 + mi.byte2.byte) : mi.byte2.byte;
+                    p->y = mi.byte1.bit.signY ? (-256 + mi.byte3.byte) : mi.byte3.byte;
+                    p->z = mi.byte4.bit.signZ ? (-128 + mi.byte4.bit.value) : mi.byte4.bit.value;
+                    emit = true;
+                    break;
+            }
+            cnt++;
         }
-        cnt++;
     }
     return emit;
 }