From 95e9323a570ff8ee52a1e209ebe4bc7bf2b14f98 Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Sun, 22 Jan 2023 11:38:25 +0100 Subject: [PATCH] update Github actions (#5) - update GitHub actions - update license 2023 - move code to .cpp - update readme.md - add performance example for **read()** - minor edits --- AnalogKeypad.cpp | 24 +++--- AnalogKeypad.h | 11 +-- CHANGELOG.md | 9 +++ README.md | 76 +++++++++++++++---- examples/analogKeypad/analogKeypad.ino | 35 +++------ .../analogKeypad_performance.ino | 43 +++++++++++ .../analogKeypad_values.ino | 8 +- library.json | 2 +- library.properties | 2 +- test/unit_test_001.cpp | 4 +- 10 files changed, 153 insertions(+), 61 deletions(-) create mode 100644 examples/analogKeypad_performance/analogKeypad_performance.ino diff --git a/AnalogKeypad.cpp b/AnalogKeypad.cpp index 121f4e1..8e114d2 100644 --- a/AnalogKeypad.cpp +++ b/AnalogKeypad.cpp @@ -1,11 +1,9 @@ // // FILE: AnalogKeypad.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.2.2 +// VERSION: 0.2.3 // DATE: 2019-01-31 // PURPOSE: Class for (Robotdyn) 4x4 and 4x3 analog keypad -// -// HISTORY: see changelog.md #include "AnalogKeypad.h" @@ -15,7 +13,7 @@ // as 8 bit compares are fast // // The _analogShift takes care if the ADC has more -// than e.g. 10 bits. +// than e.g. 10 bits. // // Arduino UNO3 build in ==> 10 bits // Other may have 12 or even 16 bits. @@ -51,21 +49,21 @@ uint8_t AnalogKeypad::pressed() int rv = NOKEY; uint8_t _key = _rawRead(); - if (_key == _lastKey) // NOKEY OR REPEAT + if (_key == _lastKey) // NOKEY OR REPEAT { rv = _lastKey; } - else if (_key == 0 && _lastKey != 0) // RELEASE + else if (_key == 0 && _lastKey != 0) // RELEASE { _lastKey = _key; rv = _lastKey; } - else if (_key != 0 && _lastKey == 0) // PRESS + else if (_key != 0 && _lastKey == 0) // PRESS { _lastKey = _key; rv = _lastKey; } - else if (_key != 0 && _lastKey != 0 && _key != _lastKey) // SUPPRESS CHANGE + else if (_key != 0 && _lastKey != 0 && _key != _lastKey) // SUPPRESS CHANGE { rv = _lastKey; } @@ -84,7 +82,7 @@ uint8_t AnalogKeypad::read() // Adjust numbers for other than 4x4 keypad uint8_t AnalogKeypad::_rawRead() { - // spends most time in analogRead (UNO ~110 microseconds) + // spends most time in analogRead (UNO ~110 microseconds) uint8_t val = (analogRead(_analogPin) >> _analogShift); // handle NOKEY first @@ -113,5 +111,11 @@ uint8_t AnalogKeypad::_rawRead() } -// -- END OF FILE -- +uint8_t AnalogKeypad::key() +{ + return _lastKey; +} + + +// -- END OF FILE -- diff --git a/AnalogKeypad.h b/AnalogKeypad.h index f1d8eae..01eab4a 100644 --- a/AnalogKeypad.h +++ b/AnalogKeypad.h @@ -2,17 +2,16 @@ // // FILE: AnalogKeypad.h // AUTHOR: Rob Tillaart -// VERSION: 0.2.2 +// VERSION: 0.2.3 // DATE: 2019-01-31 // PURPOSE: Class for (Robotdyn) 4x4 and 4x3 analogue keypad // URL: https://github.com/RobTillaart/AnalogKeypad -// #include "Arduino.h" -#define ANALOGKEYPAD_LIB_VERSION (F("0.2.2")) +#define ANALOGKEYPAD_LIB_VERSION (F("0.2.3")) #define NOKEY 0x00 #define PRESSED 0x80 @@ -38,7 +37,8 @@ class AnalogKeypad // event alike approach // switch(int e = event()) see examples uint8_t event(); - uint8_t key() { return _lastKey; } ; + uint8_t key(); + private: uint8_t _rawRead(); @@ -48,4 +48,5 @@ class AnalogKeypad }; -// -- END OF FILE -- +// -- END OF FILE -- + diff --git a/CHANGELOG.md b/CHANGELOG.md index eb488c1..8accd16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.2.3] - 2023-01-21 +- update GitHub actions +- update license 2023 +- move code to .cpp +- update readme.md +- add performance example for **read()** +- minor edits + + ## [0.2.2] - 2022-10-27 - Add RP2040 support to build-CI. - Add CHANGELOG.md diff --git a/README.md b/README.md index 962e5e9..9d4621e 100644 --- a/README.md +++ b/README.md @@ -18,47 +18,80 @@ No other keypads are tested, but they should work with this library after adjust the **MAGIC NUMBERS** in the function **rawRead()**. +#### Related + +- https://github.com/RobTillaart/I2CKeyPad +- https://github.com/RobTillaart/I2CKeyPad8x8 + + ## Interface +```cpp +#include "AnalogKeypad.h" +``` -### Constructor + +#### Constructor - **AnalogKeypad(const uint8_t pin, const uint8_t bits = 10)** constructor, pin is typical A0 etc. Bits has a default of 10, but need to be set to match the platform. If bits < 8 then the internal shift would be large causing all reads to return 0 or so. -### polling interface +#### polling interface - **uint8_t pressed()** returns 0 if no key pressed, otherwise returns key pressed (may fluctuate). - **uint8_t read()** read the key pressed returns 0 .. 16 where 0 means NOKEY. -### event interface +#### event interface - **uint8_t event()** checks if a change has occurred since last time. - **uint8_t key()** returns the key involved with last event. -Switch(int e = event()) - -| Event | value | -|:--------:|:-----:| -| PRESSED | 0x80 | -| RELEASED | 0x40 | -| REPEATED | 0x20 | -| CHANGED | 0x10 | -| NOKEY | 0x00 | +```cpp + uint8_t e = AKP.event(); + switch (e) + { + case PRESSED: + Serial.print("press\t"); + Serial.println(AKP.key()); + break; + case RELEASED: + Serial.print("release\t"); + Serial.println(AKP.key()); + break; + case REPEATED: + Serial.print("repeat\t"); + Serial.println(AKP.key()); + break; + case CHANGED: + Serial.print("change\t"); + Serial.println(AKP.key()); + break; + default: // NOKEY + break; + } +``` + +| Event | value | +|:----------:|:-------:| +| PRESSED | 0x80 | +| RELEASED | 0x40 | +| REPEATED | 0x20 | +| CHANGED | 0x10 | +| NOKEY | 0x00 | ## Operation -The simplest usage is to use the **read()** function. +The simplest usage is to use the **read()** function. This will return a 0 (NOKEY) when no key is pressed and a number 1 to 16 for the keys pressed. Note the return value may fluctuate randomly when multiple keys are pressed. The **pressed()** function is a bit more robust. -It returns the key pressed first, so multiple key presses simultaneously +It returns the key pressed first, so multiple key presses simultaneously are less likely to disturb your program. See Examples @@ -66,9 +99,20 @@ See Examples ## Future +#### Must + +#### Should + - more examples -- self-learning example? + - self-learning example? + +#### Could + - make internal mapping array runtime adaptable? - store in RAM, accessor functions - - +- version for external ADC + - see ADC712 + +#### Wont + diff --git a/examples/analogKeypad/analogKeypad.ino b/examples/analogKeypad/analogKeypad.ino index dd05f9b..3f0e329 100644 --- a/examples/analogKeypad/analogKeypad.ino +++ b/examples/analogKeypad/analogKeypad.ino @@ -9,8 +9,7 @@ #include "AnalogKeypad.h" -AnalogKeypad AKP(A0); -uint32_t start, stop; +AnalogKeypad AKP(A0); // adjust if needed void setup() @@ -21,18 +20,18 @@ void setup() Serial.println(ANALOGKEYPAD_LIB_VERSION); Serial.println(); - test3(); - + for (int i = 0; i < 100; i++) test1(); for (int i = 0; i < 100; i++) test2(); } + void loop() { } -// +// pressed interface void test1() { int button = AKP.pressed(); @@ -45,44 +44,34 @@ void test1() } -// use the "event" interface +// event interface void test2() { uint8_t e = AKP.event(); switch (e) { - case 0x80: + case PRESSED: Serial.print("press\t"); Serial.println(AKP.key()); break; - case 0x40: + case RELEASED: Serial.print("release\t"); Serial.println(AKP.key()); break; - case 0x20: + case REPEATED: Serial.print("repeat\t"); Serial.println(AKP.key()); break; - case 0x10: + case CHANGED: Serial.print("change\t"); Serial.println(AKP.key()); break; - default: + default: // NOKEY break; } delay(100); } -// timing test -void test3() -{ - start = micros(); - int button = AKP.read(); - stop = micros(); - Serial.print(stop - start); - Serial.print("\t"); - Serial.println(button); - delay(100); -} -// -- END OF FILE -- +// -- END OF FILE -- + diff --git a/examples/analogKeypad_performance/analogKeypad_performance.ino b/examples/analogKeypad_performance/analogKeypad_performance.ino new file mode 100644 index 0000000..9f02e96 --- /dev/null +++ b/examples/analogKeypad_performance/analogKeypad_performance.ino @@ -0,0 +1,43 @@ +// +// FILE: analogKeypad.ino +// AUTHOR: Rob Tillaart +// PURPOSE: demo 4x4 analogue keypad +// +// https://www.tinytronics.nl/shop/nl/arduino/accessoires/robotdyn-keypad-4x4-matrix-analoog?search=matrix +// + + +#include "AnalogKeypad.h" + +AnalogKeypad AKP(A0); +uint32_t start, stop; + +volatile int button; + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + Serial.print("ANALOGKEYPAD_LIB_VERSION:\t"); + Serial.println(ANALOGKEYPAD_LIB_VERSION); + + Serial.println(); + + start = micros(); + for (int i = 0; i < 1000; i++) + { + button = AKP.read(); + } + stop = micros(); + Serial.print("READ: \t"); + Serial.print((stop - start) * 0.001, 2); + Serial.println(" us"); + + Serial.println("\ndone..."); +} + +void loop() +{ +} + +// -- END OF FILE -- diff --git a/examples/analogKeypad_values/analogKeypad_values.ino b/examples/analogKeypad_values/analogKeypad_values.ino index 18279dd..54f9219 100644 --- a/examples/analogKeypad_values/analogKeypad_values.ino +++ b/examples/analogKeypad_values/analogKeypad_values.ino @@ -1,7 +1,7 @@ // // FILE: analogKeypad_values.ino // AUTHOR: Rob Tillaart -// PURPOSE: helper for adjust 4x4 analogue keypad +// PURPOSE: helper for adjust 4x4 analogue keypad MAGIC numbers // @@ -61,12 +61,12 @@ void testChar(const char * str) { y = analogRead(ANALOGPORT); } - while (abs(x - y) < 4); // ADAPT THRESHOLD IF NEEDED + while (abs(x - y) < 4); // ADAPT THRESHOLD IF NEEDED Serial.print(y); Serial.print("\t"); - Serial.println(y / 4); // assume 10 bits. + Serial.println(y / 4); // assume 10 bits. delay(1000); } -// -- END OF FILE -- +// -- END OF FILE -- diff --git a/library.json b/library.json index 0bbbc59..142fd34 100644 --- a/library.json +++ b/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/AnalogKeypad" }, - "version": "0.2.2", + "version": "0.2.3", "license": "MIT", "frameworks": "arduino", "platforms": "*", diff --git a/library.properties b/library.properties index bb997e2..86e0987 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=AnalogKeypad -version=0.2.2 +version=0.2.3 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino Library for (Robotdyn) 4x4 and 4x3 AnalogKeypad diff --git a/test/unit_test_001.cpp b/test/unit_test_001.cpp index 956c5cb..3e595e9 100644 --- a/test/unit_test_001.cpp +++ b/test/unit_test_001.cpp @@ -30,6 +30,7 @@ unittest_setup() fprintf(stderr, "ANALOGKEYPAD_LIB_VERSION: %s\n", (char *) ANALOGKEYPAD_LIB_VERSION); } + unittest_teardown() { } @@ -75,4 +76,5 @@ unittest(test_event) unittest_main() -// -------- +// -- END OF FILE -- +