lib/keypad/Keypad.cpp

Sun, 25 Oct 2020 17:12:22 +0100

author
David Douard <david.douard@sdf3.org>
date
Sun, 25 Oct 2020 17:12:22 +0100
changeset 31
794401fbd5d5
parent 28
424d792fea4f
child 33
167010858b8d
permissions
-rw-r--r--

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
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
5 Keypad::Keypad(
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
6 uint8_t nRows,
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
7 DigitalIn rows[],
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
8 uint8_t nColumns,
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
9 DigitalOut cols[],
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
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
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
19 for(uint8_t c=0; c<nColumns; c++)
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
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
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
25 for(uint8_t r=0; r<nRows; r++)
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
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
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
45
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
46 while(_started)
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
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
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
51 {
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
52 if (_rows[row].read())
24
b43536c064f6 Add the (missing?) Keypad lib
David Douard <david.douard@logilab.fr>
parents:
diff changeset
53 {
26
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
54 if (_keys[row + _nrows*col] < 2)
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
55 {
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
56 _keys[row + _nrows*col]++;
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
57 if ((_keys[row + _nrows*col] == 2) && _kp_callback)
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
58 _kp_callback.call(row, col);
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
59 }
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
60 }
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
61 else
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
62 {
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
63 if (_keys[row + _nrows*col] > 0)
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
64 {
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
65 _keys[row + _nrows*col]--;
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
66 if ((_keys[row + _nrows*col] == 0) && _kr_callback)
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
67 _kr_callback.call(row, col);
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
68 }
86f099bda525 Several stuff
David Douard <david.douard@sdfa3.org>
parents: 24
diff changeset
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 }

mercurial