Skip to content

Commit 41097ce

Browse files
authored
Merge pull request #1 from firmata/master
merge upstream changes
2 parents 162f8fa + 9caa095 commit 41097ce

File tree

159 files changed

+16980
-1589
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

159 files changed

+16980
-1589
lines changed

Boards.h

+258-5
Large diffs are not rendered by default.

Firmata.cpp

+94-208
Large diffs are not rendered by default.

Firmata.h

+65-109
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
Firmata.h - Firmata library v2.5.2 - 2016-2-15
2+
Firmata.h - Firmata library v2.5.8 - 2018-04-15
33
Copyright (c) 2006-2008 Hans-Christoph Steiner. All rights reserved.
4-
Copyright (C) 2009-2015 Jeff Hoefs. All rights reserved.
4+
Copyright (C) 2009-2017 Jeff Hoefs. All rights reserved.
55
66
This library is free software; you can redistribute it and/or
77
modify it under the terms of the GNU Lesser General Public
@@ -15,22 +15,9 @@
1515
#define Firmata_h
1616

1717
#include "Boards.h" /* Hardware Abstraction Layer + Wiring/Arduino */
18-
19-
/* Version numbers for the protocol. The protocol is still changing, so these
20-
* version numbers are important.
21-
* Query using the REPORT_VERSION message.
22-
*/
23-
#define FIRMATA_PROTOCOL_MAJOR_VERSION 2 // for non-compatible changes
24-
#define FIRMATA_PROTOCOL_MINOR_VERSION 5 // for backwards compatible changes
25-
#define FIRMATA_PROTOCOL_BUGFIX_VERSION 1 // for bugfix releases
26-
27-
/* Version numbers for the Firmata library.
28-
* The firmware version will not always equal the protocol version going forward.
29-
* Query using the REPORT_FIRMWARE message.
30-
*/
31-
#define FIRMATA_FIRMWARE_MAJOR_VERSION 2
32-
#define FIRMATA_FIRMWARE_MINOR_VERSION 5
33-
#define FIRMATA_FIRMWARE_BUGFIX_VERSION 2
18+
#include "FirmataDefines.h"
19+
#include "FirmataMarshaller.h"
20+
#include "FirmataParser.h"
3421

3522
/* DEPRECATED as of Firmata v2.5.1. As of 2.5.1 there are separate version numbers for
3623
* the protocol version and the firmware version.
@@ -39,52 +26,8 @@
3926
#define FIRMATA_MINOR_VERSION 5 // same as FIRMATA_PROTOCOL_MINOR_VERSION
4027
#define FIRMATA_BUGFIX_VERSION 1 // same as FIRMATA_PROTOCOL_BUGFIX_VERSION
4128

42-
#define MAX_DATA_BYTES 64 // max number of data bytes in incoming messages
43-
44-
// Arduino 101 also defines SET_PIN_MODE as a macro in scss_registers.h
45-
#ifdef SET_PIN_MODE
46-
#undef SET_PIN_MODE
47-
#endif
48-
49-
// message command bytes (128-255/0x80-0xFF)
50-
#define DIGITAL_MESSAGE 0x90 // send data for a digital port (collection of 8 pins)
51-
#define ANALOG_MESSAGE 0xE0 // send data for an analog pin (or PWM)
52-
#define REPORT_ANALOG 0xC0 // enable analog input by pin #
53-
#define REPORT_DIGITAL 0xD0 // enable digital input by port pair
54-
//
55-
#define SET_PIN_MODE 0xF4 // set a pin to INPUT/OUTPUT/PWM/etc
56-
#define SET_DIGITAL_PIN_VALUE 0xF5 // set value of an individual digital pin
57-
//
58-
#define REPORT_VERSION 0xF9 // report protocol version
59-
#define SYSTEM_RESET 0xFF // reset from MIDI
60-
//
61-
#define START_SYSEX 0xF0 // start a MIDI Sysex message
62-
#define END_SYSEX 0xF7 // end a MIDI Sysex message
63-
6429
// extended command set using sysex (0-127/0x00-0x7F)
6530
/* 0x00-0x0F reserved for user-defined commands */
66-
#define SERIAL_MESSAGE 0x60 // communicate with serial devices, including other boards
67-
#define ENCODER_DATA 0x61 // reply with encoders current positions
68-
#define SERVO_CONFIG 0x70 // set max angle, minPulse, maxPulse, freq
69-
#define STRING_DATA 0x71 // a string message with 14-bits per char
70-
#define STEPPER_DATA 0x72 // control a stepper motor
71-
#define ONEWIRE_DATA 0x73 // send an OneWire read/write/reset/select/skip/search request
72-
#define SHIFT_DATA 0x75 // a bitstream to/from a shift register
73-
#define I2C_REQUEST 0x76 // send an I2C read/write request
74-
#define I2C_REPLY 0x77 // a reply to an I2C read request
75-
#define I2C_CONFIG 0x78 // config I2C settings such as delay times and power pins
76-
#define EXTENDED_ANALOG 0x6F // analog write (PWM, Servo, etc) to any pin
77-
#define PIN_STATE_QUERY 0x6D // ask for a pin's current mode and value
78-
#define PIN_STATE_RESPONSE 0x6E // reply with pin's current mode and value
79-
#define CAPABILITY_QUERY 0x6B // ask for supported modes and resolution of all pins
80-
#define CAPABILITY_RESPONSE 0x6C // reply with supported modes and resolution
81-
#define ANALOG_MAPPING_QUERY 0x69 // ask for mapping of analog to pin numbers
82-
#define ANALOG_MAPPING_RESPONSE 0x6A // reply with mapping info
83-
#define REPORT_FIRMWARE 0x79 // report name and version of the firmware
84-
#define SAMPLING_INTERVAL 0x7A // set the poll rate of the main loop
85-
#define SCHEDULER_DATA 0x7B // send a createtask/deletetask/addtotask/schedule/querytasks/querytask request to the scheduler
86-
#define SYSEX_NON_REALTIME 0x7E // MIDI Reserved for non-realtime messages
87-
#define SYSEX_REALTIME 0x7F // MIDI Reserved for realtime messages
8831
// these are DEPRECATED to make the naming more consistent
8932
#define FIRMATA_STRING 0x71 // same as STRING_DATA
9033
#define SYSEX_I2C_REQUEST 0x76 // same as I2C_REQUEST
@@ -94,18 +37,6 @@
9437
// pin modes
9538
//#define INPUT 0x00 // defined in Arduino.h
9639
//#define OUTPUT 0x01 // defined in Arduino.h
97-
#define PIN_MODE_ANALOG 0x02 // analog pin in analogInput mode
98-
#define PIN_MODE_PWM 0x03 // digital pin in PWM output mode
99-
#define PIN_MODE_SERVO 0x04 // digital pin in Servo output mode
100-
#define PIN_MODE_SHIFT 0x05 // shiftIn/shiftOut mode
101-
#define PIN_MODE_I2C 0x06 // pin included in I2C setup
102-
#define PIN_MODE_ONEWIRE 0x07 // pin configured for 1-wire
103-
#define PIN_MODE_STEPPER 0x08 // pin configured for stepper motor
104-
#define PIN_MODE_ENCODER 0x09 // pin configured for rotary encoders
105-
#define PIN_MODE_SERIAL 0x0A // pin configured for serial communication
106-
#define PIN_MODE_PULLUP 0x0B // enable internal pull-up resistor for pin
107-
#define PIN_MODE_IGNORE 0x7F // pin configured to be ignored by digitalWrite and capabilityResponse
108-
#define TOTAL_PIN_MODES 13
10940
// DEPRECATED as of Firmata v2.5
11041
#define ANALOG 0x02 // same as PIN_MODE_ANALOG
11142
#define PWM 0x03 // same as PIN_MODE_PWM
@@ -117,35 +48,39 @@
11748
#define ENCODER 0x09 // same as PIN_MODE_ENCODER
11849
#define IGNORE 0x7F // same as PIN_MODE_IGNORE
11950

120-
extern "C" {
121-
// callback function types
122-
typedef void (*callbackFunction)(byte, int);
123-
typedef void (*systemResetCallbackFunction)(void);
124-
typedef void (*stringCallbackFunction)(char *);
125-
typedef void (*sysexCallbackFunction)(byte command, byte argc, byte *argv);
126-
}
51+
namespace firmata {
12752

12853
// TODO make it a subclass of a generic Serial/Stream base class
12954
class FirmataClass
13055
{
13156
public:
57+
typedef void (*callbackFunction)(uint8_t, int);
58+
typedef void (*systemCallbackFunction)(void);
59+
typedef void (*stringCallbackFunction)(char *);
60+
typedef void (*sysexCallbackFunction)(uint8_t command, uint8_t argc, uint8_t *argv);
61+
13262
FirmataClass();
63+
13364
/* Arduino constructors */
13465
void begin();
13566
void begin(long);
13667
void begin(Stream &s);
68+
13769
/* querying functions */
13870
void printVersion(void);
13971
void blinkVersion(void);
14072
void printFirmwareVersion(void);
73+
14174
//void setFirmwareVersion(byte major, byte minor); // see macro below
14275
void setFirmwareNameAndVersion(const char *name, byte major, byte minor);
14376
void disableBlinkVersion();
77+
14478
/* serial receive handling */
14579
int available(void);
14680
void processInput(void);
14781
void parse(unsigned char value);
14882
boolean isParsingMessage(void);
83+
14984
/* serial send handling */
15085
void sendAnalog(byte pin, int value);
15186
void sendDigital(byte pin, int value); // TODO implement this
@@ -154,16 +89,18 @@ class FirmataClass
15489
void sendString(byte command, const char *string);
15590
void sendSysex(byte command, byte bytec, byte *bytev);
15691
void write(byte c);
92+
15793
/* attach & detach callback functions to messages */
158-
void attach(byte command, callbackFunction newFunction);
159-
void attach(byte command, systemResetCallbackFunction newFunction);
160-
void attach(byte command, stringCallbackFunction newFunction);
161-
void attach(byte command, sysexCallbackFunction newFunction);
162-
void detach(byte command);
94+
void attach(uint8_t command, callbackFunction newFunction);
95+
void attach(uint8_t command, systemCallbackFunction newFunction);
96+
void attach(uint8_t command, stringCallbackFunction newFunction);
97+
void attach(uint8_t command, sysexCallbackFunction newFunction);
98+
void detach(uint8_t command);
16399

164100
/* access pin state and config */
165101
byte getPinMode(byte pin);
166102
void setPinMode(byte pin, byte config);
103+
167104
/* access pin state */
168105
int getPinState(byte pin);
169106
void setPinState(byte pin, int state);
@@ -174,42 +111,61 @@ class FirmataClass
174111
void endSysex(void);
175112

176113
private:
114+
uint8_t parserBuffer[MAX_DATA_BYTES];
115+
FirmataMarshaller marshaller;
116+
FirmataParser parser;
177117
Stream *FirmataStream;
118+
178119
/* firmware name and version */
179120
byte firmwareVersionCount;
180121
byte *firmwareVersionVector;
181-
/* input message handling */
182-
byte waitForData; // this flag says the next serial input will be data
183-
byte executeMultiByteCommand; // execute this after getting multi-byte data
184-
byte multiByteChannel; // channel data for multiByteCommands
185-
byte storedInputData[MAX_DATA_BYTES]; // multi-byte data
186-
/* sysex */
187-
boolean parsingSysex;
188-
int sysexBytesRead;
122+
189123
/* pin configuration */
190124
byte pinConfig[TOTAL_PINS];
191125
int pinState[TOTAL_PINS];
192126

193-
/* callback functions */
194-
callbackFunction currentAnalogCallback;
195-
callbackFunction currentDigitalCallback;
196-
callbackFunction currentReportAnalogCallback;
197-
callbackFunction currentReportDigitalCallback;
198-
callbackFunction currentPinModeCallback;
199-
callbackFunction currentPinValueCallback;
200-
systemResetCallbackFunction currentSystemResetCallback;
201-
stringCallbackFunction currentStringCallback;
202-
sysexCallbackFunction currentSysexCallback;
203-
204-
boolean blinkVersionDisabled = false;
127+
boolean blinkVersionDisabled;
205128

206129
/* private methods ------------------------------ */
207-
void processSysexMessage(void);
208-
void systemReset(void);
209130
void strobeBlinkPin(byte pin, int count, int onInterval, int offInterval);
131+
friend void FirmataMarshaller::encodeByteStream (size_t bytec, uint8_t * bytev, size_t max_bytes) const;
132+
133+
/* callback functions */
134+
static callbackFunction currentAnalogCallback;
135+
static callbackFunction currentDigitalCallback;
136+
static callbackFunction currentPinModeCallback;
137+
static callbackFunction currentPinValueCallback;
138+
static callbackFunction currentReportAnalogCallback;
139+
static callbackFunction currentReportDigitalCallback;
140+
static stringCallbackFunction currentStringCallback;
141+
static sysexCallbackFunction currentSysexCallback;
142+
static systemCallbackFunction currentSystemResetCallback;
143+
144+
/* static callbacks */
145+
inline static void staticAnalogCallback (void *, uint8_t command, uint16_t value) { if ( currentAnalogCallback ) { currentAnalogCallback(command,(int)value); } }
146+
inline static void staticDigitalCallback (void *, uint8_t command, uint16_t value) { if ( currentDigitalCallback ) { currentDigitalCallback(command, (int)value); } }
147+
inline static void staticPinModeCallback (void *, uint8_t command, uint16_t value) { if ( currentPinModeCallback ) { currentPinModeCallback(command, (int)value); } }
148+
inline static void staticPinValueCallback (void *, uint8_t command, uint16_t value) { if ( currentPinValueCallback ) { currentPinValueCallback(command, (int)value); } }
149+
inline static void staticReportAnalogCallback (void *, uint8_t command, uint16_t value) { if ( currentReportAnalogCallback ) { currentReportAnalogCallback(command, (int)value); } }
150+
inline static void staticReportDigitalCallback (void *, uint8_t command, uint16_t value) { if ( currentReportDigitalCallback ) { currentReportDigitalCallback(command, (int)value); } }
151+
inline static void staticStringCallback (void *, const char * c_str) { if ( currentStringCallback ) { currentStringCallback((char *)c_str); } }
152+
inline static void staticSysexCallback (void *, uint8_t command, size_t argc, uint8_t *argv) { if ( currentSysexCallback ) { currentSysexCallback(command, (uint8_t)argc, argv); } }
153+
inline static void staticReportFirmwareCallback (void * context, size_t, size_t, const char *) { if ( context ) { ((FirmataClass *)context)->printFirmwareVersion(); } }
154+
inline static void staticReportVersionCallback (void * context) { if ( context ) { ((FirmataClass *)context)->printVersion(); } }
155+
inline static void staticSystemResetCallback (void *) { if ( currentSystemResetCallback ) { currentSystemResetCallback(); } }
210156
};
211157

212-
extern FirmataClass Firmata;
158+
} // namespace firmata
159+
160+
extern "C" {
161+
// callback function types
162+
typedef firmata::FirmataClass::callbackFunction callbackFunction;
163+
typedef firmata::FirmataClass::systemCallbackFunction systemCallbackFunction;
164+
typedef firmata::FirmataClass::stringCallbackFunction stringCallbackFunction;
165+
typedef firmata::FirmataClass::sysexCallbackFunction sysexCallbackFunction;
166+
}
167+
168+
extern firmata::FirmataClass Firmata;
213169

214170
/*==============================================================================
215171
* MACROS

FirmataConstants.h

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
FirmataConstants.h
3+
Copyright (c) 2006-2008 Hans-Christoph Steiner. All rights reserved.
4+
Copyright (C) 2009-2017 Jeff Hoefs. All rights reserved.
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
See file LICENSE.txt for further informations on licensing terms.
12+
*/
13+
14+
#ifndef FirmataConstants_h
15+
#define FirmataConstants_h
16+
17+
namespace firmata {
18+
/* Version numbers for the Firmata library.
19+
* The firmware version will not always equal the protocol version going forward.
20+
* Query using the REPORT_FIRMWARE message.
21+
*/
22+
static const int FIRMWARE_MAJOR_VERSION = 2;
23+
static const int FIRMWARE_MINOR_VERSION = 5;
24+
static const int FIRMWARE_BUGFIX_VERSION = 7;
25+
26+
/* Version numbers for the protocol. The protocol is still changing, so these
27+
* version numbers are important.
28+
* Query using the REPORT_VERSION message.
29+
*/
30+
static const int PROTOCOL_MAJOR_VERSION = 2; // for non-compatible changes
31+
static const int PROTOCOL_MINOR_VERSION = 5; // for backwards compatible changes
32+
static const int PROTOCOL_BUGFIX_VERSION = 1; // for bugfix releases
33+
34+
static const int MAX_DATA_BYTES = 64; // max number of data bytes in incoming messages
35+
36+
// message command bytes (128-255/0x80-0xFF)
37+
38+
static const int DIGITAL_MESSAGE = 0x90; // send data for a digital port (collection of 8 pins)
39+
static const int ANALOG_MESSAGE = 0xE0; // send data for an analog pin (or PWM)
40+
static const int REPORT_ANALOG = 0xC0; // enable analog input by pin #
41+
static const int REPORT_DIGITAL = 0xD0; // enable digital input by port pair
42+
//
43+
static const int SET_PIN_MODE = 0xF4; // set a pin to INPUT/OUTPUT/PWM/etc
44+
static const int SET_DIGITAL_PIN_VALUE = 0xF5; // set value of an individual digital pin
45+
//
46+
static const int REPORT_VERSION = 0xF9; // report protocol version
47+
static const int SYSTEM_RESET = 0xFF; // reset from MIDI
48+
//
49+
static const int START_SYSEX = 0xF0; // start a MIDI Sysex message
50+
static const int END_SYSEX = 0xF7; // end a MIDI Sysex message
51+
52+
// extended command set using sysex (0-127/0x00-0x7F)
53+
/* 0x00-0x0F reserved for user-defined commands */
54+
55+
static const int SERIAL_DATA = 0x60; // communicate with serial devices, including other boards
56+
static const int ENCODER_DATA = 0x61; // reply with encoders current positions
57+
static const int SERVO_CONFIG = 0x70; // set max angle, minPulse, maxPulse, freq
58+
static const int STRING_DATA = 0x71; // a string message with 14-bits per char
59+
static const int STEPPER_DATA = 0x72; // control a stepper motor
60+
static const int ONEWIRE_DATA = 0x73; // send an OneWire read/write/reset/select/skip/search request
61+
static const int SHIFT_DATA = 0x75; // a bitstream to/from a shift register
62+
static const int I2C_REQUEST = 0x76; // send an I2C read/write request
63+
static const int I2C_REPLY = 0x77; // a reply to an I2C read request
64+
static const int I2C_CONFIG = 0x78; // config I2C settings such as delay times and power pins
65+
static const int REPORT_FIRMWARE = 0x79; // report name and version of the firmware
66+
static const int EXTENDED_ANALOG = 0x6F; // analog write (PWM, Servo, etc) to any pin
67+
static const int PIN_STATE_QUERY = 0x6D; // ask for a pin's current mode and value
68+
static const int PIN_STATE_RESPONSE = 0x6E; // reply with pin's current mode and value
69+
static const int CAPABILITY_QUERY = 0x6B; // ask for supported modes and resolution of all pins
70+
static const int CAPABILITY_RESPONSE = 0x6C; // reply with supported modes and resolution
71+
static const int ANALOG_MAPPING_QUERY = 0x69; // ask for mapping of analog to pin numbers
72+
static const int ANALOG_MAPPING_RESPONSE = 0x6A; // reply with mapping info
73+
static const int SAMPLING_INTERVAL = 0x7A; // set the poll rate of the main loop
74+
static const int SCHEDULER_DATA = 0x7B; // send a createtask/deletetask/addtotask/schedule/querytasks/querytask request to the scheduler
75+
static const int SYSEX_NON_REALTIME = 0x7E; // MIDI Reserved for non-realtime messages
76+
static const int SYSEX_REALTIME = 0x7F; // MIDI Reserved for realtime messages
77+
78+
// pin modes
79+
static const int PIN_MODE_INPUT = 0x00; // same as INPUT defined in Arduino.h
80+
static const int PIN_MODE_OUTPUT = 0x01; // same as OUTPUT defined in Arduino.h
81+
static const int PIN_MODE_ANALOG = 0x02; // analog pin in analogInput mode
82+
static const int PIN_MODE_PWM = 0x03; // digital pin in PWM output mode
83+
static const int PIN_MODE_SERVO = 0x04; // digital pin in Servo output mode
84+
static const int PIN_MODE_SHIFT = 0x05; // shiftIn/shiftOut mode
85+
static const int PIN_MODE_I2C = 0x06; // pin included in I2C setup
86+
static const int PIN_MODE_ONEWIRE = 0x07; // pin configured for 1-wire
87+
static const int PIN_MODE_STEPPER = 0x08; // pin configured for stepper motor
88+
static const int PIN_MODE_ENCODER = 0x09; // pin configured for rotary encoders
89+
static const int PIN_MODE_SERIAL = 0x0A; // pin configured for serial communication
90+
static const int PIN_MODE_PULLUP = 0x0B; // enable internal pull-up resistor for pin
91+
static const int PIN_MODE_IGNORE = 0x7F; // pin configured to be ignored by digitalWrite and capabilityResponse
92+
93+
static const int TOTAL_PIN_MODES = 13;
94+
95+
} // namespace firmata
96+
97+
#endif // FirmataConstants_h

0 commit comments

Comments
 (0)