Skip to content

Commit

Permalink
refactor API, begin() (#13)
Browse files Browse the repository at this point in the history
- refactor API, begin()
- update readme.md
- update examples.
  • Loading branch information
RobTillaart authored Dec 8, 2023
1 parent 19f0617 commit f03c0c8
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 46 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.1.7] - 2023-11-22
## [0.2.0] - 2023-12-08
- refactor API, begin()
- update readme.md
- update examples.

----

## [0.1.7] - 2023-11-22
- update readme.md

## [0.1.6] - 2023-09-23
- add Wire1 support for ESP32
Expand Down
52 changes: 38 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ Arduino library for TCA9555 16 channel I2C port expander.

This library gives easy control over the 16 pins of a TCA9555 chip.

The TCA9555 supports up to 400 kHz I2C.
The pins can be used for input and output, and allow to set polarity.

The pins can be set per pin, or with a mask to set either 8 or 16 pins
in one call.
Note that for the 16 bit interface settings are not simultaneous as the
16 bit interface does 2 calls to the 8 bit interface.


#### TCA9535
Expand All @@ -33,12 +38,38 @@ There is a TCA9535 class which is a (convenience) wrapper around the TCA9555 cla
This allows one to create TCA9535 objects.


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


#### Related

16 bit port expanders

- https://github.com/RobTillaart/MCP23017_RT
- https://github.com/RobTillaart/MCP23S17
- https://github.com/RobTillaart/PCF8575

8 bit port expanders

- https://github.com/RobTillaart/MCP23008
- https://github.com/RobTillaart/MCP23S08
- https://github.com/RobTillaart/PCF8574


## Hardware

#### I2C addresses
#### I2C

Allowed addresses are 0x20..0x27. to be set with pin A0, A1, A2.

The TCA9555 supports up to 400 kHz I2C.


## Interface

Expand All @@ -54,11 +85,9 @@ Check the datasheet for details
Can be overruled with Wire0..WireN.
- **TCA9535(uint8_t address, TwoWire \*wire = &Wire)** idem.
- **uint8_t getType()** returns 35 or 55 depending on type.


- **bool begin()** for UNO, returns true if successful.
- **bool begin(uint8_t sda, uint8_t scl)** for ESP32, returns true if successful.
- **bool isConnected()** returns true if connected, false otherwise.
- **bool begin()** initializes library.
Returns true if device can be seen on I2C bus, false otherwise.
- **bool isConnected()** returns true if device can be seen on I2C bus, false otherwise.
- **uint8_t getAddress()** returns set address, (debugging).


Expand Down Expand Up @@ -114,11 +143,6 @@ Reading it will reset the flag to **TCA9555_OK**.
| TCA9555_INVALID_READ | -100 |


## Operation

See examples


## Future


Expand All @@ -140,13 +164,13 @@ See examples
#### Could

- rethink class hierarchy
- TCA9535 has less functions so should be base class?
- TCA9535 has less functions so should be base class
- valid address range?

#### Wont (unless)

- add TCA9535 error codes
- better reuse them?
- better reuse them? ==> TCA95X5 codes


## Support
Expand Down
14 changes: 2 additions & 12 deletions TCA9555.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: TCA9555.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.7
// VERSION: 0.2.0
// PURPOSE: Arduino library for I2C TCA9555 16 channel port expander
// DATE: 2021-06-09
// URL: https://github.com/RobTillaart/TCA9555
Expand Down Expand Up @@ -30,19 +30,9 @@ TCA9555::TCA9555(uint8_t address, TwoWire *wire)
}


#if defined(ESP8266) || defined(ESP32)
bool TCA9555::begin(const uint8_t dataPin, const uint8_t clockPin)
{
_wire->begin(dataPin, clockPin);
if (! isConnected()) return false;
return true;
}
#endif


bool TCA9555::begin()
{
_wire->begin();
if ((_address < 0x20) || (_address > 0x27)) return false;
if (! isConnected()) return false;
return true;
}
Expand Down
8 changes: 2 additions & 6 deletions TCA9555.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// FILE: TCA9555.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.7
// VERSION: 0.2.0
// PURPOSE: Arduino library for I2C TCA9555 16 channel port expander
// DATE: 2021-06-09
// URL: https://github.com/RobTillaart/TCA9555
Expand All @@ -12,7 +12,7 @@
#include "Wire.h"


#define TCA9555_LIB_VERSION (F("0.1.7"))
#define TCA9555_LIB_VERSION (F("0.2.0"))

#define TCA9555_OK 0x00
#define TCA9555_PIN_ERROR 0x81
Expand Down Expand Up @@ -51,10 +51,6 @@ class TCA9555
public:
TCA9555(uint8_t address, TwoWire *wire = &Wire);


#if defined(ESP8266) || defined(ESP32)
bool begin(const uint8_t dataPin, const uint8_t clockPin);
#endif
bool begin();
bool isConnected();
uint8_t getAddress();
Expand Down
3 changes: 2 additions & 1 deletion examples/TCA9535_digitalRead/TCA9535_digitalRead.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ TCA9535 TCA(0x27);
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("TCA9555_LIB_VERSION: ");
Serial.println(TCA9555_LIB_VERSION);

Expand All @@ -37,5 +38,5 @@ void loop()
}


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

7 changes: 4 additions & 3 deletions examples/TCA9535_digitalWrite/TCA9535_digitalWrite.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ TCA9535 TCA(0x27);
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("TCA9555_LIB_VERSION: ");
Serial.println(TCA9555_LIB_VERSION);

Expand All @@ -26,7 +27,7 @@ void setup()
Serial.println("TEST digitalWrite(0)");
for (int i = 0; i < 16; i++)
{
TCA.digitalWrite(0, i % 2); // alternating HIGH/LOW
TCA.digitalWrite(0, i % 2); // alternating HIGH/LOW
Serial.print(i % 2);
Serial.print('\t');
delay(250);
Expand All @@ -37,7 +38,7 @@ void setup()
Serial.println("TEST digitalWrite(pin)");
for (int pin = 0; pin < 16; pin++)
{
TCA.digitalWrite(pin, 1 - pin % 2); // alternating HIGH/LOW
TCA.digitalWrite(pin, 1 - pin % 2); // alternating HIGH/LOW
Serial.print(1 - pin % 2);
Serial.print('\t');
}
Expand All @@ -62,4 +63,4 @@ void loop()
}


// -- END OF FILE --
// -- END OF FILE --
3 changes: 2 additions & 1 deletion examples/TCA9555_digitalRead/TCA9555_digitalRead.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ TCA9555 TCA(0x27);
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("TCA9555_LIB_VERSION: ");
Serial.println(TCA9555_LIB_VERSION);

Expand All @@ -37,5 +38,5 @@ void loop()
}


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

7 changes: 4 additions & 3 deletions examples/TCA9555_digitalWrite/TCA9555_digitalWrite.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ TCA9555 TCA(0x27);
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("TCA9555_LIB_VERSION: ");
Serial.println(TCA9555_LIB_VERSION);

Expand All @@ -26,7 +27,7 @@ void setup()
Serial.println("TEST digitalWrite(0)");
for (int i = 0; i < 16; i++)
{
TCA.digitalWrite(0, i % 2); // alternating HIGH/LOW
TCA.digitalWrite(0, i % 2); // alternating HIGH/LOW
Serial.print(i % 2);
Serial.print('\t');
delay(250);
Expand All @@ -37,7 +38,7 @@ void setup()
Serial.println("TEST digitalWrite(pin)");
for (int pin = 0; pin < 16; pin++)
{
TCA.digitalWrite(pin, 1 - pin % 2); // alternating HIGH/LOW
TCA.digitalWrite(pin, 1 - pin % 2); // alternating HIGH/LOW
Serial.print(1 - pin % 2);
Serial.print('\t');
}
Expand All @@ -62,5 +63,5 @@ void loop()
}


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

5 changes: 4 additions & 1 deletion examples/TCA9555_getType/TCA9555_getType.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ TCA9535 TCA1(0x21);
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("TCA9555_LIB_VERSION: ");
Serial.println(TCA9555_LIB_VERSION);

Wire.begin();

Serial.println(TCA0.getType());
Serial.println(TCA1.getType());
}
Expand All @@ -29,4 +32,4 @@ void loop()
}


// -- END OF FILE --
// -- END OF FILE --
3 changes: 2 additions & 1 deletion examples/TCA9555_setPolarity/TCA9555_setPolarity.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ TCA9555 TCA(0x27);
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("TCA9555_LIB_VERSION: ");
Serial.println(TCA9555_LIB_VERSION);

Expand Down Expand Up @@ -53,5 +54,5 @@ void loop()
}


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

2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/TCA9555.git"
},
"version": "0.1.7",
"version": "0.2.0",
"license": "MIT",
"frameworks": "*",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=TCA9555
version=0.1.7
version=0.2.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for I2C TCA9555 16 channel port expander - 16 IO-lines
Expand Down
8 changes: 7 additions & 1 deletion test/unit_test_001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ unittest(test_constructor_TCA9555)
{
TCA9555 TCA(0x22);

Wire.begin();

assertTrue(TCA.begin());
assertTrue(TCA.isConnected());
assertEqual(55, TCA.getType());
Expand All @@ -62,6 +64,8 @@ unittest(test_constructor_TCA9535)
{
TCA9535 TCA(0x22);

Wire.begin();

assertTrue(TCA.begin());
assertTrue(TCA.isConnected());
assertEqual(35, TCA.getType());
Expand Down Expand Up @@ -102,4 +106,6 @@ unittest(test_constants_II)

unittest_main()

// --------

// -- END OF FILE --

0 comments on commit f03c0c8

Please sign in to comment.