|
| 1 | +// |
| 2 | +// FILE: PCA9671.cpp |
| 3 | +// AUTHOR: Rob Tillaart |
| 4 | +// DATE: 2025-03-16 |
| 5 | +// VERSION: 0.1.0 |
| 6 | +// PURPOSE: Arduino library for the PCA9671, I2C 16-bit I/O expander |
| 7 | +// URL: https://github.com/RobTillaart/PCA9671 |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +#include "PCA9671.h" |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +PCA9671::PCA9671(const uint8_t deviceAddress, TwoWire *wire) |
| 16 | +{ |
| 17 | + _address = deviceAddress; |
| 18 | + _wire = wire; |
| 19 | + _dataIn = 0; |
| 20 | + _dataOut = 0xFFFF; |
| 21 | + _buttonMask = 0xFFFF; |
| 22 | + _error = PCA9671_OK; |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +bool PCA9671::begin(uint16_t value) |
| 27 | +{ |
| 28 | + if (! isConnected()) return false; |
| 29 | + PCA9671::write16(value); |
| 30 | + return true; |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +bool PCA9671::isConnected() |
| 35 | +{ |
| 36 | + _wire->beginTransmission(_address); |
| 37 | + return ( _wire->endTransmission() == 0); |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +bool PCA9671::setAddress(const uint8_t deviceAddress) |
| 42 | +{ |
| 43 | + if ((deviceAddress < 0x20) || (deviceAddress > 0x27)) return false; |
| 44 | + _address = deviceAddress; |
| 45 | + return isConnected(); |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +uint8_t PCA9671::getAddress() |
| 50 | +{ |
| 51 | + return _address; |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +uint16_t PCA9671::read16() |
| 56 | +{ |
| 57 | + if (_wire->requestFrom(_address, (uint8_t)2) != 2) |
| 58 | + { |
| 59 | + _error = PCA9671_I2C_ERROR; |
| 60 | + return _dataIn; // last value |
| 61 | + } |
| 62 | + _dataIn = _wire->read(); // low 8 bits |
| 63 | + _dataIn |= (_wire->read() << 8); // high 8 bits |
| 64 | + return _dataIn; |
| 65 | +} |
| 66 | + |
| 67 | + |
| 68 | +uint8_t PCA9671::read(const uint8_t pin) |
| 69 | +{ |
| 70 | + if (pin > 15) |
| 71 | + { |
| 72 | + _error = PCA9671_PIN_ERROR; |
| 73 | + return 0; |
| 74 | + } |
| 75 | + PCA9671::read16(); |
| 76 | + return (_dataIn & (1 << pin)) > 0; |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +uint16_t PCA9671::value() |
| 81 | +{ |
| 82 | + return _dataIn; |
| 83 | +}; |
| 84 | + |
| 85 | + |
| 86 | +void PCA9671::write16(const uint16_t value) |
| 87 | +{ |
| 88 | + _dataOut = value; |
| 89 | + _wire->beginTransmission(_address); |
| 90 | + _wire->write(_dataOut & 0xFF); // low 8 bits |
| 91 | + _wire->write(_dataOut >> 8); // high 8 bits |
| 92 | + _error = _wire->endTransmission(); |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +void PCA9671::write(const uint8_t pin, const uint8_t value) |
| 97 | +{ |
| 98 | + if (pin > 15) |
| 99 | + { |
| 100 | + _error = PCA9671_PIN_ERROR; |
| 101 | + return; |
| 102 | + } |
| 103 | + if (value == LOW) |
| 104 | + { |
| 105 | + _dataOut &= ~(1 << pin); |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + _dataOut |= (1 << pin); |
| 110 | + } |
| 111 | + PCA9671::write16(_dataOut); |
| 112 | +} |
| 113 | + |
| 114 | + |
| 115 | +uint16_t PCA9671::valueOut() |
| 116 | +{ |
| 117 | + return _dataOut; |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +void PCA9671::toggle(const uint8_t pin) |
| 122 | +{ |
| 123 | + if (pin > 15) |
| 124 | + { |
| 125 | + _error = PCA9671_PIN_ERROR; |
| 126 | + return; |
| 127 | + } |
| 128 | + toggleMask(1 << pin); |
| 129 | +} |
| 130 | + |
| 131 | + |
| 132 | +void PCA9671::toggleMask(const uint16_t mask) |
| 133 | +{ |
| 134 | + _dataOut ^= mask; |
| 135 | + PCA9671::write16(_dataOut); |
| 136 | +} |
| 137 | + |
| 138 | + |
| 139 | +void PCA9671::shiftRight(const uint8_t n) |
| 140 | +{ |
| 141 | + if ((n == 0) || (_dataOut == 0)) return; |
| 142 | + if (n > 15) _dataOut = 0; // shift 15++ clears all, valid... |
| 143 | + if (_dataOut != 0) _dataOut >>= n; // only shift if there are bits set |
| 144 | + PCA9671::write16(_dataOut); |
| 145 | +} |
| 146 | + |
| 147 | + |
| 148 | +void PCA9671::shiftLeft(const uint8_t n) |
| 149 | +{ |
| 150 | + if ((n == 0) || (_dataOut == 0)) return; |
| 151 | + if (n > 15) _dataOut = 0; // shift 15++ clears all, valid... |
| 152 | + if (_dataOut != 0) _dataOut <<= n; // only shift if there are bits set |
| 153 | + PCA9671::write16(_dataOut); |
| 154 | +} |
| 155 | + |
| 156 | + |
| 157 | +void PCA9671::rotateRight(const uint8_t n) |
| 158 | +{ |
| 159 | + uint8_t r = n & 15; |
| 160 | + if (r == 0) return; |
| 161 | + _dataOut = (_dataOut >> r) | (_dataOut << (15 - r)); |
| 162 | + PCA9671::write16(_dataOut); |
| 163 | +} |
| 164 | + |
| 165 | + |
| 166 | +void PCA9671::rotateLeft(const uint8_t n) |
| 167 | +{ |
| 168 | + rotateRight(16 - (n & 15)); |
| 169 | +} |
| 170 | + |
| 171 | + |
| 172 | +void PCA9671::reverse() // quite fast |
| 173 | +{ // 1 char === 1 bit |
| 174 | + uint16_t x = _dataOut; // x = 0123456789ABCDEF |
| 175 | + x = (((x & 0xAAAA) >> 1) | ((x & 0x5555) << 1)); // x = 1032547698BADCFE |
| 176 | + x = (((x & 0xCCCC) >> 2) | ((x & 0x3333) << 2)); // x = 32107654BA98FEDC |
| 177 | + x = (((x & 0xF0F0) >> 4) | ((x & 0x0F0F) << 4)); // x = 76543210FEDCBA98 |
| 178 | + x = (x >> 8) | ( x << 8); // x = FEDCBA9876543210 |
| 179 | + PCA9671::write16(x); |
| 180 | +} |
| 181 | + |
| 182 | + |
| 183 | +////////////////////////////////////////////////// |
| 184 | +// |
| 185 | +// READBUTTON |
| 186 | +// |
| 187 | +uint16_t PCA9671::readButton16(const uint16_t mask) |
| 188 | +{ |
| 189 | + uint16_t temp = _dataOut; |
| 190 | + PCA9671::write16(mask | _dataOut); // read only selected lines |
| 191 | + PCA9671::read16(); |
| 192 | + PCA9671::write16(temp); // restore |
| 193 | + return _dataIn; |
| 194 | +} |
| 195 | + |
| 196 | + |
| 197 | +uint16_t PCA9671::readButton16() |
| 198 | +{ |
| 199 | + return readButton16(_buttonMask); |
| 200 | +} |
| 201 | + |
| 202 | + |
| 203 | +uint8_t PCA9671::readButton(const uint8_t pin) |
| 204 | +{ |
| 205 | + if (pin > 15) |
| 206 | + { |
| 207 | + _error = PCA9671_PIN_ERROR; |
| 208 | + return 0; |
| 209 | + } |
| 210 | + uint16_t temp = _dataOut; |
| 211 | + PCA9671::write(pin, HIGH); |
| 212 | + uint8_t rtn = PCA9671::read(pin); |
| 213 | + PCA9671::write16(temp); |
| 214 | + return rtn; |
| 215 | +} |
| 216 | + |
| 217 | + |
| 218 | +void PCA9671::setButtonMask(uint16_t mask) |
| 219 | +{ |
| 220 | + _buttonMask = mask; |
| 221 | +}; |
| 222 | + |
| 223 | + |
| 224 | +uint16_t PCA9671::getButtonMask() |
| 225 | +{ |
| 226 | + return _buttonMask; |
| 227 | +}; |
| 228 | + |
| 229 | + |
| 230 | +////////////////////////////////////////////////// |
| 231 | +// |
| 232 | +// SELECT |
| 233 | +// |
| 234 | +void PCA9671::select(const uint8_t pin) |
| 235 | +{ |
| 236 | + uint16_t n = 0x0000; |
| 237 | + if (pin < 16) n = 1L << pin; |
| 238 | + PCA9671::write16(n); |
| 239 | +}; |
| 240 | + |
| 241 | + |
| 242 | +void PCA9671::selectN(const uint8_t pin) |
| 243 | +{ |
| 244 | + uint16_t n = 0xFFFF; |
| 245 | + if (pin < 16) n = (2L << pin) - 1; |
| 246 | + PCA9671::write16(n); |
| 247 | +}; |
| 248 | + |
| 249 | + |
| 250 | +void PCA9671::selectNone() |
| 251 | +{ |
| 252 | + PCA9671::write16(0x0000); |
| 253 | +}; |
| 254 | + |
| 255 | + |
| 256 | +void PCA9671::selectAll() |
| 257 | +{ |
| 258 | + PCA9671::write16(0xFFFF); |
| 259 | +}; |
| 260 | + |
| 261 | + |
| 262 | +int PCA9671::lastError() |
| 263 | +{ |
| 264 | + int e = _error; |
| 265 | + _error = PCA9671_OK; // reset error after read, is this wise? |
| 266 | + return e; |
| 267 | +} |
| 268 | + |
| 269 | + |
| 270 | +// -- END OF FILE -- |
| 271 | + |
0 commit comments