s
Fork of keypad by
keypad.cpp@6:8e4267765923, 2012-01-31 (annotated)
- Committer:
- yoonghm
- Date:
- Tue Jan 31 00:22:08 2012 +0000
- Revision:
- 6:8e4267765923
- Parent:
- 0:2df66331c109
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yoonghm | 0:2df66331c109 | 1 | #include "keypad.h" |
yoonghm | 0:2df66331c109 | 2 | |
yoonghm | 0:2df66331c109 | 3 | Keypad::Keypad(PinName row3, PinName row2, PinName row1, PinName row0, |
yoonghm | 0:2df66331c109 | 4 | PinName col3, PinName col2, PinName col1, PinName col0, |
yoonghm | 0:2df66331c109 | 5 | int debounce_ms): |
yoonghm | 0:2df66331c109 | 6 | _row0(row0), _row1(row1), _row2(row2), _row3(row3), |
yoonghm | 0:2df66331c109 | 7 | _cols(col0, col1, col2, col3) { |
yoonghm | 0:2df66331c109 | 8 | _debounce = debounce_ms; |
yoonghm | 0:2df66331c109 | 9 | _setupRiseTrigger(); |
yoonghm | 0:2df66331c109 | 10 | } |
yoonghm | 0:2df66331c109 | 11 | |
yoonghm | 0:2df66331c109 | 12 | void Keypad::Start(void) { |
yoonghm | 0:2df66331c109 | 13 | _cols = 0x0F; |
yoonghm | 0:2df66331c109 | 14 | } |
yoonghm | 0:2df66331c109 | 15 | |
yoonghm | 0:2df66331c109 | 16 | void Keypad::Stop(void) { |
yoonghm | 0:2df66331c109 | 17 | _cols = 0x00; |
yoonghm | 0:2df66331c109 | 18 | } |
yoonghm | 0:2df66331c109 | 19 | |
yoonghm | 0:2df66331c109 | 20 | void Keypad::CallAfterInput(uint32_t (*fptr)(uint32_t index)) { |
yoonghm | 0:2df66331c109 | 21 | _input.attach(fptr); |
yoonghm | 0:2df66331c109 | 22 | } |
yoonghm | 0:2df66331c109 | 23 | |
yoonghm | 0:2df66331c109 | 24 | void Keypad::_callback(int row, InterruptIn &therow) { |
yoonghm | 0:2df66331c109 | 25 | wait_ms(_debounce); |
yoonghm | 0:2df66331c109 | 26 | if (therow != 1) |
yoonghm | 0:2df66331c109 | 27 | return; |
yoonghm | 0:2df66331c109 | 28 | |
yoonghm | 0:2df66331c109 | 29 | int c = -1; |
yoonghm | 0:2df66331c109 | 30 | _cols = _cols & 0x0E; |
yoonghm | 0:2df66331c109 | 31 | if (therow == 0) |
yoonghm | 0:2df66331c109 | 32 | c = 0; |
yoonghm | 0:2df66331c109 | 33 | else { |
yoonghm | 0:2df66331c109 | 34 | _cols = _cols & 0x0D; |
yoonghm | 0:2df66331c109 | 35 | if (therow == 0) |
yoonghm | 0:2df66331c109 | 36 | c = 1; |
yoonghm | 0:2df66331c109 | 37 | else { |
yoonghm | 0:2df66331c109 | 38 | _cols = _cols & 0x0B; |
yoonghm | 0:2df66331c109 | 39 | if (therow == 0) |
yoonghm | 0:2df66331c109 | 40 | c = 2; |
yoonghm | 0:2df66331c109 | 41 | else |
yoonghm | 0:2df66331c109 | 42 | c = 3; |
yoonghm | 0:2df66331c109 | 43 | } |
yoonghm | 0:2df66331c109 | 44 | } |
yoonghm | 0:2df66331c109 | 45 | _input.call(row * 4 + c); |
yoonghm | 0:2df66331c109 | 46 | Start(); // Re-energize all columns |
yoonghm | 0:2df66331c109 | 47 | } |
yoonghm | 0:2df66331c109 | 48 | |
yoonghm | 0:2df66331c109 | 49 | void Keypad::_cbRow0Rise(void) { |
yoonghm | 0:2df66331c109 | 50 | _callback(0, _row0); |
yoonghm | 0:2df66331c109 | 51 | } |
yoonghm | 0:2df66331c109 | 52 | void Keypad::_cbRow1Rise(void) { |
yoonghm | 0:2df66331c109 | 53 | _callback(1, _row1); |
yoonghm | 0:2df66331c109 | 54 | } |
yoonghm | 0:2df66331c109 | 55 | void Keypad::_cbRow2Rise(void) { |
yoonghm | 0:2df66331c109 | 56 | _callback(2, _row2); |
yoonghm | 0:2df66331c109 | 57 | } |
yoonghm | 0:2df66331c109 | 58 | void Keypad::_cbRow3Rise(void) { |
yoonghm | 0:2df66331c109 | 59 | _callback(3, _row3); |
yoonghm | 0:2df66331c109 | 60 | } |
yoonghm | 0:2df66331c109 | 61 | |
yoonghm | 0:2df66331c109 | 62 | void Keypad::_setupRiseTrigger(void) { |
yoonghm | 0:2df66331c109 | 63 | _row0.rise(this, &Keypad::_cbRow0Rise); |
yoonghm | 0:2df66331c109 | 64 | _row1.rise(this, &Keypad::_cbRow1Rise); |
yoonghm | 0:2df66331c109 | 65 | _row2.rise(this, &Keypad::_cbRow2Rise); |
yoonghm | 0:2df66331c109 | 66 | _row3.rise(this, &Keypad::_cbRow3Rise); |
yoonghm | 0:2df66331c109 | 67 | } |