![](/media/cache/profiles/1006ff12c465532f8c574aeaa4461b16.50x50_q85.jpg)
A test program for PS2 library.
mylib/PS2/PS2MS.cpp@4:47b51250a168, 2010-10-13 (annotated)
- Committer:
- shintamainjp
- Date:
- Wed Oct 13 10:50:55 2010 +0000
- Revision:
- 4:47b51250a168
First version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shintamainjp | 4:47b51250a168 | 1 | /** |
shintamainjp | 4:47b51250a168 | 2 | * PS/2 mouse interface control class (Version 0.0.1) |
shintamainjp | 4:47b51250a168 | 3 | * |
shintamainjp | 4:47b51250a168 | 4 | * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems) |
shintamainjp | 4:47b51250a168 | 5 | * http://shinta.main.jp/ |
shintamainjp | 4:47b51250a168 | 6 | */ |
shintamainjp | 4:47b51250a168 | 7 | |
shintamainjp | 4:47b51250a168 | 8 | #include "PS2MS.h" |
shintamainjp | 4:47b51250a168 | 9 | |
shintamainjp | 4:47b51250a168 | 10 | /** |
shintamainjp | 4:47b51250a168 | 11 | * Create. |
shintamainjp | 4:47b51250a168 | 12 | * |
shintamainjp | 4:47b51250a168 | 13 | * @param clk_pin Clock pin. |
shintamainjp | 4:47b51250a168 | 14 | * @param dat_pin Data pin. |
shintamainjp | 4:47b51250a168 | 15 | */ |
shintamainjp | 4:47b51250a168 | 16 | PS2MS::PS2MS(PinName clk_pin, PinName dat_pin) |
shintamainjp | 4:47b51250a168 | 17 | : clk(clk_pin), dat(dat_pin) { |
shintamainjp | 4:47b51250a168 | 18 | init_work(); |
shintamainjp | 4:47b51250a168 | 19 | clk.fall(this, &PS2MS::func_fall); |
shintamainjp | 4:47b51250a168 | 20 | timeout = 1; |
shintamainjp | 4:47b51250a168 | 21 | } |
shintamainjp | 4:47b51250a168 | 22 | |
shintamainjp | 4:47b51250a168 | 23 | /** |
shintamainjp | 4:47b51250a168 | 24 | * Destory. |
shintamainjp | 4:47b51250a168 | 25 | */ |
shintamainjp | 4:47b51250a168 | 26 | PS2MS::~PS2MS() { |
shintamainjp | 4:47b51250a168 | 27 | wdt.detach(); |
shintamainjp | 4:47b51250a168 | 28 | } |
shintamainjp | 4:47b51250a168 | 29 | |
shintamainjp | 4:47b51250a168 | 30 | int PS2MS::getc() { |
shintamainjp | 4:47b51250a168 | 31 | tot.reset(); |
shintamainjp | 4:47b51250a168 | 32 | tot.start(); |
shintamainjp | 4:47b51250a168 | 33 | while (work.cStart == work.cEnd) { |
shintamainjp | 4:47b51250a168 | 34 | wait_ms(1); |
shintamainjp | 4:47b51250a168 | 35 | if ((timeout > 0) && (tot.read_ms() > timeout)) { |
shintamainjp | 4:47b51250a168 | 36 | // printf("Timeout occured.\n"); |
shintamainjp | 4:47b51250a168 | 37 | return EOF; |
shintamainjp | 4:47b51250a168 | 38 | } |
shintamainjp | 4:47b51250a168 | 39 | } |
shintamainjp | 4:47b51250a168 | 40 | tot.stop(); |
shintamainjp | 4:47b51250a168 | 41 | |
shintamainjp | 4:47b51250a168 | 42 | char c = work.buffer[work.cStart++]; |
shintamainjp | 4:47b51250a168 | 43 | work.cStart = work.cStart % RINGBUFSIZ; |
shintamainjp | 4:47b51250a168 | 44 | |
shintamainjp | 4:47b51250a168 | 45 | return c; |
shintamainjp | 4:47b51250a168 | 46 | } |
shintamainjp | 4:47b51250a168 | 47 | |
shintamainjp | 4:47b51250a168 | 48 | /** |
shintamainjp | 4:47b51250a168 | 49 | * Set timeout. |
shintamainjp | 4:47b51250a168 | 50 | * |
shintamainjp | 4:47b51250a168 | 51 | * @param ms Timeout ms. |
shintamainjp | 4:47b51250a168 | 52 | */ |
shintamainjp | 4:47b51250a168 | 53 | void PS2MS::setTimeout(int ms) { |
shintamainjp | 4:47b51250a168 | 54 | timeout = ms; |
shintamainjp | 4:47b51250a168 | 55 | } |
shintamainjp | 4:47b51250a168 | 56 | |
shintamainjp | 4:47b51250a168 | 57 | void PS2MS::func_timeout(void) { |
shintamainjp | 4:47b51250a168 | 58 | work.bitcnt = 0; |
shintamainjp | 4:47b51250a168 | 59 | } |
shintamainjp | 4:47b51250a168 | 60 | |
shintamainjp | 4:47b51250a168 | 61 | void PS2MS::func_fall(void) { |
shintamainjp | 4:47b51250a168 | 62 | int oddpar = 0; |
shintamainjp | 4:47b51250a168 | 63 | /* |
shintamainjp | 4:47b51250a168 | 64 | */ |
shintamainjp | 4:47b51250a168 | 65 | switch (work.bitcnt) { |
shintamainjp | 4:47b51250a168 | 66 | case 0: |
shintamainjp | 4:47b51250a168 | 67 | /* |
shintamainjp | 4:47b51250a168 | 68 | * Start bit. |
shintamainjp | 4:47b51250a168 | 69 | */ |
shintamainjp | 4:47b51250a168 | 70 | if (dat.read() != 0) { |
shintamainjp | 4:47b51250a168 | 71 | // printf("Illegal start bit condition.\n"); |
shintamainjp | 4:47b51250a168 | 72 | } |
shintamainjp | 4:47b51250a168 | 73 | work.bitcnt++; |
shintamainjp | 4:47b51250a168 | 74 | break; |
shintamainjp | 4:47b51250a168 | 75 | case 9: |
shintamainjp | 4:47b51250a168 | 76 | /* |
shintamainjp | 4:47b51250a168 | 77 | * Parity bit. |
shintamainjp | 4:47b51250a168 | 78 | */ |
shintamainjp | 4:47b51250a168 | 79 | for (int i = 0; i < 8; i++) { |
shintamainjp | 4:47b51250a168 | 80 | if ((work.buffer[work.cEnd] & (1 << i)) != 0) { |
shintamainjp | 4:47b51250a168 | 81 | oddpar++; |
shintamainjp | 4:47b51250a168 | 82 | } |
shintamainjp | 4:47b51250a168 | 83 | } |
shintamainjp | 4:47b51250a168 | 84 | if (dat.read() == 1) { |
shintamainjp | 4:47b51250a168 | 85 | oddpar++; |
shintamainjp | 4:47b51250a168 | 86 | } |
shintamainjp | 4:47b51250a168 | 87 | if ((oddpar % 2) != 1) { |
shintamainjp | 4:47b51250a168 | 88 | // printf("Data parity error.\n"); |
shintamainjp | 4:47b51250a168 | 89 | } |
shintamainjp | 4:47b51250a168 | 90 | work.bitcnt++; |
shintamainjp | 4:47b51250a168 | 91 | break; |
shintamainjp | 4:47b51250a168 | 92 | case 10: |
shintamainjp | 4:47b51250a168 | 93 | /* |
shintamainjp | 4:47b51250a168 | 94 | * Stop bit. |
shintamainjp | 4:47b51250a168 | 95 | */ |
shintamainjp | 4:47b51250a168 | 96 | if (dat.read() != 1) { |
shintamainjp | 4:47b51250a168 | 97 | // printf("Illegal stop bit condition.\n"); |
shintamainjp | 4:47b51250a168 | 98 | } |
shintamainjp | 4:47b51250a168 | 99 | if (work.cStart != ((work.cEnd + 1) % RINGBUFSIZ)) { |
shintamainjp | 4:47b51250a168 | 100 | work.cEnd++; |
shintamainjp | 4:47b51250a168 | 101 | work.cEnd = work.cEnd % RINGBUFSIZ; |
shintamainjp | 4:47b51250a168 | 102 | work.bitcnt = 0; |
shintamainjp | 4:47b51250a168 | 103 | } else { |
shintamainjp | 4:47b51250a168 | 104 | // printf("Buffer overrun.\n"); |
shintamainjp | 4:47b51250a168 | 105 | } |
shintamainjp | 4:47b51250a168 | 106 | break; |
shintamainjp | 4:47b51250a168 | 107 | default: |
shintamainjp | 4:47b51250a168 | 108 | if ((1 <= work.bitcnt) && (work.bitcnt <= 8)) { |
shintamainjp | 4:47b51250a168 | 109 | /* |
shintamainjp | 4:47b51250a168 | 110 | * data bit. |
shintamainjp | 4:47b51250a168 | 111 | */ |
shintamainjp | 4:47b51250a168 | 112 | if (dat.read() == 1) { |
shintamainjp | 4:47b51250a168 | 113 | work.buffer[work.cEnd] |= (1 << (work.bitcnt - 1)); |
shintamainjp | 4:47b51250a168 | 114 | } else { |
shintamainjp | 4:47b51250a168 | 115 | work.buffer[work.cEnd] &= ~(1 << (work.bitcnt - 1)); |
shintamainjp | 4:47b51250a168 | 116 | } |
shintamainjp | 4:47b51250a168 | 117 | work.bitcnt++; |
shintamainjp | 4:47b51250a168 | 118 | } else { |
shintamainjp | 4:47b51250a168 | 119 | /* |
shintamainjp | 4:47b51250a168 | 120 | * Illegal internal state. |
shintamainjp | 4:47b51250a168 | 121 | */ |
shintamainjp | 4:47b51250a168 | 122 | // printf("Illegal internal state found.\n"); |
shintamainjp | 4:47b51250a168 | 123 | init_work(); |
shintamainjp | 4:47b51250a168 | 124 | } |
shintamainjp | 4:47b51250a168 | 125 | break; |
shintamainjp | 4:47b51250a168 | 126 | } |
shintamainjp | 4:47b51250a168 | 127 | wdt.detach(); |
shintamainjp | 4:47b51250a168 | 128 | wdt.attach_us(this, &PS2MS::func_timeout, 250); |
shintamainjp | 4:47b51250a168 | 129 | } |
shintamainjp | 4:47b51250a168 | 130 | |
shintamainjp | 4:47b51250a168 | 131 | void PS2MS::init_work(void) { |
shintamainjp | 4:47b51250a168 | 132 | work.bitcnt = 0; |
shintamainjp | 4:47b51250a168 | 133 | work.cStart = 0; |
shintamainjp | 4:47b51250a168 | 134 | work.cEnd = 0; |
shintamainjp | 4:47b51250a168 | 135 | } |