Skip to content

Commit

Permalink
Incorporate 2.5.6 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sidwarkd committed Mar 21, 2017
1 parent 65ed93e commit 74754f4
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 72 deletions.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Firmata
version=2.5.5
version=2.5.6
author=Firmata Developers
maintainer=https://github.com/firmata/spark
sentence=Enables the communication with computer apps using a standard serial protocol. For all Particle devices.
Expand Down
5 changes: 3 additions & 2 deletions src/Boards.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,11 @@ writePort(port, value, bitmask): Write an 8 bit port.

// #define TOTAL_PINS 21 //defined in spark_wiring.h
// #define TOTAL_ANALOG_PINS 8 //defined in spark_wiring.h
#define VERSION_BLINK_PIN 7
#define VERSION_BLINK_PIN LED_BUILTIN
#define IS_PIN_DIGITAL(p) ((p) >= 0 && (p) < TOTAL_PINS && (p) != 8 && (p) != 9 && (p) != 20 && (p) != 21 && (p) != 22 && (p) != 23)
#define IS_PIN_ANALOG(p) ((p) >= FIRST_ANALOG_PIN && (p) < (FIRST_ANALOG_PIN+TOTAL_ANALOG_PINS))
#define IS_PIN_PWM(p) ((p) == 0 || (p) == 1 || (p) == 10 || (p) == 11 || (p) == 14 || (p) == 15 || (p) == 16 || (p) == 17)
//#define IS_PIN_PWM(p) ((p) == 0 || (p) == 1 || (p) == 10 || (p) == 11 || (p) == 14 || (p) == 15 || (p) == 16 || (p) == 17)
#define IS_PIN_PWM(p) digitalPinHasPWM(p)
#define IS_PIN_SERVO(p) ((p) >= 0 && (p) < MAX_SERVOS) //??
#define IS_PIN_I2C(p) ((p) == SDA || (p) == SCL)
#define IS_PIN_SPI(p) ((p) == SS || (p) == MOSI || (p) == MISO || (p) == SCK)
Expand Down
7 changes: 3 additions & 4 deletions src/Firmata.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Firmata.cpp - Firmata library v2.5.5 - 2017-03-06
Firmata.cpp - Firmata library v2.5.6 - 2017-03-18
Copyright (c) 2006-2008 Hans-Christoph Steiner. All rights reserved.
Copyright (C) 2009-2016 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 @@ -16,7 +16,6 @@
//******************************************************************************

#include "Firmata.h"
#include "Particle.h"

#include <string.h>
#include <stdlib.h>
Expand Down Expand Up @@ -50,7 +49,7 @@ systemCallbackFunction FirmataClass::currentSystemResetCallback = (systemCallbac
*/
void FirmataClass::sendValueAsTwo7bitBytes(int value)
{
marshaller.transformByteStreamToMessageBytes(sizeof(value), reinterpret_cast<uint8_t *>(&value), sizeof(value));
marshaller.encodeByteStream(sizeof(value), reinterpret_cast<uint8_t *>(&value), sizeof(value));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Firmata.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Firmata.h - Firmata library v2.5.5 - 2017-03-06
Firmata.h - Firmata library v2.5.6 - 2017-03-18
Copyright (c) 2006-2008 Hans-Christoph Steiner. All rights reserved.
Copyright (C) 2009-2016 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 Down Expand Up @@ -128,7 +128,7 @@ class FirmataClass

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

/* callback functions */
static callbackFunction currentAnalogCallback;
Expand Down
12 changes: 6 additions & 6 deletions src/FirmataMarshaller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const
FirmataStream->write(START_SYSEX);
FirmataStream->write(EXTENDED_ANALOG);
FirmataStream->write(pin);
transformByteStreamToMessageBytes(bytec, bytev, bytec);
encodeByteStream(bytec, bytev, bytec);
FirmataStream->write(END_SYSEX);
}

Expand All @@ -89,7 +89,7 @@ const
* @param bytev A pointer to the array of data bytes to send in the message.
* @param max_bytes Force message to be n bytes, regardless of data bits.
*/
void FirmataMarshaller::transformByteStreamToMessageBytes (size_t bytec, uint8_t * bytev, size_t max_bytes)
void FirmataMarshaller::encodeByteStream (size_t bytec, uint8_t * bytev, size_t max_bytes)
const
{
static const size_t transmit_bits = 7;
Expand Down Expand Up @@ -248,7 +248,7 @@ const
if ( (Stream *)NULL == FirmataStream ) { return; }
if ( (0xF >= pin) && (0x3FFF >= value) ) {
FirmataStream->write(ANALOG_MESSAGE|pin);
transformByteStreamToMessageBytes(sizeof(value), reinterpret_cast<uint8_t *>(&value), sizeof(value));
encodeByteStream(sizeof(value), reinterpret_cast<uint8_t *>(&value), sizeof(value));
} else {
sendExtendedAnalog(pin, sizeof(value), reinterpret_cast<uint8_t *>(&value));
}
Expand Down Expand Up @@ -306,7 +306,7 @@ const
FirmataStream->write(DIGITAL_MESSAGE | (portNumber & 0xF));
// Tx bits 0-6 (protocol v1 and higher)
// Tx bits 7-13 (bit 7 only for protocol v2 and higher)
transformByteStreamToMessageBytes(sizeof(portData), reinterpret_cast<uint8_t *>(&portData), sizeof(portData));
encodeByteStream(sizeof(portData), reinterpret_cast<uint8_t *>(&portData), sizeof(portData));
}

/**
Expand All @@ -326,7 +326,7 @@ const
FirmataStream->write(major);
FirmataStream->write(minor);
for (i = 0; i < bytec; ++i) {
transformByteStreamToMessageBytes(sizeof(bytev[i]), reinterpret_cast<uint8_t *>(&bytev[i]), sizeof(bytev[i]));
encodeByteStream(sizeof(bytev[i]), reinterpret_cast<uint8_t *>(&bytev[i]));
}
FirmataStream->write(END_SYSEX);
}
Expand Down Expand Up @@ -393,7 +393,7 @@ const
FirmataStream->write(START_SYSEX);
FirmataStream->write(command);
for (i = 0; i < bytec; ++i) {
transformByteStreamToMessageBytes(sizeof(bytev[i]), reinterpret_cast<uint8_t *>(&bytev[i]), sizeof(bytev[i]));
encodeByteStream(sizeof(bytev[i]), reinterpret_cast<uint8_t *>(&bytev[i]));
}
FirmataStream->write(END_SYSEX);
}
Expand Down
6 changes: 1 addition & 5 deletions src/FirmataMarshaller.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@
#include <stdint.h>
#endif

#ifndef PARTICLE
#include <Stream.h>
#else
#include "Particle.h"
#endif

namespace firmata {

Expand Down Expand Up @@ -68,7 +64,7 @@ class FirmataMarshaller
void reportAnalog(uint8_t pin, bool stream_enable) const;
void reportDigitalPort(uint8_t portNumber, bool stream_enable) const;
void sendExtendedAnalog(uint8_t pin, size_t bytec, uint8_t * bytev) const;
void transformByteStreamToMessageBytes (size_t bytec, uint8_t * bytev, size_t max_bytes = 0) const;
void encodeByteStream (size_t bytec, uint8_t * bytev, size_t max_bytes = 0) const;

Stream * FirmataStream;
};
Expand Down
84 changes: 33 additions & 51 deletions src/FirmataParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

#include "FirmataConstants.h"

#include "Boards.h"

using namespace firmata;

//******************************************************************************
Expand Down Expand Up @@ -69,17 +67,6 @@ FirmataParser::FirmataParser(uint8_t * const dataBuffer, size_t dataBufferSize)
allowBufferUpdate = ((uint8_t *)NULL == dataBuffer);
}

void strobeBlinkPin(byte pin, int count, int onInterval, int offInterval)
{
byte i;
for (i = 0; i < count; i++) {
delay(offInterval);
digitalWrite(pin, HIGH);
delay(onInterval);
digitalWrite(pin, LOW);
}
}

//******************************************************************************
//* Public Methods
//******************************************************************************
Expand Down Expand Up @@ -132,11 +119,8 @@ void FirmataParser::parse(uint8_t inputData)
(*currentPinModeCallback)(currentPinModeCallbackContext, dataBuffer[1], dataBuffer[0]);
break;
case SET_DIGITAL_PIN_VALUE:

if (currentPinValueCallback)
{
(*currentPinValueCallback)(currentPinValueCallbackContext, dataBuffer[1], dataBuffer[0]);
}
break;
case REPORT_ANALOG:
if (currentReportAnalogCallback)
Expand All @@ -162,16 +146,9 @@ void FirmataParser::parse(uint8_t inputData)
case ANALOG_MESSAGE:
case DIGITAL_MESSAGE:
case SET_PIN_MODE:
//strobeBlinkPin(VERSION_BLINK_PIN, 3, 40, 210);
waitForData = 2; // two data bytes needed
executeMultiByteCommand = command;
//delay(500);
break;
case SET_DIGITAL_PIN_VALUE:
//strobeBlinkPin(VERSION_BLINK_PIN, 2, 40, 210);
waitForData = 2; // two data bytes needed
executeMultiByteCommand = command;
//delay(500);
break;
case REPORT_ANALOG:
case REPORT_DIGITAL:
Expand Down Expand Up @@ -423,6 +400,25 @@ bool FirmataParser::bufferDataAtPosition(const uint8_t data, const size_t pos)
return bufferOverflow;
}

/**
* Transform 7-bit firmata message into 8-bit stream
* @param bytec The encoded data byte length of the message (max: 16383).
* @param bytev A pointer to the encoded array of data bytes.
* @return The length of the decoded data.
* @note The conversion will be done in place on the provided buffer.
* @private
*/
size_t FirmataParser::decodeByteStream(size_t bytec, uint8_t * bytev) {
size_t decoded_bytes, i;

for ( i = 0, decoded_bytes = 0 ; i < bytec ; ++decoded_bytes, ++i ) {
bytev[decoded_bytes] = bytev[i];
bytev[decoded_bytes] |= (uint8_t)(bytev[++i] << 7);
}

return decoded_bytes;
}

/**
* Process incoming sysex messages. Handles REPORT_FIRMWARE and STRING_DATA internally.
* Calls callback function for STRING_DATA and all other sysex messages.
Expand All @@ -433,39 +429,25 @@ void FirmataParser::processSysexMessage(void)
switch (dataBuffer[0]) { //first byte in buffer is command
case REPORT_FIRMWARE:
if (currentReportFirmwareCallback) {
size_t sv_major = dataBuffer[1], sv_minor = dataBuffer[2];
size_t i = 0, j = 3;
while (j < sysexBytesRead) {
// The string length will only be at most half the size of the
// stored input buffer so we can decode the string within the buffer.
bufferDataAtPosition(dataBuffer[j], i);
++i;
++j;
const size_t major_version_offset = 1;
const size_t minor_version_offset = 2;
const size_t string_offset = 3;
// Test for malformed REPORT_FIRMWARE message (used to query firmware prior to Firmata v3.0.0)
if ( 3 > sysexBytesRead ) {
(*currentReportFirmwareCallback)(currentReportFirmwareCallbackContext, 0, 0, (const char *)NULL);
} else {
const size_t end_of_string = (string_offset + decodeByteStream((sysexBytesRead - string_offset), &dataBuffer[string_offset]));
bufferDataAtPosition('\0', end_of_string); // NULL terminate the string
(*currentReportFirmwareCallback)(currentReportFirmwareCallbackContext, (size_t)dataBuffer[major_version_offset], (size_t)dataBuffer[minor_version_offset], (const char *)&dataBuffer[string_offset]);
}
bufferDataAtPosition('\0', i); // Terminate the string
(*currentReportFirmwareCallback)(currentReportFirmwareCallbackContext, sv_major, sv_minor, (const char *)&dataBuffer[0]);
}
break;
case STRING_DATA:
if (currentStringCallback) {
size_t bufferLength = (sysexBytesRead - 1) / 2;
size_t i = 1, j = 0;
while (j < bufferLength) {
// The string length will only be at most half the size of the
// stored input buffer so we can decode the string within the buffer.
bufferDataAtPosition(dataBuffer[i], j);
++i;
bufferDataAtPosition((dataBuffer[j] + (dataBuffer[i] << 7)), j);
++i;
++j;
}
// Make sure string is null terminated. This may be the case for data
// coming from client libraries in languages that don't null terminate
// strings.
if (dataBuffer[j - 1] != '\0') {
bufferDataAtPosition('\0', j);
}
(*currentStringCallback)(currentStringCallbackContext, (const char *)&dataBuffer[0]);
const size_t string_offset = 1;
const size_t end_of_string = (string_offset + decodeByteStream((sysexBytesRead - string_offset), &dataBuffer[string_offset]));
bufferDataAtPosition('\0', end_of_string); // NULL terminate the string
(*currentStringCallback)(currentStringCallbackContext, (const char *)&dataBuffer[string_offset]);
}
break;
default:
Expand Down
1 change: 1 addition & 0 deletions src/FirmataParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class FirmataParser

/* private methods ------------------------------ */
bool bufferDataAtPosition(const uint8_t data, const size_t pos);
size_t decodeByteStream(size_t bytec, uint8_t * bytev);
void processSysexMessage(void);
void systemReset(void);
};
Expand Down

0 comments on commit 74754f4

Please sign in to comment.