mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.2.3 AnalogKeypad
This commit is contained in:
parent
4924db2581
commit
3639212cf5
@ -6,7 +6,7 @@ jobs:
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v3
|
||||
- uses: arduino/arduino-lint-action@v1
|
||||
with:
|
||||
library-manager: update
|
||||
|
@ -8,7 +8,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v3
|
||||
- uses: ruby/setup-ruby@v1
|
||||
with:
|
||||
ruby-version: 2.6
|
||||
|
@ -10,7 +10,7 @@ jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v3
|
||||
- name: json-syntax-check
|
||||
uses: limitusus/json-syntax-check@v1
|
||||
with:
|
||||
|
@ -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 --
|
||||
|
||||
|
@ -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 @@ public:
|
||||
// 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 @@ private:
|
||||
};
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -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
|
||||
|
@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2019-2022 Rob Tillaart
|
||||
Copyright (c) 2019-2023 Rob Tillaart
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
@ -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
|
||||
|
||||
|
||||
|
@ -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 --
|
||||
|
||||
|
@ -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 --
|
@ -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 --
|
||||
|
@ -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": "*",
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=AnalogKeypad
|
||||
version=0.2.2
|
||||
version=0.2.3
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Arduino Library for (Robotdyn) 4x4 and 4x3 AnalogKeypad
|
||||
|
@ -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 --
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user