/
RDMDeviceTest.ino
198 lines (161 loc) · 6.32 KB
/
RDMDeviceTest.ino
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
/**************************************************************************/
/*!
@file RDMDeviceTest.ino
@author Claude Heintz
@license BSD (see LXESP8266DMX LICENSE)
@copyright 2017 by Claude Heintz
Example showing LXESP8266DMX RDM support for devices.
Control brightness of LED on GPIO14 with DMX address 1 (settable via RDM)
@section HISTORY
v1.00 - First release
*/
/**************************************************************************/
#include <LXESP8266UARTDMX.h>
#include <rdm/rdm_utility.h>
#include "ESP8266WiFi.h"
int got_dmx = 0;
int got_rdm = 0;
uint8_t discovery_enabled = 1;
uint16_t start_address = 1;
uint16_t input_value = 0;
uint8_t device_label[33];
#define DEFAULT_DEVICE_LABEL "RDM dev test v1.0"
#define MFG_LABEL "LXDMX"
#define MODEL_DESCRIPTION "RDMDeviceTest"
#define DIRECTION_PIN 15
#define LED_PIN 14
void setup() {
WiFi.forceSleepBegin();
delay(5000);
Serial.setDebugOutput(1); //use uart0 for debugging
pinMode(BUILTIN_LED, OUTPUT);
pinMode(DIRECTION_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
//diagnostic pins
pinMode(12, OUTPUT);
pinMode(16, INPUT_PULLUP);
strcpy((char*)device_label, DEFAULT_DEVICE_LABEL);
ESP8266DMX.setDataReceivedCallback(&gotDMXCallback);
ESP8266DMX.setRDMReceivedCallback(&gotRDMCallback);
LX8266DMX::THIS_DEVICE_ID.setBytes(0x6C, 0x78, 0x0F, 0x0A, 0x0C, 0x0E); //change device ID from default
ESP8266DMX.startRDM(DIRECTION_PIN, DMX_TASK_RECEIVE);
}
// ***************** input callback function *************
ICACHE_RAM_ATTR void gotDMXCallback(int slots) {
got_dmx = slots;
}
ICACHE_RAM_ATTR void gotRDMCallback(int len) {
// rdm start code and checksum are validated before this is called
got_rdm = len;
}
/************************************************************************
The main loop checks to see if dmx input is available (got_dmx>0)
And then reads the level of dimmer 1 to set PWM level of LED connected to pin 14
*************************************************************************/
void loop() {
if ( got_dmx ) {
input_value = ESP8266DMX.getSlot(start_address);
//gamma correct
input_value = (input_value * input_value ) / 255;
//ESP8266 PWM is 10bit 0-1024
analogWrite(LED_PIN,2*input_value);
got_dmx = 0; //reset
} else if ( got_rdm ) {
uint8_t* rdmdata = ESP8266DMX.receivedRDMData();
uint8_t cmdclass = rdmdata[RDM_IDX_CMD_CLASS];
uint16_t pid = (rdmdata[RDM_IDX_PID_MSB] << 8 ) | rdmdata[RDM_IDX_PID_LSB];
if ( cmdclass == RDM_DISCOVERY_COMMAND ) {
if ( pid == RDM_DISC_UNIQUE_BRANCH ) {
if ( discovery_enabled ) {
uint64_t tv = ESP8266DMX.THIS_DEVICE_ID.getValue();
UID u;
u.setBytes(&rdmdata[24]); //lower
uint64_t uv = u.getValue();
if ( tv >= uv ) {
u.setBytes(&rdmdata[30]); //upper
uv = u.getValue();
if ( tv <= uv ) {
ESP8266DMX.sendRDMDiscoverBranchResponse();
}
}
}
} else { // mute RDM_DISCOVERY_COMMAND PIDs
UID destination;
destination.setBytes(&rdmdata[RDM_IDX_DESTINATION_UID]);
if ( pid == RDM_DISC_MUTE ) {
if ( destination == ESP8266DMX.THIS_DEVICE_ID ) {
discovery_enabled = 0;
// send ACK
UID source;
source.setBytes(&rdmdata[RDM_IDX_SOURCE_UID]);
ESP8266DMX.sendMuteAckRDMResponse(RDM_DISC_COMMAND_RESPONSE, source, RDM_DISC_MUTE);
}
} else if ( pid == RDM_DISC_UNMUTE ) {
if ( destination == BROADCAST_ALL_DEVICES_ID ) {
// just un-mute
discovery_enabled = 1;
} else if ( destination == ESP8266DMX.THIS_DEVICE_ID ) {
discovery_enabled = 1;
// send ACK
UID source;
source.setBytes(&rdmdata[RDM_IDX_SOURCE_UID]);
ESP8266DMX.sendMuteAckRDMResponse(RDM_DISC_COMMAND_RESPONSE, source, RDM_DISC_UNMUTE);
}
}
}
} else if ( cmdclass == RDM_GET_COMMAND ) {
UID destination;
destination.setBytes(&rdmdata[RDM_IDX_DESTINATION_UID]);
if ( destination == ESP8266DMX.THIS_DEVICE_ID ) {
UID source;
source.setBytes(&rdmdata[RDM_IDX_SOURCE_UID]);
if ( pid == RDM_DEVICE_START_ADDR ) {
uint8_t sa[2];
sa[0] = start_address >> 8;
sa[1] = start_address & 0xff;
ESP8266DMX.sendRDMGetResponse(source, pid, sa, 2);
} else if ( pid == RDM_DEVICE_MFG_LABEL ) {
const char * label = MFG_LABEL;
ESP8266DMX.sendRDMGetResponse(source, pid, (uint8_t*)label, 5);
} else if ( pid == RDM_DEVICE_MODEL_DESC ) {
const char * label = MODEL_DESCRIPTION;
ESP8266DMX.sendRDMGetResponse(source, pid, (uint8_t*)label, 13);
} else if ( pid == RDM_DEVICE_DEV_LABEL ) {
ESP8266DMX.sendRDMGetResponse(source, pid, device_label, strlen((const char*)device_label));
}
}
} else if ( cmdclass == RDM_SET_COMMAND ) {
UID destination;
destination.setBytes(&rdmdata[RDM_IDX_DESTINATION_UID]);
if ( destination == ESP8266DMX.THIS_DEVICE_ID ) {
UID source;
source.setBytes(&rdmdata[RDM_IDX_SOURCE_UID]);
if ( pid == RDM_DEVICE_START_ADDR ) {
uint16_t scratch = (rdmdata[24] << 8) + rdmdata[25];
if (( scratch > 0 ) && ( scratch < 513 )) {
start_address = scratch;
}
ESP8266DMX.sendAckRDMResponse(RDM_SET_COMMAND_RESPONSE, source, pid);
} else if ( pid == RDM_DEVICE_DEV_LABEL ) {
uint8_t llen = 0;
if ( rdmdata[2] > 24 ) { //label not empty string
llen = rdmdata[2] - 24;
if ( llen > 32 ) { //limit to max 32 characters
llen = 32;
}
}
for ( uint8_t j=0; j<33; j++) { //copy label, zero the rest of the array
if ( j < llen ) {
device_label[j] = rdmdata[24+j];
} else {
device_label[j] = 0;
}
} // <-for
ESP8266DMX.sendAckRDMResponse(RDM_SET_COMMAND_RESPONSE, source, pid);
} // <-pid RDM_DEVICE_DEV_LABEL
}
}
got_rdm = 0;
} //gotRDM
wdt_reset();
}