2021-01-29 06:31:58 -05:00
|
|
|
//
|
|
|
|
// FILE: I2CKeyPad.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2022-11-12 11:25:54 -05:00
|
|
|
// VERSION: 0.3.3
|
2021-11-08 07:13:29 -05:00
|
|
|
// PURPOSE: Arduino library for 4x4 KeyPad connected to an I2C PCF8574
|
2021-01-29 06:31:58 -05:00
|
|
|
// URL: https://github.com/RobTillaart/I2CKeyPad
|
|
|
|
//
|
2022-11-12 11:25:54 -05:00
|
|
|
// HISTORY: see changelog.md
|
2021-11-08 07:13:29 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
#include "I2CKeyPad.h"
|
|
|
|
|
2021-11-08 07:13:29 -05:00
|
|
|
|
2021-05-08 03:52:18 -04:00
|
|
|
I2CKeyPad::I2CKeyPad(const uint8_t deviceAddress, TwoWire *wire)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
2021-05-08 03:52:18 -04:00
|
|
|
_lastKey = I2C_KEYPAD_NOKEY;
|
|
|
|
_address = deviceAddress;
|
|
|
|
_wire = wire;
|
2022-09-29 10:17:30 -04:00
|
|
|
_mode = I2C_KEYPAD_4x4;
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
2021-11-08 07:13:29 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#if defined(ESP8266) || defined(ESP32)
|
2021-05-08 03:52:18 -04:00
|
|
|
bool I2CKeyPad::begin(uint8_t sda, uint8_t scl)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
2021-05-08 03:52:18 -04:00
|
|
|
_wire->begin(sda, scl);
|
2021-12-19 14:23:20 -05:00
|
|
|
// enable interrupts
|
|
|
|
_read(0xF0);
|
2021-01-29 06:31:58 -05:00
|
|
|
return isConnected();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-11-08 07:13:29 -05:00
|
|
|
|
2021-05-08 03:52:18 -04:00
|
|
|
bool I2CKeyPad::begin()
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
2021-05-08 03:52:18 -04:00
|
|
|
_wire->begin();
|
2021-12-19 14:23:20 -05:00
|
|
|
// enable interrupts
|
2021-11-08 07:13:29 -05:00
|
|
|
_read(0xF0);
|
2021-01-29 06:31:58 -05:00
|
|
|
return isConnected();
|
|
|
|
}
|
|
|
|
|
2021-11-08 07:13:29 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t I2CKeyPad::getKey()
|
|
|
|
{
|
2022-09-29 10:17:30 -04:00
|
|
|
if (_mode == I2C_KEYPAD_5x3) return _getKey5x3();
|
|
|
|
if (_mode == I2C_KEYPAD_6x2) return _getKey6x2();
|
|
|
|
if (_mode == I2C_KEYPAD_8x1) return _getKey8x1();
|
|
|
|
// default.
|
2021-11-08 07:13:29 -05:00
|
|
|
return _getKey4x4();
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
2021-11-08 07:13:29 -05:00
|
|
|
|
2022-09-29 10:17:30 -04:00
|
|
|
uint8_t I2CKeyPad::getLastKey()
|
|
|
|
{
|
|
|
|
return _lastKey;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-12-19 14:23:20 -05:00
|
|
|
// to check "press any key"
|
2021-01-29 06:31:58 -05:00
|
|
|
bool I2CKeyPad::isPressed()
|
|
|
|
{
|
|
|
|
uint8_t a = _read(0xF0);
|
|
|
|
if (a == 0xFF) return false;
|
|
|
|
return (a != 0xF0);
|
|
|
|
}
|
|
|
|
|
2021-11-08 07:13:29 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
bool I2CKeyPad::isConnected()
|
|
|
|
{
|
2021-05-08 03:52:18 -04:00
|
|
|
_wire->beginTransmission(_address);
|
|
|
|
return (_wire->endTransmission() == 0);
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
2021-11-08 07:13:29 -05:00
|
|
|
|
2022-09-29 10:17:30 -04:00
|
|
|
uint8_t I2CKeyPad::getChar()
|
|
|
|
{
|
|
|
|
return _keyMap[getKey()];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t I2CKeyPad::getLastChar()
|
|
|
|
{
|
|
|
|
return _keyMap[_lastKey];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-11-08 07:13:29 -05:00
|
|
|
void I2CKeyPad::loadKeyMap(char * keyMap)
|
|
|
|
{
|
|
|
|
_keyMap = keyMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-29 10:17:30 -04:00
|
|
|
void I2CKeyPad::setKeyPadMode(uint8_t mode)
|
|
|
|
{
|
|
|
|
if ((mode == I2C_KEYPAD_5x3) ||
|
|
|
|
(mode == I2C_KEYPAD_6x2) ||
|
|
|
|
(mode == I2C_KEYPAD_8x1))
|
|
|
|
{
|
|
|
|
_mode = mode;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_mode = I2C_KEYPAD_4x4;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t I2CKeyPad::getKeyPadMode()
|
|
|
|
{
|
|
|
|
return _mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-08 07:13:29 -05:00
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
//
|
2022-09-29 10:17:30 -04:00
|
|
|
// PROTECTED
|
2021-11-08 07:13:29 -05:00
|
|
|
//
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t I2CKeyPad::_read(uint8_t mask)
|
|
|
|
{
|
2021-12-19 14:23:20 -05:00
|
|
|
// improve the odds that IO will not interrupted.
|
|
|
|
yield();
|
2021-05-08 03:52:18 -04:00
|
|
|
|
|
|
|
_wire->beginTransmission(_address);
|
|
|
|
_wire->write(mask);
|
|
|
|
if (_wire->endTransmission() != 0)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
2021-12-19 14:23:20 -05:00
|
|
|
// set communication error
|
2021-01-29 06:31:58 -05:00
|
|
|
return 0xFF;
|
|
|
|
}
|
2021-05-08 03:52:18 -04:00
|
|
|
_wire->requestFrom(_address, (uint8_t)1);
|
|
|
|
return _wire->read();
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
2021-11-08 07:13:29 -05:00
|
|
|
|
|
|
|
uint8_t I2CKeyPad::_getKey4x4()
|
|
|
|
{
|
2021-12-19 14:23:20 -05:00
|
|
|
// key = row + 4 x col
|
2021-11-08 07:13:29 -05:00
|
|
|
uint8_t key = 0;
|
|
|
|
|
2021-12-19 14:23:20 -05:00
|
|
|
// mask = 4 rows as input pull up, 4 columns as output
|
2021-11-08 07:13:29 -05:00
|
|
|
uint8_t rows = _read(0xF0);
|
2021-12-19 14:23:20 -05:00
|
|
|
// check if single line has gone low.
|
2021-11-08 07:13:29 -05:00
|
|
|
if (rows == 0xF0) return I2C_KEYPAD_NOKEY;
|
|
|
|
else if (rows == 0xE0) key = 0;
|
|
|
|
else if (rows == 0xD0) key = 1;
|
|
|
|
else if (rows == 0xB0) key = 2;
|
|
|
|
else if (rows == 0x70) key = 3;
|
|
|
|
else return I2C_KEYPAD_FAIL;
|
|
|
|
|
|
|
|
// 4 columns as input pull up, 4 rows as output
|
|
|
|
uint8_t cols = _read(0x0F);
|
|
|
|
// check if single line has gone low.
|
|
|
|
if (cols == 0x0F) return I2C_KEYPAD_NOKEY;
|
|
|
|
else if (cols == 0x0E) key += 0;
|
|
|
|
else if (cols == 0x0D) key += 4;
|
|
|
|
else if (cols == 0x0B) key += 8;
|
|
|
|
else if (cols == 0x07) key += 12;
|
|
|
|
else return I2C_KEYPAD_FAIL;
|
|
|
|
|
|
|
|
_lastKey = key;
|
|
|
|
|
|
|
|
return key; // 0..15
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-29 10:17:30 -04:00
|
|
|
// not tested
|
|
|
|
uint8_t I2CKeyPad::_getKey5x3()
|
|
|
|
{
|
|
|
|
// key = row + 5 x col
|
|
|
|
uint8_t key = 0;
|
|
|
|
|
|
|
|
// mask = 5 rows as input pull up, 3 columns as output
|
|
|
|
uint8_t rows = _read(0xF8);
|
|
|
|
// check if single line has gone low.
|
|
|
|
if (rows == 0xF8) return I2C_KEYPAD_NOKEY;
|
|
|
|
else if (rows == 0xF0) key = 0;
|
|
|
|
else if (rows == 0xE8) key = 1;
|
|
|
|
else if (rows == 0xD8) key = 2;
|
|
|
|
else if (rows == 0xB8) key = 3;
|
|
|
|
else if (rows == 0x78) key = 4;
|
|
|
|
else return I2C_KEYPAD_FAIL;
|
|
|
|
|
|
|
|
// 3 columns as input pull up, 5 rows as output
|
|
|
|
uint8_t cols = _read(0x07);
|
|
|
|
// check if single line has gone low.
|
|
|
|
if (cols == 0x07) return I2C_KEYPAD_NOKEY;
|
|
|
|
else if (cols == 0x06) key += 0;
|
|
|
|
else if (cols == 0x05) key += 5;
|
|
|
|
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
|
|
|
|
uint8_t key = 0;
|
|
|
|
|
|
|
|
// mask = 6 rows as input pull up, 2 columns as output
|
|
|
|
uint8_t rows = _read(0xFC);
|
|
|
|
// check if single line has gone low.
|
|
|
|
if (rows == 0xFC) return I2C_KEYPAD_NOKEY;
|
|
|
|
else if (rows == 0xF8) key = 0;
|
|
|
|
else if (rows == 0xF4) key = 1;
|
|
|
|
else if (rows == 0xEC) key = 2;
|
|
|
|
else if (rows == 0xDC) key = 3;
|
|
|
|
else if (rows == 0xBC) key = 4;
|
|
|
|
else if (rows == 0x7C) key = 5;
|
|
|
|
else return I2C_KEYPAD_FAIL;
|
|
|
|
|
|
|
|
// 2 columns as input pull up, 6 rows as output
|
|
|
|
uint8_t cols = _read(0x03);
|
|
|
|
// check if single line has gone low.
|
|
|
|
if (cols == 0x03) return I2C_KEYPAD_NOKEY;
|
|
|
|
else if (cols == 0x02) key += 0;
|
|
|
|
else if (cols == 0x01) key += 6;
|
|
|
|
else return I2C_KEYPAD_FAIL;
|
|
|
|
|
|
|
|
_lastKey = key;
|
|
|
|
|
|
|
|
return key; // 0..11
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// not tested
|
|
|
|
uint8_t I2CKeyPad::_getKey8x1()
|
|
|
|
{
|
|
|
|
// key = row
|
|
|
|
uint8_t key = 0;
|
|
|
|
|
|
|
|
// mask = 8 rows as input pull up, 0 columns as output
|
|
|
|
uint8_t rows = _read(0xFF);
|
|
|
|
// check if single line has gone low.
|
|
|
|
if (rows == 0xFF) return I2C_KEYPAD_NOKEY;
|
|
|
|
else if (rows == 0xFE) key = 0;
|
|
|
|
else if (rows == 0xFD) key = 1;
|
|
|
|
else if (rows == 0xFB) key = 2;
|
|
|
|
else if (rows == 0xF7) key = 3;
|
|
|
|
else if (rows == 0xEF) key = 4;
|
|
|
|
else if (rows == 0xDF) key = 5;
|
|
|
|
else if (rows == 0xBF) key = 6;
|
|
|
|
else if (rows == 0x7F) key = 7;
|
|
|
|
else return I2C_KEYPAD_FAIL;
|
|
|
|
|
|
|
|
_lastKey = key;
|
|
|
|
|
|
|
|
return key; // 0..7
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
// -- END OF FILE --
|
2021-11-08 07:13:29 -05:00
|
|
|
|