Fork without short circuits

Dependents:   SaveKeypad

Fork of keypad by HM Yoong

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.

Committer:
yoonghm
Date:
Mon Jan 30 09:41:47 2012 +0000
Revision:
0:2df66331c109
Child:
10:9a9ec143840b
Initial published version

Who changed what in which revision?

UserRevisionLine numberNew 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 }