Skip to content

Commit

Permalink
refactor API, begin() (#33)
Browse files Browse the repository at this point in the history
- refactor API, begin()
- update examples
- update readme.md
  • Loading branch information
RobTillaart authored Dec 11, 2023
1 parent 1e997e3 commit 6c3d22b
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 65 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.1.10] - 2023-09-24
- fix Wire1 support for ESP32 (again)
- refactor API, begin()
- update examples
- update readme.md

----

## [0.1.10] - 2023-09-24
- fix Wire1 support for ESP32 (again)

## [0.1.9] - 2023-09-23
- add Wire1 support for ESP32
Expand Down
19 changes: 1 addition & 18 deletions PCF8575.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// FILE: PCF8575.cpp
// AUTHOR: Rob Tillaart
// DATE: 2020-07-20
// VERSION: 0.1.10
// VERSION: 0.2.0
// PURPOSE: Arduino library for PCF8575 - 16 channel I2C IO expander
// URL: https://github.com/RobTillaart/PCF8575

Expand All @@ -21,25 +21,8 @@ PCF8575::PCF8575(const uint8_t deviceAddress, TwoWire *wire)
}


#if defined (ESP8266) || defined(ESP32)
bool PCF8575::begin(int dataPin, int clockPin, uint16_t value)
{
if ((dataPin < 255) && (clockPin < 255))
{
_wire->begin(dataPin, clockPin);
} else {
_wire->begin();
}
if (! isConnected()) return false;
PCF8575::write16(value);
return true;
}
#endif


bool PCF8575::begin(uint16_t value)
{
_wire->begin();
if (! isConnected()) return false;
PCF8575::write16(value);
return true;
Expand Down
7 changes: 2 additions & 5 deletions PCF8575.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// FILE: PCF8575.h
// AUTHOR: Rob Tillaart
// DATE: 2020-07-20
// VERSION: 0.1.10
// VERSION: 0.2.0
// PURPOSE: Arduino library for PCF8575 - 16 channel I2C IO expander
// URL: https://github.com/RobTillaart/PCF8575

Expand All @@ -12,7 +12,7 @@
#include "Wire.h"


#define PCF8575_LIB_VERSION (F("0.1.10"))
#define PCF8575_LIB_VERSION (F("0.2.0"))


#ifndef PCF8575_INITIAL_VALUE
Expand All @@ -30,9 +30,6 @@ class PCF8575
// deviceAddress base = 0x20 + depends on address bits
explicit PCF8575(const uint8_t deviceAddress = 0x20, TwoWire *wire = &Wire);

#if defined (ESP8266) || defined(ESP32)
bool begin(int sda, int scl, uint16_t value = PCF8575_INITIAL_VALUE);
#endif
bool begin(uint16_t value = PCF8575_INITIAL_VALUE);
bool isConnected();

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ The library allows to read and write both single pins or 16 pins at once.
Furthermore some additional functions are implemented that are playful and useful.


#### 0.2.0 Breaking change

Version 0.2.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()**.


#### Interrupts

The PCF8575 has an interrupt output line (INT) to notify an MCU that one of the input lines has changed.
Expand Down
8 changes: 5 additions & 3 deletions examples/PCF8575_Wire2/PCF8575_Wire2.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// FILE: PCF8575_Wire2.ino
// AUTHOR: Rob Tillaart
// DATE: 2021-01-03
// PUPROSE: demo
// PURPOSE: demo


#include "PCF8575.h"

// adjust addresses if needed
// adjust addresses if needed
PCF8575 PCF(0x39, &Wire2);


Expand All @@ -18,6 +18,8 @@ void setup()
Serial.print("PCF8575_LIB_VERSION:\t");
Serial.println(PCF8575_LIB_VERSION);

Wire.begin();

if (!PCF.begin())
{
Serial.println("could not initialize...");
Expand Down Expand Up @@ -75,5 +77,5 @@ void doToggle()
}


// -- END OF FILE --
// -- END OF FILE --

8 changes: 5 additions & 3 deletions examples/PCF8575_array/PCF8575_array.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// FILE: PCF8575_array.ino
// AUTHOR: Rob Tillaart
// DATE: 2021-12-13
// PUPROSE: demo array of PCF - not tested
// PURPOSE: demo array of PCF - not tested


#include "PCF8575.h"

// adjust addresses if needed
// adjust addresses if needed
PCF8575 A(0x38);
PCF8575 B(0x39);
PCF8575 C(0x3A);
Expand All @@ -22,6 +22,8 @@ void setup()
Serial.print("PCF8575_LIB_VERSION:\t");
Serial.println(PCF8575_LIB_VERSION);

Wire.begin();

for (int i = 0; i < 3; i++)
{
PCF[i].begin();
Expand Down Expand Up @@ -52,5 +54,5 @@ void loop()
}


// -- END OF FILE --
// -- END OF FILE --

16 changes: 9 additions & 7 deletions examples/PCF8575_interrupt/PCF8575_interrupt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// FILE: PCF8575_interrupt.ino
// AUTHOR: Rob Tillaart
// DATE: 2021-01-03
// PUPROSE: test PCF8575 library
// PURPOSE: test PCF8575 library
//
// TEST SETUP
// TEST SETUP
// Connect INT pin of the PCF8575 to UNO pin 2
//
// (from figure 4 datasheet
Expand All @@ -19,7 +19,7 @@ PCF8575 PCF(0x38);

////////////////////////////////////
//
// INTERRUPT ROUTINE + FLAG
// INTERRUPT ROUTINE + FLAG
//
const int IRQPIN = 2;

Expand All @@ -33,15 +33,17 @@ void pcf_irq()

////////////////////////////////////
//
// MAIN CODE
// MAIN CODE
//
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("PCF8575_LIB_VERSION: ");
Serial.print("PCF8575_LIB_VERSION:\t");
Serial.println(PCF8575_LIB_VERSION);

Wire.begin();

PCF.begin();

pinMode(IRQPIN, INPUT_PULLUP);
Expand All @@ -62,10 +64,10 @@ void loop()
Serial.print('\t');
Serial.println(x, HEX);
}
// do other things here
// do other things here
delay(10);
}


// -- END OF FILE --
// -- END OF FILE --

8 changes: 5 additions & 3 deletions examples/PCF8575_isConnected/PCF8575_isConnected.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// FILE: PCF8575_isConnected.ino
// AUTHOR: Rob Tillaart
// DATE: 2021-01-03
// PUPROSE: demo device detection
// PURPOSE: demo device detection


#include "PCF8575.h"

// adjust addresses if needed
// adjust addresses if needed
PCF8575 PCF(0x39);


Expand All @@ -18,6 +18,8 @@ void setup()
Serial.print("PCF8575_LIB_VERSION:\t");
Serial.println(PCF8575_LIB_VERSION);

Wire.begin();

if (!PCF.begin())
{
Serial.println("could not initialize...");
Expand All @@ -38,5 +40,5 @@ void loop()
}


// -- END OF FILE --
// -- END OF FILE --

6 changes: 4 additions & 2 deletions examples/PCF8575_performance/PCF8575_performance.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// FILE: PCF8575_performance.ino
// AUTHOR: Rob Tillaart
// DATE: 2021-01-24
// PUPROSE: test PCF8575 library
// PURPOSE: test PCF8575 library


#include "PCF8575.h"
Expand All @@ -21,6 +21,8 @@ void setup()
Serial.print("PCF8575_LIB_VERSION:\t");
Serial.println(PCF8575_LIB_VERSION);

Wire.begin();

PCF.begin();
Serial.println(PCF.isConnected());

Expand Down Expand Up @@ -50,5 +52,5 @@ void loop()
}


// -- END OF FILE --
// -- END OF FILE --

14 changes: 8 additions & 6 deletions examples/PCF8575_select/PCF8575_select.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// FILE: PCF8575_select.ino
// AUTHOR: Rob Tillaart
// DATE: 2022-06-18
// PUPROSE: demo PCF8575 library select functions

// PURPOSE: demo PCF8575 library select functions


#include "PCF8575.h"
Expand All @@ -20,6 +19,8 @@ void setup()
Serial.print("PCF8575_LIB_VERSION:\t");
Serial.println(PCF8575_LIB_VERSION);

Wire.begin();

PCF.begin();
Serial.println(PCF.isConnected());
Serial.println();
Expand All @@ -29,14 +30,14 @@ void setup()
PCF.selectNone();
delay(1000);

// VU meter up
// VU meter up
for (int i = 0; i < 15; i++)
{
PCF.selectN(i);
delay(100);
}

// VU meter down
// VU meter down
for (int i = 15; i >= 0; i--)
{
PCF.selectN(i);
Expand All @@ -47,7 +48,7 @@ void setup()

void loop()
{
// night rider
// night rider
for (int i = 0; i < 15; i++)
{
PCF.select(i);
Expand All @@ -61,4 +62,5 @@ void loop()
}


// -- END OF FILE --
// -- END OF FILE --

8 changes: 5 additions & 3 deletions examples/PCF8575_test/PCF8575_test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// FILE: PCF8575_test.ino
// AUTHOR: Rob Tillaart
// DATE: 2020-07-20
// PUPROSE: test PCF8575 library
// PURPOSE: test PCF8575 library
// URL: https://github.com/RobTillaart/PCF8575


Expand All @@ -15,9 +15,11 @@ void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("PCF8575_test version: ");
Serial.print("PCF8575_LIB_VERSION:\t");
Serial.println(PCF8575_LIB_VERSION);

Wire.begin();

PCF.begin();

uint16_t x = PCF.read16();
Expand Down Expand Up @@ -76,5 +78,5 @@ void printHex(uint16_t x)
}


// -- END OF FILE --
// -- END OF FILE --

14 changes: 8 additions & 6 deletions examples/PCF8575_test1/PCF8575_test1.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// FILE: pcf8575_test.ino
// AUTHOR: Rob Tillaart
// DATE: 2021-01-03
// PUPROSE: demo
// PURPOSE: demo


#include "PCF8575.h"

// adjust addresses if needed
PCF8575 PCF_38(0x38); // add switches to lines (used as input)
PCF8575 PCF_39(0x39); // add leds to lines (used as output)
// adjust addresses if needed
PCF8575 PCF_38(0x38); // add switches to lines (used as input)
PCF8575 PCF_39(0x39); // add leds to lines (used as output)


void setup()
Expand All @@ -19,6 +19,8 @@ void setup()
Serial.print("PCF8575_LIB_VERSION:\t");
Serial.println(PCF8575_LIB_VERSION);

Wire.begin();

PCF_38.begin();
PCF_39.begin();

Expand Down Expand Up @@ -65,12 +67,12 @@ void setup()

void loop()
{
// echos the lines
// echos the lines
uint16_t value = PCF_38.read16();
PCF_39.write16(value);
delay(100);
}


// -- END OF FILE --
// -- END OF FILE --

Loading

0 comments on commit 6c3d22b

Please sign in to comment.