11/*
2- * Firmata is a generic protocol for communicating with microcontrollers
3- * from software on a host computer. It is intended to work with
4- * any host computer software package.
5- *
6- * To download a host software package, please clink on the following link
7- * to open the download page in your default browser.
8- *
9- * http://firmata.org/wiki/Download
10- */
2+ Firmata is a generic protocol for communicating with microcontrollers
3+ from software on a host computer. It is intended to work with
4+ any host computer software package.
5+
6+ To download a host software package, please clink on the following link
7+ to open the download page in your default browser.
8+
9+ https://github.com/firmata/arduino#firmata-client-libraries
1110
12- /*
1311 Copyright (C) 2006-2008 Hans-Christoph Steiner. All rights reserved.
1412 Copyright (C) 2010-2011 Paul Stoffregen. All rights reserved.
1513 Copyright (C) 2009 Shigeru Kobayashi. All rights reserved.
16- Copyright (C) 2009-2014 Jeff Hoefs. All rights reserved.
14+ Copyright (C) 2009-2015 Jeff Hoefs. All rights reserved.
1715
1816 This library is free software; you can redistribute it and/or
1917 modify it under the terms of the GNU Lesser General Public
2220
2321 See file LICENSE.txt for further informations on licensing terms.
2422
25- formatted using the GNU C formatting and indenting
23+ Last updated by Jeff Hoefs: April 11, 2015
2624*/
2725
28- /*
29- * TODO: use Program Control to load stored profiles from EEPROM
30- */
31-
3226#include < SoftPWMServo.h> // Gives us PWM and Servo on every pin
3327#include < Wire.h>
3428#include < Firmata.h>
3529
36- // 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
30+ #define I2C_WRITE B00000000
31+ #define I2C_READ B00001000
32+ #define I2C_READ_CONTINUOUSLY B00010000
33+ #define I2C_STOP_READING B00011000
34+ #define I2C_READ_WRITE_MODE_MASK B00011000
4235#define I2C_10BIT_ADDRESS_MODE_MASK B00100000
36+ #define MAX_QUERIES 8
37+ #define REGISTER_NOT_SPECIFIED -1
4338
44- # define MAX_QUERIES 8
39+ // the minimum interval for sampling analog input
4540#define MINIMUM_SAMPLING_INTERVAL 10
4641
47- #define REGISTER_NOT_SPECIFIED -1
4842
4943/* ==============================================================================
5044 * GLOBAL VARIABLES
@@ -65,7 +59,7 @@ int pinState[TOTAL_PINS]; // any value that has been written
6559/* timer variables */
6660unsigned long currentMillis; // store the current value from millis()
6761unsigned long previousMillis; // for comparison with currentMillis
68- unsigned int samplingInterval = 19 ; // how often to run the main loop (in ms)
62+ unsigned int samplingInterval = 19 ; // how often to run the main loop (in ms)
6963
7064/* i2c data */
7165struct i2c_device_info {
@@ -77,19 +71,38 @@ struct i2c_device_info {
7771/* for i2c read continuous more */
7872i2c_device_info query[MAX_QUERIES];
7973
80- boolean isResetting = false ;
81-
8274byte i2cRxData[32 ];
8375boolean isI2CEnabled = false ;
8476signed char queryIndex = -1 ;
85- unsigned int i2cReadDelayTime = 0 ; // default delay time between i2c read request and Wire.requestFrom()
77+ // default delay time between i2c read request and Wire.requestFrom()
78+ unsigned int i2cReadDelayTime = 0 ;
8679
8780SoftServo servos[MAX_SERVOS];
8881byte servoPinMap[TOTAL_PINS];
8982byte detachedServos[MAX_SERVOS];
9083byte detachedServoCount = 0 ;
9184byte servoCount = 0 ;
9285
86+ boolean isResetting = false ;
87+
88+ /* utility functions */
89+ void wireWrite (byte data)
90+ {
91+ #if ARDUINO >= 100
92+ Wire.write ((byte)data);
93+ #else
94+ Wire.send (data);
95+ #endif
96+ }
97+
98+ byte wireRead (void )
99+ {
100+ #if ARDUINO >= 100
101+ return Wire.read ();
102+ #else
103+ return Wire.receive ();
104+ #endif
105+ }
93106
94107/* ==============================================================================
95108 * FUNCTIONS
@@ -139,11 +152,7 @@ void readAndReportData(byte address, int theRegister, byte numBytes) {
139152 // do not always require the register read so upon interrupt you call Wire.requestFrom()
140153 if (theRegister != REGISTER_NOT_SPECIFIED) {
141154 Wire.beginTransmission (address);
142- #if ARDUINO >= 100
143- Wire.write ((byte)theRegister);
144- #else
145- Wire.send ((byte)theRegister);
146- #endif
155+ wireWrite ((byte)theRegister);
147156 Wire.endTransmission ();
148157 // do not set a value of 0
149158 if (i2cReadDelayTime > 0 ) {
@@ -158,20 +167,16 @@ void readAndReportData(byte address, int theRegister, byte numBytes) {
158167
159168 // check to be sure correct number of bytes were returned by slave
160169 if (numBytes < Wire.available ()) {
161- Firmata.sendString (" I2C Read Error : Too many bytes received" );
170+ Firmata.sendString (" I2C: Too many bytes received" );
162171 } else if (numBytes > Wire.available ()) {
163- Firmata.sendString (" I2C Read Error : Too few bytes received" );
172+ Firmata.sendString (" I2C: Too few bytes received" );
164173 }
165174
166175 i2cRxData[0 ] = address;
167176 i2cRxData[1 ] = theRegister;
168177
169178 for (int i = 0 ; i < numBytes && Wire.available (); i++) {
170- #if ARDUINO >= 100
171- i2cRxData[2 + i] = Wire.read ();
172- #else
173- i2cRxData[2 + i] = Wire.receive ();
174- #endif
179+ i2cRxData[2 + i] = wireRead ();
175180 }
176181
177182 // send slave address, register and received bytes
@@ -231,6 +236,9 @@ void servoWrite(byte pin, int value)
231236 */
232237void setPinModeCallback (byte pin, int mode)
233238{
239+ if (pinConfig[pin] == IGNORE)
240+ return ;
241+
234242 if (pinConfig[pin] == I2C && isI2CEnabled && mode != I2C) {
235243 // disable i2c so pins can be used for other functions
236244 // the following if statements should reconfigure the pins properly
@@ -256,15 +264,15 @@ void setPinModeCallback(byte pin, int mode)
256264 case ANALOG:
257265 if (IS_PIN_ANALOG (pin)) {
258266 if (IS_PIN_DIGITAL (pin)) {
259- pinMode (PIN_TO_DIGITAL (pin), INPUT); // disable output driver
267+ pinMode (PIN_TO_DIGITAL (pin), INPUT); // disable output driver
260268 digitalWrite (PIN_TO_DIGITAL (pin), LOW); // disable internal pull-ups
261269 }
262270 pinConfig[pin] = ANALOG;
263271 }
264272 break ;
265273 case INPUT:
266274 if (IS_PIN_DIGITAL (pin)) {
267- pinMode (PIN_TO_DIGITAL (pin), INPUT); // disable output driver
275+ pinMode (PIN_TO_DIGITAL (pin), INPUT); // disable output driver
268276 digitalWrite (PIN_TO_DIGITAL (pin), LOW); // disable internal pull-ups
269277 pinConfig[pin] = INPUT;
270278 }
@@ -419,11 +427,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
419427 Wire.beginTransmission (slaveAddress);
420428 for (byte i = 2 ; i < argc; i += 2 ) {
421429 data = argv[i] + (argv[i + 1 ] << 7 );
422- #if ARDUINO >= 100
423- Wire.write (data);
424- #else
425- Wire.send (data);
426- #endif
430+ wireWrite (data);
427431 }
428432 Wire.endTransmission ();
429433 delayMicroseconds (70 );
@@ -551,19 +555,19 @@ void sysexCallback(byte command, byte argc, byte *argv)
551555 }
552556 if (IS_PIN_ANALOG (pin)) {
553557 Firmata.write (ANALOG);
554- Firmata.write (10 );
558+ Firmata.write (10 ); // 10 = 10-bit resolution
555559 }
556560 if (IS_PIN_PWM (pin)) {
557561 Firmata.write (PWM);
558- Firmata.write (8 );
562+ Firmata.write (8 ); // 8 = 8-bit resolution
559563 }
560564 if (IS_PIN_DIGITAL (pin)) {
561565 Firmata.write (SERVO);
562566 Firmata.write (14 );
563567 }
564568 if (IS_PIN_I2C (pin)) {
565569 Firmata.write (I2C);
566- Firmata.write (1 ); // to do: determine appropriate value
570+ Firmata.write (1 ); // TODO: could assign a number to map to SCL or SDA
567571 }
568572 Firmata.write (127 );
569573 }
@@ -609,7 +613,6 @@ void enableI2CPins()
609613
610614 isI2CEnabled = true ;
611615
612- // is there enough time before the first I2C request to call this here?
613616 Wire.begin ();
614617}
615618
@@ -634,7 +637,7 @@ void systemResetCallback()
634637 }
635638
636639 for (byte i = 0 ; i < TOTAL_PORTS; i++) {
637- reportPINs[i] = false ; // by default, reporting off
640+ reportPINs[i] = false ; // by default, reporting off
638641 portConfigInputs[i] = 0 ; // until activated
639642 previousPINs[i] = 0 ;
640643 }
@@ -703,14 +706,12 @@ void loop()
703706 * FTDI buffer using Serial.print() */
704707 checkDigitalInputs ();
705708
706- /* SERIALREAD - processing incoming messagse as soon as possible, while still
709+ /* STREAMREAD - processing incoming messagse as soon as possible, while still
707710 * checking digital inputs. */
708711 while (Firmata.available ())
709712 Firmata.processInput ();
710713
711- /* SEND FTDI WRITE BUFFER - make sure that the FTDI buffer doesn't go over
712- * 60 bytes. use a timer to sending an event character every 4 ms to
713- * trigger the buffer to dump. */
714+ // TODO - ensure that Stream buffer doesn't go over 60 bytes
714715
715716 currentMillis = millis ();
716717 if (currentMillis - previousMillis > samplingInterval) {
0 commit comments