-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLeoDMX.class.cpp
More file actions
96 lines (72 loc) · 2.18 KB
/
LeoDMX.class.cpp
File metadata and controls
96 lines (72 loc) · 2.18 KB
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
/**
* LeoDMX Class
* Methods
*
* (see header for information)
*/
// LeoDMX header
#include <LeoDMX.class.h>
// macro: coerce a channel index value (unsigned integer)
#define coerceIndex(index) ( (index) > 511 ? 511 : (index) )
/* :: Private Methods :: */
// send DMX bus initialization sequence and activate to UART
void LeoDMX::startFrame()
{
// set data pin as output
bitWrite( *this->portDirection, this->txPort, 1 );
// set transmit enable flag
if ( this->setEnable )
{
bitWrite( *this->portDirection, this->enablePort, 1 );
bitWrite( *this->portData, this->enablePort, 1 );
}
// “break” period (>= 88 µs low)
bitWrite( *this->portData, this->txPort, 0 );
delayMicroseconds(89);
// “mark after break” period (>= 8µs high)
bitWrite( *this->portData, this->txPort, 1 );
delayMicroseconds(9);
// switch to UART output
// (250 kbit/s, no parity, 8 data bits, 2 stop bits)
this->UART->begin( 250000, SERIAL_8N2 );
// send start null byte
this->UART->write( 0x0 );
}
// reset DMX bus to idle state
void LeoDMX::endFrame()
{
// end UART mode
this->UART->end();
// bus idle state is high
bitWrite( *this->portData, this->txPort, 1 );
// reset transmit enable flag
if ( this->setEnable )
{
bitWrite( *this->portData, this->enablePort, 0 );
}
}
/* :: Public Methods :: */
// send a DMX-512 frame with channel data
void LeoDMX::send( unsigned char channels[], unsigned short length/*=512*/ )
{
// coerce length
length = coerceIndex( length );
// send initialization sequence and activate UART
this->startFrame();
// send all channel values
this->UART->write( channels, length );
// wait until all bytes are sent
this->UART->flush();
// deactivate UART and set bus to idle state
this->endFrame();
}
// send a null frame with all channels set to zero
void LeoDMX::null( unsigned short length/*=512*/ )
{
// array of zeroes
static unsigned char zeroes[512] = {0};
// coerce length
length = coerceIndex( length );
// send frame
this->send( zeroes, length );
}