Skip to content

Commit 201af59

Browse files
Merge pull request firmata#191 from firmata/ethernet
Add StandardFirmataEthernet example
2 parents b1a39bc + 4534570 commit 201af59

File tree

12 files changed

+1225
-150
lines changed

12 files changed

+1225
-150
lines changed

Firmata.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Firmata.cpp - Firmata library v2.4.2 - 2015-3-16
2+
Firmata.cpp - Firmata library v2.4.3 - 2015-4-11
33
Copyright (C) 2006-2008 Hans-Christoph Steiner. All rights reserved.
44
55
This library is free software; you can redistribute it and/or

Firmata.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Firmata.h - Firmata library v2.4.2 - 2015-3-16
2+
Firmata.h - Firmata library v2.4.3 - 2015-4-11
33
Copyright (C) 2006-2008 Hans-Christoph Steiner. All rights reserved.
44
55
This library is free software; you can redistribute it and/or
@@ -21,7 +21,7 @@
2121
* installed firmware. */
2222
#define FIRMATA_MAJOR_VERSION 2 // for non-compatible changes
2323
#define FIRMATA_MINOR_VERSION 4 // for backwards compatible changes
24-
#define FIRMATA_BUGFIX_VERSION 2 // for bugfix releases
24+
#define FIRMATA_BUGFIX_VERSION 3 // for bugfix releases
2525

2626
#define MAX_DATA_BYTES 64 // max number of data bytes in incoming messages
2727

examples/StandardFirmata/StandardFirmata.ino

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
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
@@ -22,29 +20,25 @@
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 <Servo.h>
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 */
6660
unsigned long currentMillis; // store the current value from millis()
6761
unsigned 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 */
7165
struct i2c_device_info {
@@ -77,19 +71,38 @@ struct i2c_device_info {
7771
/* for i2c read continuous more */
7872
i2c_device_info query[MAX_QUERIES];
7973

80-
boolean isResetting = false;
81-
8274
byte i2cRxData[32];
8375
boolean isI2CEnabled = false;
8476
signed 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

8780
Servo servos[MAX_SERVOS];
8881
byte servoPinMap[TOTAL_PINS];
8982
byte detachedServos[MAX_SERVOS];
9083
byte detachedServoCount = 0;
9184
byte 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
@@ -221,6 +226,9 @@ void checkDigitalInputs(void)
221226
*/
222227
void setPinModeCallback(byte pin, int mode)
223228
{
229+
if (pinConfig[pin] == IGNORE)
230+
return;
231+
224232
if (pinConfig[pin] == I2C && isI2CEnabled && mode != I2C) {
225233
// disable i2c so pins can be used for other functions
226234
// the following if statements should reconfigure the pins properly
@@ -246,15 +254,15 @@ void setPinModeCallback(byte pin, int mode)
246254
case ANALOG:
247255
if (IS_PIN_ANALOG(pin)) {
248256
if (IS_PIN_DIGITAL(pin)) {
249-
pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver
257+
pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver
250258
digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups
251259
}
252260
pinConfig[pin] = ANALOG;
253261
}
254262
break;
255263
case INPUT:
256264
if (IS_PIN_DIGITAL(pin)) {
257-
pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver
265+
pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver
258266
digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups
259267
pinConfig[pin] = INPUT;
260268
}
@@ -409,11 +417,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
409417
Wire.beginTransmission(slaveAddress);
410418
for (byte i = 2; i < argc; i += 2) {
411419
data = argv[i] + (argv[i + 1] << 7);
412-
#if ARDUINO >= 100
413-
Wire.write(data);
414-
#else
415-
Wire.send(data);
416-
#endif
420+
wireWrite(data);
417421
}
418422
Wire.endTransmission();
419423
delayMicroseconds(70);
@@ -541,19 +545,19 @@ void sysexCallback(byte command, byte argc, byte *argv)
541545
}
542546
if (IS_PIN_ANALOG(pin)) {
543547
Firmata.write(ANALOG);
544-
Firmata.write(10);
548+
Firmata.write(10); // 10 = 10-bit resolution
545549
}
546550
if (IS_PIN_PWM(pin)) {
547551
Firmata.write(PWM);
548-
Firmata.write(8);
552+
Firmata.write(8); // 8 = 8-bit resolution
549553
}
550554
if (IS_PIN_DIGITAL(pin)) {
551555
Firmata.write(SERVO);
552556
Firmata.write(14);
553557
}
554558
if (IS_PIN_I2C(pin)) {
555559
Firmata.write(I2C);
556-
Firmata.write(1); // to do: determine appropriate value
560+
Firmata.write(1); // TODO: could assign a number to map to SCL or SDA
557561
}
558562
Firmata.write(127);
559563
}
@@ -599,7 +603,6 @@ void enableI2CPins()
599603

600604
isI2CEnabled = true;
601605

602-
// is there enough time before the first I2C request to call this here?
603606
Wire.begin();
604607
}
605608

@@ -617,14 +620,16 @@ void disableI2CPins() {
617620
void systemResetCallback()
618621
{
619622
isResetting = true;
623+
620624
// initialize a defalt state
621625
// TODO: option to load config from EEPROM instead of default
626+
622627
if (isI2CEnabled) {
623628
disableI2CPins();
624629
}
625630

626631
for (byte i = 0; i < TOTAL_PORTS; i++) {
627-
reportPINs[i] = false; // by default, reporting off
632+
reportPINs[i] = false; // by default, reporting off
628633
portConfigInputs[i] = 0; // until activated
629634
previousPINs[i] = 0;
630635
}
@@ -687,14 +692,12 @@ void loop()
687692
* FTDI buffer using Serial.print() */
688693
checkDigitalInputs();
689694

690-
/* SERIALREAD - processing incoming messagse as soon as possible, while still
695+
/* STREAMREAD - processing incoming messagse as soon as possible, while still
691696
* checking digital inputs. */
692697
while (Firmata.available())
693698
Firmata.processInput();
694699

695-
/* SEND FTDI WRITE BUFFER - make sure that the FTDI buffer doesn't go over
696-
* 60 bytes. use a timer to sending an event character every 4 ms to
697-
* trigger the buffer to dump. */
700+
// TODO - ensure that Stream buffer doesn't go over 60 bytes
698701

699702
currentMillis = millis();
700703
if (currentMillis - previousMillis > samplingInterval) {

0 commit comments

Comments
 (0)