+ 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,22 +21,30 @@
#include <Wire.h>
PCF8574::PCF8574(int address)
PCF8574::PCF8574(uint8_t deviceAddress)
{
_address = address;
_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();
#else
_data = Wire.read();
#endif
_error = Wire.endTransmission();
return _data;
}
@ -45,18 +55,20 @@ uint8_t PCF8574::value()
void PCF8574::write8(uint8_t value)
{
Wire.beginTransmission(_address);
_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;
}
// pin should be 0..7
void PCF8574::write(uint8_t pin, uint8_t value)
{
PCF8574::read8();
@ -71,6 +83,7 @@ void PCF8574::write(uint8_t pin, uint8_t value)
PCF8574::write8(_data);
}
// pin should be 0..7
void PCF8574::toggle(uint8_t pin)
{
PCF8574::read8();
@ -78,6 +91,7 @@ void PCF8574::toggle(uint8_t pin)
PCF8574::write8(_data);
}
// n should be 0..7
void PCF8574::shiftRight(uint8_t n)
{
if (n == 0 || n > 7 ) return;
@ -86,6 +100,7 @@ void PCF8574::shiftRight(uint8_t n)
PCF8574::write8(_data);
}
// n should be 0..7
void PCF8574::shiftLeft(uint8_t n)
{
if (n == 0 || n > 7) return;

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,12 +19,12 @@
#include "WProgram.h"
#endif
#define PCF8574_LIB_VERSION "0.1.02"
#define PCF8574_LIB_VERSION "0.1.04"
class PCF8574
{
public:
PCF8574(int address);
PCF8574(uint8_t deviceAddress);
uint8_t read8();
uint8_t read(uint8_t pin);
@ -40,7 +40,7 @@ class PCF8574
int lastError();
private:
int _address;
uint8_t _address;
uint8_t _data;
int _error;
};