summaryrefslogtreecommitdiff
path: root/OpenModelicaArduino/Firmware/Tiva C/StandardFirmata/FirmataParser.h
blob: bb0c8be8a7ba6a01bab8a89a40bfe42d62010742 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
  FirmataParser.h
  Copyright (c) 2006-2008 Hans-Christoph Steiner.  All rights reserved.
  Copyright (C) 2009-2016 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
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  See file LICENSE.txt for further informations on licensing terms.
*/

#ifndef FirmataParser_h
#define FirmataParser_h

#if defined(__cplusplus) && !defined(ARDUINO)
  #include <cstddef>
  #include <cstdint>
#else
  #include <stddef.h>
  #include <stdint.h>
#endif

namespace firmata {

class FirmataParser
{
  public:
    /* callback function types */
    typedef void (*callbackFunction)(void * context, uint8_t command, uint16_t value);
    typedef void (*dataBufferOverflowCallbackFunction)(void * context);
    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 * firmware);

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

    /* serial receive handling */
    void parse(uint8_t value);
    bool isParsingMessage(void) const;
    int setDataBufferOfSize(uint8_t * dataBuffer, size_t dataBufferSize);

    /* attach & detach callback functions to messages */
    void attach(uint8_t command, callbackFunction newFunction, void * context = NULL);
    void attach(dataBufferOverflowCallbackFunction newFunction, void * context = NULL);
    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);

  private:
    /* input message handling */
    bool allowBufferUpdate;
    uint8_t * dataBuffer; // multi-byte data
    size_t dataBufferSize;
    uint8_t executeMultiByteCommand; // execute this after getting multi-byte data
    uint8_t multiByteChannel; // channel data for multiByteCommands
    size_t waitForData; // this flag says the next serial input will be data

    /* sysex */
    bool parsingSysex;
    size_t sysexBytesRead;

    /* callback context */
    void * currentAnalogCallbackContext;
    void * currentDigitalCallbackContext;
    void * currentReportAnalogCallbackContext;
    void * currentReportDigitalCallbackContext;
    void * currentPinModeCallbackContext;
    void * currentPinValueCallbackContext;
    void * currentReportFirmwareCallbackContext;
    void * currentReportVersionCallbackContext;
    void * currentDataBufferOverflowCallbackContext;
    void * currentStringCallbackContext;
    void * currentSysexCallbackContext;
    void * currentSystemResetCallbackContext;

    /* callback functions */
    callbackFunction currentAnalogCallback;
    callbackFunction currentDigitalCallback;
    callbackFunction currentReportAnalogCallback;
    callbackFunction currentReportDigitalCallback;
    callbackFunction currentPinModeCallback;
    callbackFunction currentPinValueCallback;
    dataBufferOverflowCallbackFunction currentDataBufferOverflowCallback;
    stringCallbackFunction currentStringCallback;
    sysexCallbackFunction currentSysexCallback;
    versionCallbackFunction currentReportFirmwareCallback;
    systemCallbackFunction currentReportVersionCallback;
    systemCallbackFunction currentSystemResetCallback;

    /* 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);
};

} // firmata

#endif /* FirmataParser_h */