Skip to content

Commit

Permalink
TicI2C: Added an optional parameter to the constructor to specify
Browse files Browse the repository at this point in the history
what I2C bus to use.  Also added `setBus`, `getBus`, and `setAddress`.

Also changed the version number to 2.2.0.
  • Loading branch information
DavidEGrayson committed Nov 20, 2024
1 parent 894b1e8 commit d5080b8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 30 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Tic Stepper Motor Controller library for Arduino

Version: 2.1.1<br>
Release date: 2021-06-22<br>
[www.pololu.com](https://www.pololu.com/)

## Summary
Expand Down Expand Up @@ -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):
Expand Down
40 changes: 20 additions & 20 deletions Tic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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++;
}
}
46 changes: 39 additions & 7 deletions Tic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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=Tic
version=2.1.1
version=2.2.0
author=Pololu
maintainer=Pololu <[email protected]>
sentence=Tic Stepper Motor Controller library for Arduino
Expand Down

0 comments on commit d5080b8

Please sign in to comment.