Skip to content

Commit ee4903e

Browse files
enable option to auto restart i2c transmission per issue #82
1 parent 1299ac5 commit ee4903e

File tree

2 files changed

+40
-27
lines changed

2 files changed

+40
-27
lines changed

Firmata.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ void FirmataClass::processInput(void)
249249
break;
250250
}
251251
executeMultiByteCommand = 0;
252-
}
252+
}
253253
} else {
254254
// remove channel info from command byte if less than 0xF0
255255
if(inputData < 0xF0) {

examples/StandardFirmata/StandardFirmata.ino

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
* from software on a host computer. It is intended to work with
44
* any host computer software package.
55
*
6-
* To download a host software package, please clink on the following link
7-
* to open the download page in your default browser.
6+
* To download a host software package, please clink on one of the following
7+
* links for a list of Firmata client software packages.
88
*
9+
* http://github.com/firmata/arduino (clients listed in README.md)
910
* http://firmata.org/wiki/Download
1011
*/
1112

@@ -25,26 +26,25 @@
2526
formatted using the GNU C formatting and indenting
2627
*/
2728

28-
/*
29-
* TODO: use Program Control to load stored profiles from EEPROM
30-
*/
31-
3229
#include <Servo.h>
3330
#include <Wire.h>
3431
#include <Firmata.h>
3532

3633
// move the following defines to Firmata.h?
37-
#define I2C_WRITE B00000000
38-
#define I2C_READ B00001000
39-
#define I2C_READ_CONTINUOUSLY B00010000
40-
#define I2C_STOP_READING B00011000
41-
#define I2C_READ_WRITE_MODE_MASK B00011000
42-
#define I2C_10BIT_ADDRESS_MODE_MASK B00100000
34+
#define I2C_WRITE B00000000
35+
#define I2C_READ B00001000
36+
#define I2C_READ_CONTINUOUSLY B00010000
37+
#define I2C_STOP_READING B00011000
38+
#define I2C_READ_WRITE_MODE_MASK B00011000
39+
#define I2C_10BIT_ADDRESS_MODE_MASK B00100000
40+
#define I2C_END_TX_MASK B01000000
41+
#define I2C_STOP_TX 1
42+
#define I2C_RESTART_TX 0
4343

44-
#define MAX_QUERIES 8
45-
#define MINIMUM_SAMPLING_INTERVAL 10
44+
#define MAX_QUERIES 8
45+
#define MINIMUM_SAMPLING_INTERVAL 10
4646

47-
#define REGISTER_NOT_SPECIFIED -1
47+
#define REGISTER_NOT_SPECIFIED -1
4848

4949
/*==============================================================================
5050
* GLOBAL VARIABLES
@@ -65,13 +65,14 @@ int pinState[TOTAL_PINS]; // any value that has been written
6565
/* timer variables */
6666
unsigned long currentMillis; // store the current value from millis()
6767
unsigned long previousMillis; // for comparison with currentMillis
68-
unsigned int samplingInterval = 19; // how often to run the main loop (in ms)
68+
unsigned int samplingInterval = 19; // how often to run the main loop (in ms)
6969

7070
/* i2c data */
7171
struct i2c_device_info {
7272
byte addr;
7373
byte reg;
7474
byte bytes;
75+
byte stopTX;
7576
};
7677

7778
/* for i2c read continuous more */
@@ -87,7 +88,7 @@ Servo servos[MAX_SERVOS];
8788
* FUNCTIONS
8889
*============================================================================*/
8990

90-
void readAndReportData(byte address, int theRegister, byte numBytes) {
91+
void readAndReportData(byte address, int theRegister, byte numBytes, byte stopTX) {
9192
// allow I2C requests that don't require a register read
9293
// for example, some devices using an interrupt pin to signify new data available
9394
// do not always require the register read so upon interrupt you call Wire.requestFrom()
@@ -98,7 +99,7 @@ void readAndReportData(byte address, int theRegister, byte numBytes) {
9899
#else
99100
Wire.send((byte)theRegister);
100101
#endif
101-
Wire.endTransmission();
102+
Wire.endTransmission(stopTX); // default = true
102103
// do not set a value of 0
103104
if (i2cReadDelayTime > 0) {
104105
// delay is necessary for some devices such as WiiNunchuck
@@ -328,6 +329,7 @@ void reportDigitalCallback(byte port, int value)
328329
void sysexCallback(byte command, byte argc, byte *argv)
329330
{
330331
byte mode;
332+
byte stopTX;
331333
byte slaveAddress;
332334
byte slaveRegister;
333335
byte data;
@@ -344,6 +346,15 @@ void sysexCallback(byte command, byte argc, byte *argv)
344346
slaveAddress = argv[0];
345347
}
346348

349+
// need to invert the logic here since 0 will be default for client
350+
// libraries that have not updated to add support for restart tx
351+
if (argv[1] & I2C_END_TX_MASK) {
352+
stopTX = I2C_RESTART_TX;
353+
}
354+
else {
355+
stopTX = I2C_STOP_TX; // default
356+
}
357+
347358
switch(mode) {
348359
case I2C_WRITE:
349360
Wire.beginTransmission(slaveAddress);
@@ -363,12 +374,12 @@ void sysexCallback(byte command, byte argc, byte *argv)
363374
// a slave register is specified
364375
slaveRegister = argv[2] + (argv[3] << 7);
365376
data = argv[4] + (argv[5] << 7); // bytes to read
366-
readAndReportData(slaveAddress, (int)slaveRegister, data);
377+
readAndReportData(slaveAddress, (int)slaveRegister, data, stopTX);
367378
}
368379
else {
369380
// a slave register is NOT specified
370381
data = argv[2] + (argv[3] << 7); // bytes to read
371-
readAndReportData(slaveAddress, (int)REGISTER_NOT_SPECIFIED, data);
382+
readAndReportData(slaveAddress, (int)REGISTER_NOT_SPECIFIED, data, stopTX);
372383
}
373384
break;
374385
case I2C_READ_CONTINUOUSLY:
@@ -381,6 +392,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
381392
query[queryIndex].addr = slaveAddress;
382393
query[queryIndex].reg = argv[2] + (argv[3] << 7);
383394
query[queryIndex].bytes = argv[4] + (argv[5] << 7);
395+
query[queryIndex].stopTX = stopTX;
384396
break;
385397
case I2C_STOP_READING:
386398
byte queryIndexToSkip;
@@ -404,6 +416,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
404416
query[i].addr = query[i+1].addr;
405417
query[i].reg = query[i+1].addr;
406418
query[i].bytes = query[i+1].bytes;
419+
query[i].stopTX = query[i+1].stopTX;
407420
}
408421
}
409422
queryIndex--;
@@ -534,11 +547,11 @@ void enableI2CPins()
534547

535548
/* disable the i2c pins so they can be used for other functions */
536549
void disableI2CPins() {
537-
isI2CEnabled = false;
538-
// disable read continuous mode for all devices
539-
queryIndex = -1;
540-
// uncomment the following if or when the end() method is added to Wire library
541-
// Wire.end();
550+
isI2CEnabled = false;
551+
// disable read continuous mode for all devices
552+
queryIndex = -1;
553+
// uncomment the following if or when the end() method is added to Wire library
554+
// Wire.end();
542555
}
543556

544557
/*==============================================================================
@@ -633,7 +646,7 @@ void loop()
633646
// report i2c data for all device with read continuous mode enabled
634647
if (queryIndex > -1) {
635648
for (byte i = 0; i < queryIndex + 1; i++) {
636-
readAndReportData(query[i].addr, query[i].reg, query[i].bytes);
649+
readAndReportData(query[i].addr, query[i].reg, query[i].bytes, query[i].stopTX);
637650
}
638651
}
639652
}

0 commit comments

Comments
 (0)