Skip to content

Commit d5080b8

Browse files
committed
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.
1 parent 894b1e8 commit d5080b8

File tree

4 files changed

+63
-30
lines changed

4 files changed

+63
-30
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Tic Stepper Motor Controller library for Arduino
22

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

75
## Summary
@@ -125,6 +123,9 @@ For complete documentation of this library, see [the tic-arduino documentation][
125123

126124
## Version history
127125

126+
* 2.2.0 (2024-11-20):
127+
- TicI2C: Added an optional parameter to the constructor to specify
128+
what I2C bus to use. Also added `setBus`, `getBus`, and `setAddress`.
128129
* 2.1.1 (2021-06-22):
129130
- Fixed some compilation errors and warnings.
130131
* 2.1.0 (2019-09-16):

Tic.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -178,37 +178,37 @@ void TicSerial::sendCommandHeader(TicCommand cmd)
178178

179179
void TicI2C::commandQuick(TicCommand cmd)
180180
{
181-
Wire.beginTransmission(_address);
182-
Wire.write((uint8_t)cmd);
183-
_lastError = Wire.endTransmission();
181+
_bus->beginTransmission(_address);
182+
_bus->write((uint8_t)cmd);
183+
_lastError = _bus->endTransmission();
184184
}
185185

186186
void TicI2C::commandW32(TicCommand cmd, uint32_t val)
187187
{
188-
Wire.beginTransmission(_address);
189-
Wire.write((uint8_t)cmd);
190-
Wire.write((uint8_t)(val >> 0)); // lowest byte
191-
Wire.write((uint8_t)(val >> 8));
192-
Wire.write((uint8_t)(val >> 16));
193-
Wire.write((uint8_t)(val >> 24)); // highest byte
194-
_lastError = Wire.endTransmission();
188+
_bus->beginTransmission(_address);
189+
_bus->write((uint8_t)cmd);
190+
_bus->write((uint8_t)(val >> 0)); // lowest byte
191+
_bus->write((uint8_t)(val >> 8));
192+
_bus->write((uint8_t)(val >> 16));
193+
_bus->write((uint8_t)(val >> 24)); // highest byte
194+
_lastError = _bus->endTransmission();
195195
}
196196

197197
void TicI2C::commandW7(TicCommand cmd, uint8_t val)
198198
{
199-
Wire.beginTransmission(_address);
200-
Wire.write((uint8_t)cmd);
201-
Wire.write((uint8_t)(val & 0x7F));
202-
_lastError = Wire.endTransmission();
199+
_bus->beginTransmission(_address);
200+
_bus->write((uint8_t)cmd);
201+
_bus->write((uint8_t)(val & 0x7F));
202+
_lastError = _bus->endTransmission();
203203
}
204204

205205
void TicI2C::getSegment(TicCommand cmd, uint8_t offset,
206206
uint8_t length, void * buffer)
207207
{
208-
Wire.beginTransmission(_address);
209-
Wire.write((uint8_t)cmd);
210-
Wire.write(offset);
211-
_lastError = Wire.endTransmission(false); // no stop (repeated start)
208+
_bus->beginTransmission(_address);
209+
_bus->write((uint8_t)cmd);
210+
_bus->write(offset);
211+
_lastError = _bus->endTransmission(false); // no stop (repeated start)
212212
if (_lastError)
213213
{
214214
// 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,
217217
return;
218218
}
219219

220-
uint8_t byteCount = Wire.requestFrom(_address, (uint8_t)length);
220+
uint8_t byteCount = _bus->requestFrom(_address, (uint8_t)length);
221221
if (byteCount != length)
222222
{
223223
_lastError = 50;
@@ -229,7 +229,7 @@ void TicI2C::getSegment(TicCommand cmd, uint8_t offset,
229229
uint8_t * ptr = (uint8_t *)buffer;
230230
for (uint8_t i = 0; i < length; i++)
231231
{
232-
*ptr = Wire.read();
232+
*ptr = _bus->read();
233233
ptr++;
234234
}
235235
}

Tic.h

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,19 +1423,51 @@ class TicI2C : public TicBase
14231423
/// Creates a new TicI2C object that will use the `Wire` object to communicate
14241424
/// with the Tic over I2C.
14251425
///
1426-
/// The `address` parameter specifies the 7-bit I2C address to use, and it
1427-
/// must match the Tic's "Device number" setting. It defaults to 14.
1428-
TicI2C(uint8_t address = 14) : _address(address)
1426+
/// The optional `address` parameter specifies the 7-bit I2C address to use,
1427+
/// and it must match the Tic's "Device number" setting. It defaults to 14.
1428+
///
1429+
/// The optional `bus` parameter specifies the I2C bus to use. You can also
1430+
/// set the bus with setBus().
1431+
TicI2C(uint8_t address = 14, TwoWire * bus = &Wire)
1432+
: _address(address), _bus(bus)
1433+
{
1434+
}
1435+
1436+
/// Configures this object to use the specified I2C bus.
1437+
/// The default bus is Wire, which is typically the first or only I2C bus on
1438+
/// an Arduino. To use Wire1 instead, you can write:
1439+
/// ```{.cpp}
1440+
/// tic.setBus(&Wire1);
1441+
/// ```
1442+
/// \param bus A pointer to a TwoWire object representing the I2C bus to use.
1443+
void setBus(TwoWire * bus)
1444+
{
1445+
this->_bus = bus;
1446+
}
1447+
1448+
/// Returns a pointer to the I2C bus that this object is configured to
1449+
/// use.
1450+
TwoWire * getBus()
14291451
{
1452+
return _bus;
14301453
}
14311454

1432-
// TODO: support Wire1 on Arduino Due, and bit-banging I2C on any board?
1455+
/// Configures this object to use the specified 7-bit I2C address.
1456+
/// This must match the address that the Motoron is configured to use.
1457+
void setAddress(uint8_t address)
1458+
{
1459+
this->_address = address;
1460+
}
14331461

1434-
/// Gets the I2C address specified in the constructor.
1435-
uint8_t getAddress() { return _address; }
1462+
/// Returns the 7-bit I2C address that this object is configured to use.
1463+
uint8_t getAddress()
1464+
{
1465+
return _address;
1466+
}
14361467

14371468
private:
1438-
const uint8_t _address;
1469+
uint8_t _address;
1470+
TwoWire * _bus;
14391471

14401472
void commandQuick(TicCommand cmd);
14411473
void commandW32(TicCommand cmd, uint32_t val);

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Tic
2-
version=2.1.1
2+
version=2.2.0
33
author=Pololu
44
maintainer=Pololu <[email protected]>
55
sentence=Tic Stepper Motor Controller library for Arduino

0 commit comments

Comments
 (0)