mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
first stable for (Robotdyn) 4x4 analog keypad
This commit is contained in:
parent
6f2041f470
commit
835c61aad3
115
libraries/AnalogKeypad/AnalogKeypad.cpp
Normal file
115
libraries/AnalogKeypad/AnalogKeypad.cpp
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
//
|
||||||
|
// FILE: AnalogKeypad.cpp
|
||||||
|
// AUTHOR: Rob Tillaart
|
||||||
|
// VERSION: 0.1.2
|
||||||
|
// DATE: 2019-01-31
|
||||||
|
// PURPOSE: Class for analog keypad
|
||||||
|
//
|
||||||
|
// HISTORY:
|
||||||
|
// 0.1.0 - 2019-01-31 initial version
|
||||||
|
// 0.1.1 - 2019-02-01 add pressed() event() last()
|
||||||
|
// 0.1.2 - 2019-02-01 refactored rawRead(), first stable version
|
||||||
|
//
|
||||||
|
// Released to the public domain
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "AnalogKeypad.h"
|
||||||
|
|
||||||
|
// NOTE the MAGIC NUMBERS in rawRead() are for 8 BIT ADC
|
||||||
|
// (8 bit compares are fast)
|
||||||
|
//
|
||||||
|
// The AKP_SHIFT takes care if the ADC generates more
|
||||||
|
// than e.g. 10 bits. Change AKP_BITS to match your
|
||||||
|
// build in ADC.
|
||||||
|
//
|
||||||
|
// Arduino UNO3 build in ==> 10 BITS
|
||||||
|
//
|
||||||
|
#define AKP_BITS 10
|
||||||
|
#define AKP_SHIFT (AKP_BITS - 8)
|
||||||
|
|
||||||
|
AnalogKeypad::AnalogKeypad(const uint8_t pin)
|
||||||
|
{
|
||||||
|
_pin = pin;
|
||||||
|
_lastKey = NOKEY;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t AnalogKeypad::event()
|
||||||
|
{
|
||||||
|
int rv = NOKEY;
|
||||||
|
uint8_t _key = rawRead();
|
||||||
|
|
||||||
|
if (_key == 0 && _lastKey == 0) rv = NOKEY;
|
||||||
|
else if (_key != 0 && _lastKey == 0) rv = PRESSED;
|
||||||
|
else if (_key == 0 && _lastKey != 0) rv = RELEASED;
|
||||||
|
else if (_key != 0 && _lastKey != 0 && _key == _lastKey) rv = REPEATED;
|
||||||
|
else if (_key != 0 && _lastKey != 0 && _key != _lastKey) rv = CHANGED;
|
||||||
|
|
||||||
|
_lastKey = _key;
|
||||||
|
|
||||||
|
return rv; // return rv | _key; ????
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t AnalogKeypad::pressed()
|
||||||
|
{
|
||||||
|
int rv = NOKEY;
|
||||||
|
|
||||||
|
uint8_t _key = rawRead();
|
||||||
|
if (_key == _lastKey) // NOKEY OR REPEAT
|
||||||
|
{
|
||||||
|
rv = _lastKey;
|
||||||
|
}
|
||||||
|
else if (_key == 0 && _lastKey != 0) // RELEASE
|
||||||
|
{
|
||||||
|
_lastKey = _key;
|
||||||
|
rv = _lastKey;
|
||||||
|
}
|
||||||
|
else if (_key != 0 && _lastKey == 0) // PRESS
|
||||||
|
{
|
||||||
|
_lastKey = _key;
|
||||||
|
rv = _lastKey;
|
||||||
|
}
|
||||||
|
else if (_key != 0 && _lastKey != 0 && _key != _lastKey) // SUPPRESS CHANGE
|
||||||
|
{
|
||||||
|
rv = _lastKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t AnalogKeypad::read()
|
||||||
|
{
|
||||||
|
_lastKey = rawRead();
|
||||||
|
return _lastKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adjust numbers for other than 4x4 keypad
|
||||||
|
uint8_t AnalogKeypad::rawRead()
|
||||||
|
{
|
||||||
|
// spends most time in analogRead (uno ~110 usec)
|
||||||
|
uint8_t val = analogRead(_pin) >> AKP_SHIFT;
|
||||||
|
|
||||||
|
// handle NOKEY first
|
||||||
|
if (val < 57) return 0;
|
||||||
|
// reduce average # compares by 2
|
||||||
|
if (val < 135)
|
||||||
|
{
|
||||||
|
if (val < 62) return 16;
|
||||||
|
if (val < 75) return 15;
|
||||||
|
if (val < 92) return 14;
|
||||||
|
if (val < 106) return 13;
|
||||||
|
if (val < 113) return 12;
|
||||||
|
if (val < 119) return 11;
|
||||||
|
if (val < 125) return 10;
|
||||||
|
return 9;
|
||||||
|
}
|
||||||
|
if (val < 146) return 8;
|
||||||
|
if (val < 155) return 7;
|
||||||
|
if (val < 165) return 6;
|
||||||
|
if (val < 187) return 5;
|
||||||
|
if (val < 205) return 4;
|
||||||
|
if (val < 222) return 3;
|
||||||
|
if (val < 244) return 2;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -- END OF FILE --
|
52
libraries/AnalogKeypad/AnalogKeypad.h
Normal file
52
libraries/AnalogKeypad/AnalogKeypad.h
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
//
|
||||||
|
// FILE: AnalogKeypad.h
|
||||||
|
// AUTHOR: Rob Tillaart
|
||||||
|
// VERSION: 0.1.2
|
||||||
|
// DATE: 2019-01-31
|
||||||
|
// PURPOSE: Class for analog keypad
|
||||||
|
// URL: https://github.com/RobTillaart/Arduino.git
|
||||||
|
//
|
||||||
|
// Released to the public domain
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef AnalogKeypad_h
|
||||||
|
#define AnalogKeypad_h
|
||||||
|
|
||||||
|
#include "Arduino.h"
|
||||||
|
|
||||||
|
#define ANALOGKEYPAD_LIB_VERSION "0.1.2"
|
||||||
|
|
||||||
|
#define NOKEY 0x00
|
||||||
|
#define PRESSED 0x80
|
||||||
|
#define RELEASED 0x40
|
||||||
|
#define REPEATED 0x20
|
||||||
|
#define CHANGED 0x10
|
||||||
|
|
||||||
|
class AnalogKeypad
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit AnalogKeypad(const uint8_t pin);
|
||||||
|
|
||||||
|
// returns 0 if no key pressed
|
||||||
|
// otherwise returns key pressed first => ignoring fluctuations
|
||||||
|
// 2nd or more presses simultaneous are ignored
|
||||||
|
uint8_t pressed();
|
||||||
|
|
||||||
|
// returns 0 if no key pressed
|
||||||
|
// otherwise returns key pressed (may fluctuate)
|
||||||
|
uint8_t read();
|
||||||
|
|
||||||
|
|
||||||
|
// event alike approach
|
||||||
|
// switch(int e = event())
|
||||||
|
uint8_t event();
|
||||||
|
uint8_t key() { return _lastKey; } ;
|
||||||
|
|
||||||
|
private:
|
||||||
|
uint8_t rawRead();
|
||||||
|
uint8_t _pin;
|
||||||
|
uint8_t _lastKey;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
// -- END OF FILE --
|
@ -0,0 +1,81 @@
|
|||||||
|
//
|
||||||
|
// FILE: analogKeypad.ino
|
||||||
|
// AUTHOR: Rob Tillaart
|
||||||
|
// VERSION: 0.0.1
|
||||||
|
// PURPOSE:
|
||||||
|
//
|
||||||
|
// HISTORY:
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
void setup()
|
||||||
|
{
|
||||||
|
Serial.begin(115200);
|
||||||
|
Serial.println(__FILE__);
|
||||||
|
Serial.print("ANALOGKEYPAD_LIB_VERSION:\t");
|
||||||
|
Serial.println(ANALOGKEYPAD_LIB_VERSION);
|
||||||
|
|
||||||
|
for (int i = 0; i < 100; i++) test1();
|
||||||
|
for (int i = 0; i < 100; i++) test2();
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
void test1()
|
||||||
|
{
|
||||||
|
int button = AKP.pressed();
|
||||||
|
if (button != 0)
|
||||||
|
{
|
||||||
|
Serial.print("pressed:\t");
|
||||||
|
Serial.println(button, HEX);
|
||||||
|
}
|
||||||
|
delay(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
// use the "event" interface
|
||||||
|
void test2()
|
||||||
|
{
|
||||||
|
uint8_t e = AKP.event();
|
||||||
|
switch (e)
|
||||||
|
{
|
||||||
|
case 0x80:
|
||||||
|
Serial.print("press\t");
|
||||||
|
Serial.println(AKP.key());
|
||||||
|
break;
|
||||||
|
case 0x40:
|
||||||
|
Serial.print("release\t");
|
||||||
|
Serial.println(AKP.key());
|
||||||
|
break;
|
||||||
|
case 0x20:
|
||||||
|
Serial.print("repeat\t");
|
||||||
|
Serial.println(AKP.key());
|
||||||
|
break;
|
||||||
|
case 0x10:
|
||||||
|
Serial.print("change\t");
|
||||||
|
Serial.println(AKP.key());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
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);
|
||||||
|
}
|
24
libraries/AnalogKeypad/keywords.txt
Normal file
24
libraries/AnalogKeypad/keywords.txt
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#######################################
|
||||||
|
# Syntax Coloring Map For AnalogKeypad
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
# Datatypes (KEYWORD1)
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
AnalogKeypad KEYWORD1
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
# Methods and Functions (KEYWORD2)
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
read KEYWORD2
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
# Instances (KEYWORD2)
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
# Constants (LITERAL1)
|
||||||
|
#######################################
|
||||||
|
|
24
libraries/AnalogKeypad/library.json
Normal file
24
libraries/AnalogKeypad/library.json
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"name": "AnalogKeypad",
|
||||||
|
"keywords": "Analog Keypad",
|
||||||
|
"description": "Class for Robotdyn 4x4 analog keypad.",
|
||||||
|
"authors":
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"name": "Rob Tillaart",
|
||||||
|
"email": "Rob.Tillaart@gmail.com",
|
||||||
|
"maintainer": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"repository":
|
||||||
|
{
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/RobTillaart/Arduino.git"
|
||||||
|
},
|
||||||
|
"version":"0.1.2",
|
||||||
|
"frameworks": "arduino",
|
||||||
|
"platforms": "*",
|
||||||
|
"export": {
|
||||||
|
"include": "libraries/AnalogKeypad"
|
||||||
|
}
|
||||||
|
}
|
9
libraries/AnalogKeypad/library.properties
Normal file
9
libraries/AnalogKeypad/library.properties
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
name=AnalogKeypad
|
||||||
|
version=0.1.2
|
||||||
|
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||||
|
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||||
|
sentence=Arduino Library for AnalogKeypad
|
||||||
|
paragraph=Class for (Robotdyn) 4x4 and 4x3 AnalogKeypad
|
||||||
|
category=Signal Input/Output
|
||||||
|
url=https://github.com/RobTillaart/Arduino/tree/master/libraries/AnalogKeypad
|
||||||
|
architectures=*
|
Loading…
Reference in New Issue
Block a user