diff --git a/libraries/PCF8574/PCF8574.cpp b/libraries/PCF8574/PCF8574.cpp index debd8236..db62f9da 100644 --- a/libraries/PCF8574/PCF8574.cpp +++ b/libraries/PCF8574/PCF8574.cpp @@ -2,19 +2,21 @@ // FILE: PCF8574.cpp // AUTHOR: Rob Tillaart // DATE: 02-febr-2013 -// VERSION: 0.1.04 +// VERSION: 0.1.07 // PURPOSE: I2C PCF8574 library for Arduino -// URL: +// URL: http://forum.arduino.cc/index.php?topic=184800 // // HISTORY: -// 0.1.07 2016-04-28 Septillion -// added dataOut so a write() doesn't read first, +// 0.1.07 2016-05-02 (manually merged) Septillion +// added dataOut so a write() doesn't read first, // possibly corrupting a input pin; // fixed shift comment, should read 1..7; // added begin() to be sure it's in a known state, // states could be different if uC is reset and the PCF8574 isn't; -// added buttonRead() and buttonRead8() -// which only effect the ouput while reading +// added buttonRead() and buttonRead8() +// which only effect the output while reading +// 0.1.06 (intermediate) added defined errors + refactor rotate +// 0.1.05 2016-04-30 refactor, +toggleMask, +rotLeft, +rotRight // 0.1.04 2015-05-09 removed ambiguity in read8() // 0.1.03 2015-03-02 address int -> uint8_t // 0.1.02 replaced ints with uint8_t to reduce footprint; @@ -29,11 +31,16 @@ #include -PCF8574::PCF8574(uint8_t deviceAddress) +PCF8574::PCF8574(const uint8_t deviceAddress) { _address = deviceAddress; Wire.begin(); // TWBR = 12; // 400KHz + + _dataIn = 0; + _dataOut = 0; + + _error = PCF8574_OK; } //added 0.1.07 @@ -51,29 +58,18 @@ uint8_t PCF8574::read8() { if (Wire.requestFrom(_address, (uint8_t)1) != 1) { - _error = 10; - return _data; // last value + _error = PCF8574_I2C_ERROR; + return _dataIn; // last value } #if (ARDUINO < 100) - _data = Wire.receive(); + _dataIn = Wire.receive(); #else - _data = Wire.read(); + _dataIn = Wire.read(); #endif - return _data; + return _dataIn; } -uint8_t PCF8574::value() -{ - return _data; -} - -//added 0.1.07 -uint8_t PCF8574::valueOut () -{ - return _dataOut; -} - -void PCF8574::write8(uint8_t value) +void PCF8574::write8(const uint8_t value) { _dataOut = value; Wire.beginTransmission(_address); @@ -81,75 +77,61 @@ void PCF8574::write8(uint8_t value) _error = Wire.endTransmission(); } -// pin should be 0..7 -uint8_t PCF8574::read(uint8_t pin) +uint8_t PCF8574::read(const uint8_t pin) { + if (pin > 7) + { + _error = PCF8574_PIN_ERROR; + return 0; + } PCF8574::read8(); - return (_data & (1< 0; + return (_dataIn & (1 << pin)) > 0; } -// pin should be 0..7 -void PCF8574::write(uint8_t pin, uint8_t value) +void PCF8574::write(const uint8_t pin, const uint8_t value) { - //Don't read because we could bring pulled low inputs into output state - //PCF8574::read8(); + if (pin > 7) + { + _error = PCF8574_PIN_ERROR; + return; + } if (value == LOW) { - _dataOut &= ~(1< 7) + { + _error = PCF8574_PIN_ERROR; + return; + } + toggleMask(1 << pin); +} + +void PCF8574::toggleMask(const uint8_t mask) +{ + _dataOut ^= mask; PCF8574::write8(_dataOut); } -//added 0.1.07 -uint8_t PCF8574::readButton8() +void PCF8574::shiftRight(const uint8_t n) { - uint8_t temp = _dataOut; - PCF8574::begin(); - uint8_t rtn = PCF8574::read8(); - PCF8574::write8(temp); - - return rtn; -} - -//added 0.1.07 -uint8_t PCF8574::readButton(uint8_t pin) -{ - uint8_t temp = _dataOut; - PCF8574::write(pin, HIGH); - uint8_t rtn = PCF8574::read(pin); - PCF8574::write8(temp); - - return rtn; -} - -// pin should be 0..7 -void PCF8574::toggle(uint8_t pin) -{ - //Same as write - //Don't read because that may upset pulled low inputs - //PCF8574::read8(); - _dataOut ^= (1 << pin); - PCF8574::write8(_dataOut); -} - -// n should be 1..7 -void PCF8574::shiftRight(uint8_t n) -{ - if (n == 0 || n > 7 ) return; - //PCF8574::read8(); + if (n == 0 || n > 7) return; _dataOut >>= n; PCF8574::write8(_dataOut); } -// n should be 1..7 -void PCF8574::shiftLeft(uint8_t n) +void PCF8574::shiftLeft(const uint8_t n) { if (n == 0 || n > 7) return; - //PCF8574::read8(); _dataOut <<= n; PCF8574::write8(_dataOut); } @@ -157,9 +139,47 @@ void PCF8574::shiftLeft(uint8_t n) int PCF8574::lastError() { int e = _error; - _error = 0; + _error = PCF8574_OK; return e; } + +void PCF8574::rotateRight(const uint8_t n) +{ + uint8_t r = n & 7; + _dataOut = (_dataOut >> r) | (_dataOut << (8-r)); + PCF8574::write8(_dataOut); +} + +void PCF8574::rotateLeft(const uint8_t n) +{ + rotateRight(8- (n & 7)); +} + +//added 0.1.07 Septillion +uint8_t PCF8574::readButton8() +{ + uint8_t temp = _dataOut; + PCF8574::begin(); + uint8_t rtn = PCF8574::read8(); + PCF8574::write8(temp); + return rtn; +} + +//added 0.1.07 Septillion +uint8_t PCF8574::readButton(const uint8_t pin) +{ + if (pin > 7) + { + _error = PCF8574_PIN_ERROR; + return 0; + } + uint8_t temp = _dataOut; + PCF8574::write(pin, HIGH); + uint8_t rtn = PCF8574::read(pin); + PCF8574::write8(temp); + return rtn; +} + // // END OF FILE // \ No newline at end of file diff --git a/libraries/PCF8574/PCF8574.h b/libraries/PCF8574/PCF8574.h index d4fe7923..d5064cc9 100644 --- a/libraries/PCF8574/PCF8574.h +++ b/libraries/PCF8574/PCF8574.h @@ -2,9 +2,9 @@ // FILE: PCF8574.H // AUTHOR: Rob Tillaart // DATE: 02-febr-2013 -// VERSION: 0.1.04 +// VERSION: 0.1.07 // PURPOSE: I2C PCF8574 library for Arduino -// URL: +// URL: http://forum.arduino.cc/index.php?topic=184800 // // HISTORY: // see PCF8574.cpp file @@ -19,36 +19,46 @@ #include "WProgram.h" #endif -#define PCF8574_LIB_VERSION "0.1.04" +#define PCF8574_LIB_VERSION "0.1.07" + +#define PCF8574_OK 0x00 +#define PCF8574_PIN_ERROR 0x81 +#define PCF8574_I2C_ERROR 0x82 + class PCF8574 { public: - PCF8574(uint8_t deviceAddress); - + explicit PCF8574(const uint8_t deviceAddress); + void begin(); uint8_t read8(); uint8_t read(uint8_t pin); - uint8_t value(); - uint8_t valueOut(); + uint8_t value() const { return _dataIn; }; - void write8(uint8_t value); - void write(uint8_t pin, uint8_t value); - + void write8(const uint8_t value); + void write(const uint8_t pin, const uint8_t value); + uint8_t valueOut() const { return _dataOut; } + + //added 0.1.07 Septillion uint8_t readButton8(); - uint8_t readButton(uint8_t pin); + uint8_t readButton(const uint8_t pin); - void toggle(uint8_t pin); - void shiftRight(uint8_t n=1); - void shiftLeft(uint8_t n=1); + // rotate, shift, toggle expect all lines are output + void toggle(const uint8_t pin); + void toggleMask(const uint8_t mask); // invertAll() = toggleMask(0xFF) + void shiftRight(const uint8_t n=1); + void shiftLeft(const uint8_t n=1); + void rotateRight(const uint8_t n=1); + void rotateLeft(const uint8_t n=1); int lastError(); private: uint8_t _address; - uint8_t _data; - uint8_t _dataOut = 0xFF; + uint8_t _dataIn; + uint8_t _dataOut; int _error; }; diff --git a/libraries/PCF8574/examples/PCF8574_test2/PCF8574_test2.ino b/libraries/PCF8574/examples/PCF8574_test2/PCF8574_test2.ino new file mode 100644 index 00000000..169d4510 --- /dev/null +++ b/libraries/PCF8574/examples/PCF8574_test2/PCF8574_test2.ino @@ -0,0 +1,58 @@ +// +// FILE: pcf8574_test2.ino +// AUTHOR: Rob Tillaart +// DATE: 2016-04-30 +// +// PUPROSE: demo rotateLeft, -Right and toggleMask +// + +#include "PCF8574.h" +#include + +// adjust addresses if needed +PCF8574 PCF_39(0x39); // add leds to lines (used as output) + +void setup() +{ + Serial.begin(115200); + Serial.print("\npcf8574_test2.ino\nlib version: "); + Serial.println(PCF8574_LIB_VERSION); + + PCF_39.write(0, 1); + for (int i=0; i<7; i++) + { + PCF_39.rotateLeft(); + delay(100); + } + + for (int i=0; i<7; i++) + { + PCF_39.rotateRight(); + delay(100); + } + + for (int i=0; i<7; i++) + { + PCF_39.rotateLeft(3); + delay(100); + } + + for (int i=0; i<7; i++) + { + PCF_39.rotateRight(2); + delay(100); + } + + for (int i=0; i<255; i++) + { + PCF_39.toggleMask(i); + delay(100); + } +} + +void loop() +{ +} +// +// END OF FILE +// \ No newline at end of file