0.4.0 PCF8574

This commit is contained in:
Rob Tillaart 2023-12-11 09:17:06 +01:00
parent d551a92053
commit f764634eeb
22 changed files with 323 additions and 94 deletions

View File

@ -6,12 +6,20 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.4.0] - 2023-09-23
- refactor API, begin()
- update readme.md
- update examples
- add examples ESP32 RP2040
- minor edits
----
## [0.3.9] - 2023-09-23
- Add Wire1 support for ESP32
- update readme.md
- minor edits
## [0.3.8] - 2023-02-04
- update readme.md
- update GitHub actions

View File

@ -2,7 +2,7 @@
// FILE: PCF8574.cpp
// AUTHOR: Rob Tillaart
// DATE: 02-febr-2013
// VERSION: 0.3.9
// VERSION: 0.4.0
// PURPOSE: Arduino library for PCF8574 - 8 channel I2C IO expander
// URL: https://github.com/RobTillaart/PCF8574
// http://forum.arduino.cc/index.php?topic=184800
@ -22,25 +22,8 @@ PCF8574::PCF8574(const uint8_t deviceAddress, TwoWire *wire)
}
#if defined (ESP8266) || defined(ESP32)
bool PCF8574::begin(int dataPin, int clockPin, uint8_t value)
{
if ((dataPin < 255) && (clockPin < 255))
{
_wire->begin(dataPin, clockPin);
} else {
_wire->begin();
}
if (! isConnected()) return false;
PCF8574::write8(value);
return true;
}
#endif
bool PCF8574::begin(uint8_t value)
{
_wire->begin();
if (! isConnected()) return false;
PCF8574::write8(value);
return true;

View File

@ -3,7 +3,7 @@
// FILE: PCF8574.h
// AUTHOR: Rob Tillaart
// DATE: 02-febr-2013
// VERSION: 0.3.9
// VERSION: 0.4.0
// PURPOSE: Arduino library for PCF8574 - 8 channel I2C IO expander
// URL: https://github.com/RobTillaart/PCF8574
// http://forum.arduino.cc/index.php?topic=184800
@ -13,7 +13,7 @@
#include "Wire.h"
#define PCF8574_LIB_VERSION (F("0.3.9"))
#define PCF8574_LIB_VERSION (F("0.4.0"))
#ifndef PCF8574_INITIAL_VALUE
#define PCF8574_INITIAL_VALUE 0xFF
@ -29,9 +29,6 @@ class PCF8574
public:
explicit PCF8574(const uint8_t deviceAddress = 0x20, TwoWire *wire = &Wire);
#if defined (ESP8266) || defined(ESP32)
bool begin(int sda, int scl, uint8_t value = PCF8574_INITIAL_VALUE);
#endif
bool begin(uint8_t value = PCF8574_INITIAL_VALUE);
bool isConnected();

View File

@ -49,6 +49,15 @@ There are two examples to show how interrupts can be used:
- PCF8574_rotaryEncoder.ino
#### 0.4.0 Breaking change
Version 0.4.0 introduced a breaking change.
You cannot set the pins in **begin()** any more.
This reduces the dependency of processor dependent Wire implementations.
The user has to call **Wire.begin()** and can optionally set the Wire pins
before calling **begin()**.
#### Related
16 bit port expanders
@ -87,24 +96,23 @@ However when performance is needed you can try to overclock the chip.
#include "PCF8574.h"
```
**PCF8574_INITIAL_VALUE** is a define that can be set compile time or before
**PCF8574_INITIAL_VALUE** is a define 0xFF that can be set compile time or before
the include of "pcf8574.h" to overrule the default value used with the **begin()** call.
### Constructor
#### Constructor
- **PCF8574(uint8_t deviceAddress = 0x20, TwoWire \*wire = &Wire)** Constructor with optional device address, default 0x20,
- **PCF8574(uint8_t deviceAddress = 0x20, TwoWire \*wire = &Wire)** Constructor with optional address, default 0x20,
and the optional Wire interface as parameter.
- **bool begin(uint8_t value = PCF8574_INITIAL_VALUE)** set the initial value for the pins and masks.
- **bool begin(int sda, int scl, uint8_t value = PCF8574_INITIAL_VALUE)** idem, for the ESP32 where one can choose the I2C pins.
- **bool begin(uint8_t value = PCF8574_INITIAL_VALUE)** set the initial value (default 0xFF) for the pins and masks.
- **bool isConnected()** checks if the address set in the constructor or by **setAddress()** is visible on the I2C bus.
- **bool setAddress(const uint8_t deviceAddress)** sets the device address after construction.
Can be used to switch between PCF8574 modules runtime. Note this corrupts internal buffered values,
so one might need to call **read8()** and/or **write8()**. Returns true if address can be found on I2C bus.
- **uint8_t getAddress()** returns the device address.
- **uint8_t getAddress()** Returns the device address.
### Read and Write
#### Read and Write
- **uint8_t read8()** reads all 8 pins at once. This one does the actual reading.
- **uint8_t read(uint8_t pin)** reads a single pin; pin = 0..7
@ -116,7 +124,7 @@ value is HIGH(1) or LOW (0)
- **uint8_t valueOut()** returns the last written data.
### Button
#### Button
The **"button"** functions are to be used when you mix input and output on one IC.
It does not change / affect the pins used for output by masking these.
@ -133,7 +141,7 @@ Note this can be a subset of the pins set with **setButtonMask()** if one wants
Background - https://github.com/RobTillaart/Arduino/issues/38
### Special
#### Special
- **void toggle(const uint8_t pin)** toggles a single pin
- **void toggleMask(const uint8_t mask = 0xFF)** toggles a selection of pins,
@ -147,7 +155,7 @@ Fills the lower lines with zero's.
- **void reverse()** reverse the "bit pattern" of the lines, swapping pin 7 with 0, 6 with 1, 5 with 2 etc.
### Select
#### Select
Some convenience wrappers.
@ -161,7 +169,7 @@ This can typical be used to implement a VU meter.
- **void selectAll()** sets all pins to HIGH.
### Miscellaneous
#### Miscellaneous
- **int lastError()** returns the last error from the lib. (see .h file).

View File

@ -2,12 +2,12 @@
// FILE: PCF8574_Wire2.ino
// AUTHOR: Rob Tillaart
// DATE: 2016-04-30
// PUPROSE: demo
// PURPOSE: demo
#include "PCF8574.h"
// adjust addresses if needed
// adjust addresses if needed
PCF8574 PCF(0x39, &Wire2);
@ -18,6 +18,8 @@ void setup()
Serial.print("PCF8574_LIB_VERSION:\t");
Serial.println(PCF8574_LIB_VERSION);
Wire2.begin();
if (!PCF.begin())
{
Serial.println("could not initialize...");
@ -75,5 +77,5 @@ void doToggle()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -2,7 +2,7 @@
// FILE: PCF8574_interrupt.ino
// AUTHOR: Rob Tillaart
// DATE: 2020-12-07
// PUPROSE: test PCF8574 library
// PURPOSE: test PCF8574 library
//
// TEST SETUP
// Connect INT pin of the PCF8574 to UNO pin 2
@ -19,7 +19,7 @@ PCF8574 PCF(0x38);
////////////////////////////////////
//
// INTERRUPT ROUTINE + FLAG
// INTERRUPT ROUTINE + FLAG
//
const int IRQPIN = 2;
@ -33,7 +33,7 @@ void pcf_irq()
////////////////////////////////////
//
// MAIN CODE
// MAIN CODE
//
void setup()
{
@ -41,7 +41,8 @@ void setup()
Serial.println(__FILE__);
Serial.print("PCF8574_LIB_VERSION: ");
Serial.println(PCF8574_LIB_VERSION);
Wire.begin();
PCF.begin();
pinMode(IRQPIN, INPUT_PULLUP);
@ -67,5 +68,5 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -2,12 +2,12 @@
// FILE: PCF8574_isConnected.ino
// AUTHOR: Rob Tillaart
// DATE: 2021-01-03
// PUPROSE: demo isConnected function
// PURPOSE: demo isConnected function
#include "PCF8574.h"
// adjust addresses if needed
// adjust addresses if needed
PCF8574 PCF_39(0x39);
@ -18,6 +18,8 @@ void setup()
Serial.print("PCF8574_LIB_VERSION:\t");
Serial.println(PCF8574_LIB_VERSION);
Wire.begin();
if (!PCF_39.begin())
{
Serial.println("could not initialize...");
@ -38,5 +40,5 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -2,7 +2,7 @@
// FILE: PCF8574_performance.ino
// AUTHOR: Rob Tillaart
// DATE: 2021-01-24
// PUPROSE: test PCF8574 library at different I2C speeds.
// PURPOSE: test PCF8574 library at different I2C speeds.
#include "PCF8574.h"
@ -19,6 +19,8 @@ void setup()
Serial.print("PCF8574_LIB_VERSION:\t");
Serial.println(PCF8574_LIB_VERSION);
Wire.begin();
PCF.begin();
Serial.println(PCF.isConnected());
@ -50,4 +52,4 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -2,7 +2,7 @@
// FILE: PCF8574_rotaryEncoder.ino
// AUTHOR: Rob Tillaart
// DATE: 2021-05-08
// PUPROSE: demo PCF8574 as rotary encoder reader.
// PURPOSE: demo PCF8574 as rotary encoder reader.
//
//
// RotaryEncoder PCF8574 UNO
@ -49,6 +49,7 @@ void setup()
flag = false;
Wire.begin();
if (decoder.begin() == false)
{
Serial.println("\nERROR: cannot communicate to PCF8574.");
@ -101,7 +102,7 @@ void updateRotaryDecoder()
{
uint8_t val = decoder.read8();
// check which of 4 has changed
// check which of 4 has changed
for (uint8_t i = 0; i < 4; i++)
{
uint8_t currentpos = (val & 0x03);
@ -130,5 +131,5 @@ void updateRotaryDecoder()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -2,8 +2,7 @@
// FILE: PCF8574_select.ino
// AUTHOR: Rob Tillaart
// DATE: 2022-06-18
// PUPROSE: demo PCF8574 library select functions
// PURPOSE: demo PCF8574 library select functions
#include "PCF8574.h"
@ -20,6 +19,8 @@ void setup()
Serial.print("PCF8574_LIB_VERSION:\t");
Serial.println(PCF8574_LIB_VERSION);
Wire.begin();
PCF.begin();
Serial.println(PCF.isConnected());
Serial.println();
@ -29,14 +30,14 @@ void setup()
PCF.selectNone();
delay(1000);
// VU meter up
// VU meter up
for (int i = 0; i < 7; i++)
{
PCF.selectN(i);
delay(100);
}
// VU meter down
// VU meter down
for (int i = 7; i >= 0; i--)
{
PCF.selectN(i);
@ -47,7 +48,7 @@ void setup()
void loop()
{
// night rider
// night rider
for (int i = 0; i < 7; i++)
{
PCF.select(i);
@ -61,4 +62,4 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -2,7 +2,7 @@
// FILE: PCF8574_test.ino
// AUTHOR: Rob Tillaart
// DATE: 7-febr-2013
// PUPROSE: test PCF8574 library
// PURPOSE: test PCF8574 library
#include "PCF8574.h"
@ -17,6 +17,8 @@ void setup()
Serial.print("PCF8574_LIB_VERSION:\t");
Serial.println(PCF8574_LIB_VERSION);
Wire.begin();
PCF_01.begin();
int x = PCF_01.read8();
@ -66,5 +68,5 @@ void doToggle()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -2,14 +2,14 @@
// FILE: PCF8574_test.ino
// AUTHOR: Rob Tillaart
// DATE: 27-08-2013
// PUPROSE: demo
// PURPOSE: demo
#include "PCF8574.h"
// adjust addresses if needed
PCF8574 PCF_38(0x38); // add switches to lines (used as input)
PCF8574 PCF_39(0x39); // add LEDs to lines (used as output)
// adjust addresses if needed
PCF8574 PCF_38(0x38); // add switches to lines (used as input)
PCF8574 PCF_39(0x39); // add LEDs to lines (used as output)
void setup()
@ -19,6 +19,8 @@ void setup()
Serial.print("PCF8574_LIB_VERSION:\t");
Serial.println(PCF8574_LIB_VERSION);
Wire.begin();
PCF_38.begin();
PCF_39.begin();
@ -65,12 +67,12 @@ void setup()
void loop()
{
// echo the state of the lines on the other PCF
// echo the state of the lines on the other PCF
uint8_t value = PCF_38.read8();
PCF_39.write8(value);
delay(100);
}
// END OF FILE
// -- END OF FILE --

View File

@ -2,13 +2,13 @@
// FILE: pcf8574_test2.ino
// AUTHOR: Rob Tillaart
// DATE: 2016-04-30
// PUPROSE: demo rotateLeft, -Right and toggleMask
// PURPOSE: demo rotateLeft, rotateRight and toggleMask
#include "PCF8574.h"
// adjust addresses if needed
PCF8574 PCF_39(0x39); // add LEDs to lines (used as output)
// adjust addresses if needed
PCF8574 PCF_39(0x39); // add LEDs to lines (used as output)
void setup()
@ -18,6 +18,8 @@ void setup()
Serial.print("PCF8574_LIB_VERSION:\t");
Serial.println(PCF8574_LIB_VERSION);
Wire.begin();
PCF_39.begin();
PCF_39.write(0, 1);
@ -51,8 +53,8 @@ void setup()
delay(100);
}
// 0010 0111 -> 0x27
// 1110 0100
// 0010 0111 -> 0x27
// 1110 0100
PCF_39.write8(0x27);
for (int i = 0; i < 255; i++)
{
@ -67,5 +69,5 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -0,0 +1,27 @@
platforms:
rpipico:
board: rp2040:rp2040:rpipico
package: rp2040:rp2040
gcc:
features:
defines:
- ARDUINO_ARCH_RP2040
warnings:
flags:
packages:
rp2040:rp2040:
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
# - uno
# - due
# - zero
# - leonardo
# - m4
- esp32
- esp8266
# - mega2560
# - rpipico

View File

@ -0,0 +1,73 @@
//
// FILE: PCF8574_test_ESP32.ino
// AUTHOR: Rob Tillaart
// DATE: 7-febr-2013
// PURPOSE: test PCF8574 library
#include "PCF8574.h"
PCF8574 PCF_01(0x38);
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("PCF8574_LIB_VERSION:\t");
Serial.println(PCF8574_LIB_VERSION);
// adjust pins if needed
Wire.begin(20, 21);
PCF_01.begin();
int x = PCF_01.read8();
Serial.print("Read ");
Serial.println(x, HEX);
delay(1000);
}
void loop()
{
Serial.println("HLT");
while (Serial.available() == 0);
switch (Serial.read())
{
case 'H': doHigh(); break;
case 'L': doLow(); break;
case 'T': doToggle(); break;
}
}
void doHigh()
{
PCF_01.write(4, HIGH);
int x = PCF_01.read8();
Serial.print("Read ");
Serial.println(x, HEX);
}
void doLow()
{
PCF_01.write(4, LOW);
int x = PCF_01.read8();
Serial.print("Read ");
Serial.println(x, HEX);
}
void doToggle()
{
PCF_01.toggle(4);
int x = PCF_01.read8();
Serial.print("Read ");
Serial.println(x, HEX);
}
// -- END OF FILE --

View File

@ -0,0 +1,27 @@
platforms:
rpipico:
board: rp2040:rp2040:rpipico
package: rp2040:rp2040
gcc:
features:
defines:
- ARDUINO_ARCH_RP2040
warnings:
flags:
packages:
rp2040:rp2040:
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
# - uno
# - due
# - zero
# - leonardo
# - m4
# - esp32
# - esp8266
# - mega2560
- rpipico

View File

@ -0,0 +1,75 @@
//
// FILE: PCF8574_test_RP2040.ino
// AUTHOR: Rob Tillaart
// DATE: 2023-12-10
// PURPOSE: test PCF8574 library
#include "PCF8574.h"
PCF8574 PCF_01(0x38);
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("PCF8574_LIB_VERSION:\t");
Serial.println(PCF8574_LIB_VERSION);
// adjust pins if needed
Wire.setSDA(22);
Wire.setSCL(23);
Wire.begin();
PCF_01.begin();
int x = PCF_01.read8();
Serial.print("Read ");
Serial.println(x, HEX);
delay(1000);
}
void loop()
{
Serial.println("HLT");
while (Serial.available() == 0);
switch (Serial.read())
{
case 'H': doHigh(); break;
case 'L': doLow(); break;
case 'T': doToggle(); break;
}
}
void doHigh()
{
PCF_01.write(4, HIGH);
int x = PCF_01.read8();
Serial.print("Read ");
Serial.println(x, HEX);
}
void doLow()
{
PCF_01.write(4, LOW);
int x = PCF_01.read8();
Serial.print("Read ");
Serial.println(x, HEX);
}
void doToggle()
{
PCF_01.toggle(4);
int x = PCF_01.read8();
Serial.print("Read ");
Serial.println(x, HEX);
}
// -- END OF FILE --

View File

@ -38,8 +38,14 @@ unsigned int blinkMillis;
unsigned int buttonMillis;
void setup() {
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("PCF8574_LIB_VERSION: ");
Serial.println(PCF8574_LIB_VERSION);
Wire.begin();
pcf20.begin();
pinMode(onboardLed, OUTPUT);
@ -50,25 +56,25 @@ void loop() {
static bool state;
unsigned int currentMillis = millis();
//Limit button read to 20 times a second
//Fast enough for most buttons
//but this way you don't have a dimmer output because it's blanked during button read
//a read takes 460us t 16Mhz Arduino and normal I2C speed.
if(currentMillis - buttonMillis >= 50){
// Limit button read to 20 times a second
// Fast enough for most buttons
// but this way you don't have a dimmer output because it's blanked during button read
// a read takes 460us t 16Mhz Arduino and normal I2C speed.
if ((currentMillis - buttonMillis) >= 50) {
buttonMillis = currentMillis;
if(state != pcf20.readButton(PcfButtonLedPin)){
if(state){
//toggle the LED
if (state != pcf20.readButton(PcfButtonLedPin)) {
if( state) {
// toggle the LED
digitalWrite(onboardLed, !digitalRead(onboardLed));
}
state = !state;
}
}
//Lets blink the same output
if(currentMillis - blinkMillis >= 500){
//Update time
// Lets blink the same output
if ((currentMillis - blinkMillis) >= 500) {
// Update time
blinkMillis = currentMillis;
pcf20.toggle(PcfButtonLedPin);
@ -77,5 +83,5 @@ void loop() {
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -39,8 +39,14 @@ unsigned int blinkMillis;
unsigned int buttonMillis;
void setup() {
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("PCF8574_LIB_VERSION: ");
Serial.println(PCF8574_LIB_VERSION);
Wire.begin();
pcf20.begin();
pinMode(onboardLed, OUTPUT);
@ -61,7 +67,7 @@ void loop() {
// Fast enough for most buttons
// but this way you don't have a dimmer output because it's blanked during button read
// a read takes 460us t 16Mhz Arduino and normal I2C speed.
if(currentMillis - buttonMillis >= 50){
if ((currentMillis - buttonMillis) >= 50) {
buttonMillis = currentMillis;
// read all states but only force PcfButtonLedPin HIGH during the
@ -69,11 +75,11 @@ void loop() {
// Alternatively the mask could have been set with setButtonMask().
// Then the mask can be omitted here. See setup()
// byte inputStates = pcf20.readButton8(_BV(PcfButtonLedPin));
byte inputStates = pcf20.readButton8(1 << PcfButtonLedPin); // Keep Arduino-CI happy
byte inputStates = pcf20.readButton8(1 << PcfButtonLedPin); // Keep Arduino-CI happy
// check the bit of PcfButtonLedPin
if(state != bitRead(inputStates, PcfButtonLedPin)){
if(state){
if (state != bitRead(inputStates, PcfButtonLedPin)) {
if (state) {
// toggle the LED
digitalWrite(onboardLed, !digitalRead(onboardLed));
}
@ -82,7 +88,7 @@ void loop() {
}
// Lets blink the same output
if(currentMillis - blinkMillis >= 500){
if ((currentMillis - blinkMillis) >= 500) {
// Update time
blinkMillis = currentMillis;
@ -93,5 +99,5 @@ void loop() {
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/PCF8574.git"
},
"version": "0.3.9",
"version": "0.4.0",
"license": "MIT",
"frameworks": "*",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=PCF8574
version=0.3.9
version=0.4.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for PCF8574 - 8 channel I2C IO expander

View File

@ -55,6 +55,7 @@ unittest(test_begin)
{
PCF8574 PCF(0x38);
Wire.begin();
PCF.begin();
int readValue = PCF.read8();
@ -70,6 +71,7 @@ unittest(test_read)
PCF8574 PCF(0x38);
int readValue;
Wire.begin();
PCF.begin();
for (int i = 0; i < 8; i++)
{
@ -92,7 +94,9 @@ unittest(test_address)
{
PCF8574 PCF(0x38);
// incorrect in test environment.
Wire.begin();
// incorrect in test environment.
assertTrue(PCF.begin());
assertTrue(PCF.isConnected());
assertEqual(0x38, PCF.getAddress());