2019-02-01 09:18:01 -05:00
|
|
|
//
|
|
|
|
// FILE: AnalogKeypad.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2023-01-22 05:41:01 -05:00
|
|
|
// VERSION: 0.2.3
|
2019-02-01 09:18:01 -05:00
|
|
|
// DATE: 2019-01-31
|
2020-11-27 05:10:47 -05:00
|
|
|
// PURPOSE: Class for (Robotdyn) 4x4 and 4x3 analog keypad
|
2021-05-28 07:19:00 -04:00
|
|
|
|
2019-02-01 09:18:01 -05:00
|
|
|
|
|
|
|
#include "AnalogKeypad.h"
|
|
|
|
|
2021-05-28 07:19:00 -04:00
|
|
|
|
2022-10-27 11:49:36 -04:00
|
|
|
// NOTE the MAGIC NUMBERS in rawRead() are for 8 BIT ADC
|
|
|
|
// as 8 bit compares are fast
|
2019-02-01 09:18:01 -05:00
|
|
|
//
|
2022-10-27 11:49:36 -04:00
|
|
|
// The _analogShift takes care if the ADC has more
|
2023-01-22 05:41:01 -05:00
|
|
|
// than e.g. 10 bits.
|
2019-02-01 09:18:01 -05:00
|
|
|
//
|
2022-10-27 11:49:36 -04:00
|
|
|
// Arduino UNO3 build in ==> 10 bits
|
|
|
|
// Other may have 12 or even 16 bits.
|
2019-02-01 09:18:01 -05:00
|
|
|
|
2021-05-28 07:19:00 -04:00
|
|
|
|
2021-10-17 11:47:04 -04:00
|
|
|
AnalogKeypad::AnalogKeypad(const uint8_t pin, const uint8_t bits)
|
2019-02-01 09:18:01 -05:00
|
|
|
{
|
2021-10-17 11:47:04 -04:00
|
|
|
_analogPin = pin;
|
|
|
|
_analogShift = bits - 8;
|
|
|
|
_lastKey = NOKEY;
|
2019-02-01 09:18:01 -05:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:19:00 -04:00
|
|
|
|
2019-02-01 09:18:01 -05:00
|
|
|
uint8_t AnalogKeypad::event()
|
|
|
|
{
|
|
|
|
int rv = NOKEY;
|
2021-10-17 11:47:04 -04:00
|
|
|
uint8_t _key = _rawRead();
|
2019-02-01 09:18:01 -05:00
|
|
|
|
2021-10-17 11:47:04 -04:00
|
|
|
if (_key == 0 && _lastKey == 0) rv = NOKEY;
|
2019-02-01 09:18:01 -05:00
|
|
|
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;
|
|
|
|
|
2020-11-27 05:10:47 -05:00
|
|
|
return rv;
|
2019-02-01 09:18:01 -05:00
|
|
|
}
|
|
|
|
|
2021-05-28 07:19:00 -04:00
|
|
|
|
2019-02-01 09:18:01 -05:00
|
|
|
uint8_t AnalogKeypad::pressed()
|
|
|
|
{
|
|
|
|
int rv = NOKEY;
|
|
|
|
|
2021-10-17 11:47:04 -04:00
|
|
|
uint8_t _key = _rawRead();
|
2023-01-22 05:41:01 -05:00
|
|
|
if (_key == _lastKey) // NOKEY OR REPEAT
|
2019-02-01 09:18:01 -05:00
|
|
|
{
|
|
|
|
rv = _lastKey;
|
|
|
|
}
|
2023-01-22 05:41:01 -05:00
|
|
|
else if (_key == 0 && _lastKey != 0) // RELEASE
|
2019-02-01 09:18:01 -05:00
|
|
|
{
|
|
|
|
_lastKey = _key;
|
|
|
|
rv = _lastKey;
|
|
|
|
}
|
2023-01-22 05:41:01 -05:00
|
|
|
else if (_key != 0 && _lastKey == 0) // PRESS
|
2019-02-01 09:18:01 -05:00
|
|
|
{
|
|
|
|
_lastKey = _key;
|
|
|
|
rv = _lastKey;
|
|
|
|
}
|
2023-01-22 05:41:01 -05:00
|
|
|
else if (_key != 0 && _lastKey != 0 && _key != _lastKey) // SUPPRESS CHANGE
|
2019-02-01 09:18:01 -05:00
|
|
|
{
|
|
|
|
rv = _lastKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2021-05-28 07:19:00 -04:00
|
|
|
|
2019-02-01 09:18:01 -05:00
|
|
|
uint8_t AnalogKeypad::read()
|
|
|
|
{
|
2021-10-17 11:47:04 -04:00
|
|
|
_lastKey = _rawRead();
|
2019-02-01 09:18:01 -05:00
|
|
|
return _lastKey;
|
|
|
|
}
|
|
|
|
|
2021-05-28 07:19:00 -04:00
|
|
|
|
2022-10-27 11:49:36 -04:00
|
|
|
// Adjust numbers for other than 4x4 keypad
|
2021-10-17 11:47:04 -04:00
|
|
|
uint8_t AnalogKeypad::_rawRead()
|
2019-02-01 09:18:01 -05:00
|
|
|
{
|
2023-01-22 05:41:01 -05:00
|
|
|
// spends most time in analogRead (UNO ~110 microseconds)
|
2021-10-17 11:47:04 -04:00
|
|
|
uint8_t val = (analogRead(_analogPin) >> _analogShift);
|
2019-02-01 09:18:01 -05:00
|
|
|
|
2022-10-27 11:49:36 -04:00
|
|
|
// handle NOKEY first
|
2019-02-01 09:18:01 -05:00
|
|
|
if (val < 57) return 0;
|
2020-11-27 05:10:47 -05:00
|
|
|
|
2022-10-27 11:49:36 -04:00
|
|
|
// reduce average # compares by 2 (4x4 keypad)
|
2019-02-01 09:18:01 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-12-12 09:08:09 -05:00
|
|
|
|
2023-01-22 05:41:01 -05:00
|
|
|
uint8_t AnalogKeypad::key()
|
|
|
|
{
|
|
|
|
return _lastKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -- END OF FILE --
|
2021-12-12 09:08:09 -05:00
|
|
|
|