Skip to content

Commit

Permalink
Fix #20, disableChannel bug() a.o. (#21)
Browse files Browse the repository at this point in the history
- Fix #20, **disableChannel()** bug + optimize.
- Fix **isConnected(address, channel)** bug.
- update keywords.txt
- update readme.md
- add **uint8_t find(address)** function, returns mask.
- update / add examples.
  • Loading branch information
RobTillaart authored Apr 18, 2024
1 parent 503d254 commit e8f6b83
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 37 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.3.0] - 2024-04-15
- Fix #20, **disableChannel()** bug + optimize.
- Fix **isConnected(address, channel)** bug.
- update keywords.txt
- update readme.md
- add **uint8_t find(address)** function, returns mask.
- update / add examples.

----

## [0.2.2] - 2024-01-01
- add **bool isConnected(uint8_t address, uint8_t channel)**
- minor edits


## [0.2.1] - 2023-12-09
- add derived classes
- PCA9543 (2 channel), PCA9545 (4 channel), PCA9546 (4 channel)
Expand Down
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ There are however small differences, check the data sheets to see the details.
deviceAddress = 0x70 .. 0x77, wire = Wire or WireN.
- **bool begin(uint8_t mask = 0x00)** set mask of channels to be enabled, default all disabled.
- **bool isConnected()** returns true if address of the multiplexer is found on I2C bus.
- **bool isConnected(uint8_t address, uint8_t channel)** find address on selected channel.
Note that this changes the selected and or enabled channels.
Returns true if found.

The derived classes PCA9548/PCA9546 have the same interface, except constructor.
(see #15)
Expand All @@ -114,9 +111,17 @@ The derived classes PCA9548/PCA9546 have the same interface, except constructor.

#### Find device

- **bool isConnected(uint8_t address)** returns true if arbitrary address is found on I2C bus.
This can be used to verify if a certain device is available (or not) on an **enabled** channel.
So it does not scan all 8 channels to see if any of them has a device with the address given.
- **bool isConnected(uint8_t address)** returns true if arbitrary address is found on the
current I2C bus + selected channels.
This can be used to verify if a certain device is available (or not) on any **enabled** channel.
So it does not scan all (8) channels to see if any of them has a device with the address given.
- **bool isConnected(uint8_t address, uint8_t channel)** find address on selected channel.
Note that this function changes the selected and or enabled channels.
Returns true if found.
- **uint8_t find(uint8_t address)** returns a mask with bits set for channels
where the address is found. It scans all channels available.
Note that this function changes the selected and or enabled channels.
Returns 0 when the address is not found on any channel, or one bit set per channel found.


#### Channel functions
Expand Down Expand Up @@ -145,17 +150,19 @@ Optional the library can reset the device.
- **void setResetPin(uint8_t resetPin)** sets the pin to reset the chip.
- **void reset()** trigger the reset pin.


#### Debug

- **int getError()** returns the last I2C error.
- **int getError()** returns the last (I2C) status / error.


#### Forced IO

When forced IO is set, all writes and read, e.g. **uint8_t getChannelMask()**, will go to the device.
If the **forced-IO** flag is set to false, it will cache the value of the channels enabled.
This will result in far more responsive and faster calls.
Note that writes are only optimized if the channels are already set.
Note that writes are only optimized if the channels are already set.
Forced IO is also used to speed up **getChannelMask()**.

- **void setForced(bool forced = false)** set forced write, slower but more robust.
- forced == false == fast mode (default).
Expand All @@ -165,6 +172,8 @@ Note that writes are only optimized if the channels are already set.

#### Interrupts

(not tested)

The PCA9545 and PCA9543 support interrupts.
These two derived classes have implemented the

Expand Down Expand Up @@ -200,6 +209,10 @@ Not implemented yet, preparation for future.
- set an "always enabled" mask.
- investigate the consequences!
- extend the unit tests.
- **uint8_t find(address)** returns a mask of channel where address is found
- restore channel in **isConnected(address, channel);
- add guard in **reset()**, is the pin set?


#### Wont

Expand Down
32 changes: 18 additions & 14 deletions TCA9548.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: TCA9548.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.2
// VERSION: 0.3.0
// DATE: 2021-03-16
// PURPOSE: Arduino Library for TCA9548 I2C multiplexer and compatibles.

Expand Down Expand Up @@ -45,7 +45,20 @@ bool TCA9548::isConnected(uint8_t address)
bool TCA9548::isConnected(uint8_t address, uint8_t channel)
{
if (!selectChannel(channel)) return false;
return isConnected(_address);
return isConnected(address);
}


uint8_t TCA9548::find(uint8_t address)
{
uint8_t mask = 0x00;
for (uint8_t ch = 0; ch < _channels; ch++)
{
// will work partially if MP is off line (by choice).
selectChannel(ch);
if (isConnected(address)) mask |= (1 << ch);
}
return mask;
}


Expand All @@ -58,30 +71,21 @@ uint8_t TCA9548::channelCount()
bool TCA9548::enableChannel(uint8_t channel)
{
if (channel >= _channels) return false;
if (!isEnabled(channel))
{
setChannelMask(_mask | (0x01 << channel));
}
return true;
return setChannelMask(_mask | (0x01 << channel));
}


bool TCA9548::disableChannel(uint8_t channel)
{
if (channel >= _channels) return false;
if (!isEnabled(channel))
{
setChannelMask(_mask & ~(0x01 << channel));
}
return true;
return setChannelMask(_mask & ~(0x01 << channel));
}


bool TCA9548::selectChannel(uint8_t channel)
{
if (channel >= _channels) return false;
setChannelMask(0x01 << channel);
return true;
return setChannelMask(0x01 << channel);
}


Expand Down
20 changes: 11 additions & 9 deletions TCA9548.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// FILE: TCA9548.h
// AUTHOR: Rob Tillaart
// VERSION: 0.2.2
// VERSION: 0.3.0
// DATE: 2021-03-16
// PURPOSE: Arduino Library for TCA9548 I2C multiplexer and compatibles.
// URL: https://github.com/RobTillaart/TCA9548
Expand All @@ -12,7 +12,7 @@
#include "Wire.h"


#define TCA9548_LIB_VERSION (F("0.2.2"))
#define TCA9548_LIB_VERSION (F("0.3.0"))


// ERROR CODES (to be elaborated)
Expand All @@ -33,6 +33,8 @@ class TCA9548
bool isConnected(uint8_t address); // find any address on I2C bus
bool isConnected(uint8_t address, uint8_t channel); // find address on selected channel

uint8_t find(uint8_t address); // returns a mask with channels

// channel = 0..channelCount()-1
uint8_t channelCount();
bool enableChannel(uint8_t channel); // enable this channel non exclusive
Expand Down Expand Up @@ -61,13 +63,13 @@ class TCA9548


protected:
uint8_t _address;
TwoWire* _wire;
uint8_t _mask; // caching mask = status of channels
uint8_t _resetPin; // default not set == -1 (255)
bool _forced;
int _error;
uint8_t _channels; // PCA954x support.
uint8_t _address;
TwoWire * _wire;
uint8_t _mask; // caching mask = status of channels
uint8_t _resetPin; // default not set == -1 (255)
bool _forced;
int _error;
uint8_t _channels; // PCA954x support.
};


Expand Down
52 changes: 52 additions & 0 deletions examples/TCA9548_find/TCA9548_find.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// FILE: TCA9548_find.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo TCA9548 I2C multiplexer
// URL: https://github.com/RobTillaart/TCA9548


#include "TCA9548.h"

TCA9548 MP(0x70);

uint8_t channels = 0;


void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("TCA9548_LIB_VERSION: ");
Serial.println(TCA9548_LIB_VERSION);
Serial.println();

Wire.begin();
if (MP.begin() == false)
{
Serial.println("COULD NOT CONNECT TO MULTIPLEXER");
}

channels = MP.channelCount();
Serial.print("CHAN:\t");
Serial.println(MP.channelCount());

// adjust address range to your needs.
for (uint8_t addr = 60; addr < 70; addr++)
{
if (addr % 10 == 0) Serial.println();
Serial.print(addr);
Serial.print("\t");
Serial.print(MP.find(addr), BIN);
Serial.println();
}

Serial.println("done...");
}


void loop()
{
}


// -- END OF FILE --
2 changes: 1 addition & 1 deletion examples/tca9548_demo/tca9548_demo.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// FILE: tca9548_demo.ino
// FILE: TCA9548_demo.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo TCA9548 I2C multiplexer
// URL: https://github.com/RobTillaart/TCA9548
Expand Down
6 changes: 4 additions & 2 deletions examples/tca9548_search_device/tca9548_search_device.ino
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//
// FILE: tca9548_search_device.ino
// FILE: TCA9548_search_device.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo TCA9548 I2C multiplexer
// URL: https://github.com/RobTillaart/TCA9548
//
// NOTE: since 0.3.0 a find function is added.


#include "TCA9548.h"
Expand All @@ -28,7 +30,7 @@ void setup()
}

Serial.println("\nScan the channels of the multiplexer for searchAddress.\n");
for (int chan = 0; chan < 8; chan++)
for (int chan = 0; chan < MP.channelCount(); chan++)
{
MP.selectChannel(chan);
bool b = MP.isConnected(searchAddress);
Expand Down
4 changes: 4 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ PCA9543 KEYWORD1
# Methods and Functions (KEYWORD2)
begin KEYWORD2
isConnected KEYWORD2
find KEYWORD2

channelCount KEYWORD2
enableChannel KEYWORD2
disableChannel KEYWORD2
selectChannel KEYWORD2
Expand All @@ -29,6 +31,8 @@ getForced KEYWORD2

getError KEYWORD2


# Devices with interrupt
getInterruptMask KEYWORD2


Expand Down
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/TCA9548"
},
"version": "0.2.2",
"version": "0.3.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=TCA9548
version=0.2.2
version=0.3.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino Library for TCA9548 I2C multiplexer and compatibles.
Expand Down

0 comments on commit e8f6b83

Please sign in to comment.