Skip to content

Commit

Permalink
version callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
zfields committed Mar 15, 2017
1 parent 0d9e533 commit fbe73f2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Firmata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ FirmataClass::FirmataClass()
parser.attach(STRING_DATA, (FirmataParser::stringCallbackFunction)staticStringCallback, (void *)NULL);
parser.attach(START_SYSEX, (FirmataParser::sysexCallbackFunction)staticSysexCallback, (void *)NULL);
parser.attach(REPORT_FIRMWARE, (FirmataParser::versionCallbackFunction)staticReportFirmwareCallback, this);
parser.attach(REPORT_VERSION, (FirmataParser::systemCallbackFunction)staticReportVersionCallback, this);
parser.attach(REPORT_VERSION, (FirmataParser::versionCallbackFunction)staticReportVersionCallback, this);
parser.attach(SYSTEM_RESET, (FirmataParser::systemCallbackFunction)staticSystemResetCallback, (void *)NULL);
}

Expand Down
2 changes: 1 addition & 1 deletion Firmata.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class FirmataClass
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 staticReportVersionCallback (void * context, size_t, size_t, const char *) { if ( context ) { ((FirmataClass *)context)->printVersion(); } }
inline static void staticSystemResetCallback (void *) { if ( currentSystemResetCallback ) { currentSystemResetCallback(); } }
};

Expand Down
2 changes: 2 additions & 0 deletions FirmataMarshaller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ const
{
if ( (Stream *)NULL == FirmataStream ) { return; }
FirmataStream->write(REPORT_VERSION);
FirmataStream->write(PROTOCOL_MAJOR_VERSION);
FirmataStream->write(PROTOCOL_MINOR_VERSION);
}

/**
Expand Down
24 changes: 15 additions & 9 deletions FirmataParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ FirmataParser::FirmataParser(uint8_t * const dataBuffer, size_t dataBufferSize)
currentStringCallback((stringCallbackFunction)NULL),
currentSysexCallback((sysexCallbackFunction)NULL),
currentReportFirmwareCallback((versionCallbackFunction)NULL),
currentReportVersionCallback((systemCallbackFunction)NULL),
currentReportVersionCallback((versionCallbackFunction)NULL),
currentSystemResetCallback((systemCallbackFunction)NULL)
{
allowBufferUpdate = ((uint8_t *)NULL == dataBuffer);
Expand Down Expand Up @@ -130,6 +130,9 @@ void FirmataParser::parse(uint8_t inputData)
if (currentReportDigitalCallback)
(*currentReportDigitalCallback)(currentReportDigitalCallbackContext, multiByteChannel, dataBuffer[0]);
break;
case REPORT_VERSION:
if (currentReportVersionCallback)
(*currentReportVersionCallback)(currentReportVersionCallbackContext, dataBuffer[0], dataBuffer[1], (const char *)NULL);
}
executeMultiByteCommand = 0;
}
Expand Down Expand Up @@ -163,8 +166,8 @@ void FirmataParser::parse(uint8_t inputData)
systemReset();
break;
case REPORT_VERSION:
if (currentReportVersionCallback)
(*currentReportVersionCallback)(currentReportVersionCallbackContext);
waitForData = 2; // two data bytes needed
executeMultiByteCommand = command;
break;
}
}
Expand Down Expand Up @@ -244,12 +247,13 @@ void FirmataParser::attach(uint8_t command, callbackFunction newFunction, void *
}

/**
* Attach a version callback function (supported option: REPORT_FIRMWARE).
* Attach a version callback function (supported options are: REPORT_FIRMWARE, REPORT_VERSION).
* @param command The ID of the command to attach a callback function to.
* @param newFunction A reference to the callback function to attach.
* @param context An optional context to be provided to the callback function (NULL by default).
* @note The context parameter is provided so you can pass a parameter, by reference, to
* your callback function.
* @note The description value in the REPORT_VERSION callback will always be NULL
*/
void FirmataParser::attach(uint8_t command, versionCallbackFunction newFunction, void * context)
{
Expand All @@ -258,11 +262,15 @@ void FirmataParser::attach(uint8_t command, versionCallbackFunction newFunction,
currentReportFirmwareCallback = newFunction;
currentReportFirmwareCallbackContext = context;
break;
case REPORT_VERSION:
currentReportVersionCallback = newFunction;
currentReportVersionCallbackContext = context;
break;
}
}

/**
* Attach a system callback function (supported options are: SYSTEM_RESET, REPORT_VERSION).
* Attach a system callback function (supported option: SYSTEM_RESET).
* @param command The ID of the command to attach a callback function to.
* @param newFunction A reference to the callback function to attach.
* @param context An optional context to be provided to the callback function (NULL by default).
Expand All @@ -272,10 +280,6 @@ void FirmataParser::attach(uint8_t command, versionCallbackFunction newFunction,
void FirmataParser::attach(uint8_t command, systemCallbackFunction newFunction, void * context)
{
switch (command) {
case REPORT_VERSION:
currentReportVersionCallback = newFunction;
currentReportVersionCallbackContext = context;
break;
case SYSTEM_RESET:
currentSystemResetCallback = newFunction;
currentSystemResetCallbackContext = context;
Expand Down Expand Up @@ -341,6 +345,8 @@ void FirmataParser::detach(uint8_t command)
attach(command, (versionCallbackFunction)NULL, NULL);
break;
case REPORT_VERSION:
attach(command, (versionCallbackFunction)NULL, NULL);
break;
case SYSTEM_RESET:
attach(command, (systemCallbackFunction)NULL, NULL);
break;
Expand Down
2 changes: 1 addition & 1 deletion FirmataParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class FirmataParser
stringCallbackFunction currentStringCallback;
sysexCallbackFunction currentSysexCallback;
versionCallbackFunction currentReportFirmwareCallback;
systemCallbackFunction currentReportVersionCallback;
versionCallbackFunction currentReportVersionCallback;
systemCallbackFunction currentSystemResetCallback;

/* private methods ------------------------------ */
Expand Down

0 comments on commit fbe73f2

Please sign in to comment.