lib/keypad/Keypad.cpp

Mon, 04 Oct 2021 21:15:35 +0200

author
David Douard <david.douard@sdfa3.org>
date
Mon, 04 Oct 2021 21:15:35 +0200
changeset 66
48f29a1d43d6
parent 33
167010858b8d
permissions
-rw-r--r--

Clean several compilation warnings

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

mercurial