Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
keypad.cpp@0:372ef8c791a1, 2019-07-05 (annotated)
- Committer:
- irsanjul
- Date:
- Fri Jul 05 10:33:24 2019 +0000
- Revision:
- 0:372ef8c791a1
hhhh
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| irsanjul | 0:372ef8c791a1 | 1 | #include "keypad.h" |
| irsanjul | 0:372ef8c791a1 | 2 | |
| irsanjul | 0:372ef8c791a1 | 3 | Keypad::Keypad(PinName row3, PinName row2, PinName row1, PinName row0, |
| irsanjul | 0:372ef8c791a1 | 4 | PinName col3, PinName col2, PinName col1, PinName col0, |
| irsanjul | 0:372ef8c791a1 | 5 | int debounce_ms): |
| irsanjul | 0:372ef8c791a1 | 6 | _row0(row0), _row1(row1), _row2(row2), _row3(row3), |
| irsanjul | 0:372ef8c791a1 | 7 | _cols(col0, col1, col2, col3) |
| irsanjul | 0:372ef8c791a1 | 8 | { |
| irsanjul | 0:372ef8c791a1 | 9 | _debounce = debounce_ms; |
| irsanjul | 0:372ef8c791a1 | 10 | _rows[0] = &_row0; |
| irsanjul | 0:372ef8c791a1 | 11 | _rows[1] = &_row1; |
| irsanjul | 0:372ef8c791a1 | 12 | _rows[2] = &_row2; |
| irsanjul | 0:372ef8c791a1 | 13 | _rows[3] = &_row3; |
| irsanjul | 0:372ef8c791a1 | 14 | for (int r = 0; r < row_count; r++) |
| irsanjul | 0:372ef8c791a1 | 15 | _rows[r]->mode(PullUp); |
| irsanjul | 0:372ef8c791a1 | 16 | _cols.mode(PullDown); |
| irsanjul | 0:372ef8c791a1 | 17 | _cols.output(); |
| irsanjul | 0:372ef8c791a1 | 18 | } |
| irsanjul | 0:372ef8c791a1 | 19 | |
| irsanjul | 0:372ef8c791a1 | 20 | void Keypad::_setupFallTrigger(void) |
| irsanjul | 0:372ef8c791a1 | 21 | { |
| irsanjul | 0:372ef8c791a1 | 22 | for (int r = 0; r < row_count; r++) |
| irsanjul | 0:372ef8c791a1 | 23 | _rows[r]->fall(this, &Keypad::_callback); |
| irsanjul | 0:372ef8c791a1 | 24 | } |
| irsanjul | 0:372ef8c791a1 | 25 | |
| irsanjul | 0:372ef8c791a1 | 26 | void Keypad::Start(void) |
| irsanjul | 0:372ef8c791a1 | 27 | { |
| irsanjul | 0:372ef8c791a1 | 28 | /* make the columns zero so they can pull rows down */ |
| irsanjul | 0:372ef8c791a1 | 29 | _cols = 0x00; |
| irsanjul | 0:372ef8c791a1 | 30 | } |
| irsanjul | 0:372ef8c791a1 | 31 | |
| irsanjul | 0:372ef8c791a1 | 32 | void Keypad::Stop(void) |
| irsanjul | 0:372ef8c791a1 | 33 | { |
| irsanjul | 0:372ef8c791a1 | 34 | /* make the columns one so they cannot pull any rows down anymore */ |
| irsanjul | 0:372ef8c791a1 | 35 | _cols = ~0x00; |
| irsanjul | 0:372ef8c791a1 | 36 | } |
| irsanjul | 0:372ef8c791a1 | 37 | |
| irsanjul | 0:372ef8c791a1 | 38 | void Keypad::CallAfterInput(uint32_t (*fptr)(uint32_t index)) |
| irsanjul | 0:372ef8c791a1 | 39 | { |
| irsanjul | 0:372ef8c791a1 | 40 | _input.attach(fptr); |
| irsanjul | 0:372ef8c791a1 | 41 | _setupFallTrigger(); |
| irsanjul | 0:372ef8c791a1 | 42 | } |
| irsanjul | 0:372ef8c791a1 | 43 | |
| irsanjul | 0:372ef8c791a1 | 44 | int Keypad::DebouncedScan() |
| irsanjul | 0:372ef8c791a1 | 45 | { |
| irsanjul | 0:372ef8c791a1 | 46 | /* debounce */ |
| irsanjul | 0:372ef8c791a1 | 47 | wait_ms(_debounce); |
| irsanjul | 0:372ef8c791a1 | 48 | |
| irsanjul | 0:372ef8c791a1 | 49 | return Scan(); |
| irsanjul | 0:372ef8c791a1 | 50 | } |
| irsanjul | 0:372ef8c791a1 | 51 | |
| irsanjul | 0:372ef8c791a1 | 52 | int Keypad::Scan() |
| irsanjul | 0:372ef8c791a1 | 53 | { |
| irsanjul | 0:372ef8c791a1 | 54 | /* lookup row */ |
| irsanjul | 0:372ef8c791a1 | 55 | int r = -1; |
| irsanjul | 0:372ef8c791a1 | 56 | for (r = 0; r < row_count; r++) { |
| irsanjul | 0:372ef8c791a1 | 57 | if (*_rows[r] == 0) |
| irsanjul | 0:372ef8c791a1 | 58 | break; |
| irsanjul | 0:372ef8c791a1 | 59 | } |
| irsanjul | 0:372ef8c791a1 | 60 | |
| irsanjul | 0:372ef8c791a1 | 61 | /* if we didn't find a valid row, return */ |
| irsanjul | 0:372ef8c791a1 | 62 | if (!(0 <= r && r < row_count)) |
| irsanjul | 0:372ef8c791a1 | 63 | return -1; |
| irsanjul | 0:372ef8c791a1 | 64 | |
| irsanjul | 0:372ef8c791a1 | 65 | /* scan columns to find out which one pulls down the row */ |
| irsanjul | 0:372ef8c791a1 | 66 | int c = -1; |
| irsanjul | 0:372ef8c791a1 | 67 | for (c = 0; c < col_count; c++) { |
| irsanjul | 0:372ef8c791a1 | 68 | _cols = ~(1 << c); |
| irsanjul | 0:372ef8c791a1 | 69 | if (*_rows[r] == 0) |
| irsanjul | 0:372ef8c791a1 | 70 | break; |
| irsanjul | 0:372ef8c791a1 | 71 | } |
| irsanjul | 0:372ef8c791a1 | 72 | |
| irsanjul | 0:372ef8c791a1 | 73 | /* re-energize all columns */ |
| irsanjul | 0:372ef8c791a1 | 74 | Start(); |
| irsanjul | 0:372ef8c791a1 | 75 | |
| irsanjul | 0:372ef8c791a1 | 76 | /* if we didn't find a valid column, return */ |
| irsanjul | 0:372ef8c791a1 | 77 | if (!(0 <= c && c < col_count)) |
| irsanjul | 0:372ef8c791a1 | 78 | return -1; |
| irsanjul | 0:372ef8c791a1 | 79 | |
| irsanjul | 0:372ef8c791a1 | 80 | return r * col_count + c; |
| irsanjul | 0:372ef8c791a1 | 81 | } |
| irsanjul | 0:372ef8c791a1 | 82 | |
| irsanjul | 0:372ef8c791a1 | 83 | int Keypad::DebouncedScanMultiple() |
| irsanjul | 0:372ef8c791a1 | 84 | { |
| irsanjul | 0:372ef8c791a1 | 85 | /* debounce */ |
| irsanjul | 0:372ef8c791a1 | 86 | wait_ms(_debounce); |
| irsanjul | 0:372ef8c791a1 | 87 | |
| irsanjul | 0:372ef8c791a1 | 88 | return ScanMultiple(); |
| irsanjul | 0:372ef8c791a1 | 89 | } |
| irsanjul | 0:372ef8c791a1 | 90 | |
| irsanjul | 0:372ef8c791a1 | 91 | int Keypad::ScanMultiple() |
| irsanjul | 0:372ef8c791a1 | 92 | { |
| irsanjul | 0:372ef8c791a1 | 93 | int res = 0; |
| irsanjul | 0:372ef8c791a1 | 94 | for (int c = 0; c < col_count; c++) { |
| irsanjul | 0:372ef8c791a1 | 95 | _cols = ~(1 << c); |
| irsanjul | 0:372ef8c791a1 | 96 | for (int r = 0; r < row_count; r++) { |
| irsanjul | 0:372ef8c791a1 | 97 | if (*_rows[r] == 0) { |
| irsanjul | 0:372ef8c791a1 | 98 | res |= 1 << (r * col_count + c); |
| irsanjul | 0:372ef8c791a1 | 99 | } |
| irsanjul | 0:372ef8c791a1 | 100 | } |
| irsanjul | 0:372ef8c791a1 | 101 | } |
| irsanjul | 0:372ef8c791a1 | 102 | |
| irsanjul | 0:372ef8c791a1 | 103 | return res; |
| irsanjul | 0:372ef8c791a1 | 104 | } |
| irsanjul | 0:372ef8c791a1 | 105 | |
| irsanjul | 0:372ef8c791a1 | 106 | void Keypad::_callback() |
| irsanjul | 0:372ef8c791a1 | 107 | { |
| irsanjul | 0:372ef8c791a1 | 108 | /* lookup */ |
| irsanjul | 0:372ef8c791a1 | 109 | int position = DebouncedScan(); |
| irsanjul | 0:372ef8c791a1 | 110 | |
| irsanjul | 0:372ef8c791a1 | 111 | /* call back a valid position */ |
| irsanjul | 0:372ef8c791a1 | 112 | if (position >= 0) |
| irsanjul | 0:372ef8c791a1 | 113 | _input.call(position); |
| irsanjul | 0:372ef8c791a1 | 114 | } |
| irsanjul | 0:372ef8c791a1 | 115 |