Aleksandar Kodzhabashev / Mbed 2 deprecated TrackballQuery

Dependencies:   Servo mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PS2Mouse.cpp Source File

PS2Mouse.cpp

00001 /**
00002  * PS/2 mouse interface control class (Version 0.0.1)
00003  *
00004  * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
00005  * http://shinta.main.jp/
00006  */
00007 #include "PS2Mouse.h"
00008 
00009 PS2Mouse::PS2Mouse(PinName clk_pin, PinName dat_pin)
00010         : ps2ms_init(clk_pin, dat_pin), ps2ms(clk_pin, dat_pin) {
00011     cnt = 0;
00012 }
00013 
00014 PS2Mouse::~PS2Mouse() {
00015 }
00016 
00017 const char *byte_to_binary(int x)
00018 {
00019     static char b[9];
00020     b[0] = '\0';
00021 
00022     int z;
00023     for (z = 128; z > 0; z >>= 1)
00024     {
00025         strcat(b, ((x & z) == z) ? "1" : "0");
00026     }
00027 
00028     return b;
00029 }
00030 
00031 bool PS2Mouse::processing(mouse_event_t *p) {
00032     bool emit = false;
00033     for (int i = 0; i < 4; i++) {
00034         const int c = ps2ms.getc();
00035         if (0 <= c) {
00036             switch (cnt % 4) {
00037                 case 0:
00038                     mi.byte1.byte = c;
00039                     /*
00040                      * Check and reset a buffer if state is wrong.
00041                      */
00042                     if (mi.byte1.bit.always1 == 0) {
00043                         cnt = -1;
00044                         while (0 <= ps2ms.getc()) {
00045                         }
00046                     }
00047                     break;
00048                 case 1:
00049                     mi.byte2.byte = c;
00050                     break;
00051                 case 2:
00052                     mi.byte3.byte = c;
00053                     break;
00054                 case 3:
00055                     mi.byte4.byte = c;
00056                     /*
00057                      * Store a event data.
00058                      */
00059                     if (mi.byte1.bit.overflowX) {
00060                         printf("OverflowX!\n\r");
00061                     }
00062                     if (mi.byte1.bit.overflowY) {
00063                         printf("OverflowY!\n\r");
00064                     }
00065                     if (mi.byte1.bit.signX) {
00066                         printf("SignX!\n\r");
00067                     }
00068                     if (mi.byte1.bit.signY) {
00069                         printf("SignY!\n\r");
00070                     }
00071                     p->left = mi.byte1.bit.btnLeft ? true : false;
00072                     p->center = mi.byte1.bit.btnCenter ? true : false;
00073                     p->right = mi.byte1.bit.btnRight ? true : false;
00074                     p->x = mi.byte1.bit.signX ? (-256 + mi.byte2.byte) : mi.byte2.byte;
00075                     p->y = mi.byte1.bit.signY ? (-256 + mi.byte3.byte) : mi.byte3.byte;
00076                     p->z = mi.byte4.bit.signZ ? (-128 + mi.byte4.bit.value) : mi.byte4.bit.value;
00077                     emit = true;
00078                     break;
00079             }
00080             cnt++;
00081         }
00082     }
00083     return emit;
00084 }