Skip to content

Commit

Permalink
update Github actions (#5)
Browse files Browse the repository at this point in the history
- update GitHub actions
- update license 2023
- move code to .cpp
- update readme.md
- add performance example for **read()**
- minor edits
  • Loading branch information
RobTillaart authored Jan 22, 2023
1 parent 0813fe1 commit 95e9323
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 61 deletions.
24 changes: 14 additions & 10 deletions AnalogKeypad.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand Down Expand Up @@ -113,5 +111,11 @@ uint8_t AnalogKeypad::_rawRead()
}


// -- END OF FILE --
uint8_t AnalogKeypad::key()
{
return _lastKey;
}


// -- END OF FILE --

11 changes: 6 additions & 5 deletions AnalogKeypad.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -48,4 +48,5 @@ class AnalogKeypad
};


// -- END OF FILE --
// -- END OF FILE --

9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
76 changes: 60 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,57 +18,101 @@ 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
## 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
35 changes: 12 additions & 23 deletions examples/analogKeypad/analogKeypad.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

#include "AnalogKeypad.h"

AnalogKeypad AKP(A0);
uint32_t start, stop;
AnalogKeypad AKP(A0); // adjust if needed


void setup()
Expand All @@ -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();
Expand All @@ -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 --

43 changes: 43 additions & 0 deletions examples/analogKeypad_performance/analogKeypad_performance.ino
Original file line number Diff line number Diff line change
@@ -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 --
8 changes: 4 additions & 4 deletions examples/analogKeypad_values/analogKeypad_values.ino
Original file line number Diff line number Diff line change
@@ -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
//


Expand Down Expand Up @@ -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 --
Loading

0 comments on commit 95e9323

Please sign in to comment.