Skip to content

Commit 5fc9d72

Browse files
enable option to auto restart i2c transmission per issue firmata#82
1 parent 2c82cc1 commit 5fc9d72

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

examples/StandardFirmata/StandardFirmata.ino

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -435,6 +440,7 @@ void reportDigitalCallback(byte port, int value)
435440
void sysexCallback(byte command, byte argc, byte *argv)
436441
{
437442
byte mode;
443+
byte stopTX;
438444
byte slaveAddress;
439445
byte data;
440446
int slaveRegister;
@@ -451,6 +457,14 @@ void sysexCallback(byte command, byte argc, byte *argv)
451457
slaveAddress = argv[0];
452458
}
453459

460+
// need to invert the logic here since 0 will be default for client
461+
// libraries that have not updated to add support for restart tx
462+
if (argv[1] & I2C_END_TX_MASK) {
463+
stopTX = I2C_RESTART_TX;
464+
}
465+
else {
466+
stopTX = I2C_STOP_TX; // default
467+
}
454468
switch (mode) {
455469
case I2C_WRITE:
456470
Wire.beginTransmission(slaveAddress);
@@ -472,7 +486,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
472486
slaveRegister = I2C_REGISTER_NOT_SPECIFIED;
473487
data = argv[2] + (argv[3] << 7); // bytes to read
474488
}
475-
readAndReportData(slaveAddress, (int)slaveRegister, data);
489+
readAndReportData(slaveAddress, (int)slaveRegister, data, stopTX);
476490
break;
477491
case I2C_READ_CONTINUOUSLY:
478492
if ((queryIndex + 1) >= I2C_MAX_QUERIES) {
@@ -494,6 +508,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
494508
query[queryIndex].addr = slaveAddress;
495509
query[queryIndex].reg = slaveRegister;
496510
query[queryIndex].bytes = data;
511+
query[queryIndex].stopTX = stopTX;
497512
break;
498513
case I2C_STOP_READING:
499514
byte queryIndexToSkip;
@@ -517,6 +532,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
517532
query[i].addr = query[i + 1].addr;
518533
query[i].reg = query[i + 1].reg;
519534
query[i].bytes = query[i + 1].bytes;
535+
query[i].stopTX = query[i + 1].stopTX;
520536
}
521537
}
522538
queryIndex--;
@@ -765,7 +781,7 @@ void loop()
765781
// report i2c data for all device with read continuous mode enabled
766782
if (queryIndex > -1) {
767783
for (byte i = 0; i < queryIndex + 1; i++) {
768-
readAndReportData(query[i].addr, query[i].reg, query[i].bytes);
784+
readAndReportData(query[i].addr, query[i].reg, query[i].bytes, query[i].stopTX);
769785
}
770786
}
771787
}

0 commit comments

Comments
 (0)