![](/media/cache/profiles/1006ff12c465532f8c574aeaa4461b16.50x50_q85.jpg)
A test program for PS2 library.
mylib/PS2/PS2KB.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 keyboard 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 "PS2KB.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 | PS2KB::PS2KB(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, &PS2KB::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 | PS2KB::~PS2KB() { |
shintamainjp | 4:47b51250a168 | 27 | wdt.detach(); |
shintamainjp | 4:47b51250a168 | 28 | } |
shintamainjp | 4:47b51250a168 | 29 | |
shintamainjp | 4:47b51250a168 | 30 | /** |
shintamainjp | 4:47b51250a168 | 31 | * Get a data from a PS/2 device. |
shintamainjp | 4:47b51250a168 | 32 | * |
shintamainjp | 4:47b51250a168 | 33 | * @return A data from a PS/2 device. |
shintamainjp | 4:47b51250a168 | 34 | */ |
shintamainjp | 4:47b51250a168 | 35 | int PS2KB::getc() { |
shintamainjp | 4:47b51250a168 | 36 | tot.reset(); |
shintamainjp | 4:47b51250a168 | 37 | tot.start(); |
shintamainjp | 4:47b51250a168 | 38 | while (work.cStart == work.cEnd) { |
shintamainjp | 4:47b51250a168 | 39 | wait_ms(1); |
shintamainjp | 4:47b51250a168 | 40 | if ((timeout > 0) && (tot.read_ms() > timeout)) { |
shintamainjp | 4:47b51250a168 | 41 | // printf("Timeout occured.\n"); |
shintamainjp | 4:47b51250a168 | 42 | return EOF; |
shintamainjp | 4:47b51250a168 | 43 | } |
shintamainjp | 4:47b51250a168 | 44 | } |
shintamainjp | 4:47b51250a168 | 45 | tot.stop(); |
shintamainjp | 4:47b51250a168 | 46 | |
shintamainjp | 4:47b51250a168 | 47 | char c = work.buffer[work.cStart++]; |
shintamainjp | 4:47b51250a168 | 48 | work.cStart = work.cStart % RINGBUFSIZ; |
shintamainjp | 4:47b51250a168 | 49 | |
shintamainjp | 4:47b51250a168 | 50 | return c; |
shintamainjp | 4:47b51250a168 | 51 | } |
shintamainjp | 4:47b51250a168 | 52 | |
shintamainjp | 4:47b51250a168 | 53 | /** |
shintamainjp | 4:47b51250a168 | 54 | * Set timeout. |
shintamainjp | 4:47b51250a168 | 55 | * |
shintamainjp | 4:47b51250a168 | 56 | * @param ms Timeout ms. |
shintamainjp | 4:47b51250a168 | 57 | */ |
shintamainjp | 4:47b51250a168 | 58 | void PS2KB::setTimeout(int ms) { |
shintamainjp | 4:47b51250a168 | 59 | timeout = ms; |
shintamainjp | 4:47b51250a168 | 60 | } |
shintamainjp | 4:47b51250a168 | 61 | |
shintamainjp | 4:47b51250a168 | 62 | void PS2KB::func_timeout(void) { |
shintamainjp | 4:47b51250a168 | 63 | work.bitcnt = 0; |
shintamainjp | 4:47b51250a168 | 64 | } |
shintamainjp | 4:47b51250a168 | 65 | |
shintamainjp | 4:47b51250a168 | 66 | void PS2KB::func_fall(void) { |
shintamainjp | 4:47b51250a168 | 67 | int oddpar = 0; |
shintamainjp | 4:47b51250a168 | 68 | /* |
shintamainjp | 4:47b51250a168 | 69 | */ |
shintamainjp | 4:47b51250a168 | 70 | switch (work.bitcnt) { |
shintamainjp | 4:47b51250a168 | 71 | case 0: |
shintamainjp | 4:47b51250a168 | 72 | /* |
shintamainjp | 4:47b51250a168 | 73 | * Start bit. |
shintamainjp | 4:47b51250a168 | 74 | */ |
shintamainjp | 4:47b51250a168 | 75 | if (dat.read() != 0) { |
shintamainjp | 4:47b51250a168 | 76 | // printf("Illegal start bit condition.\n"); |
shintamainjp | 4:47b51250a168 | 77 | } |
shintamainjp | 4:47b51250a168 | 78 | work.bitcnt++; |
shintamainjp | 4:47b51250a168 | 79 | break; |
shintamainjp | 4:47b51250a168 | 80 | case 9: |
shintamainjp | 4:47b51250a168 | 81 | /* |
shintamainjp | 4:47b51250a168 | 82 | * Parity bit. |
shintamainjp | 4:47b51250a168 | 83 | */ |
shintamainjp | 4:47b51250a168 | 84 | for (int i = 0; i < 8; i++) { |
shintamainjp | 4:47b51250a168 | 85 | if ((work.buffer[work.cEnd] & (1 << i)) != 0) { |
shintamainjp | 4:47b51250a168 | 86 | oddpar++; |
shintamainjp | 4:47b51250a168 | 87 | } |
shintamainjp | 4:47b51250a168 | 88 | } |
shintamainjp | 4:47b51250a168 | 89 | if (dat.read() == 1) { |
shintamainjp | 4:47b51250a168 | 90 | oddpar++; |
shintamainjp | 4:47b51250a168 | 91 | } |
shintamainjp | 4:47b51250a168 | 92 | if ((oddpar % 2) != 1) { |
shintamainjp | 4:47b51250a168 | 93 | // printf("Data parity error.\n"); |
shintamainjp | 4:47b51250a168 | 94 | } |
shintamainjp | 4:47b51250a168 | 95 | work.bitcnt++; |
shintamainjp | 4:47b51250a168 | 96 | break; |
shintamainjp | 4:47b51250a168 | 97 | case 10: |
shintamainjp | 4:47b51250a168 | 98 | /* |
shintamainjp | 4:47b51250a168 | 99 | * Stop bit. |
shintamainjp | 4:47b51250a168 | 100 | */ |
shintamainjp | 4:47b51250a168 | 101 | if (dat.read() != 1) { |
shintamainjp | 4:47b51250a168 | 102 | // printf("Illegal stop bit condition.\n"); |
shintamainjp | 4:47b51250a168 | 103 | } |
shintamainjp | 4:47b51250a168 | 104 | if (work.cStart != ((work.cEnd + 1) % RINGBUFSIZ)) { |
shintamainjp | 4:47b51250a168 | 105 | work.cEnd++; |
shintamainjp | 4:47b51250a168 | 106 | work.cEnd = work.cEnd % RINGBUFSIZ; |
shintamainjp | 4:47b51250a168 | 107 | work.bitcnt = 0; |
shintamainjp | 4:47b51250a168 | 108 | } else { |
shintamainjp | 4:47b51250a168 | 109 | // printf("Buffer overrun.\n"); |
shintamainjp | 4:47b51250a168 | 110 | } |
shintamainjp | 4:47b51250a168 | 111 | break; |
shintamainjp | 4:47b51250a168 | 112 | default: |
shintamainjp | 4:47b51250a168 | 113 | if ((1 <= work.bitcnt) && (work.bitcnt <= 8)) { |
shintamainjp | 4:47b51250a168 | 114 | /* |
shintamainjp | 4:47b51250a168 | 115 | * data bit. |
shintamainjp | 4:47b51250a168 | 116 | */ |
shintamainjp | 4:47b51250a168 | 117 | if (dat.read() == 1) { |
shintamainjp | 4:47b51250a168 | 118 | work.buffer[work.cEnd] |= (1 << (work.bitcnt - 1)); |
shintamainjp | 4:47b51250a168 | 119 | } else { |
shintamainjp | 4:47b51250a168 | 120 | work.buffer[work.cEnd] &= ~(1 << (work.bitcnt - 1)); |
shintamainjp | 4:47b51250a168 | 121 | } |
shintamainjp | 4:47b51250a168 | 122 | work.bitcnt++; |
shintamainjp | 4:47b51250a168 | 123 | } else { |
shintamainjp | 4:47b51250a168 | 124 | /* |
shintamainjp | 4:47b51250a168 | 125 | * Illegal internal state. |
shintamainjp | 4:47b51250a168 | 126 | */ |
shintamainjp | 4:47b51250a168 | 127 | // printf("Illegal internal state found.\n"); |
shintamainjp | 4:47b51250a168 | 128 | init_work(); |
shintamainjp | 4:47b51250a168 | 129 | } |
shintamainjp | 4:47b51250a168 | 130 | break; |
shintamainjp | 4:47b51250a168 | 131 | } |
shintamainjp | 4:47b51250a168 | 132 | wdt.detach(); |
shintamainjp | 4:47b51250a168 | 133 | wdt.attach_us(this, &PS2KB::func_timeout, 250); |
shintamainjp | 4:47b51250a168 | 134 | } |
shintamainjp | 4:47b51250a168 | 135 | |
shintamainjp | 4:47b51250a168 | 136 | void PS2KB::init_work(void) { |
shintamainjp | 4:47b51250a168 | 137 | work.bitcnt = 0; |
shintamainjp | 4:47b51250a168 | 138 | work.cStart = 0; |
shintamainjp | 4:47b51250a168 | 139 | work.cEnd = 0; |
shintamainjp | 4:47b51250a168 | 140 | } |