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
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.
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
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
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
12954class 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
0 commit comments