|
| 1 | +// |
| 2 | +// FILE: I2CKeyPad8x8.cpp |
| 3 | +// AUTHOR: Rob Tillaart |
| 4 | +// VERSION: 0.1.0 |
| 5 | +// PURPOSE: Arduino library for 8x8 or smaller KeyPad connected to an I2C PCF8575. |
| 6 | +// URL: https://github.com/RobTillaart/I2CKeyPad8x8 |
| 7 | +// |
| 8 | +// HISTORY: |
| 9 | +// 0.1.0 2022-09-29 initial version |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +#include "I2CKeyPad8x8.h" |
| 14 | + |
| 15 | + |
| 16 | +I2CKeyPad8x8::I2CKeyPad8x8(const uint8_t deviceAddress, TwoWire *wire) |
| 17 | +{ |
| 18 | + _lastKey = I2C_KEYPAD8x8_NOKEY; |
| 19 | + _address = deviceAddress; |
| 20 | + _wire = wire; |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +#if defined(ESP8266) || defined(ESP32) |
| 25 | +bool I2CKeyPad8x8::begin(uint8_t sda, uint8_t scl) |
| 26 | +{ |
| 27 | + _wire->begin(sda, scl); |
| 28 | + // enable interrupts |
| 29 | + _read(0xFF00); |
| 30 | + return isConnected(); |
| 31 | +} |
| 32 | +#endif |
| 33 | + |
| 34 | + |
| 35 | +bool I2CKeyPad8x8::begin() |
| 36 | +{ |
| 37 | + _wire->begin(); |
| 38 | + // enable interrupts |
| 39 | + _read(0xFF00); |
| 40 | + return isConnected(); |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +uint8_t I2CKeyPad8x8::getKey() |
| 45 | +{ |
| 46 | + // key = row + 8 x col |
| 47 | + uint8_t key = 0; |
| 48 | + |
| 49 | + // mask = 8 rows as input pull up, 8 columns as output |
| 50 | + uint16_t rows = _read(0xFF00); |
| 51 | + |
| 52 | + // check if single line has gone low. |
| 53 | + if (rows == 0xFF00) return I2C_KEYPAD8x8_NOKEY; |
| 54 | + else if (rows == 0xFE00) key = 0; |
| 55 | + else if (rows == 0xFD00) key = 1; |
| 56 | + else if (rows == 0xFB00) key = 2; |
| 57 | + else if (rows == 0xF700) key = 3; |
| 58 | + else if (rows == 0xEF00) key = 4; |
| 59 | + else if (rows == 0xDF00) key = 5; |
| 60 | + else if (rows == 0xBF00) key = 6; |
| 61 | + else if (rows == 0x7F00) key = 7; |
| 62 | + else return I2C_KEYPAD8x8_FAIL; |
| 63 | + |
| 64 | + // 8 columns as input pull up, 8 rows as output |
| 65 | + uint16_t cols = _read(0x00FF); |
| 66 | + // check if single line has gone low. |
| 67 | + if (cols == 0x00FF) return I2C_KEYPAD8x8_NOKEY; |
| 68 | + else if (cols == 0x00FE) key += 0; |
| 69 | + else if (cols == 0x00FD) key += 8; |
| 70 | + else if (cols == 0x00FB) key += 16; |
| 71 | + else if (cols == 0x00F7) key += 24; |
| 72 | + else if (cols == 0x00EF) key += 32; |
| 73 | + else if (cols == 0x00DF) key += 40; |
| 74 | + else if (cols == 0x00BF) key += 48; |
| 75 | + else if (cols == 0x007F) key += 56; |
| 76 | + else return I2C_KEYPAD8x8_FAIL; |
| 77 | + |
| 78 | + _lastKey = key; |
| 79 | + |
| 80 | + return key; // 0..65 |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +uint8_t I2CKeyPad8x8::getLastKey() |
| 85 | +{ |
| 86 | + return _lastKey; |
| 87 | +}; |
| 88 | + |
| 89 | + |
| 90 | +// to check "press any key" |
| 91 | +bool I2CKeyPad8x8::isPressed() |
| 92 | +{ |
| 93 | + uint16_t a = _read(0xFF00); |
| 94 | + if (a == 0xFF00) return false; |
| 95 | + return (a != 0xFF00); |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +bool I2CKeyPad8x8::isConnected() |
| 100 | +{ |
| 101 | + _wire->beginTransmission(_address); |
| 102 | + return (_wire->endTransmission() == 0); |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | +uint8_t I2CKeyPad8x8::getChar() |
| 107 | +{ |
| 108 | + return _keyMap[getKey()]; |
| 109 | +}; |
| 110 | + |
| 111 | + |
| 112 | +uint8_t I2CKeyPad8x8::getLastChar() |
| 113 | +{ |
| 114 | + return _keyMap[_lastKey]; |
| 115 | +}; |
| 116 | + |
| 117 | + |
| 118 | +void I2CKeyPad8x8::loadKeyMap(char * keyMap) |
| 119 | +{ |
| 120 | + _keyMap = keyMap; |
| 121 | +} |
| 122 | + |
| 123 | + |
| 124 | +////////////////////////////////////////////////////// |
| 125 | +// |
| 126 | +// PROTECTED |
| 127 | +// |
| 128 | +uint16_t I2CKeyPad8x8::_read(uint16_t mask) |
| 129 | +{ |
| 130 | + // improve the odds that IO will not interrupted. |
| 131 | + yield(); |
| 132 | + |
| 133 | + _wire->beginTransmission(_address); |
| 134 | + _wire->write(mask >> 8); |
| 135 | + _wire->write(mask & 0xFF); |
| 136 | + if (_wire->endTransmission() != 0) |
| 137 | + { |
| 138 | + // set communication error |
| 139 | + return 0xFFFF; |
| 140 | + } |
| 141 | + _wire->requestFrom(_address, (uint8_t)2); |
| 142 | + uint16_t value = _wire->read() << 8; |
| 143 | + value += _wire->read(); |
| 144 | + return value; |
| 145 | +} |
| 146 | + |
| 147 | + |
| 148 | +// -- END OF FILE -- |
| 149 | + |
0 commit comments