Skip to content

Split marshalling behavior into new FirmataMarshaller class #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 11 additions & 23 deletions Firmata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ void printFirmwareVersion (void) {
*/
void FirmataClass::sendValueAsTwo7bitBytes(int value)
{
FirmataStream->write(value & 0x7F); // LSB
FirmataStream->write(value >> 7 & 0x7F); // MSB
marshaller.sendValueAsTwo7bitBytes(value);
}

/**
Expand Down Expand Up @@ -106,10 +105,8 @@ void FirmataClass::begin(void)
void FirmataClass::begin(long speed)
{
Serial.begin(speed);
FirmataStream = &Serial;
blinkVersion();
printVersion(); // send the protocol version
printFirmwareVersion(); // send the firmware name and version
begin(Serial);
}

/**
Expand All @@ -121,10 +118,11 @@ void FirmataClass::begin(long speed)
void FirmataClass::begin(Stream &s)
{
FirmataStream = &s;
marshaller.begin(s);
// do not call blinkVersion() here because some hardware such as the
// Ethernet shield use pin 13
printVersion();
printFirmwareVersion();
printVersion(); // send the protocol version
printFirmwareVersion(); // send the firmware name and version
}

/**
Expand Down Expand Up @@ -182,7 +180,7 @@ void FirmataClass::printFirmwareVersion(void)
FirmataStream->write(firmwareVersionVector[0]); // major version number
FirmataStream->write(firmwareVersionVector[1]); // minor version number
for (i = 2; i < firmwareVersionCount; ++i) {
sendValueAsTwo7bitBytes(firmwareVersionVector[i]);
marshaller.sendValueAsTwo7bitBytes(firmwareVersionVector[i]);
}
endSysex();
}
Expand Down Expand Up @@ -285,9 +283,7 @@ boolean FirmataClass::isParsingMessage(void)
*/
void FirmataClass::sendAnalog(byte pin, int value)
{
// pin can only be 0-15, so chop higher bits
FirmataStream->write(ANALOG_MESSAGE | (pin & 0xF));
sendValueAsTwo7bitBytes(value);
marshaller.sendAnalog(pin, value);
}

/* (intentionally left out asterix here)
Expand Down Expand Up @@ -326,9 +322,7 @@ void FirmataClass::sendDigital(byte pin, int value)
*/
void FirmataClass::sendDigitalPort(byte portNumber, int portData)
{
FirmataStream->write(DIGITAL_MESSAGE | (portNumber & 0xF));
FirmataStream->write((byte)portData % 128); // Tx bits 0-6 (protocol v1 and higher)
FirmataStream->write(portData >> 7); // Tx bits 7-13 (bit 7 only for protocol v2 and higher)
marshaller.sendDigitalPort(portNumber, portData);
}

/**
Expand All @@ -340,13 +334,7 @@ void FirmataClass::sendDigitalPort(byte portNumber, int portData)
*/
void FirmataClass::sendSysex(byte command, byte bytec, byte *bytev)
{
byte i;
startSysex();
FirmataStream->write(command);
for (i = 0; i < bytec; i++) {
sendValueAsTwo7bitBytes(bytev[i]);
}
endSysex();
marshaller.sendSysex(command, bytec, bytev);
}

/**
Expand All @@ -357,7 +345,7 @@ void FirmataClass::sendSysex(byte command, byte bytec, byte *bytev)
void FirmataClass::sendString(byte command, const char *string)
{
if (command == STRING_DATA) {
sendSysex(command, strlen(string), (byte *)string);
marshaller.sendString(string);
}
}

Expand All @@ -367,7 +355,7 @@ void FirmataClass::sendString(byte command, const char *string)
*/
void FirmataClass::sendString(const char *string)
{
sendString(STRING_DATA, string);
marshaller.sendString(string);
}

/**
Expand Down
44 changes: 4 additions & 40 deletions Firmata.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,10 @@
#define Firmata_h

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

/* 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 4

/* 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 @@ -34,26 +28,6 @@

// 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 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 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 3 deprecated defines need to be included in Firmata.h or older Firmata sketches will break.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant to leave those in there. Let me see what I've done.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally left them in there. I must have removed them by accident, I'll put them right back. I'm sorry.

#define FIRMATA_STRING 0x71 // same as STRING_DATA
#define SYSEX_I2C_REQUEST 0x76 // same as I2C_REQUEST
Expand All @@ -63,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 Down Expand Up @@ -135,6 +97,7 @@ class FirmataClass
void endSysex(void);

private:
FirmataMarshaller marshaller;
FirmataParser parser;
Stream *FirmataStream;
/* firmware name and version */
Expand All @@ -148,6 +111,7 @@ class FirmataClass

/* private methods ------------------------------ */
void strobeBlinkPin(byte pin, int count, int onInterval, int offInterval);
friend void FirmataMarshaller::sendValueAsTwo7bitBytes(uint16_t value);
};

extern FirmataClass Firmata;
Expand Down
Loading