lib/keypad/Keypad.cpp

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

mercurial