-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmxreceiver.h
268 lines (239 loc) · 7.28 KB
/
dmxreceiver.h
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* Kabuki Tek Toolkit @version 0.x
@link https://github.com/kabuki-starship/kabuki.toolkit.tek.git
@file /dmx_receiver.h
@author Cale McCollough <https://cookingwithcale.org>
@license Copyright 2014-20 (C) Kabuki Starship (TM) <kabukistarship.com>.
This Source Code Form is subject to the terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not distributed with this file, You can obtain one
at <https://mozilla.org/MPL/2.0/>. */
#pragma once
#include <_config.h>
#ifndef KABUKI_TEK_DMX_DMXRECIEVER_H
#define KABUKI_TEK_DMX_DMXRECIEVER_H
namespace _ {
/* DMXReceiver receiver.
@param kChannelCount The number of DMX channels (must be less than 512).
*/
template <IUC kChannelCount>
class DMXReceiver {
public:
enum {
kNumDMXChannels = 512,
kDMXStartCode = 0,
kDMXTimeBreak = 100, // 100us (88us-1s)
kDMXTimeMAB = 12, // 12us (8us-1s)
DMXTimeMBB = 200 // 10us (0-1s)
};
enum DMXModes {
kStateBegin,
kStateDMXStart,
kStateMode,
kStateMAB,
kStateData,
kStateError,
kStateDMXStop,
};
/* Simple constructor.
@param TxPin UART Tx pin.
@param RxPin UART Rx pin. */
DMXReceiver(PinName TxPin, PinName RxPin) : rawSerial(Tx, Rx) {
Clear();
// state = kStateBegin;
state_ = kStateBegin;
dataReceived = 0;
dataSent = 0;
memset(buffer, 0, sizeof(buffer));
memset(data_rx, 0, sizeof(data_rx));
timeBreak = kDMXTimeBreak;
timeMAB = kDMXTimeMAB;
timeMBB = DMXTimeMBB;
#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
if (p_rx == p10) {
uart = LPC_UART3;
NVIC_SetPriority(UART3_IRQn, 1);
} else if (p_rx == p14) {
uart = (LPC_UART_TypeDef*)LPC_UART1;
NVIC_SetPriority(UART1_IRQn, 1);
} else if (p_rx == p27) {
uart = LPC_UART2;
NVIC_SetPriority(UART2_IRQn, 1);
}
#elif defined(TARGET_LPC11U24)
if (p_rx == p10) {
uart = LPC_USART;
NVIC_SetPriority(UART_IRQn, 1);
}
#elif defined(TARGET_K64F) || defined(TARGET_KL46Z)
// Need to associate the _uart private variable with the user selected pin.
// Hook the interrupt pin.
if ((p_rx == PTE21) || (p_rx == PTA1)) {
_uart = (UARTLP_Type *)UART0;
NVIC_SetPriority(UART0_IRQn, 1);
} else if (p_rx == PTE1) {
_uart = (UARTLP_Type *)UART1;
NVIC_SetPriority(UART1_IRQn, 1);
} else if ((p_rx == PTE23) || (p_rx == PTE17)) {
_uart = (UARTLP_Type *)UART2;
NVIC_SetPriority(UART2_IRQn, 1);
}
#endif
serial.baud(250000);
serial.format(8, Serial::None, 2);
serial.attach(this, &DMXReceiver::InitSerial, Serial::RxIrq);
// timeout.attach_us (this, &DMXReceiver::InitTimer, DMX_TIME_BETWEEN);
}
/* Send the data
@param Index DMX channel 0-511.
@param Value DMX channel data 0-255. */
void SetChannel(IUB Index, IUA Value) {
if (Index >= kNumDMXChannels || Index < StartChannel ||
Index > StartChannel + kChannelCount - 1) {
return;
}
buffer[Index] = Value;
}
/* Gets the channel data at the given index.
@param Index DMX channel 0-511.
@param Value DMX channel data 0-255. */
IUA GetChannel(IUC index) {
if (index >= StartChannel + kChannelCount) return 0;
return data[index];
}
/* Send the data
@param Buffer DMX data buffer
@param Index DMX data address
@param Length Data length */
void Copy(IUA* Buffer, ISC Index = 0, ISC Length = kNumDMXChannels) {
if (Index >= kNumDMXChannels || Length > kNumDMXChannels - Index ||
Buffer == nullptr)
return;
memcpy(buf, &data_rx[Index], len);
}
/* Start DMX send operation */
void Start() {
if (state_ == kStateDMXStop) {
state_ = kStateBegin;
dataSent = 0;
timeout.attach_us(this, &DMXReceiver::InitTimer, timeMBB);
}
}
/* Stop DMX send operation */
void Stop() {
serial.attach(0, Serial::TxIrq);
timeout.detach();
state_ = kStateDMXStop;
}
/* Clear DMX data */
void Clear() {
ISC i;
for (i = 0; i < kNumDMXChannels; i++) {
buffer[i] = 0;
data_rx[i] = 0;
}
}
/* Marks the DMX data as being received. */
ISC MarkReceived() {
ISC r = dataReceived;
dataReceived = 0;
return r;
}
/* Marks data as being sent. */
ISC MarkSent() {
ISC r = dataSent;
dataSent = 0;
return r;
}
/* Checks the timing parameters. */
void CheckTimingParameters() {
if (BreakTime < 88 || BreakTime > 1000000) return -1;
if (MABTime < 8 || MABTime > 1000000) return -1;
if (MBBTime < 0 || MBBTime > 1000000) return -1;
timeBreak = BreakTime;
timeMAB = MABTime;
timeMBB = MBBTime;
return 0;
}
private:
#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) || \
defined(TARGET_LPC4088) || defined(TARGET_LPC11XX)
LPCserial_TypeDef* uart;
#elif defined(TARGET_LPC11UXX)
LPC_USART_Type* uart;
#else
///#error "CPU not supported."
Assert(false, "CPU not supported!");
#endif
Serial dmx_; //< The DMX serial port.
Timeout timeout_; //< The timeout timer.
volatile DMXState state_; //< The current state.
volatile ISC start_channel_, //< The start DMX channel.
channel_count_, //< The number of DMX channels.
index_; //< The index in the 512 DMX channels.
ISC is_done_; //< Flag for if the Rx or Tx process is done.
IUA* data_; //< Pointer to the data.
void InitTimer() { //< Initializes the timer.
switch (state_) {
case kStateBegin: //< Break Time
timeout.detach();
rawSerial->LCR |= (1 << 6);
state_ = kStateMode;
timeout.attach_us(this, &DMXReceiver::InitTimer, timeBreak);
break;
case kStateMode: //< Mark After Break
timeout.detach();
rawSerial->LCR &= ~(1 << 6);
state_ = kStateMAB;
timeout.attach_us(this, &DMXReceiver::InitTimer, timeMAB);
break;
case kStateMAB: //< Start code
timeout.detach();
Channel_tx = 0;
state_ = kStateData;
serial.attach(this, &DMXReceiver::InitSerial, Serial::TxIrq);
#ifdef DMXserial_DIRECT
while (!(rawSerial->LSR & (1 << 5)))
;
rawSerial->THR = DMX_START_CODE;
#else
serial.putc(DMX_START_CODE);
#endif
break;
}
}
void InitSerial() {
ISC flg, dat;
flg = rawSerial->LSR;
#ifdef DMXserial_DIRECT
dat = rawSerial->RBR;
#else
dat = serial.getc();
#endif
if (flg & ((1 << 7) | (1 << 3) | (1 << 4))) {
// Break Time
if (Channel_rx >= 24 && state_ == kStateData) {
dataReceived = 1;
}
state_ = kStateMode;
return;
}
if (state_ == kStateMode) {
// Start Code
if (dat == DMX_START_CODE) {
Channel_rx = 0;
state_ = kStateData;
} else {
state_ = kStateError;
}
} else if (state_ == kStateData) {
// Data
data_rx[Channel_rx] = dat;
Channel_rx++;
if (Channel_rx >= kNumDMXChannels) {
dataReceived = 1;
state_ = kStateBegin;
}
}
}
};
} // namespace _
#endif