From d5080b80b6cc4fd76129bc61e6c3e61eee0dcf7b Mon Sep 17 00:00:00 2001 From: David Grayson Date: Wed, 20 Nov 2024 12:10:35 -0800 Subject: [PATCH] TicI2C: Added an optional parameter to the constructor to specify what I2C bus to use. Also added `setBus`, `getBus`, and `setAddress`. Also changed the version number to 2.2.0. --- README.md | 5 +++-- Tic.cpp | 40 ++++++++++++++++++++-------------------- Tic.h | 46 +++++++++++++++++++++++++++++++++++++++------- library.properties | 2 +- 4 files changed, 63 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 543ba03..4f9aa98 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,5 @@ # Tic Stepper Motor Controller library for Arduino -Version: 2.1.1
-Release date: 2021-06-22
[www.pololu.com](https://www.pololu.com/) ## Summary @@ -125,6 +123,9 @@ For complete documentation of this library, see [the tic-arduino documentation][ ## Version history +* 2.2.0 (2024-11-20): + - TicI2C: Added an optional parameter to the constructor to specify + what I2C bus to use. Also added `setBus`, `getBus`, and `setAddress`. * 2.1.1 (2021-06-22): - Fixed some compilation errors and warnings. * 2.1.0 (2019-09-16): diff --git a/Tic.cpp b/Tic.cpp index cfff1fb..8c6fa07 100644 --- a/Tic.cpp +++ b/Tic.cpp @@ -178,37 +178,37 @@ void TicSerial::sendCommandHeader(TicCommand cmd) void TicI2C::commandQuick(TicCommand cmd) { - Wire.beginTransmission(_address); - Wire.write((uint8_t)cmd); - _lastError = Wire.endTransmission(); + _bus->beginTransmission(_address); + _bus->write((uint8_t)cmd); + _lastError = _bus->endTransmission(); } void TicI2C::commandW32(TicCommand cmd, uint32_t val) { - Wire.beginTransmission(_address); - Wire.write((uint8_t)cmd); - Wire.write((uint8_t)(val >> 0)); // lowest byte - Wire.write((uint8_t)(val >> 8)); - Wire.write((uint8_t)(val >> 16)); - Wire.write((uint8_t)(val >> 24)); // highest byte - _lastError = Wire.endTransmission(); + _bus->beginTransmission(_address); + _bus->write((uint8_t)cmd); + _bus->write((uint8_t)(val >> 0)); // lowest byte + _bus->write((uint8_t)(val >> 8)); + _bus->write((uint8_t)(val >> 16)); + _bus->write((uint8_t)(val >> 24)); // highest byte + _lastError = _bus->endTransmission(); } void TicI2C::commandW7(TicCommand cmd, uint8_t val) { - Wire.beginTransmission(_address); - Wire.write((uint8_t)cmd); - Wire.write((uint8_t)(val & 0x7F)); - _lastError = Wire.endTransmission(); + _bus->beginTransmission(_address); + _bus->write((uint8_t)cmd); + _bus->write((uint8_t)(val & 0x7F)); + _lastError = _bus->endTransmission(); } void TicI2C::getSegment(TicCommand cmd, uint8_t offset, uint8_t length, void * buffer) { - Wire.beginTransmission(_address); - Wire.write((uint8_t)cmd); - Wire.write(offset); - _lastError = Wire.endTransmission(false); // no stop (repeated start) + _bus->beginTransmission(_address); + _bus->write((uint8_t)cmd); + _bus->write(offset); + _lastError = _bus->endTransmission(false); // no stop (repeated start) if (_lastError) { // Set the buffer bytes to 0 so the program will not use an uninitialized @@ -217,7 +217,7 @@ void TicI2C::getSegment(TicCommand cmd, uint8_t offset, return; } - uint8_t byteCount = Wire.requestFrom(_address, (uint8_t)length); + uint8_t byteCount = _bus->requestFrom(_address, (uint8_t)length); if (byteCount != length) { _lastError = 50; @@ -229,7 +229,7 @@ void TicI2C::getSegment(TicCommand cmd, uint8_t offset, uint8_t * ptr = (uint8_t *)buffer; for (uint8_t i = 0; i < length; i++) { - *ptr = Wire.read(); + *ptr = _bus->read(); ptr++; } } diff --git a/Tic.h b/Tic.h index 69ea833..433f205 100644 --- a/Tic.h +++ b/Tic.h @@ -1423,19 +1423,51 @@ class TicI2C : public TicBase /// Creates a new TicI2C object that will use the `Wire` object to communicate /// with the Tic over I2C. /// - /// The `address` parameter specifies the 7-bit I2C address to use, and it - /// must match the Tic's "Device number" setting. It defaults to 14. - TicI2C(uint8_t address = 14) : _address(address) + /// The optional `address` parameter specifies the 7-bit I2C address to use, + /// and it must match the Tic's "Device number" setting. It defaults to 14. + /// + /// The optional `bus` parameter specifies the I2C bus to use. You can also + /// set the bus with setBus(). + TicI2C(uint8_t address = 14, TwoWire * bus = &Wire) + : _address(address), _bus(bus) + { + } + + /// Configures this object to use the specified I2C bus. + /// The default bus is Wire, which is typically the first or only I2C bus on + /// an Arduino. To use Wire1 instead, you can write: + /// ```{.cpp} + /// tic.setBus(&Wire1); + /// ``` + /// \param bus A pointer to a TwoWire object representing the I2C bus to use. + void setBus(TwoWire * bus) + { + this->_bus = bus; + } + + /// Returns a pointer to the I2C bus that this object is configured to + /// use. + TwoWire * getBus() { + return _bus; } - // TODO: support Wire1 on Arduino Due, and bit-banging I2C on any board? + /// Configures this object to use the specified 7-bit I2C address. + /// This must match the address that the Motoron is configured to use. + void setAddress(uint8_t address) + { + this->_address = address; + } - /// Gets the I2C address specified in the constructor. - uint8_t getAddress() { return _address; } + /// Returns the 7-bit I2C address that this object is configured to use. + uint8_t getAddress() + { + return _address; + } private: - const uint8_t _address; + uint8_t _address; + TwoWire * _bus; void commandQuick(TicCommand cmd); void commandW32(TicCommand cmd, uint32_t val); diff --git a/library.properties b/library.properties index 6e6103b..c2d5df1 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Tic -version=2.1.1 +version=2.2.0 author=Pololu maintainer=Pololu sentence=Tic Stepper Motor Controller library for Arduino