PS2 Library
PS2Mouse.cpp@0:ce15490e89e9, 2011-02-27 (annotated)
- Committer:
- wjohnsto
- Date:
- Sun Feb 27 23:34:31 2011 +0000
- Revision:
- 0:ce15490e89e9
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wjohnsto | 0:ce15490e89e9 | 1 | /** |
wjohnsto | 0:ce15490e89e9 | 2 | * PS/2 mouse interface control class (Version 0.0.1) |
wjohnsto | 0:ce15490e89e9 | 3 | * |
wjohnsto | 0:ce15490e89e9 | 4 | * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems) |
wjohnsto | 0:ce15490e89e9 | 5 | * http://shinta.main.jp/ |
wjohnsto | 0:ce15490e89e9 | 6 | */ |
wjohnsto | 0:ce15490e89e9 | 7 | #include "PS2Mouse.h" |
wjohnsto | 0:ce15490e89e9 | 8 | |
wjohnsto | 0:ce15490e89e9 | 9 | PS2Mouse::PS2Mouse(PinName clk_pin, PinName dat_pin) |
wjohnsto | 0:ce15490e89e9 | 10 | : ps2ms_init(clk_pin, dat_pin), ps2ms(clk_pin, dat_pin) { |
wjohnsto | 0:ce15490e89e9 | 11 | cnt = 0; |
wjohnsto | 0:ce15490e89e9 | 12 | } |
wjohnsto | 0:ce15490e89e9 | 13 | |
wjohnsto | 0:ce15490e89e9 | 14 | PS2Mouse::~PS2Mouse() { |
wjohnsto | 0:ce15490e89e9 | 15 | } |
wjohnsto | 0:ce15490e89e9 | 16 | |
wjohnsto | 0:ce15490e89e9 | 17 | bool PS2Mouse::processing(mouse_event_t *p) { |
wjohnsto | 0:ce15490e89e9 | 18 | bool emit = false; |
wjohnsto | 0:ce15490e89e9 | 19 | for (int i = 0; i < 4; i++) { |
wjohnsto | 0:ce15490e89e9 | 20 | const int c = ps2ms.getc(); |
wjohnsto | 0:ce15490e89e9 | 21 | if (0 <= c) { |
wjohnsto | 0:ce15490e89e9 | 22 | switch (cnt % 4) { |
wjohnsto | 0:ce15490e89e9 | 23 | case 0: |
wjohnsto | 0:ce15490e89e9 | 24 | mi.byte1.byte = c; |
wjohnsto | 0:ce15490e89e9 | 25 | /* |
wjohnsto | 0:ce15490e89e9 | 26 | * Check and reset a buffer if state is wrong. |
wjohnsto | 0:ce15490e89e9 | 27 | */ |
wjohnsto | 0:ce15490e89e9 | 28 | if (mi.byte1.bit.always1 == 0) { |
wjohnsto | 0:ce15490e89e9 | 29 | cnt = 0; |
wjohnsto | 0:ce15490e89e9 | 30 | while (0 <= ps2ms.getc()) { |
wjohnsto | 0:ce15490e89e9 | 31 | } |
wjohnsto | 0:ce15490e89e9 | 32 | } |
wjohnsto | 0:ce15490e89e9 | 33 | break; |
wjohnsto | 0:ce15490e89e9 | 34 | case 1: |
wjohnsto | 0:ce15490e89e9 | 35 | mi.byte2.byte = c; |
wjohnsto | 0:ce15490e89e9 | 36 | break; |
wjohnsto | 0:ce15490e89e9 | 37 | case 2: |
wjohnsto | 0:ce15490e89e9 | 38 | mi.byte3.byte = c; |
wjohnsto | 0:ce15490e89e9 | 39 | break; |
wjohnsto | 0:ce15490e89e9 | 40 | case 3: |
wjohnsto | 0:ce15490e89e9 | 41 | mi.byte4.byte = c; |
wjohnsto | 0:ce15490e89e9 | 42 | /* |
wjohnsto | 0:ce15490e89e9 | 43 | * Store a event data. |
wjohnsto | 0:ce15490e89e9 | 44 | */ |
wjohnsto | 0:ce15490e89e9 | 45 | p->left = mi.byte1.bit.btnLeft ? true : false; |
wjohnsto | 0:ce15490e89e9 | 46 | p->center = mi.byte1.bit.btnCenter ? true : false; |
wjohnsto | 0:ce15490e89e9 | 47 | p->right = mi.byte1.bit.btnRight ? true : false; |
wjohnsto | 0:ce15490e89e9 | 48 | p->x = mi.byte1.bit.signX ? (-256 + mi.byte2.byte) : mi.byte2.byte; |
wjohnsto | 0:ce15490e89e9 | 49 | p->y = mi.byte1.bit.signY ? (-256 + mi.byte3.byte) : mi.byte3.byte; |
wjohnsto | 0:ce15490e89e9 | 50 | p->z = mi.byte4.bit.signZ ? (-128 + mi.byte4.bit.value) : mi.byte4.bit.value; |
wjohnsto | 0:ce15490e89e9 | 51 | emit = true; |
wjohnsto | 0:ce15490e89e9 | 52 | break; |
wjohnsto | 0:ce15490e89e9 | 53 | } |
wjohnsto | 0:ce15490e89e9 | 54 | cnt++; |
wjohnsto | 0:ce15490e89e9 | 55 | } |
wjohnsto | 0:ce15490e89e9 | 56 | } |
wjohnsto | 0:ce15490e89e9 | 57 | return emit; |
wjohnsto | 0:ce15490e89e9 | 58 | } |