11

Dependents:   Program_R11

Committer:
gj_schoneveld
Date:
Thu Nov 08 19:04:08 2012 +0000
Revision:
16:a9feaa7ac039
Parent:
15:873a9a452ef1
Updated Debounce

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 {
gj_schoneveld 12:e6623f165199 9 _debounce = debounce_ms;
gj_schoneveld 11:a45e64141ce6 10 _rows[0] = &_row0;
gj_schoneveld 11:a45e64141ce6 11 _rows[1] = &_row1;
gj_schoneveld 11:a45e64141ce6 12 _rows[2] = &_row2;
gj_schoneveld 11:a45e64141ce6 13 _rows[3] = &_row3;
gj_schoneveld 12:e6623f165199 14 for (int r = 0; r < row_count; r++)
gj_schoneveld 12:e6623f165199 15 _rows[r]->mode(PullUp);
gj_schoneveld 10:9a9ec143840b 16 _cols.mode(OpenDrain);
gj_schoneveld 10:9a9ec143840b 17 _cols.output();
gj_schoneveld 11:a45e64141ce6 18 }
gj_schoneveld 11:a45e64141ce6 19
gj_schoneveld 11:a45e64141ce6 20 void Keypad::_setupFallTrigger(void)
gj_schoneveld 11:a45e64141ce6 21 {
gj_schoneveld 12:e6623f165199 22 for (int r = 0; r < row_count; r++)
gj_schoneveld 12:e6623f165199 23 _rows[r]->fall(this, &Keypad::_callback);
yoonghm 0:2df66331c109 24 }
yoonghm 0:2df66331c109 25
gj_schoneveld 10:9a9ec143840b 26 void Keypad::Start(void)
gj_schoneveld 10:9a9ec143840b 27 {
gj_schoneveld 11:a45e64141ce6 28 /* make the columns zero so they can pull rows down */
yoonghm 0:2df66331c109 29 _cols = 0x00;
yoonghm 0:2df66331c109 30 }
yoonghm 0:2df66331c109 31
gj_schoneveld 10:9a9ec143840b 32 void Keypad::Stop(void)
gj_schoneveld 10:9a9ec143840b 33 {
gj_schoneveld 11:a45e64141ce6 34 /* make the columns one so they cannot pull any rows down anymore */
gj_schoneveld 11:a45e64141ce6 35 _cols = ~0x00;
gj_schoneveld 10:9a9ec143840b 36 }
gj_schoneveld 10:9a9ec143840b 37
gj_schoneveld 10:9a9ec143840b 38 void Keypad::CallAfterInput(uint32_t (*fptr)(uint32_t index))
gj_schoneveld 10:9a9ec143840b 39 {
yoonghm 0:2df66331c109 40 _input.attach(fptr);
gj_schoneveld 11:a45e64141ce6 41 _setupFallTrigger();
gj_schoneveld 11:a45e64141ce6 42 }
gj_schoneveld 11:a45e64141ce6 43
gj_schoneveld 11:a45e64141ce6 44 int Keypad::DebouncedScan()
gj_schoneveld 11:a45e64141ce6 45 {
gj_schoneveld 12:e6623f165199 46 /* debounce */
gj_schoneveld 11:a45e64141ce6 47 wait_ms(_debounce);
gj_schoneveld 16:a9feaa7ac039 48
gj_schoneveld 16:a9feaa7ac039 49 return Scan();
yoonghm 0:2df66331c109 50 }
yoonghm 0:2df66331c109 51
gj_schoneveld 11:a45e64141ce6 52 int Keypad::Scan()
gj_schoneveld 10:9a9ec143840b 53 {
gj_schoneveld 11:a45e64141ce6 54 /* lookup row */
gj_schoneveld 11:a45e64141ce6 55 int r = -1;
gj_schoneveld 11:a45e64141ce6 56 for (r = 0; r < row_count; r++) {
gj_schoneveld 11:a45e64141ce6 57 if (*_rows[r] == 0)
gj_schoneveld 11:a45e64141ce6 58 break;
gj_schoneveld 11:a45e64141ce6 59 }
yoonghm 0:2df66331c109 60
gj_schoneveld 11:a45e64141ce6 61 /* if we didn't find a valid row, return */
gj_schoneveld 11:a45e64141ce6 62 if (!(0 <= r && r < row_count))
gj_schoneveld 11:a45e64141ce6 63 return -1;
gj_schoneveld 11:a45e64141ce6 64
gj_schoneveld 16:a9feaa7ac039 65 /* scan columns to find out which one pulls down the row */
yoonghm 0:2df66331c109 66 int c = -1;
gj_schoneveld 11:a45e64141ce6 67 for (c = 0; c < col_count; c++) {
gj_schoneveld 11:a45e64141ce6 68 _cols = ~(1 << c);
gj_schoneveld 11:a45e64141ce6 69 if (*_rows[r] == 0)
gj_schoneveld 11:a45e64141ce6 70 break;
yoonghm 0:2df66331c109 71 }
gj_schoneveld 11:a45e64141ce6 72
gj_schoneveld 11:a45e64141ce6 73 /* re-energize all columns */
gj_schoneveld 11:a45e64141ce6 74 Start();
gj_schoneveld 11:a45e64141ce6 75
gj_schoneveld 11:a45e64141ce6 76 /* if we didn't find a valid column, return */
gj_schoneveld 11:a45e64141ce6 77 if (!(0 <= c && c < col_count))
gj_schoneveld 11:a45e64141ce6 78 return -1;
gj_schoneveld 11:a45e64141ce6 79
gj_schoneveld 11:a45e64141ce6 80 return r * col_count + c;
yoonghm 0:2df66331c109 81 }
gj_schoneveld 16:a9feaa7ac039 82
gj_schoneveld 13:fb6929fac0db 83 int Keypad::DebouncedScanMultiple()
gj_schoneveld 13:fb6929fac0db 84 {
gj_schoneveld 13:fb6929fac0db 85 /* debounce */
gj_schoneveld 13:fb6929fac0db 86 wait_ms(_debounce);
gj_schoneveld 16:a9feaa7ac039 87
gj_schoneveld 16:a9feaa7ac039 88 return ScanMultiple();
gj_schoneveld 13:fb6929fac0db 89 }
gj_schoneveld 13:fb6929fac0db 90
gj_schoneveld 13:fb6929fac0db 91 int Keypad::ScanMultiple()
gj_schoneveld 13:fb6929fac0db 92 {
gj_schoneveld 16:a9feaa7ac039 93 int res = 0;
gj_schoneveld 13:fb6929fac0db 94 for (int c = 0; c < col_count; c++) {
gj_schoneveld 13:fb6929fac0db 95 _cols = ~(1 << c);
gj_schoneveld 13:fb6929fac0db 96 for (int r = 0; r < row_count; r++) {
gj_schoneveld 13:fb6929fac0db 97 if (*_rows[r] == 0) {
gj_schoneveld 13:fb6929fac0db 98 res |= 1 << (r * col_count + c);
gj_schoneveld 13:fb6929fac0db 99 }
gj_schoneveld 13:fb6929fac0db 100 }
gj_schoneveld 13:fb6929fac0db 101 }
gj_schoneveld 16:a9feaa7ac039 102
gj_schoneveld 13:fb6929fac0db 103 return res;
gj_schoneveld 13:fb6929fac0db 104 }
yoonghm 0:2df66331c109 105
gj_schoneveld 11:a45e64141ce6 106 void Keypad::_callback()
gj_schoneveld 10:9a9ec143840b 107 {
gj_schoneveld 11:a45e64141ce6 108 /* lookup */
gj_schoneveld 11:a45e64141ce6 109 int position = DebouncedScan();
gj_schoneveld 16:a9feaa7ac039 110
gj_schoneveld 11:a45e64141ce6 111 /* call back a valid position */
gj_schoneveld 11:a45e64141ce6 112 if (position >= 0)
gj_schoneveld 11:a45e64141ce6 113 _input.call(position);
yoonghm 0:2df66331c109 114 }
gj_schoneveld 14:f6c3647bf421 115