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:
gj_schoneveld
Date:
Fri Nov 02 22:42:28 2012 +0000
Revision:
10:9a9ec143840b
Parent:
0:2df66331c109
Child:
11:a45e64141ce6
A fork without short circuits

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):
gj_schoneveld 10:9a9ec143840b 6 _row0(row0), _row1(row1), _row2(row2), _row3(row3),
gj_schoneveld 10:9a9ec143840b 7 _cols(col0, col1, col2, col3)
gj_schoneveld 10:9a9ec143840b 8 {
yoonghm 0:2df66331c109 9 _debounce = debounce_ms;
gj_schoneveld 10:9a9ec143840b 10 _row0.mode(PullUp);
gj_schoneveld 10:9a9ec143840b 11 _row1.mode(PullUp);
gj_schoneveld 10:9a9ec143840b 12 _row2.mode(PullUp);
gj_schoneveld 10:9a9ec143840b 13 _row3.mode(PullUp);
gj_schoneveld 10:9a9ec143840b 14 _cols.mode(OpenDrain);
gj_schoneveld 10:9a9ec143840b 15 _cols.output();
gj_schoneveld 10:9a9ec143840b 16 _setupFallTrigger();
yoonghm 0:2df66331c109 17 }
yoonghm 0:2df66331c109 18
gj_schoneveld 10:9a9ec143840b 19 void Keypad::Start(void)
gj_schoneveld 10:9a9ec143840b 20 {
yoonghm 0:2df66331c109 21 _cols = 0x00;
yoonghm 0:2df66331c109 22 }
yoonghm 0:2df66331c109 23
gj_schoneveld 10:9a9ec143840b 24 void Keypad::Stop(void)
gj_schoneveld 10:9a9ec143840b 25 {
gj_schoneveld 10:9a9ec143840b 26 _cols = 0x0F;
gj_schoneveld 10:9a9ec143840b 27 }
gj_schoneveld 10:9a9ec143840b 28
gj_schoneveld 10:9a9ec143840b 29 void Keypad::CallAfterInput(uint32_t (*fptr)(uint32_t index))
gj_schoneveld 10:9a9ec143840b 30 {
yoonghm 0:2df66331c109 31 _input.attach(fptr);
yoonghm 0:2df66331c109 32 }
yoonghm 0:2df66331c109 33
gj_schoneveld 10:9a9ec143840b 34 void Keypad::_callback(int row, InterruptIn &therow)
gj_schoneveld 10:9a9ec143840b 35 {
yoonghm 0:2df66331c109 36 wait_ms(_debounce);
gj_schoneveld 10:9a9ec143840b 37 if (therow != 0)
yoonghm 0:2df66331c109 38 return;
yoonghm 0:2df66331c109 39
yoonghm 0:2df66331c109 40 int c = -1;
gj_schoneveld 10:9a9ec143840b 41 _cols = 0x0E;
yoonghm 0:2df66331c109 42 if (therow == 0)
yoonghm 0:2df66331c109 43 c = 0;
yoonghm 0:2df66331c109 44 else {
gj_schoneveld 10:9a9ec143840b 45 _cols = 0x0D;
yoonghm 0:2df66331c109 46 if (therow == 0)
yoonghm 0:2df66331c109 47 c = 1;
yoonghm 0:2df66331c109 48 else {
gj_schoneveld 10:9a9ec143840b 49 _cols = 0x0B;
yoonghm 0:2df66331c109 50 if (therow == 0)
yoonghm 0:2df66331c109 51 c = 2;
yoonghm 0:2df66331c109 52 else
yoonghm 0:2df66331c109 53 c = 3;
yoonghm 0:2df66331c109 54 }
yoonghm 0:2df66331c109 55 }
yoonghm 0:2df66331c109 56 _input.call(row * 4 + c);
yoonghm 0:2df66331c109 57 Start(); // Re-energize all columns
yoonghm 0:2df66331c109 58 }
yoonghm 0:2df66331c109 59
gj_schoneveld 10:9a9ec143840b 60 void Keypad::_cbRow0Fall(void)
gj_schoneveld 10:9a9ec143840b 61 {
yoonghm 0:2df66331c109 62 _callback(0, _row0);
yoonghm 0:2df66331c109 63 }
gj_schoneveld 10:9a9ec143840b 64 void Keypad::_cbRow1Fall(void)
gj_schoneveld 10:9a9ec143840b 65 {
yoonghm 0:2df66331c109 66 _callback(1, _row1);
yoonghm 0:2df66331c109 67 }
gj_schoneveld 10:9a9ec143840b 68 void Keypad::_cbRow2Fall(void)
gj_schoneveld 10:9a9ec143840b 69 {
yoonghm 0:2df66331c109 70 _callback(2, _row2);
yoonghm 0:2df66331c109 71 }
gj_schoneveld 10:9a9ec143840b 72 void Keypad::_cbRow3Fall(void)
gj_schoneveld 10:9a9ec143840b 73 {
yoonghm 0:2df66331c109 74 _callback(3, _row3);
yoonghm 0:2df66331c109 75 }
yoonghm 0:2df66331c109 76
gj_schoneveld 10:9a9ec143840b 77 void Keypad::_setupFallTrigger(void)
gj_schoneveld 10:9a9ec143840b 78 {
gj_schoneveld 10:9a9ec143840b 79 _row0.fall(this, &Keypad::_cbRow0Fall);
gj_schoneveld 10:9a9ec143840b 80 _row1.fall(this, &Keypad::_cbRow1Fall);
gj_schoneveld 10:9a9ec143840b 81 _row2.fall(this, &Keypad::_cbRow2Fall);
gj_schoneveld 10:9a9ec143840b 82 _row3.fall(this, &Keypad::_cbRow3Fall);
gj_schoneveld 10:9a9ec143840b 83 }