-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDmxReceiver.h
109 lines (97 loc) · 2.67 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
#ifndef DmxReceiver_h
#define DmxReceiver_h
#include <Arduino.h>
/**
* Arduino library for receiving DMX signals.
*
* Based on dmx_serial_sender by Kevin Deus:
* https://github.com/ktgow/dmx_serial_sender
*/
class DmxReceiver {
public:
/**
* Constructor.
*
* @param pin pin to receive data from
* @param channels number of channels to receive
* @todo Add channelOffset parameter.
*/
DmxReceiver(byte pin, unsigned int channels);
/**
* Destructor.
*/
~DmxReceiver();
/**
* Check for new data. Call as often as possible from your
* main loop.
*
* @return true if new data was available, otherwise false
*/
bool poll();
/**
* Returns the value of the given channel.
*
* @param channel index of the channel
* @return value of the channel or zero, if an invalid index was given
*/
byte getValue(unsigned int channel);
private:
/**
* Waits until the value of the given pin changes or a timeout occurs.
*
* @param pinValue contains the value of the pin at the beginning
* @param valueTime contains the time until the pin changed
* @param timeoutUs timeout in microseconds
* @return false if a timeout occured, otherwise true
*/
bool readPinUntilChange(unsigned char* pinValue, unsigned long* valueTime,
unsigned long timeoutUs);
/**
* Configuration.
*/
byte _pin;
unsigned int _channels;
/**
* Value of millis() when the last data was read successfully.
*/
unsigned long _lastRead;
/**
* Values of the channels.
*/
byte* _data;
};
#define DMXRECEIVER_DELAY_1_US \
__asm__ __volatile__ ( \
"nop" "\n\t" \
"nop" "\n\t" \
"nop" "\n\t" \
"nop" "\n\t" \
"nop" "\n\t" \
"nop" "\n\t" \
"nop" "\n\t" \
"nop" "\n\t" \
"nop" "\n\t" \
"nop" "\n\t" \
"nop" "\n\t" \
"nop" "\n\t" \
"nop" "\n\t" \
"nop" "\n\t" \
"nop" "\n\t" \
"nop" "\n\t" \
)
#define DMXRECEIVER_DELAY_2_US \
DMXRECEIVER_DELAY_1_US; \
DMXRECEIVER_DELAY_1_US
#define DMXRECEIVER_DELAY_AFTER_READ \
__asm__ __volatile__ ( \
"nop" "\n\t" \
"nop" "\n\t" \
"nop" "\n\t" \
"nop" "\n\t" \
)
#define DMXRECEIVER_DELAY_4_US_AFTER_READ \
DMXRECEIVER_DELAY_AFTER_READ; \
DMXRECEIVER_DELAY_1_US; \
DMXRECEIVER_DELAY_1_US; \
DMXRECEIVER_DELAY_1_US
#endif