-
Notifications
You must be signed in to change notification settings - Fork 3
/
dmx.h
116 lines (87 loc) · 2.35 KB
/
dmx.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
#ifndef dmx_h
#define dmx_h
/*
* light-duino v2
* MQTT <-> DMX controller with hw switches, based on ESP8266
* See attached Readme.md for details
*
* This is based on the work of Jorgen (aka Juergen Skrotzky, [email protected]), buy him a beer. ;-)
* Rest if not noted otherwise by Peter Froehlich, [email protected] - Munich Maker Lab e.V. (January 2016)
*
* Published under Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
* You'll find a copy of the licence text in this repo.
*/
#include <ESPDMX.h> //https://github.com/Rickgg/ESP-Dmx
DMXESPSerial dmx;
// vars to hold state
int dmxChannels[intMaxChannel] = { 255 };
// rx tx switching logic
void prepare_send() {
digitalWrite(rx_tx_switch_pin, HIGH);
}
void end_send() {
digitalWrite(rx_tx_switch_pin, LOW);
}
void updateDMX() {
prepare_send();
delay(10);
dmx.update();
end_send();
}
// State communication
void updateStatesEntry() {
String strStates;
for( int i = 0; i < intMaxChannel; i++ ) {
strStates = strStates + i + ":" + dmxChannels[i] + ",";
}
strStates.remove(strStates.length() - 1);
// set mqtt state msg with retain set to true
publishMQTTMessage(strTopicPrefixID + "state", strStates, true);
}
// Channel handling
void channelValue(int channel, int value) {
dmx.write(channel, value);
dmxChannels[channel] = value;
DEBUG_PRINT("Switching ");
DEBUG_PRINT(channel);
DEBUG_PRINT(" to ");
DEBUG_PRINTLN(value);
}
void channelOn(int channel) {
channelValue(channel, 255);
dmxChannels[channel] = 255;
}
void channelOff(int channel) {
channelValue(channel, 0);
dmxChannels[channel] = 0;
}
void toggleChannel(int channel) {
if (dmxChannels[channel] > 0) {
channelOff(channel);
} else {
channelOn(channel);
}
}
void dmxApplyChanges() {
// Not necessary anymore, replaced by ticker interrupt
//updateDMX();
if (!dmxChangedStates)
triggedChange = millis();
lastChange = millis();
dmxChangedStates = 1;
DEBUG_PRINTLN("Applied pending changes to DMX");
updateStatesEntry();
}
void allChannelsOff() {
DEBUG_PRINTLN("Switching all channels to off");
for ( int i = 0; i < intMaxChannel; i++ ) {
channelOff(i);
}
dmxApplyChanges();
}
// setup functions
void setupDmx() {
pinMode(rx_tx_switch_pin, OUTPUT);
dmx.init(intMaxChannel);
}
#endif