+ version 0.1.04 (0.1.03 = intermediate)

+ removed beginTransmission from read8() -> speed++
+ removed endTransmission from read8() -> speed
+ changed address to uint8_t
This commit is contained in:
rob tillaart 2015-05-09 17:07:09 +02:00
parent b527f06da5
commit 1c1de9bfeb
2 changed files with 81 additions and 66 deletions

View File

@ -2,11 +2,13 @@
// FILE: PCF8574.cpp
// AUTHOR: Rob Tillaart
// DATE: 02-febr-2013
// VERSION: 0.1.02
// VERSION: 0.1.04
// PURPOSE: I2C PCF8574 library for Arduino
// URL:
//
// HISTORY:
// 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;
// added default value for shiftLeft() and shiftRight()
// renamed status() to lastError();
@ -19,86 +21,99 @@
#include <Wire.h>
PCF8574::PCF8574(int address)
PCF8574::PCF8574(uint8_t deviceAddress)
{
_address = address;
Wire.begin();
_address = deviceAddress;
Wire.begin();
// TWBR = 12; // 400KHz
}
// removed Wire.beginTransmission(addr);
// with @100KHz -> 265 micros()
// without @100KHz -> 132 micros()
// without @400KHz -> 52 micros()
// TODO @800KHz -> ??
uint8_t PCF8574::read8()
{
Wire.beginTransmission(_address);
Wire.requestFrom(_address, 1);
if (Wire.requestFrom(_address, (uint8_t)1) != 1)
{
_error = 10;
return _data; // last value
}
#if (ARDUINO < 100)
_data = Wire.receive();
_data = Wire.receive();
#else
_data = Wire.read();
_data = Wire.read();
#endif
_error = Wire.endTransmission();
return _data;
return _data;
}
uint8_t PCF8574::value()
{
return _data;
return _data;
}
void PCF8574::write8(uint8_t value)
{
Wire.beginTransmission(_address);
_data = value;
Wire.write(_data);
_error = Wire.endTransmission();
_data = value;
Wire.beginTransmission(_address);
Wire.write(_data);
_error = Wire.endTransmission();
}
// pin should be 0..7
uint8_t PCF8574::read(uint8_t pin)
{
PCF8574::read8();
return (_data & (1<<pin)) > 0;
PCF8574::read8();
return (_data & (1<<pin)) > 0;
}
// pin should be 0..7
void PCF8574::write(uint8_t pin, uint8_t value)
{
PCF8574::read8();
if (value == LOW)
{
_data &= ~(1<<pin);
}
else
{
_data |= (1<<pin);
}
PCF8574::write8(_data);
PCF8574::read8();
if (value == LOW)
{
_data &= ~(1<<pin);
}
else
{
_data |= (1<<pin);
}
PCF8574::write8(_data);
}
// pin should be 0..7
void PCF8574::toggle(uint8_t pin)
{
PCF8574::read8();
_data ^= (1 << pin);
PCF8574::write8(_data);
PCF8574::read8();
_data ^= (1 << pin);
PCF8574::write8(_data);
}
// n should be 0..7
void PCF8574::shiftRight(uint8_t n)
{
if (n == 0 || n > 7 ) return;
PCF8574::read8();
_data >>= n;
PCF8574::write8(_data);
if (n == 0 || n > 7 ) return;
PCF8574::read8();
_data >>= n;
PCF8574::write8(_data);
}
// n should be 0..7
void PCF8574::shiftLeft(uint8_t n)
{
if (n == 0 || n > 7) return;
PCF8574::read8();
_data <<= n;
PCF8574::write8(_data);
if (n == 0 || n > 7) return;
PCF8574::read8();
_data <<= n;
PCF8574::write8(_data);
}
int PCF8574::lastError()
{
int e = _error;
_error = 0;
return e;
int e = _error;
_error = 0;
return e;
}
//
// END OF FILE

View File

@ -2,7 +2,7 @@
// FILE: PCF8574.H
// AUTHOR: Rob Tillaart
// DATE: 02-febr-2013
// VERSION: 0.1.02
// VERSION: 0.1.04
// PURPOSE: I2C PCF8574 library for Arduino
// URL:
//
@ -19,30 +19,30 @@
#include "WProgram.h"
#endif
#define PCF8574_LIB_VERSION "0.1.02"
#define PCF8574_LIB_VERSION "0.1.04"
class PCF8574
{
public:
PCF8574(int address);
public:
PCF8574(uint8_t deviceAddress);
uint8_t read8();
uint8_t read(uint8_t pin);
uint8_t value();
uint8_t read8();
uint8_t read(uint8_t pin);
uint8_t value();
void write8(uint8_t value);
void write(uint8_t pin, uint8_t value);
void write8(uint8_t value);
void write(uint8_t pin, uint8_t value);
void toggle(uint8_t pin);
void shiftRight(uint8_t n=1);
void shiftLeft(uint8_t n=1);
void toggle(uint8_t pin);
void shiftRight(uint8_t n=1);
void shiftLeft(uint8_t n=1);
int lastError();
int lastError();
private:
int _address;
uint8_t _data;
int _error;
private:
uint8_t _address;
uint8_t _data;
int _error;
};
#endif