Skip to content

Commit

Permalink
Merge pull request #1 from firmata/master
Browse files Browse the repository at this point in the history
merge upstream changes
  • Loading branch information
jnsbyr authored Mar 9, 2020
2 parents 162f8fa + 9caa095 commit 41097ce
Show file tree
Hide file tree
Showing 159 changed files with 16,980 additions and 1,589 deletions.
263 changes: 258 additions & 5 deletions Boards.h

Large diffs are not rendered by default.

302 changes: 94 additions & 208 deletions Firmata.cpp

Large diffs are not rendered by default.

174 changes: 65 additions & 109 deletions Firmata.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Firmata.h - Firmata library v2.5.2 - 2016-2-15
Firmata.h - Firmata library v2.5.8 - 2018-04-15
Copyright (c) 2006-2008 Hans-Christoph Steiner. All rights reserved.
Copyright (C) 2009-2015 Jeff Hoefs. All rights reserved.
Copyright (C) 2009-2017 Jeff Hoefs. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand All @@ -15,22 +15,9 @@
#define Firmata_h

#include "Boards.h" /* Hardware Abstraction Layer + Wiring/Arduino */

/* Version numbers for the protocol. The protocol is still changing, so these
* version numbers are important.
* Query using the REPORT_VERSION message.
*/
#define FIRMATA_PROTOCOL_MAJOR_VERSION 2 // for non-compatible changes
#define FIRMATA_PROTOCOL_MINOR_VERSION 5 // for backwards compatible changes
#define FIRMATA_PROTOCOL_BUGFIX_VERSION 1 // for bugfix releases

/* Version numbers for the Firmata library.
* The firmware version will not always equal the protocol version going forward.
* Query using the REPORT_FIRMWARE message.
*/
#define FIRMATA_FIRMWARE_MAJOR_VERSION 2
#define FIRMATA_FIRMWARE_MINOR_VERSION 5
#define FIRMATA_FIRMWARE_BUGFIX_VERSION 2
#include "FirmataDefines.h"
#include "FirmataMarshaller.h"
#include "FirmataParser.h"

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

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

// Arduino 101 also defines SET_PIN_MODE as a macro in scss_registers.h
#ifdef SET_PIN_MODE
#undef SET_PIN_MODE
#endif

// message command bytes (128-255/0x80-0xFF)
#define DIGITAL_MESSAGE 0x90 // send data for a digital port (collection of 8 pins)
#define ANALOG_MESSAGE 0xE0 // send data for an analog pin (or PWM)
#define REPORT_ANALOG 0xC0 // enable analog input by pin #
#define REPORT_DIGITAL 0xD0 // enable digital input by port pair
//
#define SET_PIN_MODE 0xF4 // set a pin to INPUT/OUTPUT/PWM/etc
#define SET_DIGITAL_PIN_VALUE 0xF5 // set value of an individual digital pin
//
#define REPORT_VERSION 0xF9 // report protocol version
#define SYSTEM_RESET 0xFF // reset from MIDI
//
#define START_SYSEX 0xF0 // start a MIDI Sysex message
#define END_SYSEX 0xF7 // end a MIDI Sysex message

// extended command set using sysex (0-127/0x00-0x7F)
/* 0x00-0x0F reserved for user-defined commands */
#define SERIAL_MESSAGE 0x60 // communicate with serial devices, including other boards
#define ENCODER_DATA 0x61 // reply with encoders current positions
#define SERVO_CONFIG 0x70 // set max angle, minPulse, maxPulse, freq
#define STRING_DATA 0x71 // a string message with 14-bits per char
#define STEPPER_DATA 0x72 // control a stepper motor
#define ONEWIRE_DATA 0x73 // send an OneWire read/write/reset/select/skip/search request
#define SHIFT_DATA 0x75 // a bitstream to/from a shift register
#define I2C_REQUEST 0x76 // send an I2C read/write request
#define I2C_REPLY 0x77 // a reply to an I2C read request
#define I2C_CONFIG 0x78 // config I2C settings such as delay times and power pins
#define EXTENDED_ANALOG 0x6F // analog write (PWM, Servo, etc) to any pin
#define PIN_STATE_QUERY 0x6D // ask for a pin's current mode and value
#define PIN_STATE_RESPONSE 0x6E // reply with pin's current mode and value
#define CAPABILITY_QUERY 0x6B // ask for supported modes and resolution of all pins
#define CAPABILITY_RESPONSE 0x6C // reply with supported modes and resolution
#define ANALOG_MAPPING_QUERY 0x69 // ask for mapping of analog to pin numbers
#define ANALOG_MAPPING_RESPONSE 0x6A // reply with mapping info
#define REPORT_FIRMWARE 0x79 // report name and version of the firmware
#define SAMPLING_INTERVAL 0x7A // set the poll rate of the main loop
#define SCHEDULER_DATA 0x7B // send a createtask/deletetask/addtotask/schedule/querytasks/querytask request to the scheduler
#define SYSEX_NON_REALTIME 0x7E // MIDI Reserved for non-realtime messages
#define SYSEX_REALTIME 0x7F // MIDI Reserved for realtime messages
// these are DEPRECATED to make the naming more consistent
#define FIRMATA_STRING 0x71 // same as STRING_DATA
#define SYSEX_I2C_REQUEST 0x76 // same as I2C_REQUEST
Expand All @@ -94,18 +37,6 @@
// pin modes
//#define INPUT 0x00 // defined in Arduino.h
//#define OUTPUT 0x01 // defined in Arduino.h
#define PIN_MODE_ANALOG 0x02 // analog pin in analogInput mode
#define PIN_MODE_PWM 0x03 // digital pin in PWM output mode
#define PIN_MODE_SERVO 0x04 // digital pin in Servo output mode
#define PIN_MODE_SHIFT 0x05 // shiftIn/shiftOut mode
#define PIN_MODE_I2C 0x06 // pin included in I2C setup
#define PIN_MODE_ONEWIRE 0x07 // pin configured for 1-wire
#define PIN_MODE_STEPPER 0x08 // pin configured for stepper motor
#define PIN_MODE_ENCODER 0x09 // pin configured for rotary encoders
#define PIN_MODE_SERIAL 0x0A // pin configured for serial communication
#define PIN_MODE_PULLUP 0x0B // enable internal pull-up resistor for pin
#define PIN_MODE_IGNORE 0x7F // pin configured to be ignored by digitalWrite and capabilityResponse
#define TOTAL_PIN_MODES 13
// DEPRECATED as of Firmata v2.5
#define ANALOG 0x02 // same as PIN_MODE_ANALOG
#define PWM 0x03 // same as PIN_MODE_PWM
Expand All @@ -117,35 +48,39 @@
#define ENCODER 0x09 // same as PIN_MODE_ENCODER
#define IGNORE 0x7F // same as PIN_MODE_IGNORE

extern "C" {
// callback function types
typedef void (*callbackFunction)(byte, int);
typedef void (*systemResetCallbackFunction)(void);
typedef void (*stringCallbackFunction)(char *);
typedef void (*sysexCallbackFunction)(byte command, byte argc, byte *argv);
}
namespace firmata {

// TODO make it a subclass of a generic Serial/Stream base class
class FirmataClass
{
public:
typedef void (*callbackFunction)(uint8_t, int);
typedef void (*systemCallbackFunction)(void);
typedef void (*stringCallbackFunction)(char *);
typedef void (*sysexCallbackFunction)(uint8_t command, uint8_t argc, uint8_t *argv);

FirmataClass();

/* Arduino constructors */
void begin();
void begin(long);
void begin(Stream &s);

/* querying functions */
void printVersion(void);
void blinkVersion(void);
void printFirmwareVersion(void);

//void setFirmwareVersion(byte major, byte minor); // see macro below
void setFirmwareNameAndVersion(const char *name, byte major, byte minor);
void disableBlinkVersion();

/* serial receive handling */
int available(void);
void processInput(void);
void parse(unsigned char value);
boolean isParsingMessage(void);

/* serial send handling */
void sendAnalog(byte pin, int value);
void sendDigital(byte pin, int value); // TODO implement this
Expand All @@ -154,16 +89,18 @@ class FirmataClass
void sendString(byte command, const char *string);
void sendSysex(byte command, byte bytec, byte *bytev);
void write(byte c);

/* attach & detach callback functions to messages */
void attach(byte command, callbackFunction newFunction);
void attach(byte command, systemResetCallbackFunction newFunction);
void attach(byte command, stringCallbackFunction newFunction);
void attach(byte command, sysexCallbackFunction newFunction);
void detach(byte command);
void attach(uint8_t command, callbackFunction newFunction);
void attach(uint8_t command, systemCallbackFunction newFunction);
void attach(uint8_t command, stringCallbackFunction newFunction);
void attach(uint8_t command, sysexCallbackFunction newFunction);
void detach(uint8_t command);

/* access pin state and config */
byte getPinMode(byte pin);
void setPinMode(byte pin, byte config);

/* access pin state */
int getPinState(byte pin);
void setPinState(byte pin, int state);
Expand All @@ -174,42 +111,61 @@ class FirmataClass
void endSysex(void);

private:
uint8_t parserBuffer[MAX_DATA_BYTES];
FirmataMarshaller marshaller;
FirmataParser parser;
Stream *FirmataStream;

/* firmware name and version */
byte firmwareVersionCount;
byte *firmwareVersionVector;
/* input message handling */
byte waitForData; // this flag says the next serial input will be data
byte executeMultiByteCommand; // execute this after getting multi-byte data
byte multiByteChannel; // channel data for multiByteCommands
byte storedInputData[MAX_DATA_BYTES]; // multi-byte data
/* sysex */
boolean parsingSysex;
int sysexBytesRead;

/* pin configuration */
byte pinConfig[TOTAL_PINS];
int pinState[TOTAL_PINS];

/* callback functions */
callbackFunction currentAnalogCallback;
callbackFunction currentDigitalCallback;
callbackFunction currentReportAnalogCallback;
callbackFunction currentReportDigitalCallback;
callbackFunction currentPinModeCallback;
callbackFunction currentPinValueCallback;
systemResetCallbackFunction currentSystemResetCallback;
stringCallbackFunction currentStringCallback;
sysexCallbackFunction currentSysexCallback;

boolean blinkVersionDisabled = false;
boolean blinkVersionDisabled;

/* private methods ------------------------------ */
void processSysexMessage(void);
void systemReset(void);
void strobeBlinkPin(byte pin, int count, int onInterval, int offInterval);
friend void FirmataMarshaller::encodeByteStream (size_t bytec, uint8_t * bytev, size_t max_bytes) const;

/* callback functions */
static callbackFunction currentAnalogCallback;
static callbackFunction currentDigitalCallback;
static callbackFunction currentPinModeCallback;
static callbackFunction currentPinValueCallback;
static callbackFunction currentReportAnalogCallback;
static callbackFunction currentReportDigitalCallback;
static stringCallbackFunction currentStringCallback;
static sysexCallbackFunction currentSysexCallback;
static systemCallbackFunction currentSystemResetCallback;

/* static callbacks */
inline static void staticAnalogCallback (void *, uint8_t command, uint16_t value) { if ( currentAnalogCallback ) { currentAnalogCallback(command,(int)value); } }
inline static void staticDigitalCallback (void *, uint8_t command, uint16_t value) { if ( currentDigitalCallback ) { currentDigitalCallback(command, (int)value); } }
inline static void staticPinModeCallback (void *, uint8_t command, uint16_t value) { if ( currentPinModeCallback ) { currentPinModeCallback(command, (int)value); } }
inline static void staticPinValueCallback (void *, uint8_t command, uint16_t value) { if ( currentPinValueCallback ) { currentPinValueCallback(command, (int)value); } }
inline static void staticReportAnalogCallback (void *, uint8_t command, uint16_t value) { if ( currentReportAnalogCallback ) { currentReportAnalogCallback(command, (int)value); } }
inline static void staticReportDigitalCallback (void *, uint8_t command, uint16_t value) { if ( currentReportDigitalCallback ) { currentReportDigitalCallback(command, (int)value); } }
inline static void staticStringCallback (void *, const char * c_str) { if ( currentStringCallback ) { currentStringCallback((char *)c_str); } }
inline static void staticSysexCallback (void *, uint8_t command, size_t argc, uint8_t *argv) { if ( currentSysexCallback ) { currentSysexCallback(command, (uint8_t)argc, argv); } }
inline static void staticReportFirmwareCallback (void * context, size_t, size_t, const char *) { if ( context ) { ((FirmataClass *)context)->printFirmwareVersion(); } }
inline static void staticReportVersionCallback (void * context) { if ( context ) { ((FirmataClass *)context)->printVersion(); } }
inline static void staticSystemResetCallback (void *) { if ( currentSystemResetCallback ) { currentSystemResetCallback(); } }
};

extern FirmataClass Firmata;
} // namespace firmata

extern "C" {
// callback function types
typedef firmata::FirmataClass::callbackFunction callbackFunction;
typedef firmata::FirmataClass::systemCallbackFunction systemCallbackFunction;
typedef firmata::FirmataClass::stringCallbackFunction stringCallbackFunction;
typedef firmata::FirmataClass::sysexCallbackFunction sysexCallbackFunction;
}

extern firmata::FirmataClass Firmata;

/*==============================================================================
* MACROS
Expand Down
97 changes: 97 additions & 0 deletions FirmataConstants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
FirmataConstants.h
Copyright (c) 2006-2008 Hans-Christoph Steiner. All rights reserved.
Copyright (C) 2009-2017 Jeff Hoefs. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
See file LICENSE.txt for further informations on licensing terms.
*/

#ifndef FirmataConstants_h
#define FirmataConstants_h

namespace firmata {
/* Version numbers for the Firmata library.
* The firmware version will not always equal the protocol version going forward.
* Query using the REPORT_FIRMWARE message.
*/
static const int FIRMWARE_MAJOR_VERSION = 2;
static const int FIRMWARE_MINOR_VERSION = 5;
static const int FIRMWARE_BUGFIX_VERSION = 7;

/* Version numbers for the protocol. The protocol is still changing, so these
* version numbers are important.
* Query using the REPORT_VERSION message.
*/
static const int PROTOCOL_MAJOR_VERSION = 2; // for non-compatible changes
static const int PROTOCOL_MINOR_VERSION = 5; // for backwards compatible changes
static const int PROTOCOL_BUGFIX_VERSION = 1; // for bugfix releases

static const int MAX_DATA_BYTES = 64; // max number of data bytes in incoming messages

// message command bytes (128-255/0x80-0xFF)

static const int DIGITAL_MESSAGE = 0x90; // send data for a digital port (collection of 8 pins)
static const int ANALOG_MESSAGE = 0xE0; // send data for an analog pin (or PWM)
static const int REPORT_ANALOG = 0xC0; // enable analog input by pin #
static const int REPORT_DIGITAL = 0xD0; // enable digital input by port pair
//
static const int SET_PIN_MODE = 0xF4; // set a pin to INPUT/OUTPUT/PWM/etc
static const int SET_DIGITAL_PIN_VALUE = 0xF5; // set value of an individual digital pin
//
static const int REPORT_VERSION = 0xF9; // report protocol version
static const int SYSTEM_RESET = 0xFF; // reset from MIDI
//
static const int START_SYSEX = 0xF0; // start a MIDI Sysex message
static const int END_SYSEX = 0xF7; // end a MIDI Sysex message

// extended command set using sysex (0-127/0x00-0x7F)
/* 0x00-0x0F reserved for user-defined commands */

static const int SERIAL_DATA = 0x60; // communicate with serial devices, including other boards
static const int ENCODER_DATA = 0x61; // reply with encoders current positions
static const int SERVO_CONFIG = 0x70; // set max angle, minPulse, maxPulse, freq
static const int STRING_DATA = 0x71; // a string message with 14-bits per char
static const int STEPPER_DATA = 0x72; // control a stepper motor
static const int ONEWIRE_DATA = 0x73; // send an OneWire read/write/reset/select/skip/search request
static const int SHIFT_DATA = 0x75; // a bitstream to/from a shift register
static const int I2C_REQUEST = 0x76; // send an I2C read/write request
static const int I2C_REPLY = 0x77; // a reply to an I2C read request
static const int I2C_CONFIG = 0x78; // config I2C settings such as delay times and power pins
static const int REPORT_FIRMWARE = 0x79; // report name and version of the firmware
static const int EXTENDED_ANALOG = 0x6F; // analog write (PWM, Servo, etc) to any pin
static const int PIN_STATE_QUERY = 0x6D; // ask for a pin's current mode and value
static const int PIN_STATE_RESPONSE = 0x6E; // reply with pin's current mode and value
static const int CAPABILITY_QUERY = 0x6B; // ask for supported modes and resolution of all pins
static const int CAPABILITY_RESPONSE = 0x6C; // reply with supported modes and resolution
static const int ANALOG_MAPPING_QUERY = 0x69; // ask for mapping of analog to pin numbers
static const int ANALOG_MAPPING_RESPONSE = 0x6A; // reply with mapping info
static const int SAMPLING_INTERVAL = 0x7A; // set the poll rate of the main loop
static const int SCHEDULER_DATA = 0x7B; // send a createtask/deletetask/addtotask/schedule/querytasks/querytask request to the scheduler
static const int SYSEX_NON_REALTIME = 0x7E; // MIDI Reserved for non-realtime messages
static const int SYSEX_REALTIME = 0x7F; // MIDI Reserved for realtime messages

// pin modes
static const int PIN_MODE_INPUT = 0x00; // same as INPUT defined in Arduino.h
static const int PIN_MODE_OUTPUT = 0x01; // same as OUTPUT defined in Arduino.h
static const int PIN_MODE_ANALOG = 0x02; // analog pin in analogInput mode
static const int PIN_MODE_PWM = 0x03; // digital pin in PWM output mode
static const int PIN_MODE_SERVO = 0x04; // digital pin in Servo output mode
static const int PIN_MODE_SHIFT = 0x05; // shiftIn/shiftOut mode
static const int PIN_MODE_I2C = 0x06; // pin included in I2C setup
static const int PIN_MODE_ONEWIRE = 0x07; // pin configured for 1-wire
static const int PIN_MODE_STEPPER = 0x08; // pin configured for stepper motor
static const int PIN_MODE_ENCODER = 0x09; // pin configured for rotary encoders
static const int PIN_MODE_SERIAL = 0x0A; // pin configured for serial communication
static const int PIN_MODE_PULLUP = 0x0B; // enable internal pull-up resistor for pin
static const int PIN_MODE_IGNORE = 0x7F; // pin configured to be ignored by digitalWrite and capabilityResponse

static const int TOTAL_PIN_MODES = 13;

} // namespace firmata

#endif // FirmataConstants_h
Loading

0 comments on commit 41097ce

Please sign in to comment.