Skip to content

Commit

Permalink
update readme.md (#23)
Browse files Browse the repository at this point in the history
- Fix #21, add section to readme.md
- Fix #22, implement **debounceThreshold**
- add constant **I2C_KEYPAD_THRESHOLD**
- add **uint32_t getLastTimeRead()**
- update **getChar()** to support **I2C_KEYPAD_THRESHOLD**
- update readme.md
- update unit test
- update keywords.txt
- minor edits
  • Loading branch information
RobTillaart authored Jul 18, 2024
1 parent 17cad15 commit 222e837
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 105 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.5.0] - 2024-07-14
- Fix #21, add section to readme.md
- Fix #22, implement **debounceThreshold**
- add constant **I2C_KEYPAD_THRESHOLD**
- add **uint32_t getLastTimeRead()**
- update **getChar()** to support **I2C_KEYPAD_THRESHOLD**
- update readme.md
- update unit test
- update keywords.txt
- minor edits

----

## [0.4.0] - 2023-11-09
- simplify begin()
- added I2Ckeypad_Wire1_ESP32.ino
Expand Down
98 changes: 65 additions & 33 deletions I2CKeyPad .cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: I2CKeyPad.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.4.0
// VERSION: 0.5.0
// PURPOSE: Arduino library for 4x4 KeyPad connected to an I2C PCF8574
// URL: https://github.com/RobTillaart/I2CKeyPad

Expand All @@ -15,6 +15,8 @@ I2CKeyPad::I2CKeyPad(const uint8_t deviceAddress, TwoWire *wire)
_address = deviceAddress;
_wire = wire;
_mode = I2C_KEYPAD_4x4;
_debounceThreshold = 0;
_lastTimeRead = 0;
}


Expand All @@ -26,20 +28,42 @@ bool I2CKeyPad::begin()
}


bool I2CKeyPad::isConnected()
{
_wire->beginTransmission(_address);
return (_wire->endTransmission() == 0);
}


uint8_t I2CKeyPad::getKey()
{
if (_mode == I2C_KEYPAD_5x3) return _getKey5x3();
if (_mode == I2C_KEYPAD_6x2) return _getKey6x2();
if (_mode == I2C_KEYPAD_8x1) return _getKey8x1();
// default.
return _getKey4x4();
uint32_t now = millis();
if (_debounceThreshold > 0)
{
if (now - _debounceThreshold < _lastTimeRead)
{
return I2C_KEYPAD_THRESHOLD;
}
}

uint8_t key = 0;
if (_mode == I2C_KEYPAD_5x3) key = _getKey5x3();
else if (_mode == I2C_KEYPAD_6x2) key = _getKey6x2();
else if (_mode == I2C_KEYPAD_8x1) key = _getKey8x1();
else key = _getKey4x4(); // default.

if (key == I2C_KEYPAD_FAIL) return key; // propagate error.
// valid keys + NOKEY
_lastKey = key;
_lastTimeRead = now;
return key;
}


uint8_t I2CKeyPad::getLastKey()
{
uint8_t I2CKeyPad::getLastKey()
{
return _lastKey;
};
}


// to check "press any key"
Expand All @@ -51,23 +75,21 @@ bool I2CKeyPad::isPressed()
}


bool I2CKeyPad::isConnected()
uint8_t I2CKeyPad::getChar()
{
_wire->beginTransmission(_address);
return (_wire->endTransmission() == 0);
uint8_t key = getKey();
if (key != I2C_KEYPAD_THRESHOLD)
{
return _keyMap[key];
}
return I2C_KEYPAD_THRESHOLD;
}


uint8_t I2CKeyPad::getChar()
{
return _keyMap[getKey()];
};


uint8_t I2CKeyPad::getLastChar()
{
return _keyMap[_lastKey];
};
{
return _keyMap[_lastKey];
}


void I2CKeyPad::loadKeyMap(char * keyMap)
Expand All @@ -78,7 +100,7 @@ void I2CKeyPad::loadKeyMap(char * keyMap)

void I2CKeyPad::setKeyPadMode(uint8_t mode)
{
if ((mode == I2C_KEYPAD_5x3) ||
if ((mode == I2C_KEYPAD_5x3) ||
(mode == I2C_KEYPAD_6x2) ||
(mode == I2C_KEYPAD_8x1))
{
Expand All @@ -95,6 +117,24 @@ uint8_t I2CKeyPad::getKeyPadMode()
}


void I2CKeyPad::setDebounceThreshold(uint16_t value)
{
_debounceThreshold = value;
}


uint16_t I2CKeyPad::getDebounceThreshold()
{
return _debounceThreshold;
}


uint32_t I2CKeyPad::getLastTimeRead()
{
return _lastTimeRead;
}


//////////////////////////////////////////////////////
//
// PROTECTED
Expand All @@ -118,7 +158,7 @@ uint8_t I2CKeyPad::_read(uint8_t mask)

uint8_t I2CKeyPad::_getKey4x4()
{
// key = row + 4 x col
// key = row + 4 x column
uint8_t key = 0;

// mask = 4 rows as input pull up, 4 columns as output
Expand All @@ -141,16 +181,14 @@ uint8_t I2CKeyPad::_getKey4x4()
else if (cols == 0x07) key += 12;
else return I2C_KEYPAD_FAIL;

_lastKey = key;

return key; // 0..15
}


// not tested
uint8_t I2CKeyPad::_getKey5x3()
{
// key = row + 5 x col
// key = row + 5 x column
uint8_t key = 0;

// mask = 5 rows as input pull up, 3 columns as output
Expand All @@ -173,16 +211,14 @@ uint8_t I2CKeyPad::_getKey5x3()
else if (cols == 0x03) key += 10;
else return I2C_KEYPAD_FAIL;

_lastKey = key;

return key; // 0..14
}


// not tested
uint8_t I2CKeyPad::_getKey6x2()
{
// key = row + 6 x col
// key = row + 6 x column
uint8_t key = 0;

// mask = 6 rows as input pull up, 2 columns as output
Expand All @@ -205,8 +241,6 @@ uint8_t I2CKeyPad::_getKey6x2()
else if (cols == 0x01) key += 6;
else return I2C_KEYPAD_FAIL;

_lastKey = key;

return key; // 0..11
}

Expand All @@ -231,8 +265,6 @@ uint8_t I2CKeyPad::_getKey8x1()
else if (rows == 0x7F) key = 7;
else return I2C_KEYPAD_FAIL;

_lastKey = key;

return key; // 0..7
}

Expand Down
54 changes: 31 additions & 23 deletions I2CKeyPad.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// FILE: I2CKeyPad.h
// AUTHOR: Rob Tillaart
// VERSION: 0.4.0
// VERSION: 0.5.0
// PURPOSE: Arduino library for 4x4 KeyPad connected to an I2C PCF8574
// URL: https://github.com/RobTillaart/I2CKeyPad

Expand All @@ -11,10 +11,13 @@
#include "Wire.h"


#define I2C_KEYPAD_LIB_VERSION (F("0.4.0"))
#define I2C_KEYPAD_LIB_VERSION (F("0.5.0"))

#define I2C_KEYPAD_NOKEY 16
#define I2C_KEYPAD_FAIL 17
//
#define I2C_KEYPAD_THRESHOLD 255


// experimental
#define I2C_KEYPAD_4x4 44
Expand All @@ -29,37 +32,42 @@ class I2CKeyPad
I2CKeyPad(const uint8_t deviceAddress, TwoWire *wire = &Wire);

// call Wire.begin() first!
bool begin();
bool begin();
bool isConnected();

// get raw key's 0..15
uint8_t getKey();
uint8_t getLastKey();

bool isPressed();
bool isConnected();
uint8_t getKey();
uint8_t getLastKey();
bool isPressed();

// get 'translated' keys
// user must load KeyMap, there is no check.
uint8_t getChar();
uint8_t getLastChar();
void loadKeyMap(char * keyMap); // char[19]
uint8_t getChar();
uint8_t getLastChar();
void loadKeyMap(char * keyMap); // char[19]

// mode functions - experimental
void setKeyPadMode(uint8_t mode = I2C_KEYPAD_4x4);
uint8_t getKeyPadMode();
void setKeyPadMode(uint8_t mode = I2C_KEYPAD_4x4);
uint8_t getKeyPadMode();

// value in milliseconds, max 65535 ms
void setDebounceThreshold(uint16_t value = 0);
uint16_t getDebounceThreshold();
uint32_t getLastTimeRead();


protected:
uint8_t _address;
uint8_t _lastKey;
uint8_t _mode;
uint8_t _read(uint8_t mask);
uint8_t _getKey4x4();

// experimental - could be public ?!
uint8_t _getKey5x3();
uint8_t _getKey6x2();
uint8_t _getKey8x1();
uint8_t _address;
uint8_t _lastKey;
uint8_t _mode;
uint8_t _read(uint8_t mask);
uint16_t _debounceThreshold;
uint32_t _lastTimeRead;

uint8_t _getKey4x4();
uint8_t _getKey5x3();
uint8_t _getKey6x2();
uint8_t _getKey8x1();

TwoWire* _wire;

Expand Down
Loading

0 comments on commit 222e837

Please sign in to comment.