Skip to content

Commit 246326a

Browse files
enable option to auto restart i2c transmission per issue #82
1 parent b7a5b1a commit 246326a

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

examples/StandardFirmata/StandardFirmata.ino

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
See file LICENSE.txt for further informations on licensing terms.
2222
23-
Last updated by Jeff Hoefs: August 9th, 2015
23+
Last updated by Jeff Hoefs: October 10th, 2015
2424
*/
2525

2626
#include <Servo.h>
@@ -33,6 +33,10 @@
3333
#define I2C_STOP_READING B00011000
3434
#define I2C_READ_WRITE_MODE_MASK B00011000
3535
#define I2C_10BIT_ADDRESS_MODE_MASK B00100000
36+
#define I2C_END_TX_MASK B01000000
37+
#define I2C_STOP_TX 1
38+
#define I2C_RESTART_TX 0
39+
3640
#define I2C_MAX_QUERIES 8
3741
#define I2C_REGISTER_NOT_SPECIFIED -1
3842

@@ -66,6 +70,7 @@ struct i2c_device_info {
6670
byte addr;
6771
int reg;
6872
byte bytes;
73+
byte stopTX;
6974
};
7075

7176
/* for i2c read continuous more */
@@ -153,7 +158,7 @@ void readAndReportData(byte address, int theRegister, byte numBytes) {
153158
if (theRegister != I2C_REGISTER_NOT_SPECIFIED) {
154159
Wire.beginTransmission(address);
155160
wireWrite((byte)theRegister);
156-
Wire.endTransmission();
161+
Wire.endTransmission(stopTX); // default = true
157162
// do not set a value of 0
158163
if (i2cReadDelayTime > 0) {
159164
// delay is necessary for some devices such as WiiNunchuck
@@ -410,6 +415,7 @@ void reportDigitalCallback(byte port, int value)
410415
void sysexCallback(byte command, byte argc, byte *argv)
411416
{
412417
byte mode;
418+
byte stopTX;
413419
byte slaveAddress;
414420
byte data;
415421
int slaveRegister;
@@ -426,6 +432,14 @@ void sysexCallback(byte command, byte argc, byte *argv)
426432
slaveAddress = argv[0];
427433
}
428434

435+
// need to invert the logic here since 0 will be default for client
436+
// libraries that have not updated to add support for restart tx
437+
if (argv[1] & I2C_END_TX_MASK) {
438+
stopTX = I2C_RESTART_TX;
439+
}
440+
else {
441+
stopTX = I2C_STOP_TX; // default
442+
}
429443
switch (mode) {
430444
case I2C_WRITE:
431445
Wire.beginTransmission(slaveAddress);
@@ -447,7 +461,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
447461
slaveRegister = I2C_REGISTER_NOT_SPECIFIED;
448462
data = argv[2] + (argv[3] << 7); // bytes to read
449463
}
450-
readAndReportData(slaveAddress, (int)slaveRegister, data);
464+
readAndReportData(slaveAddress, (int)slaveRegister, data, stopTX);
451465
break;
452466
case I2C_READ_CONTINUOUSLY:
453467
if ((queryIndex + 1) >= I2C_MAX_QUERIES) {
@@ -469,6 +483,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
469483
query[queryIndex].addr = slaveAddress;
470484
query[queryIndex].reg = slaveRegister;
471485
query[queryIndex].bytes = data;
486+
query[queryIndex].stopTX = stopTX;
472487
break;
473488
case I2C_STOP_READING:
474489
byte queryIndexToSkip;
@@ -492,6 +507,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
492507
query[i].addr = query[i + 1].addr;
493508
query[i].reg = query[i + 1].reg;
494509
query[i].bytes = query[i + 1].bytes;
510+
query[i].stopTX = query[i + 1].stopTX;
495511
}
496512
}
497513
queryIndex--;
@@ -738,7 +754,7 @@ void loop()
738754
// report i2c data for all device with read continuous mode enabled
739755
if (queryIndex > -1) {
740756
for (byte i = 0; i < queryIndex + 1; i++) {
741-
readAndReportData(query[i].addr, query[i].reg, query[i].bytes);
757+
readAndReportData(query[i].addr, query[i].reg, query[i].bytes, query[i].stopTX);
742758
}
743759
}
744760
}

0 commit comments

Comments
 (0)