Skip to content

Commit 3f5220e

Browse files
committed
Updating to Firmata-2.1beta1 (rev 23).
1 parent 17dd739 commit 3f5220e

File tree

10 files changed

+687
-373
lines changed

10 files changed

+687
-373
lines changed

libraries/Firmata/Firmata.cpp

Lines changed: 233 additions & 233 deletions
Large diffs are not rendered by default.

libraries/Firmata/Firmata.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* software can test whether it will be compatible with the currently
2323
* installed firmware. */
2424
#define FIRMATA_MAJOR_VERSION 2 // for non-compatible changes
25-
#define FIRMATA_MINOR_VERSION 0 // for backwards compatible changes
25+
#define FIRMATA_MINOR_VERSION 1 // for backwards compatible changes
2626
#define VERSION_BLINK_PIN 13 // digital pin to blink version on
2727

2828
#define MAX_DATA_BYTES 32 // max number of data bytes in non-Sysex messages
@@ -42,20 +42,31 @@
4242
#define END_SYSEX 0xF7 // end a MIDI Sysex message
4343

4444
// extended command set using sysex (0-127/0x00-0x7F)
45-
/* 0x00-0x0F reserved for custom commands */
45+
/* 0x00-0x0F reserved for user-defined commands */
4646
#define SERVO_CONFIG 0x70 // set max angle, minPulse, maxPulse, freq
47-
#define FIRMATA_STRING 0x71 // a string message with 14-bits per char
47+
#define STRING_DATA 0x71 // a string message with 14-bits per char
48+
#define SHIFT_DATA 0x75 // a bitstream to/from a shift register
49+
#define I2C_REQUEST 0x76 // send an I2C read/write request
50+
#define I2C_REPLY 0x77 // a reply to an I2C read request
51+
#define I2C_CONFIG 0x78 // config I2C settings such as delay times and power pins
4852
#define REPORT_FIRMWARE 0x79 // report name and version of the firmware
53+
#define SAMPLING_INTERVAL 0x7A // set the poll rate of the main loop
4954
#define SYSEX_NON_REALTIME 0x7E // MIDI Reserved for non-realtime messages
5055
#define SYSEX_REALTIME 0x7F // MIDI Reserved for realtime messages
56+
// these are DEPRECATED to make the naming more consistent
57+
#define FIRMATA_STRING 0x71 // same as STRING_DATA
58+
#define SYSEX_I2C_REQUEST 0x76 // same as I2C_REQUEST
59+
#define SYSEX_I2C_REPLY 0x77 // same as I2C_REPLY
60+
#define SYSEX_SAMPLING_INTERVAL 0x7A // same as SAMPLING_INTERVAL
5161

5262
// pin modes
5363
//#define INPUT 0x00 // defined in wiring.h
5464
//#define OUTPUT 0x01 // defined in wiring.h
5565
#define ANALOG 0x02 // analog pin in analogInput mode
5666
#define PWM 0x03 // digital pin in PWM output mode
5767
#define SERVO 0x04 // digital pin in Servo output mode
58-
68+
#define SHIFT 0x05 // shiftIn/shiftOut mode
69+
#define I2C 0x06 // pin included in I2C setup
5970

6071
extern "C" {
6172
// callback function types
@@ -151,6 +162,11 @@ extern FirmataClass Firmata;
151162
#define TOTAL_DIGITAL_PINS 20 // 14 digital + 6 analog
152163
#define TOTAL_PORTS 3 // total number of ports for the board
153164
#define ANALOG_PORT 2 // port# of analog used as digital
165+
#elif defined(__AVR_ATmega1280__)// Arduino Mega
166+
#define TOTAL_ANALOG_PINS 16
167+
#define TOTAL_DIGITAL_PINS 54
168+
#define TOTAL_PORTS 8 // total number of ports for the board
169+
#define ANALOG_PORT 2 // port# of analog used as digital
154170
#elif defined(__AVR_ATmega128__)// Wiring
155171
#define TOTAL_ANALOG_PINS 8
156172
#define TOTAL_DIGITAL_PINS 51

libraries/Firmata/examples/AnalogFirmata/AnalogFirmata.pde

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void setup()
6161

6262
servo9.attach(9);
6363
servo10.attach(10);
64-
Firmata.begin();
64+
Firmata.begin(57600);
6565
}
6666

6767
/*==============================================================================

libraries/Firmata/examples/EchoString/EchoString.pde

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ void sysexCallback(byte command, byte argc, byte*argv)
2525
void setup()
2626
{
2727
Firmata.setFirmwareVersion(0, 1);
28-
Firmata.attach(FIRMATA_STRING, stringCallback);
28+
Firmata.attach(STRING_DATA, stringCallback);
2929
Firmata.attach(START_SYSEX, sysexCallback);
30-
Firmata.begin();
30+
Firmata.begin(57600);
3131
}
3232

3333
void loop()
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/*
2+
Copyright (C) 2009 Jeff Hoefs. All rights reserved.
3+
Copyright (C) 2009 Shigeru Kobayashi. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
See file LICENSE.txt for further informations on licensing terms.
11+
*/
12+
13+
#include <Wire.h>
14+
#include <Firmata.h>
15+
16+
17+
#define I2C_WRITE B00000000
18+
#define I2C_READ B00001000
19+
#define I2C_READ_CONTINUOUSLY B00010000
20+
#define I2C_STOP_READING B00011000
21+
#define I2C_READ_WRITE_MODE_MASK B00011000
22+
23+
#define MAX_QUERIES 8
24+
25+
unsigned long currentMillis; // store the current value from millis()
26+
unsigned long nextExecuteMillis; // for comparison with currentMillis
27+
unsigned int samplingInterval = 32; // default sampling interval is 33ms
28+
unsigned int i2cReadDelayTime = 0; // default delay time between i2c read request and Wire.requestFrom()
29+
unsigned int powerPinsEnabled = 0; // use as boolean to prevent enablePowerPins from being called more than once
30+
31+
#define MINIMUM_SAMPLING_INTERVAL 10
32+
33+
#define REGISTER_NOT_SPECIFIED -1
34+
35+
struct i2c_device_info {
36+
byte addr;
37+
byte reg;
38+
byte bytes;
39+
};
40+
41+
i2c_device_info query[MAX_QUERIES];
42+
43+
byte i2cRxData[32];
44+
boolean readingContinuously = false;
45+
byte queryIndex = 0;
46+
47+
void readAndReportData(byte address, int theRegister, byte numBytes)
48+
{
49+
if (theRegister != REGISTER_NOT_SPECIFIED) {
50+
Wire.beginTransmission(address);
51+
Wire.send((byte)theRegister);
52+
Wire.endTransmission();
53+
delayMicroseconds(i2cReadDelayTime); // delay is necessary for some devices such as WiiNunchuck
54+
}
55+
else {
56+
theRegister = 0; // fill the register with a dummy value
57+
}
58+
59+
Wire.requestFrom(address, numBytes);
60+
61+
// check to be sure correct number of bytes were returned by slave
62+
if(numBytes == Wire.available()) {
63+
i2cRxData[0] = address;
64+
i2cRxData[1] = theRegister;
65+
for (int i = 0; i < numBytes; i++) {
66+
i2cRxData[2 + i] = Wire.receive();
67+
}
68+
// send slave address, register and received bytes
69+
Firmata.sendSysex(I2C_REPLY, numBytes + 2, i2cRxData);
70+
}
71+
else {
72+
if(numBytes > Wire.available()) {
73+
Firmata.sendString("I2C Read Error: Too many bytes received");
74+
} else {
75+
Firmata.sendString("I2C Read Error: Too few bytes received");
76+
}
77+
}
78+
79+
}
80+
81+
void sysexCallback(byte command, byte argc, byte *argv)
82+
{
83+
byte mode;
84+
byte slaveAddress;
85+
byte slaveRegister;
86+
byte data;
87+
int delayTime;
88+
89+
if (command == I2C_REQUEST) {
90+
mode = argv[1] & I2C_READ_WRITE_MODE_MASK;
91+
slaveAddress = argv[0];
92+
93+
switch(mode) {
94+
case I2C_WRITE:
95+
Wire.beginTransmission(slaveAddress);
96+
for (byte i = 2; i < argc; i += 2) {
97+
data = argv[i] + (argv[i + 1] << 7);
98+
Wire.send(data);
99+
}
100+
Wire.endTransmission();
101+
delayMicroseconds(70); // TODO is this needed?
102+
break;
103+
case I2C_READ:
104+
if (argc == 6) {
105+
// a slave register is specified
106+
slaveRegister = argv[2] + (argv[3] << 7);
107+
data = argv[4] + (argv[5] << 7); // bytes to read
108+
readAndReportData(slaveAddress, (int)slaveRegister, data);
109+
}
110+
else {
111+
// a slave register is NOT specified
112+
data = argv[2] + (argv[3] << 7); // bytes to read
113+
readAndReportData(slaveAddress, (int)REGISTER_NOT_SPECIFIED, data);
114+
}
115+
break;
116+
case I2C_READ_CONTINUOUSLY:
117+
if ((queryIndex + 1) >= MAX_QUERIES) {
118+
// too many queries, just ignore
119+
Firmata.sendString("too many queries");
120+
break;
121+
}
122+
query[queryIndex].addr = slaveAddress;
123+
query[queryIndex].reg = argv[2] + (argv[3] << 7);
124+
query[queryIndex].bytes = argv[4] + (argv[5] << 7);
125+
readingContinuously = true;
126+
queryIndex++;
127+
break;
128+
case I2C_STOP_READING:
129+
readingContinuously = false;
130+
queryIndex = 0;
131+
break;
132+
default:
133+
break;
134+
}
135+
}
136+
else if (command == SAMPLING_INTERVAL) {
137+
samplingInterval = argv[0] + (argv[1] << 7);
138+
139+
if (samplingInterval < MINIMUM_SAMPLING_INTERVAL) {
140+
samplingInterval = MINIMUM_SAMPLING_INTERVAL;
141+
}
142+
143+
samplingInterval -= 1;
144+
Firmata.sendString("sampling interval");
145+
}
146+
147+
else if (command == I2C_CONFIG) {
148+
delayTime = (argv[4] + (argv[5] << 7)); // MSB
149+
delayTime = (delayTime << 8) + (argv[2] + (argv[3] << 7)); // add LSB
150+
151+
if((argv[0] + (argv[1] << 7)) > 0) {
152+
enablePowerPins(PC3, PC2);
153+
}
154+
155+
if(delayTime > 0) {
156+
i2cReadDelayTime = delayTime;
157+
}
158+
159+
if(argc > 6) {
160+
// If you extend I2C_Config, handle your data here
161+
}
162+
163+
}
164+
}
165+
166+
void systemResetCallback()
167+
{
168+
readingContinuously = false;
169+
queryIndex = 0;
170+
}
171+
172+
/* reference: BlinkM_funcs.h by Tod E. Kurt, ThingM, http://thingm.com/ */
173+
// Enables Pins A2 and A3 to be used as GND and Power
174+
// so that I2C devices can be plugged directly
175+
// into Arduino header (pins A2 - A5)
176+
static void enablePowerPins(byte pwrpin, byte gndpin)
177+
{
178+
if(powerPinsEnabled == 0) {
179+
DDRC |= _BV(pwrpin) | _BV(gndpin);
180+
PORTC &=~ _BV(gndpin);
181+
PORTC |= _BV(pwrpin);
182+
powerPinsEnabled = 1;
183+
Firmata.sendString("Power pins enabled");
184+
delay(100);
185+
}
186+
}
187+
188+
void setup()
189+
{
190+
Firmata.setFirmwareVersion(2, 0);
191+
192+
Firmata.attach(START_SYSEX, sysexCallback);
193+
Firmata.attach(SYSTEM_RESET, systemResetCallback);
194+
195+
for (int i = 0; i < TOTAL_DIGITAL_PINS; ++i) {
196+
pinMode(i, OUTPUT);
197+
}
198+
199+
/* I2C data is not reliable at higher baud rates, you'll need to change the
200+
baud rate on the host computer as well. To get a firmware running with
201+
minimal effort, you can try using the default baud rate (115200) */
202+
Firmata.begin(38400);
203+
Wire.begin();
204+
}
205+
206+
void loop()
207+
{
208+
while (Firmata.available()) {
209+
Firmata.processInput();
210+
}
211+
212+
currentMillis = millis();
213+
if (currentMillis > nextExecuteMillis) {
214+
nextExecuteMillis = currentMillis + samplingInterval;
215+
216+
for (byte i = 0; i < queryIndex; i++) {
217+
readAndReportData(query[i].addr, query[i].reg, query[i].bytes);
218+
}
219+
}
220+
}

libraries/Firmata/examples/ServoFirmata/ServoFirmata.pde

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void setup()
2828
servo9.attach(9);
2929
servo10.attach(10);
3030

31-
Firmata.begin();
31+
Firmata.begin(57600);
3232
}
3333

3434
void loop()

libraries/Firmata/examples/SimpleAnalogFirmata/SimpleAnalogFirmata.pde

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void setup()
1616
{
1717
Firmata.setFirmwareVersion(0, 1);
1818
Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
19-
Firmata.begin();
19+
Firmata.begin(57600);
2020
}
2121

2222
void loop()

libraries/Firmata/examples/SimpleDigitalFirmata/SimpleDigitalFirmata.pde

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void setup()
4545
Firmata.setFirmwareVersion(0, 1);
4646
Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
4747
Firmata.attach(SET_PIN_MODE, setPinModeCallback);
48-
Firmata.begin();
48+
Firmata.begin(57600);
4949
}
5050

5151
void loop()

libraries/Firmata/examples/StandardFirmata/Makefile

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,20 @@ TARGET := $(shell pwd | sed 's|.*/\(.*\)|\1|')
5050
ARDUINO = /Applications/arduino
5151
ARDUINO_SRC = $(ARDUINO)/hardware/cores/arduino
5252
ARDUINO_LIB_SRC = $(ARDUINO)/hardware/libraries
53+
ARDUINO_TOOLS = $(ARDUINO)/hardware/tools
5354
INCLUDE = -I$(ARDUINO_SRC) -I$(ARDUINO)/hardware/tools/avr/avr/include \
5455
-I$(ARDUINO_LIB_SRC)/EEPROM \
5556
-I$(ARDUINO_LIB_SRC)/Firmata \
57+
-I$(ARDUINO_LIB_SRC)/Matrix \
58+
-I$(ARDUINO_LIB_SRC)/Servo \
59+
-I$(ARDUINO_LIB_SRC)/Wire \
5660
-I$(ARDUINO_LIB_SRC)
5761
SRC = $(wildcard $(ARDUINO_SRC)/*.c)
5862
CXXSRC = applet/$(TARGET).cpp $(ARDUINO_SRC)/HardwareSerial.cpp \
5963
$(ARDUINO_LIB_SRC)/EEPROM/EEPROM.cpp \
6064
$(ARDUINO_LIB_SRC)/Firmata/Firmata.cpp \
65+
$(ARDUINO_LIB_SRC)/Servo/Servo.cpp \
66+
$(ARDUINO_SRC)/Print.cpp \
6167
$(ARDUINO_SRC)/WMath.cpp
6268
HEADERS = $(wildcard $(ARDUINO_SRC)/*.h) $(wildcard $(ARDUINO_LIB_SRC)/*/*.h)
6369

@@ -106,12 +112,14 @@ AVRDUDE_FLAGS = -F -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) \
106112
-b $(UPLOAD_RATE) -q -V
107113

108114
# Program settings
109-
CC = avr-gcc
110-
CXX = avr-g++
111-
OBJCOPY = avr-objcopy
112-
OBJDUMP = avr-objdump
113-
SIZE = avr-size
114-
NM = avr-nm
115+
ARDUINO_AVR_BIN = $(ARDUINO_TOOLS)/avr/bin
116+
CC = $(ARDUINO_AVR_BIN)/avr-gcc
117+
CXX = $(ARDUINO_AVR_BIN)/avr-g++
118+
OBJCOPY = $(ARDUINO_AVR_BIN)/avr-objcopy
119+
OBJDUMP = $(ARDUINO_AVR_BIN)/avr-objdump
120+
SIZE = $(ARDUINO_AVR_BIN)/avr-size
121+
NM = $(ARDUINO_AVR_BIN)/avr-nm
122+
#AVRDUDE = $(ARDUINO_AVR_BIN)/avrdude
115123
AVRDUDE = avrdude
116124
REMOVE = rm -f
117125
MV = mv -f
@@ -204,7 +212,8 @@ applet/$(TARGET).cpp: $(TARGET).pde
204212

205213
# Link: create ELF output file from object files.
206214
applet/$(TARGET).elf: applet/$(TARGET).cpp $(OBJ)
207-
$(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS)
215+
$(CC) $(ALL_CFLAGS) $(OBJ) -lm --output $@ $(LDFLAGS)
216+
# $(CC) $(ALL_CFLAGS) $(OBJ) $(ARDUINO_TOOLS)/avr/avr/lib/avr5/crtm168.o --output $@ $(LDFLAGS)
208217

209218
pd_close_serial:
210219
echo 'close;' | /Applications/Pd-extended.app/Contents/Resources/bin/pdsend 34567 || true
@@ -258,4 +267,7 @@ etags_MINGW:
258267
# etags -a /usr/include/*.h /usr/include/sys/*.h
259268

260269

270+
path:
271+
echo $(PATH)
272+
echo $$PATH
261273

0 commit comments

Comments
 (0)