|
| 1 | +// |
| 2 | +// FILE: PCA9549.cpp |
| 3 | +// AUTHOR: Rob Tillaart |
| 4 | +// VERSION: 0.1.0 |
| 5 | +// DATE: 2023-12-12 |
| 6 | +// PURPOSE: Arduino Library for PCA9549 I2C octal bus switch. |
| 7 | +// URL: https://github.com/RobTillaart/PCA9549 |
| 8 | + |
| 9 | + |
| 10 | +#include "PCA9549.h" |
| 11 | + |
| 12 | + |
| 13 | +PCA9549::PCA9549(uint8_t deviceAddress, TwoWire *wire) |
| 14 | +{ |
| 15 | + _address = deviceAddress; |
| 16 | + _wire = wire; |
| 17 | + _mask = 0x00; |
| 18 | + _resetPin = -1; |
| 19 | + _forced = false; |
| 20 | + _error = PCA9549_OK; |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +bool PCA9549::begin(uint8_t mask) |
| 25 | +{ |
| 26 | + if (! isConnected()) return false; |
| 27 | + setChannelMask(mask); |
| 28 | + return true; |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +bool PCA9549::isConnected() |
| 33 | +{ |
| 34 | + _wire->beginTransmission(_address); |
| 35 | + return ( _wire->endTransmission() == 0); |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +bool PCA9549::enableChannel(uint8_t channel) |
| 40 | +{ |
| 41 | + if (channel >= 8) return false; |
| 42 | + if (!isEnabled(channel)) |
| 43 | + { |
| 44 | + setChannelMask(_mask | (0x01 << channel)); |
| 45 | + } |
| 46 | + return true; |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +bool PCA9549::disableChannel(uint8_t channel) |
| 51 | +{ |
| 52 | + if (channel >= 8) return false; |
| 53 | + if (!isEnabled(channel)) |
| 54 | + { |
| 55 | + setChannelMask(_mask & ~(0x01 << channel)); |
| 56 | + } |
| 57 | + return true; |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +bool PCA9549::selectChannel(uint8_t channel) |
| 62 | +{ |
| 63 | + if (channel >= 8) return false; |
| 64 | + setChannelMask(0x01 << channel); |
| 65 | + return true; |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +bool PCA9549::isEnabled(uint8_t channel) |
| 70 | +{ |
| 71 | + if (channel >= 8) return false; |
| 72 | + return (_mask & (0x01 << channel)); |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +bool PCA9549::disableAllChannels() |
| 77 | +{ |
| 78 | + return setChannelMask(0x00); |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +bool PCA9549::setChannelMask(uint8_t mask) |
| 83 | +{ |
| 84 | + if ((_mask == mask) && (not _forced)) return true; |
| 85 | + _mask = mask; |
| 86 | + _wire->beginTransmission(_address); |
| 87 | + _wire->write(_mask); |
| 88 | + _error = _wire->endTransmission(); |
| 89 | + return (_error == 0); |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | +uint8_t PCA9549::getChannelMask() |
| 94 | +{ |
| 95 | + if (_forced) // read from device. |
| 96 | + { |
| 97 | + _wire->requestFrom(_address, (uint8_t)1); |
| 98 | + _mask = _wire->read(); |
| 99 | + } |
| 100 | + return _mask; |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +void PCA9549::setResetPin(uint8_t resetPin) |
| 105 | +{ |
| 106 | + _resetPin = resetPin; |
| 107 | + pinMode(_resetPin, OUTPUT); |
| 108 | + digitalWrite(_resetPin, HIGH); |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | +void PCA9549::reset() |
| 113 | +{ |
| 114 | + digitalWrite(_resetPin, LOW); |
| 115 | + delayMicroseconds(1); // TODO datasheet page ? - 500 ns |
| 116 | + digitalWrite(_resetPin, HIGH); |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +void PCA9549::setForced(bool forced) |
| 121 | +{ |
| 122 | + _forced = forced; |
| 123 | +}; |
| 124 | + |
| 125 | + |
| 126 | +bool PCA9549::getForced() |
| 127 | +{ |
| 128 | + return _forced; |
| 129 | +}; |
| 130 | + |
| 131 | + |
| 132 | +int PCA9549::getError() |
| 133 | +{ |
| 134 | + int error = _error; |
| 135 | + _error = PCA9549_OK; |
| 136 | + return error; |
| 137 | +} |
| 138 | + |
| 139 | + |
| 140 | +// -- END OF FILE -- |
| 141 | + |
0 commit comments