lib/keypad/Keypad.cpp

changeset 26
86f099bda525
parent 24
b43536c064f6
child 28
424d792fea4f
equal deleted inserted replaced
25:5b1e0b384d31 26:86f099bda525
1 #include "mbed.h" 1 #include "mbed.h"
2 #include "Keypad.h" 2 #include "Keypad.h"
3 3
4 4
5 Keypad::Keypad(uint8_t nRows, DigitalIn rows[], 5 Keypad::Keypad(
6 uint8_t nColumns, DigitalOut cols[], 6 uint8_t nRows,
7 int debounce_ms): 7 DigitalIn rows[],
8 uint8_t nColumns,
9 DigitalOut cols[],
10 int debounce_ms):
8 _rows(), 11 _rows(),
9 _columns(), 12 _columns(),
10 _debounce(debounce_ms), 13 _debounce(debounce_ms),
11 _started(false), 14 _started(false),
12 _nrows(nRows), 15 _nrows(nRows),
13 _ncols(nColumns) 16 _ncols(nColumns)
14 { 17 {
15 printf("Setup Keypad: %dx%d\r\n", nColumns, nRows); 18 printf("Setup Keypad: %dx%d\r\n", nColumns, nRows);
16 for(uint8_t c=0; c<nColumns; c++) { 19 for(uint8_t c=0; c<nColumns; c++)
20 {
17 _columns.push_back(cols[c]); 21 _columns.push_back(cols[c]);
18 _columns[c].write(0); 22 _columns[c].write(0);
19 } 23 }
20 24
21 for(uint8_t r=0; r<nRows; r++) { 25 for(uint8_t r=0; r<nRows; r++)
26 {
22 _rows.push_back(rows[r]); 27 _rows.push_back(rows[r]);
23 _rows[r].mode(PullDown); 28 _rows[r].mode(PullDown);
24 } 29 }
25 _keys = new uint8_t[_nrows*_ncols]; 30 _keys = new uint8_t[_nrows*_ncols];
26 } 31 }
35 uint8_t col=0; 40 uint8_t col=0;
36 uint8_t row; 41 uint8_t row;
37 42
38 for (row=0; row<(_ncols*_nrows); row++) 43 for (row=0; row<(_ncols*_nrows); row++)
39 _keys[row] = 0; 44 _keys[row] = 0;
40 while(_started) { 45
46 while(_started)
47 {
41 _columns[col].write(1); 48 _columns[col].write(1);
42 Thread::wait(5); 49 Thread::wait(5);
43 for(row=0; row<_nrows; row++) 50 for(row=0; row<_nrows; row++)
51 {
52 if (_rows[row].read())
44 { 53 {
45 if (_rows[row].read()) 54 if (_keys[row + _nrows*col] < 2)
46 { 55 {
47 if (_keys[row + _nrows*col] < 2) 56 _keys[row + _nrows*col]++;
48 { 57 if ((_keys[row + _nrows*col] == 2) && _kp_callback)
49 _keys[row + _nrows*col]++; 58 _kp_callback.call(row, col);
50 if ((_keys[row + _nrows*col] == 2) && _kp_callback) 59 }
51 _kp_callback.call(row, col); 60 }
52 } 61 else
53 } 62 {
54 else 63 if (_keys[row + _nrows*col] > 0)
55 { 64 {
56 if (_keys[row + _nrows*col] > 0) 65 _keys[row + _nrows*col]--;
57 { 66 if ((_keys[row + _nrows*col] == 0) && _kr_callback)
58 _keys[row + _nrows*col]--; 67 _kr_callback.call(row, col);
59 if ((_keys[row + _nrows*col] == 0) && _kr_callback) 68 }
60 _kr_callback.call(row, col); 69 }
61 }
62 }
63 } 70 }
64 _columns[col].write(0); 71 _columns[col].write(0);
65 col = (col+1) % _ncols; 72 col = (col+1) % _ncols;
66 Thread::wait(5); 73 Thread::wait(5);
67 } 74 }
68 } 75 }
69 76
70 void Keypad::attach(const keyevent_callback_t& kp_callback, 77 void Keypad::attach(const keyevent_callback_t& kp_callback,
71 const keyevent_callback_t& kr_callback) 78 const keyevent_callback_t& kr_callback)
72 { 79 {
73 _kp_callback = kp_callback; 80 _kp_callback = kp_callback;
74 _kr_callback = kr_callback; 81 _kr_callback = kr_callback;
75 } 82 }
76 83

mercurial