Skip to content

Commit a0dc3e0

Browse files
apply some changes from StandardFirmataEthernet to StandardFirmata
1 parent 979d46f commit a0dc3e0

File tree

2 files changed

+61
-57
lines changed

2 files changed

+61
-57
lines changed

examples/StandardFirmata/StandardFirmata.ino

Lines changed: 59 additions & 55 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,26 @@
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

3630
// 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
31+
#define I2C_WRITE B00000000
32+
#define I2C_READ B00001000
33+
#define I2C_READ_CONTINUOUSLY B00010000
34+
#define I2C_STOP_READING B00011000
35+
#define I2C_READ_WRITE_MODE_MASK B00011000
4236
#define I2C_10BIT_ADDRESS_MODE_MASK B00100000
37+
#define MAX_QUERIES 8
38+
#define REGISTER_NOT_SPECIFIED -1
4339

44-
#define MAX_QUERIES 8
40+
// the minimum interval for sampling analog input
4541
#define MINIMUM_SAMPLING_INTERVAL 10
4642

47-
#define REGISTER_NOT_SPECIFIED -1
4843

4944
/*==============================================================================
5045
* GLOBAL VARIABLES
@@ -65,7 +60,7 @@ int pinState[TOTAL_PINS]; // any value that has been written
6560
/* timer variables */
6661
unsigned long currentMillis; // store the current value from millis()
6762
unsigned long previousMillis; // for comparison with currentMillis
68-
unsigned int samplingInterval = 19; // how often to run the main loop (in ms)
63+
unsigned int samplingInterval = 19; // how often to run the main loop (in ms)
6964

7065
/* i2c data */
7166
struct i2c_device_info {
@@ -77,19 +72,38 @@ struct i2c_device_info {
7772
/* for i2c read continuous more */
7873
i2c_device_info query[MAX_QUERIES];
7974

80-
boolean isResetting = false;
81-
8275
byte i2cRxData[32];
8376
boolean isI2CEnabled = false;
8477
signed char queryIndex = -1;
85-
unsigned int i2cReadDelayTime = 0; // default delay time between i2c read request and Wire.requestFrom()
78+
// default delay time between i2c read request and Wire.requestFrom()
79+
unsigned int i2cReadDelayTime = 0;
8680

8781
Servo servos[MAX_SERVOS];
8882
byte servoPinMap[TOTAL_PINS];
8983
byte detachedServos[MAX_SERVOS];
9084
byte detachedServoCount = 0;
9185
byte servoCount = 0;
9286

87+
boolean isResetting = false;
88+
89+
/* utility functions */
90+
void wireWrite(byte data)
91+
{
92+
#if ARDUINO >= 100
93+
Wire.write((byte)data);
94+
#else
95+
Wire.send(data);
96+
#endif
97+
}
98+
99+
byte wireRead(void)
100+
{
101+
#if ARDUINO >= 100
102+
return Wire.read();
103+
#else
104+
return Wire.receive();
105+
#endif
106+
}
93107

94108
/*==============================================================================
95109
* FUNCTIONS
@@ -139,11 +153,7 @@ void readAndReportData(byte address, int theRegister, byte numBytes) {
139153
// do not always require the register read so upon interrupt you call Wire.requestFrom()
140154
if (theRegister != REGISTER_NOT_SPECIFIED) {
141155
Wire.beginTransmission(address);
142-
#if ARDUINO >= 100
143-
Wire.write((byte)theRegister);
144-
#else
145-
Wire.send((byte)theRegister);
146-
#endif
156+
wireWrite((byte)theRegister);
147157
Wire.endTransmission();
148158
// do not set a value of 0
149159
if (i2cReadDelayTime > 0) {
@@ -158,20 +168,16 @@ void readAndReportData(byte address, int theRegister, byte numBytes) {
158168

159169
// check to be sure correct number of bytes were returned by slave
160170
if (numBytes < Wire.available()) {
161-
Firmata.sendString("I2C Read Error: Too many bytes received");
171+
Firmata.sendString("I2C: Too many bytes received");
162172
} else if (numBytes > Wire.available()) {
163-
Firmata.sendString("I2C Read Error: Too few bytes received");
173+
Firmata.sendString("I2C: Too few bytes received");
164174
}
165175

166176
i2cRxData[0] = address;
167177
i2cRxData[1] = theRegister;
168178

169179
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
180+
i2cRxData[2 + i] = wireRead();
175181
}
176182

177183
// send slave address, register and received bytes
@@ -221,6 +227,9 @@ void checkDigitalInputs(void)
221227
*/
222228
void setPinModeCallback(byte pin, int mode)
223229
{
230+
if (pinConfig[pin] == IGNORE)
231+
return;
232+
224233
if (pinConfig[pin] == I2C && isI2CEnabled && mode != I2C) {
225234
// disable i2c so pins can be used for other functions
226235
// the following if statements should reconfigure the pins properly
@@ -246,15 +255,15 @@ void setPinModeCallback(byte pin, int mode)
246255
case ANALOG:
247256
if (IS_PIN_ANALOG(pin)) {
248257
if (IS_PIN_DIGITAL(pin)) {
249-
pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver
258+
pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver
250259
digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups
251260
}
252261
pinConfig[pin] = ANALOG;
253262
}
254263
break;
255264
case INPUT:
256265
if (IS_PIN_DIGITAL(pin)) {
257-
pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver
266+
pinMode(PIN_TO_DIGITAL(pin), INPUT); // disable output driver
258267
digitalWrite(PIN_TO_DIGITAL(pin), LOW); // disable internal pull-ups
259268
pinConfig[pin] = INPUT;
260269
}
@@ -409,11 +418,7 @@ void sysexCallback(byte command, byte argc, byte *argv)
409418
Wire.beginTransmission(slaveAddress);
410419
for (byte i = 2; i < argc; i += 2) {
411420
data = argv[i] + (argv[i + 1] << 7);
412-
#if ARDUINO >= 100
413-
Wire.write(data);
414-
#else
415-
Wire.send(data);
416-
#endif
421+
wireWrite(data);
417422
}
418423
Wire.endTransmission();
419424
delayMicroseconds(70);
@@ -541,19 +546,19 @@ void sysexCallback(byte command, byte argc, byte *argv)
541546
}
542547
if (IS_PIN_ANALOG(pin)) {
543548
Firmata.write(ANALOG);
544-
Firmata.write(10);
549+
Firmata.write(10); // 10 = 10-bit resolution
545550
}
546551
if (IS_PIN_PWM(pin)) {
547552
Firmata.write(PWM);
548-
Firmata.write(8);
553+
Firmata.write(8); // 8 = 8-bit resolution
549554
}
550555
if (IS_PIN_DIGITAL(pin)) {
551556
Firmata.write(SERVO);
552557
Firmata.write(14);
553558
}
554559
if (IS_PIN_I2C(pin)) {
555560
Firmata.write(I2C);
556-
Firmata.write(1); // to do: determine appropriate value
561+
Firmata.write(1); // TODO: could assign a number to map to SCL or SDA
557562
}
558563
Firmata.write(127);
559564
}
@@ -599,7 +604,6 @@ void enableI2CPins()
599604

600605
isI2CEnabled = true;
601606

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

@@ -617,14 +621,16 @@ void disableI2CPins() {
617621
void systemResetCallback()
618622
{
619623
isResetting = true;
624+
620625
// initialize a defalt state
621626
// TODO: option to load config from EEPROM instead of default
627+
622628
if (isI2CEnabled) {
623629
disableI2CPins();
624630
}
625631

626632
for (byte i = 0; i < TOTAL_PORTS; i++) {
627-
reportPINs[i] = false; // by default, reporting off
633+
reportPINs[i] = false; // by default, reporting off
628634
portConfigInputs[i] = 0; // until activated
629635
previousPINs[i] = 0;
630636
}
@@ -687,14 +693,12 @@ void loop()
687693
* FTDI buffer using Serial.print() */
688694
checkDigitalInputs();
689695

690-
/* SERIALREAD - processing incoming messagse as soon as possible, while still
696+
/* STREAMREAD - processing incoming messagse as soon as possible, while still
691697
* checking digital inputs. */
692698
while (Firmata.available())
693699
Firmata.processInput();
694700

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. */
701+
// TODO - ensure that Stream buffer doesn't go over 60 bytes
698702

699703
currentMillis = millis();
700704
if (currentMillis - previousMillis > samplingInterval) {

examples/StandardFirmataEthernet/StandardFirmataEthernet.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,11 +650,11 @@ void sysexCallback(byte command, byte argc, byte *argv)
650650
}
651651
if (IS_PIN_ANALOG(pin)) {
652652
Firmata.write(ANALOG);
653-
Firmata.write(10);
653+
Firmata.write(10); // 10 = 10-bit resolution
654654
}
655655
if (IS_PIN_PWM(pin)) {
656656
Firmata.write(PWM);
657-
Firmata.write(8);
657+
Firmata.write(8); // 8 = 8-bit resolution
658658
}
659659
if (IS_PIN_DIGITAL(pin)) {
660660
Firmata.write(SERVO);

0 commit comments

Comments
 (0)