Skip to content

Commit

Permalink
version callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
zfields committed Mar 12, 2017
1 parent 168a0c0 commit 94aba8f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
6 changes: 3 additions & 3 deletions Firmata.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ class FirmataClass
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 *, char * c_str) { if ( currentStringCallback ) { currentStringCallback(c_str); } }
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) { if ( context ) { ((FirmataClass *)context)->printFirmwareVersion(); } }
inline static void staticReportVersionCallback (void * context) { if ( context ) { ((FirmataClass *)context)->printVersion(); } }
inline static void staticReportFirmwareCallback (void * context, size_t sv_major, size_t sv_minor, const char * description) { (void)sv_major; (void)sv_minor; (void)description; if ( context ) { ((FirmataClass *)context)->printFirmwareVersion(); } }
inline static void staticReportVersionCallback (void * context, size_t sv_major, size_t sv_minor, const char * description) { (void)sv_major; (void)sv_minor; (void)description; if ( context ) { ((FirmataClass *)context)->printVersion(); } }
inline static void staticSystemResetCallback (void *) { if ( currentSystemResetCallback ) { currentSystemResetCallback(); } }
};

Expand Down
53 changes: 41 additions & 12 deletions FirmataParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ FirmataParser::FirmataParser(uint8_t * const dataBuffer, size_t dataBufferSize)
currentDataBufferOverflowCallback((dataBufferOverflowCallbackFunction)NULL),
currentStringCallback((stringCallbackFunction)NULL),
currentSysexCallback((sysexCallbackFunction)NULL),
currentReportFirmwareCallback((systemCallbackFunction)NULL),
currentReportVersionCallback((systemCallbackFunction)NULL),
currentReportFirmwareCallback((versionCallbackFunction)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,15 +247,15 @@ void FirmataParser::attach(uint8_t command, callbackFunction newFunction, void *
}

/**
* Attach a system callback function (options are: REPORT_FIRMWARE, REPORT_VERSION
* and SYSTEM_RESET).
* Attach a version callback function (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, systemCallbackFunction newFunction, void * context)
void FirmataParser::attach(uint8_t command, versionCallbackFunction newFunction, void * context)
{
switch (command) {
case REPORT_FIRMWARE:
Expand All @@ -263,6 +266,20 @@ void FirmataParser::attach(uint8_t command, systemCallbackFunction newFunction,
currentReportVersionCallback = newFunction;
currentReportVersionCallbackContext = context;
break;
}
}

/**
* Attach a system callback function (currently supports 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).
* @note The context parameter is provided so you can pass a parameter, by reference, to
* your callback function.
*/
void FirmataParser::attach(uint8_t command, systemCallbackFunction newFunction, void * context)
{
switch (command) {
case SYSTEM_RESET:
currentSystemResetCallback = newFunction;
currentSystemResetCallbackContext = context;
Expand Down Expand Up @@ -326,6 +343,8 @@ void FirmataParser::detach(uint8_t command)
switch (command) {
case REPORT_FIRMWARE:
case REPORT_VERSION:
attach(command, (versionCallbackFunction)NULL, NULL);
break;
case SYSTEM_RESET:
attach(command, (systemCallbackFunction)NULL, NULL);
break;
Expand Down Expand Up @@ -394,14 +413,24 @@ void FirmataParser::processSysexMessage(void)
{
switch (dataBuffer[0]) { //first byte in buffer is command
case REPORT_FIRMWARE:
if (currentReportFirmwareCallback)
(*currentReportFirmwareCallback)(currentReportFirmwareCallbackContext);
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;
}
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;
size_t j = 0;
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.
Expand All @@ -417,7 +446,7 @@ void FirmataParser::processSysexMessage(void)
if (dataBuffer[j - 1] != '\0') {
bufferDataAtPosition('\0', j);
}
(*currentStringCallback)(currentStringCallbackContext, (char *)&dataBuffer[0]);
(*currentStringCallback)(currentStringCallbackContext, (const char *)&dataBuffer[0]);
}
break;
default:
Expand Down
10 changes: 6 additions & 4 deletions FirmataParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ class FirmataParser
/* callback function types */
typedef void (*callbackFunction)(void * context, uint8_t command, uint16_t value);
typedef void (*dataBufferOverflowCallbackFunction)(void * context);
typedef void (*stringCallbackFunction)(void * context, char * c_str);
typedef void (*stringCallbackFunction)(void * context, const char * c_str);
typedef void (*sysexCallbackFunction)(void * context, uint8_t command, size_t argc, uint8_t * argv);
typedef void (*systemCallbackFunction)(void * context);
typedef void (*versionCallbackFunction)(void * context, size_t sv_major, size_t sv_minor, const char * description);

FirmataParser(uint8_t * dataBuffer = (uint8_t *)NULL, size_t dataBufferSize = 0);

Expand All @@ -47,6 +48,7 @@ class FirmataParser
void attach(uint8_t command, stringCallbackFunction newFunction, void * context = NULL);
void attach(uint8_t command, sysexCallbackFunction newFunction, void * context = NULL);
void attach(uint8_t command, systemCallbackFunction newFunction, void * context = NULL);
void attach(uint8_t command, versionCallbackFunction newFunction, void * context = NULL);
void detach(uint8_t command);
void detach(dataBufferOverflowCallbackFunction);

Expand Down Expand Up @@ -87,14 +89,14 @@ class FirmataParser
dataBufferOverflowCallbackFunction currentDataBufferOverflowCallback;
stringCallbackFunction currentStringCallback;
sysexCallbackFunction currentSysexCallback;
systemCallbackFunction currentReportFirmwareCallback;
systemCallbackFunction currentReportVersionCallback;
versionCallbackFunction currentReportFirmwareCallback;
versionCallbackFunction currentReportVersionCallback;
systemCallbackFunction currentSystemResetCallback;

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

} // firmata
Expand Down

0 comments on commit 94aba8f

Please sign in to comment.