PS2 Library

Dependents:   Pong Brickbreaker

Committer:
wjohnsto
Date:
Sun Feb 27 23:34:31 2011 +0000
Revision:
0:ce15490e89e9

        

Who changed what in which revision?

UserRevisionLine numberNew 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
wjohnsto 0:ce15490e89e9 8 #include "PS2MS.h"
wjohnsto 0:ce15490e89e9 9
wjohnsto 0:ce15490e89e9 10 /**
wjohnsto 0:ce15490e89e9 11 * Create.
wjohnsto 0:ce15490e89e9 12 *
wjohnsto 0:ce15490e89e9 13 * @param clk_pin Clock pin.
wjohnsto 0:ce15490e89e9 14 * @param dat_pin Data pin.
wjohnsto 0:ce15490e89e9 15 */
wjohnsto 0:ce15490e89e9 16 PS2MS::PS2MS(PinName clk_pin, PinName dat_pin)
wjohnsto 0:ce15490e89e9 17 : clk(clk_pin), dat(dat_pin) {
wjohnsto 0:ce15490e89e9 18 init_work();
wjohnsto 0:ce15490e89e9 19 clk.fall(this, &PS2MS::func_fall);
wjohnsto 0:ce15490e89e9 20 timeout = 1;
wjohnsto 0:ce15490e89e9 21 }
wjohnsto 0:ce15490e89e9 22
wjohnsto 0:ce15490e89e9 23 /**
wjohnsto 0:ce15490e89e9 24 * Destory.
wjohnsto 0:ce15490e89e9 25 */
wjohnsto 0:ce15490e89e9 26 PS2MS::~PS2MS() {
wjohnsto 0:ce15490e89e9 27 wdt.detach();
wjohnsto 0:ce15490e89e9 28 }
wjohnsto 0:ce15490e89e9 29
wjohnsto 0:ce15490e89e9 30 int PS2MS::getc() {
wjohnsto 0:ce15490e89e9 31 tot.reset();
wjohnsto 0:ce15490e89e9 32 tot.start();
wjohnsto 0:ce15490e89e9 33 while (work.cStart == work.cEnd) {
wjohnsto 0:ce15490e89e9 34 wait_ms(1);
wjohnsto 0:ce15490e89e9 35 if ((timeout > 0) && (tot.read_ms() > timeout)) {
wjohnsto 0:ce15490e89e9 36 // printf("Timeout occured.\n");
wjohnsto 0:ce15490e89e9 37 return EOF;
wjohnsto 0:ce15490e89e9 38 }
wjohnsto 0:ce15490e89e9 39 }
wjohnsto 0:ce15490e89e9 40 tot.stop();
wjohnsto 0:ce15490e89e9 41
wjohnsto 0:ce15490e89e9 42 char c = work.buffer[work.cStart++];
wjohnsto 0:ce15490e89e9 43 work.cStart = work.cStart % RINGBUFSIZ;
wjohnsto 0:ce15490e89e9 44
wjohnsto 0:ce15490e89e9 45 return c;
wjohnsto 0:ce15490e89e9 46 }
wjohnsto 0:ce15490e89e9 47
wjohnsto 0:ce15490e89e9 48 /**
wjohnsto 0:ce15490e89e9 49 * Set timeout.
wjohnsto 0:ce15490e89e9 50 *
wjohnsto 0:ce15490e89e9 51 * @param ms Timeout ms.
wjohnsto 0:ce15490e89e9 52 */
wjohnsto 0:ce15490e89e9 53 void PS2MS::setTimeout(int ms) {
wjohnsto 0:ce15490e89e9 54 timeout = ms;
wjohnsto 0:ce15490e89e9 55 }
wjohnsto 0:ce15490e89e9 56
wjohnsto 0:ce15490e89e9 57 void PS2MS::func_timeout(void) {
wjohnsto 0:ce15490e89e9 58 work.bitcnt = 0;
wjohnsto 0:ce15490e89e9 59 }
wjohnsto 0:ce15490e89e9 60
wjohnsto 0:ce15490e89e9 61 void PS2MS::func_fall(void) {
wjohnsto 0:ce15490e89e9 62 int oddpar = 0;
wjohnsto 0:ce15490e89e9 63 /*
wjohnsto 0:ce15490e89e9 64 */
wjohnsto 0:ce15490e89e9 65 switch (work.bitcnt) {
wjohnsto 0:ce15490e89e9 66 case 0:
wjohnsto 0:ce15490e89e9 67 /*
wjohnsto 0:ce15490e89e9 68 * Start bit.
wjohnsto 0:ce15490e89e9 69 */
wjohnsto 0:ce15490e89e9 70 if (dat.read() != 0) {
wjohnsto 0:ce15490e89e9 71 // printf("Illegal start bit condition.\n");
wjohnsto 0:ce15490e89e9 72 }
wjohnsto 0:ce15490e89e9 73 work.bitcnt++;
wjohnsto 0:ce15490e89e9 74 break;
wjohnsto 0:ce15490e89e9 75 case 9:
wjohnsto 0:ce15490e89e9 76 /*
wjohnsto 0:ce15490e89e9 77 * Parity bit.
wjohnsto 0:ce15490e89e9 78 */
wjohnsto 0:ce15490e89e9 79 for (int i = 0; i < 8; i++) {
wjohnsto 0:ce15490e89e9 80 if ((work.buffer[work.cEnd] & (1 << i)) != 0) {
wjohnsto 0:ce15490e89e9 81 oddpar++;
wjohnsto 0:ce15490e89e9 82 }
wjohnsto 0:ce15490e89e9 83 }
wjohnsto 0:ce15490e89e9 84 if (dat.read() == 1) {
wjohnsto 0:ce15490e89e9 85 oddpar++;
wjohnsto 0:ce15490e89e9 86 }
wjohnsto 0:ce15490e89e9 87 if ((oddpar % 2) != 1) {
wjohnsto 0:ce15490e89e9 88 // printf("Data parity error.\n");
wjohnsto 0:ce15490e89e9 89 }
wjohnsto 0:ce15490e89e9 90 work.bitcnt++;
wjohnsto 0:ce15490e89e9 91 break;
wjohnsto 0:ce15490e89e9 92 case 10:
wjohnsto 0:ce15490e89e9 93 /*
wjohnsto 0:ce15490e89e9 94 * Stop bit.
wjohnsto 0:ce15490e89e9 95 */
wjohnsto 0:ce15490e89e9 96 if (dat.read() != 1) {
wjohnsto 0:ce15490e89e9 97 // printf("Illegal stop bit condition.\n");
wjohnsto 0:ce15490e89e9 98 }
wjohnsto 0:ce15490e89e9 99 if (work.cStart != ((work.cEnd + 1) % RINGBUFSIZ)) {
wjohnsto 0:ce15490e89e9 100 work.cEnd++;
wjohnsto 0:ce15490e89e9 101 work.cEnd = work.cEnd % RINGBUFSIZ;
wjohnsto 0:ce15490e89e9 102 work.bitcnt = 0;
wjohnsto 0:ce15490e89e9 103 } else {
wjohnsto 0:ce15490e89e9 104 // printf("Buffer overrun.\n");
wjohnsto 0:ce15490e89e9 105 }
wjohnsto 0:ce15490e89e9 106 break;
wjohnsto 0:ce15490e89e9 107 default:
wjohnsto 0:ce15490e89e9 108 if ((1 <= work.bitcnt) && (work.bitcnt <= 8)) {
wjohnsto 0:ce15490e89e9 109 /*
wjohnsto 0:ce15490e89e9 110 * data bit.
wjohnsto 0:ce15490e89e9 111 */
wjohnsto 0:ce15490e89e9 112 if (dat.read() == 1) {
wjohnsto 0:ce15490e89e9 113 work.buffer[work.cEnd] |= (1 << (work.bitcnt - 1));
wjohnsto 0:ce15490e89e9 114 } else {
wjohnsto 0:ce15490e89e9 115 work.buffer[work.cEnd] &= ~(1 << (work.bitcnt - 1));
wjohnsto 0:ce15490e89e9 116 }
wjohnsto 0:ce15490e89e9 117 work.bitcnt++;
wjohnsto 0:ce15490e89e9 118 } else {
wjohnsto 0:ce15490e89e9 119 /*
wjohnsto 0:ce15490e89e9 120 * Illegal internal state.
wjohnsto 0:ce15490e89e9 121 */
wjohnsto 0:ce15490e89e9 122 // printf("Illegal internal state found.\n");
wjohnsto 0:ce15490e89e9 123 init_work();
wjohnsto 0:ce15490e89e9 124 }
wjohnsto 0:ce15490e89e9 125 break;
wjohnsto 0:ce15490e89e9 126 }
wjohnsto 0:ce15490e89e9 127 wdt.detach();
wjohnsto 0:ce15490e89e9 128 wdt.attach_us(this, &PS2MS::func_timeout, 250);
wjohnsto 0:ce15490e89e9 129 }
wjohnsto 0:ce15490e89e9 130
wjohnsto 0:ce15490e89e9 131 void PS2MS::init_work(void) {
wjohnsto 0:ce15490e89e9 132 work.bitcnt = 0;
wjohnsto 0:ce15490e89e9 133 work.cStart = 0;
wjohnsto 0:ce15490e89e9 134 work.cEnd = 0;
wjohnsto 0:ce15490e89e9 135 }