Sun, 25 Oct 2020 17:12:22 +0100
Add a few patterns in hgignore
24
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
1 | #include "mbed.h" |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
2 | #include "Keypad.h" |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
3 | |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
4 | |
26 | 5 | Keypad::Keypad( |
6 | uint8_t nRows, | |
7 | DigitalIn rows[], | |
8 | uint8_t nColumns, | |
9 | DigitalOut cols[], | |
10 | int debounce_ms): | |
24
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
11 | _rows(), |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
12 | _columns(), |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
13 | _debounce(debounce_ms), |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
14 | _started(false), |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
15 | _nrows(nRows), |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
16 | _ncols(nColumns) |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
17 | { |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
18 | printf("Setup Keypad: %dx%d\r\n", nColumns, nRows); |
26 | 19 | for(uint8_t c=0; c<nColumns; c++) |
20 | { | |
24
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
21 | _columns.push_back(cols[c]); |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
22 | _columns[c].write(0); |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
23 | } |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
24 | |
26 | 25 | for(uint8_t r=0; r<nRows; r++) |
26 | { | |
24
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
27 | _rows.push_back(rows[r]); |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
28 | _rows[r].mode(PullDown); |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
29 | } |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
30 | _keys = new uint8_t[_nrows*_ncols]; |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
31 | } |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
32 | |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
33 | Keypad::~Keypad() |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
34 | { |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
35 | delete _keys; |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
36 | } |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
37 | |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
38 | void Keypad::run() |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
39 | { |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
40 | uint8_t col=0; |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
41 | uint8_t row; |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
42 | |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
43 | for (row=0; row<(_ncols*_nrows); row++) |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
44 | _keys[row] = 0; |
26 | 45 | |
46 | while(_started) | |
47 | { | |
24
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
48 | _columns[col].write(1); |
28
424d792fea4f
compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents:
26
diff
changeset
|
49 | ThisThread::sleep_for(5); |
24
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
50 | for(row=0; row<_nrows; row++) |
26 | 51 | { |
52 | if (_rows[row].read()) | |
24
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
53 | { |
26 | 54 | if (_keys[row + _nrows*col] < 2) |
55 | { | |
56 | _keys[row + _nrows*col]++; | |
57 | if ((_keys[row + _nrows*col] == 2) && _kp_callback) | |
58 | _kp_callback.call(row, col); | |
59 | } | |
60 | } | |
61 | else | |
62 | { | |
63 | if (_keys[row + _nrows*col] > 0) | |
64 | { | |
65 | _keys[row + _nrows*col]--; | |
66 | if ((_keys[row + _nrows*col] == 0) && _kr_callback) | |
67 | _kr_callback.call(row, col); | |
68 | } | |
69 | } | |
24
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
70 | } |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
71 | _columns[col].write(0); |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
72 | col = (col+1) % _ncols; |
28
424d792fea4f
compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents:
26
diff
changeset
|
73 | ThisThread::sleep_for(5); |
24
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
74 | } |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
75 | } |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
76 | |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
77 | void Keypad::attach(const keyevent_callback_t& kp_callback, |
28
424d792fea4f
compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents:
26
diff
changeset
|
78 | const keyevent_callback_t& kr_callback) |
24
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
79 | { |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
80 | _kp_callback = kp_callback; |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
81 | _kr_callback = kr_callback; |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
82 | } |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
83 | |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
84 | void Keypad::start(void) |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
85 | { |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
86 | if (_started) |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
87 | return; |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
88 | _started = true; |
28
424d792fea4f
compile for nucleo f446re & f303re with mbed 6
David Douard <david.douard@sdfa3.org>
parents:
26
diff
changeset
|
89 | _loop.start(callback(this, &Keypad::run)); |
24
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
90 | } |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
91 | |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
92 | void Keypad::stop(void) |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
93 | { |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
94 | _started = false; |
b43536c064f6
Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff
changeset
|
95 | } |