3 Copyright (c) 2006-2008 Hans-Christoph Steiner. All rights reserved.
4 Copyright (C) 2009-2016 Jeff Hoefs. All rights reserved.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 See file LICENSE.txt for further informations on licensing terms.
14 #ifndef FirmataParser_h
15 #define FirmataParser_h
17 #if defined(__cplusplus) && !defined(ARDUINO)
30 /* callback function types */
31 typedef void (*callbackFunction)(void * context, uint8_t command, uint16_t value);
32 typedef void (*dataBufferOverflowCallbackFunction)(void * context);
33 typedef void (*stringCallbackFunction)(void * context, const char * c_str);
34 typedef void (*sysexCallbackFunction)(void * context, uint8_t command, size_t argc, uint8_t * argv);
35 typedef void (*systemCallbackFunction)(void * context);
36 typedef void (*versionCallbackFunction)(void * context, size_t sv_major, size_t sv_minor, const char * firmware);
38 FirmataParser(uint8_t * dataBuffer = (uint8_t *)NULL, size_t dataBufferSize = 0);
40 /* serial receive handling */
41 void parse(uint8_t value);
42 bool isParsingMessage(void) const;
43 int setDataBufferOfSize(uint8_t * dataBuffer, size_t dataBufferSize);
45 /* attach & detach callback functions to messages */
46 void attach(uint8_t command, callbackFunction newFunction, void * context = NULL);
47 void attach(dataBufferOverflowCallbackFunction newFunction, void * context = NULL);
48 void attach(uint8_t command, stringCallbackFunction newFunction, void * context = NULL);
49 void attach(uint8_t command, sysexCallbackFunction newFunction, void * context = NULL);
50 void attach(uint8_t command, systemCallbackFunction newFunction, void * context = NULL);
51 void attach(uint8_t command, versionCallbackFunction newFunction, void * context = NULL);
52 void detach(uint8_t command);
53 void detach(dataBufferOverflowCallbackFunction);
56 /* input message handling */
57 bool allowBufferUpdate;
58 uint8_t * dataBuffer; // multi-byte data
59 size_t dataBufferSize;
60 uint8_t executeMultiByteCommand; // execute this after getting multi-byte data
61 uint8_t multiByteChannel; // channel data for multiByteCommands
62 size_t waitForData; // this flag says the next serial input will be data
66 size_t sysexBytesRead;
68 /* callback context */
69 void * currentAnalogCallbackContext;
70 void * currentDigitalCallbackContext;
71 void * currentReportAnalogCallbackContext;
72 void * currentReportDigitalCallbackContext;
73 void * currentPinModeCallbackContext;
74 void * currentPinValueCallbackContext;
75 void * currentReportFirmwareCallbackContext;
76 void * currentReportVersionCallbackContext;
77 void * currentDataBufferOverflowCallbackContext;
78 void * currentStringCallbackContext;
79 void * currentSysexCallbackContext;
80 void * currentSystemResetCallbackContext;
82 /* callback functions */
83 callbackFunction currentAnalogCallback;
84 callbackFunction currentDigitalCallback;
85 callbackFunction currentReportAnalogCallback;
86 callbackFunction currentReportDigitalCallback;
87 callbackFunction currentPinModeCallback;
88 callbackFunction currentPinValueCallback;
89 dataBufferOverflowCallbackFunction currentDataBufferOverflowCallback;
90 stringCallbackFunction currentStringCallback;
91 sysexCallbackFunction currentSysexCallback;
92 versionCallbackFunction currentReportFirmwareCallback;
93 systemCallbackFunction currentReportVersionCallback;
94 systemCallbackFunction currentSystemResetCallback;
96 /* private methods ------------------------------ */
97 bool bufferDataAtPosition(const uint8_t data, const size_t pos);
98 size_t decodeByteStream(size_t bytec, uint8_t * bytev);
99 void processSysexMessage(void);
100 void systemReset(void);
105 #endif /* FirmataParser_h */