Open Lighting Architecture  Latest Git
SPIDMXParser.h
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  * SPIDMXParser.h
17  * This parses a SPI buffer into a DmxBuffer and notifies a callback when a
18  * packet is received completely.
19  * Copyright (C) 2017 Florian Edelmann
20  */
21 
22 #ifndef PLUGINS_SPIDMX_SPIDMXPARSER_H_
23 #define PLUGINS_SPIDMX_SPIDMXPARSER_H_
24 
25 #include "ola/Callback.h"
26 #include "ola/DmxBuffer.h"
27 #include "ola/base/Macro.h"
28 
29 namespace ola {
30 namespace plugin {
31 namespace spidmx {
32 
33 class SPIDMXParser {
34  public:
35  SPIDMXParser(DmxBuffer *buffer, Callback0<void> *callback)
36  : m_dmx_buffer(buffer),
37  m_callback(callback),
38  m_state(WAIT_FOR_BREAK), // reset in ChangeState()
39  m_chunk(NULL), // reset in ParseDmx()
40  m_chunk_spi_bytecount(0), // first reset in ParseDmx()
41  m_state_spi_bitcount(0), // first reset in ChangeState()
42  m_current_dmx_value(0), // first reset in InDataStartbit()
43  m_channel_count(0), // first reset in ChangeState()
44  m_sampling_position(0) { // reset in InDataStartbit()
45  }
46  void ParseDmx(uint8_t *buffer, uint64_t chunksize);
47  void SetCallback(Callback0<void> *callback) {
48  m_callback = callback;
49  }
50 
51  private:
52  typedef enum {
53  WAIT_FOR_BREAK,
54  IN_BREAK,
55  WAIT_FOR_MAB,
56  IN_MAB,
57  IN_STARTCODE,
58  IN_STARTCODE_STOPBITS,
59  IN_DATA_STARTBIT,
60  IN_DATA_BITS,
61  IN_DATA_STOPBITS
62  } dmx_state_t;
63 
64  // helper functions
65  int8_t DetectFallingEdge(uint8_t byte);
66  int8_t DetectRisingEdge(uint8_t byte);
67 
68  void ChangeState(SPIDMXParser::dmx_state_t new_state);
69  void PacketComplete();
70 
71  // handle one state each
72  void WaitForBreak();
73  void InBreak();
74  void WaitForMab();
75  void InMab();
76  void InStartcode();
77  void InStartcodeStopbits();
78  void InDataStartbit();
79  void InDataBits();
80  void InLastDataBit();
81  void InDataStopbits();
82 
84  DmxBuffer *m_dmx_buffer;
85 
87  Callback0<void> *m_callback;
88 
90  SPIDMXParser::dmx_state_t m_state;
91 
93  uint8_t *m_chunk;
94 
96  uint64_t m_chunk_spi_bytecount;
97 
102  uint64_t m_state_spi_bitcount;
103 
105  uint8_t m_current_dmx_value;
106 
108  int16_t m_channel_count;
109 
111  uint8_t m_sampling_position;
112 
113  DISALLOW_COPY_AND_ASSIGN(SPIDMXParser);
114 };
115 
116 } // namespace spidmx
117 } // namespace plugin
118 } // namespace ola
119 #endif // PLUGINS_SPIDMX_SPIDMXPARSER_H_
Used to hold a single universe of DMX data.
Definition: DmxBuffer.h:49
A class used to hold a single universe of DMX data.
Helper macros.
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
Definition: SPIDMXParser.h:33
void ParseDmx(uint8_t *buffer, uint64_t chunksize)
Definition: SPIDMXParser.cpp:56