From b5e840a6d3db9c789b9b897698b5f15841929dab Mon Sep 17 00:00:00 2001 From: rob tillaart Date: Sun, 12 Feb 2023 14:15:22 +0100 Subject: [PATCH] 0.1.5 TCA9548 --- libraries/TCA9548/CHANGELOG.md | 12 ++++- libraries/TCA9548/README.md | 36 +++++++++----- libraries/TCA9548/TCA9548.cpp | 8 +-- libraries/TCA9548/TCA9548.h | 13 +++-- .../tca9548_search_device.ino | 49 +++++++++++++++++++ libraries/TCA9548/keywords.txt | 2 + libraries/TCA9548/library.json | 2 +- libraries/TCA9548/library.properties | 4 +- libraries/TCA9548/test/unit_test_001.cpp | 13 ++++- 9 files changed, 112 insertions(+), 27 deletions(-) create mode 100644 libraries/TCA9548/examples/tca9548_search_device/tca9548_search_device.ino diff --git a/libraries/TCA9548/CHANGELOG.md b/libraries/TCA9548/CHANGELOG.md index 5cb0271a..259df3d9 100644 --- a/libraries/TCA9548/CHANGELOG.md +++ b/libraries/TCA9548/CHANGELOG.md @@ -6,6 +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.5] - 2023-02-12 +- fix #8 ambiguity +- update keywords.txt +- add example **tca9548_search_device.ino** +- update readme.md +- prepare error handling (constants) +- some minor edits. + + ## [0.1.4] - 2023-01-23 - update GitHub actions - update license 2023 @@ -15,12 +24,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - all channel functions return bool, true on success. - minor edits - ## [0.1.3] - 2022-11-26 - Add RP2040 support to build-CI. - Add CHANGELOG.md - update readme.md -- move code to .cpp +- move code to .cpp ## [0.1.2] - 2021-12-28 - update license diff --git a/libraries/TCA9548/README.md b/libraries/TCA9548/README.md index 430873c8..8dbe72d8 100644 --- a/libraries/TCA9548/README.md +++ b/libraries/TCA9548/README.md @@ -40,7 +40,7 @@ This library is expected to work for the PCA9548(a) too as the TCA is pin compat | PCA9548a | n | -There are however small differences, check the datasheets to see the details. +There are however small differences, check the data sheets to see the details. - [difference TCA PCA](https://e2e.ti.com/support/interface-group/interface/f/interface-forum/815758/faq-what-is-the-difference-between-an-i2c-device-with-the-family-name-pca-and-tca) - https://electronics.stackexchange.com/questions/209616/is-nxps-pca9548a-compatible-with-tis-tca9548a - https://www.nxp.com/docs/en/application-note/AN262.pdf @@ -100,32 +100,42 @@ Note that writes are only optimized if the channels are already set. - **void setForced(bool forced = false)** set forced write, slower but more robust. - forced == false == fast mode (default). - - forced == true == robust mode. + - forced == true == slower, robust mode. - **bool getForced()** returns set flag. +## Error Codes + +Not implemented yet, preparation for 0.2.0. + +| name | value | description | +|:------------------------|:-------:|:------------------------| +| TCA9548_OK | 00 | no error | +| TCA9548_ERROR_I2C | -10 | detected an I2C error | +| TCA9548_ERROR_CHANNEL | -20 | channel out of range | + + ## Future #### Must - improve documentation. -- improve error handling - +- improve error handling. #### Should -- add examples -- test test and test -- write unit test -- create derived classes for compatible devices - +- add examples. +- test test and test. +- write unit test. +- create derived classes for compatible devices (0.2.0). + - see above PCA9548 and PCA9548a. +- **bool isConnected(channel, address)** (0.2.0) + - keep previous mask? #### Could -- set an "always enabled" mask (have to investigate the consequences) -- verify channel in range - - e.g. **bool enableChannel(uint8_t channel)**, disable(), select() - +- set an "always enabled" mask. + - investigate the consequences! #### Wont diff --git a/libraries/TCA9548/TCA9548.cpp b/libraries/TCA9548/TCA9548.cpp index 8dbc9b37..e8b1a82c 100644 --- a/libraries/TCA9548/TCA9548.cpp +++ b/libraries/TCA9548/TCA9548.cpp @@ -1,7 +1,7 @@ // // FILE: TCA9548.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.1.4 +// VERSION: 0.1.5 // DATE: 2021-03-16 // PURPOSE: Library for TCA9548 I2C multiplexer @@ -16,7 +16,7 @@ TCA9548::TCA9548(const uint8_t deviceAddress, TwoWire *wire) _mask = 0x00; _resetPin = -1; _forced = false; - _error = 0; + _error = TCA9548_OK; } @@ -118,7 +118,7 @@ uint8_t TCA9548::getChannelMask() { if (_forced) // read from device. { - _wire->requestFrom(_address, 1); + _wire->requestFrom(_address, (uint8_t)1); _mask = _wire->read(); } return _mask; @@ -156,7 +156,7 @@ bool TCA9548::getForced() int TCA9548::getError() { int error = _error; - _error = 0; + _error = TCA9548_OK; return error; } diff --git a/libraries/TCA9548/TCA9548.h b/libraries/TCA9548/TCA9548.h index 71f06793..55dc467a 100644 --- a/libraries/TCA9548/TCA9548.h +++ b/libraries/TCA9548/TCA9548.h @@ -2,7 +2,7 @@ // // FILE: TCA9548.h // AUTHOR: Rob Tillaart -// VERSION: 0.1.4 +// VERSION: 0.1.5 // DATE: 2021-03-16 // PURPOSE: Library for TCA9548 I2C multiplexer // URL: https://github.com/RobTillaart/TCA9548 @@ -12,7 +12,14 @@ #include "Wire.h" -#define TCA9548_LIB_VERSION (F("0.1.4")) +#define TCA9548_LIB_VERSION (F("0.1.5")) + + +// ERROR CODES (to be elaborated) +#define TCA9548_OK 0 +#define TCA9548_ERROR_I2C -10 // not used yet +#define TCA9548_ERROR_CHANNEL -20 // not used yet + class TCA9548 @@ -54,7 +61,7 @@ public: private: uint8_t _address; TwoWire* _wire; - uint8_t _mask; // caching mask = status of channels + uint8_t _mask; // caching mask = status of channels uint8_t _resetPin; // default not set == -1 (255) bool _forced; int _error; diff --git a/libraries/TCA9548/examples/tca9548_search_device/tca9548_search_device.ino b/libraries/TCA9548/examples/tca9548_search_device/tca9548_search_device.ino new file mode 100644 index 00000000..471ba535 --- /dev/null +++ b/libraries/TCA9548/examples/tca9548_search_device/tca9548_search_device.ino @@ -0,0 +1,49 @@ +// +// FILE: tca9548_search_device.ino +// AUTHOR: Rob Tillaart +// PURPOSE: demo TCA9548 I2C multiplexer +// URL: https://github.com/RobTillaart/TCA9548 + + +#include "TCA9548.h" + +TCA9548 MP(0x70); + +uint8_t searchAddress = 0x38; // dummy, adjust to your needs. + + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + Serial.println(); + + Wire.begin(); + if (MP.begin() == false) + { + Serial.println("Could not connect to TCA9548 multiplexer."); + } + + Serial.println("\nScan the channels of the multiplexer for searchAddress.\n"); + for (int chan = 0; chan < 8; chan++) + { + MP.selectChannel(chan); + bool b = MP.isConnected(searchAddress); + Serial.print("CHAN: "); + Serial.print(chan); + Serial.print("\t"); + Serial.print( b ? "found!" : "x"); + } + Serial.println(); + + Serial.println("done..."); +} + + +void loop() +{ +} + + +// -- END OF FILE -- + diff --git a/libraries/TCA9548/keywords.txt b/libraries/TCA9548/keywords.txt index aee4bcf7..718bee2d 100644 --- a/libraries/TCA9548/keywords.txt +++ b/libraries/TCA9548/keywords.txt @@ -12,6 +12,7 @@ enableChannel KEYWORD2 disableChannel KEYWORD2 selectChannel KEYWORD2 isEnabled KEYWORD2 +disableAllChannels KEYWORD2 setChannelMask KEYWORD2 getChannelMask KEYWORD2 @@ -28,3 +29,4 @@ getError KEYWORD2 # Constants (LITERAL1) TCA9548_LIB_VERSION LITERAL1 +TCA9548_OK LITERAL1 diff --git a/libraries/TCA9548/library.json b/libraries/TCA9548/library.json index e6b33515..4082bac0 100644 --- a/libraries/TCA9548/library.json +++ b/libraries/TCA9548/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/TCA9548" }, - "version": "0.1.4", + "version": "0.1.5", "license": "MIT", "frameworks": "arduino", "platforms": "*", diff --git a/libraries/TCA9548/library.properties b/libraries/TCA9548/library.properties index aa602681..9237a4f9 100644 --- a/libraries/TCA9548/library.properties +++ b/libraries/TCA9548/library.properties @@ -1,5 +1,5 @@ name=TCA9548 -version=0.1.4 +version=0.1.5 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino Library for TCA9548 I2C multiplexer. @@ -7,5 +7,5 @@ paragraph=TCA9548, TCA9548a, PCA9548, PCA9548a category=Signal Input/Output url=https://github.com/RobTillaart/TCA9548 architectures=* -includes=TCA9548.h +includes=TCA9548.h depends= diff --git a/libraries/TCA9548/test/unit_test_001.cpp b/libraries/TCA9548/test/unit_test_001.cpp index ac542fa3..572e0f4b 100644 --- a/libraries/TCA9548/test/unit_test_001.cpp +++ b/libraries/TCA9548/test/unit_test_001.cpp @@ -61,6 +61,14 @@ unittest_teardown() } +unittest(test_constants) +{ + assertEqual(0, TCA9548_OK); + assertEqual(-10, TCA9548_ERROR_I2C); + assertEqual(-20, TCA9548_ERROR_CHANNEL); +} + + unittest(test_begin) { TCA9548 tca(0x70); @@ -69,7 +77,6 @@ unittest(test_begin) assertEqual(b, true); assertTrue(tca.isConnected()); - } @@ -113,4 +120,6 @@ unittest(test_select) unittest_main() -// -- END OF FILE -- \ No newline at end of file + +// -- END OF FILE -- +