Fork without short circuits
Fork of keypad by
No extra hardware is needed besides the wires and switches. The columns are outputs configured with open drain. The rows are inputs configured with pull up resistors. A key press pulls down its row. With scanning the column is determined thereafter.
See SaveKeypad for an example usage.
Revision 10:9a9ec143840b, committed 2012-11-02
- Comitter:
- gj_schoneveld
- Date:
- Fri Nov 02 22:42:28 2012 +0000
- Parent:
- 9:e48ba5b4c497
- Child:
- 11:a45e64141ce6
- Commit message:
- A fork without short circuits
Changed in this revision
keypad.cpp | Show annotated file Show diff for this revision Revisions of this file |
keypad.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/keypad.cpp Tue Jan 31 00:31:58 2012 +0000 +++ b/keypad.cpp Fri Nov 02 22:42:28 2012 +0000 @@ -3,39 +3,50 @@ Keypad::Keypad(PinName row3, PinName row2, PinName row1, PinName row0, PinName col3, PinName col2, PinName col1, PinName col0, int debounce_ms): - _row0(row0), _row1(row1), _row2(row2), _row3(row3), - _cols(col0, col1, col2, col3) { + _row0(row0), _row1(row1), _row2(row2), _row3(row3), + _cols(col0, col1, col2, col3) +{ _debounce = debounce_ms; - _setupRiseTrigger(); + _row0.mode(PullUp); + _row1.mode(PullUp); + _row2.mode(PullUp); + _row3.mode(PullUp); + _cols.mode(OpenDrain); + _cols.output(); + _setupFallTrigger(); } -void Keypad::Start(void) { - _cols = 0x0F; -} - -void Keypad::Stop(void) { +void Keypad::Start(void) +{ _cols = 0x00; } -void Keypad::CallAfterInput(uint32_t (*fptr)(uint32_t index)) { +void Keypad::Stop(void) +{ + _cols = 0x0F; +} + +void Keypad::CallAfterInput(uint32_t (*fptr)(uint32_t index)) +{ _input.attach(fptr); } -void Keypad::_callback(int row, InterruptIn &therow) { +void Keypad::_callback(int row, InterruptIn &therow) +{ wait_ms(_debounce); - if (therow != 1) + if (therow != 0) return; int c = -1; - _cols = _cols & 0x0E; + _cols = 0x0E; if (therow == 0) c = 0; else { - _cols = _cols & 0x0D; + _cols = 0x0D; if (therow == 0) c = 1; else { - _cols = _cols & 0x0B; + _cols = 0x0B; if (therow == 0) c = 2; else @@ -46,22 +57,27 @@ Start(); // Re-energize all columns } -void Keypad::_cbRow0Rise(void) { +void Keypad::_cbRow0Fall(void) +{ _callback(0, _row0); } -void Keypad::_cbRow1Rise(void) { +void Keypad::_cbRow1Fall(void) +{ _callback(1, _row1); } -void Keypad::_cbRow2Rise(void) { +void Keypad::_cbRow2Fall(void) +{ _callback(2, _row2); } -void Keypad::_cbRow3Rise(void) { +void Keypad::_cbRow3Fall(void) +{ _callback(3, _row3); } -void Keypad::_setupRiseTrigger(void) { - _row0.rise(this, &Keypad::_cbRow0Rise); - _row1.rise(this, &Keypad::_cbRow1Rise); - _row2.rise(this, &Keypad::_cbRow2Rise); - _row3.rise(this, &Keypad::_cbRow3Rise); -} \ No newline at end of file +void Keypad::_setupFallTrigger(void) +{ + _row0.fall(this, &Keypad::_cbRow0Fall); + _row1.fall(this, &Keypad::_cbRow1Fall); + _row2.fall(this, &Keypad::_cbRow2Fall); + _row3.fall(this, &Keypad::_cbRow3Fall); +}
--- a/keypad.h Tue Jan 31 00:31:58 2012 +0000 +++ b/keypad.h Fri Nov 02 22:42:28 2012 +0000 @@ -40,31 +40,32 @@ * @code * #include "mbed.h" * #include "keypad.h" - * + * * // Define your own keypad values * char Keytable[] = { '1', '2', '3', 'A', * '4', '5', '6', 'B', * '7', '8', '9', 'C', * '*', '0', '#', 'D' * }; - * + * * uint32_t cbAfterInput(uint32_t index) { * printf("Index:%d => Key:%c\n", key, Keytable[index]); * return 0; * } - * + * * int main() { * Keypad keypad(p25, p26, p27, p28, p21, p22, p23, p24); * keypad.CallAfterInput(&cbAfterInput); * keypad.Start(); - * + * * while (1) { * wait_ms(100); * } * } * @endcode */ -class Keypad { +class Keypad +{ public: /** Create a Keypad interface * @@ -95,17 +96,17 @@ InterruptIn _row1; InterruptIn _row2; InterruptIn _row3; - BusOut _cols; + BusInOut _cols; // BusOut doesn't support mode() yet; need open drain to prevent short circuits... int _debounce; FPointer _input; // Called after each input void _callback(int row, InterruptIn &therow); - void _cbRow0Rise(void); - void _cbRow1Rise(void); - void _cbRow2Rise(void); - void _cbRow3Rise(void); - void _setupRiseTrigger(void); + void _cbRow0Fall(void); + void _cbRow1Fall(void); + void _cbRow2Fall(void); + void _cbRow3Fall(void); + void _setupFallTrigger(void); void _dummy(void) { }; }; -#endif // KEYPAD_H \ No newline at end of file +#endif // KEYPAD_H