11//
22// FILE: AnalogKeypad.cpp
33// AUTHOR: Rob Tillaart
4- // VERSION: 0.1.6
4+ // VERSION: 0.2.0
55// DATE: 2019-01-31
66// PURPOSE: Class for (Robotdyn) 4x4 and 4x3 analog keypad
77//
8- // HISTORY:
9- // 0.1.0 2019-01-31 initial version
10- // 0.1.1 2019-02-01 add pressed() event() last()
11- // 0.1.2 2019-02-01 refactored rawRead(), first stable version
12- // 0.1.3 2020-03-25 minor refactoring
13- // 0.1.4 2020-05-27 update library.json
14- // 0.1.5 2020-12-09 add arduino-ci
15- // 0.1.6 2021-05-27 fix arduino-lint
8+ // HISTORY:
9+ // 0.1.0 2019-01-31 initial version
10+ // 0.1.1 2019-02-01 add pressed() event() last()
11+ // 0.1.2 2019-02-01 refactored rawRead(), first stable version
12+ // 0.1.3 2020-03-25 minor refactoring
13+ // 0.1.4 2020-05-27 update library.json
14+ // 0.1.5 2020-12-09 add arduino-ci
15+ // 0.1.6 2021-05-27 fix arduino-lint
16+ // 0.2.0 2021-10-17 update build-ci, readme,
17+ // add bits as parameter in constructor.
1618
1719
1820#include " AnalogKeypad.h"
1921
2022
2123// NOTE the MAGIC NUMBERS in rawRead() are for 8 BIT ADC
22- // ( 8 bit compares are fast)
24+ // as 8 bit compares are fast
2325//
24- // The AKP_SHIFT takes care if the ADC generates more
25- // than e.g. 10 bits. Change AKP_BITS to match your
26- // build in ADC.
26+ // The _analogShift takes care if the ADC has more
27+ // than e.g. 10 bits.
2728//
28- // Arduino UNO3 build in ==> 10 BITS
29- // so AKP_SHIFT ==> 2
30- //
31- #define AKP_BITS 10
32- #define AKP_SHIFT (AKP_BITS - 8 )
29+ // Arduino UNO3 build in ==> 10 bits
30+ // Other may have 12 or even 16 bits.
3331
3432
35- AnalogKeypad::AnalogKeypad (const uint8_t pin)
33+ AnalogKeypad::AnalogKeypad (const uint8_t pin, const uint8_t bits )
3634{
37- _pin = pin;
38- _lastKey = NOKEY;
35+ _analogPin = pin;
36+ _analogShift = bits - 8 ;
37+ _lastKey = NOKEY;
3938}
4039
4140
4241uint8_t AnalogKeypad::event ()
4342{
4443 int rv = NOKEY;
45- uint8_t _key = rawRead ();
44+ uint8_t _key = _rawRead ();
4645
47- if (_key == 0 && _lastKey == 0 ) rv = NOKEY;
46+ if (_key == 0 && _lastKey == 0 ) rv = NOKEY;
4847 else if (_key != 0 && _lastKey == 0 ) rv = PRESSED;
4948 else if (_key == 0 && _lastKey != 0 ) rv = RELEASED;
5049 else if (_key != 0 && _lastKey != 0 && _key == _lastKey) rv = REPEATED;
@@ -60,7 +59,7 @@ uint8_t AnalogKeypad::pressed()
6059{
6160 int rv = NOKEY;
6261
63- uint8_t _key = rawRead ();
62+ uint8_t _key = _rawRead ();
6463 if (_key == _lastKey) // NOKEY OR REPEAT
6564 {
6665 rv = _lastKey;
@@ -86,16 +85,16 @@ uint8_t AnalogKeypad::pressed()
8685
8786uint8_t AnalogKeypad::read ()
8887{
89- _lastKey = rawRead ();
88+ _lastKey = _rawRead ();
9089 return _lastKey;
9190}
9291
9392
9493// Adjust numbers for other than 4x4 keypad
95- uint8_t AnalogKeypad::rawRead ()
94+ uint8_t AnalogKeypad::_rawRead ()
9695{
97- // spends most time in analogRead (uno ~110 usec )
98- uint8_t val = analogRead (_pin ) >> AKP_SHIFT ;
96+ // spends most time in analogRead (UNO ~110 microseconds )
97+ uint8_t val = ( analogRead (_analogPin ) >> _analogShift) ;
9998
10099 // handle NOKEY first
101100 if (val < 57 ) return 0 ;
0 commit comments