Arduino_DMX.ino 6.61 KB
Newer Older
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
//    Copyright (C) 2018, ETH Zurich, D-ITET
//          Martin Reinhard         remartin       @ee.ethz.ch
//
//    This file is part of D-FaLL-System.
//    
//    D-FaLL-System is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//    
//    D-FaLL-System is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//    
//    You should have received a copy of the GNU General Public License
//    along with D-FaLL-System.  If not, see <http://www.gnu.org/licenses/>.
//    
//
//    ----------------------------------------------------------------------------------
//    DDDD        FFFFF        L     L           SSSS  Y   Y   SSSS  TTTTT  EEEEE  M   M
//    D   D       F      aaa   L     L          S       Y Y   S        T    E      MM MM
//    D   D  ---  FFFF  a   a  L     L     ---   SSS     Y     SSS     T    EEE    M M M
//    D   D       F     a  aa  L     L              S    Y        S    T    E      M   M
//    DDDD        F      aa a  LLLL  LLLL       SSSS     Y    SSSS     T    EEEEE  M   M
//
//
//    DESCRIPTION:
//    Arduino program required to control the spotlight. 
//    Listens to serial port and translates int the DMX protocol. 
//
//    ----------------------------------------------------------------------------------


#include <DmxSimple.h>

const byte channelMode = 25; // The spotlight requires all values. 
byte currentDmxMessage[channelMode];
byte channelOffset = 0; // corresponding to the device addressed. 
byte posPT = 3; // offset of the pan/tilt DMX values. 
unsigned long time = 0;

// open the serial connection. 
void setup(){
  Serial.begin(9600); // 9600 baud
  Serial.println("Arduino_DMX has started.");
  Serial.println();
  Serial.println("Syntax:");
  Serial.print(" c*c : DMX channel index is *. ");
  Serial.println("Must be below 128-(14/25) due to Arduino limitations.");
  Serial.println(" m199,255,40,10,0,33,m  : some DMX command. Comma at the end! ");
  Serial.println(" p200,200,30,0,p : setting pan, pan fine, tilt, tilt fine only. "); 
  Serial.println();
  Serial.println();
  
  sendFullDmxMessage(); // required to send it out once.  
}

void loop() {
  //  t2 = milis()
  //printCurrentDmxMessage(); 
  //sendDmxMessage(); 
}

void sendFullDmxMessage(){
    //Serial.print("Sent out (ch/Setting):"); 

  for(int gg = channelOffset; gg < channelOffset + channelMode; gg++){
    //if(gg>128) Serial.println(std7); covered when setting
    DmxSimple.write(gg, currentDmxMessage[gg-channelOffset]); 
//    Serial.print(gg); 
//    Serial.print("/"); 
//    Serial.print(currentDmxMessage[gg-channelOffset]); 
//    Serial.print(", "); 
  }
//  Serial.println(); 

}

void printCurrentDmxMessage(){
  Serial.print("Msg atm: "); 
  for(int a = channelOffset; a < channelOffset + 14; a++){
    Serial.print(currentDmxMessage[a-channelOffset]); 
    Serial.print(", "); 
  }
  Serial.println(); 
}

int parserTemp = 0; 
int i = 0; // Offset in the currentDmxMessage-Array
bool readingMessage = false; 
bool readingChannelOffset = false; 
bool readingPT = false; 

// Reads messages from the serial port: Either new channel numbers (= channelOffset) or DmxMessages. 
void serialEvent(){
  //Serial.println("Received some stuff here"); 

  //Serial.print("Received nr. of bytes: "); 
  //Serial.println(Serial.available()); 
  
  while(Serial.available()){
    char ch = Serial.read(); 
    //Serial.write(ch); // Necessary as ACK? 

    // Detection structure: dm
    // Problem: Arduino sends some info by default -> needs to match precise format. 
    if(!readingMessage && ch == 'm'){
      readingMessage = true; 
      i = 0; 
       time = micros();
    }else if(!readingChannelOffset && ch == 'c'){
      readingChannelOffset = true; 
    }else if(!readingPT && ch == 'p'){
      readingPT = true; 
      i = 3; 
       time = micros();
    }else if(readingMessage){
      if(ch == 'm'){//End reached
        parserTemp = 0; 
        i = 0; // reset array offset
        readingMessage = false; 
        time = micros() - time;
        Serial.print("Parsing took uS: "); 
        Serial.println(time, DEC);    
      }else if(ch == ','){
        if(parserTemp>255 || parserTemp<0){
          Serial.println("Warning: DMX-Range"); //(std3); 
        }else{
          currentDmxMessage[i] = parserTemp; 
          DmxSimple.write(channelOffset+i, parserTemp); 
        }
        parserTemp = 0; 
        i++; 
      }else if(isDigit(ch)){
        parserTemp = 10*parserTemp + (ch-'0'); 
      }else{// abort that attempt and reset everything.  
        parserTemp = 0; 
        i = 0; 
        readingMessage = false; 
        //Serial.println("Error while parsing a message.");   
      }
    }else if(readingChannelOffset){
      if(ch == 'c'){//End of channel setting
        if(parserTemp>103){
          Serial.println("Warning: ChNr");//(std4); 
        }else{
          channelOffset = parserTemp; 
          Serial.print("New offset: "); 
          Serial.print(channelOffset); 
          Serial.println(); 
        }
        parserTemp = 0; 
        readingChannelOffset = false; 
      }else if(isDigit(ch)){
        parserTemp = 10*parserTemp + (ch-'0'); 
      }else{
        parserTemp = 0; 
        readingChannelOffset = false; 
        //Serial.println("Error while parsing a channel number."); 
      }

    }else if(readingPT){
      if(ch == 'p'){//End reached
        parserTemp = 0; 
        i = 0; // reset array offset
        readingPT = false; 
        time = micros() - time;
        Serial.print("Parsing a PT took uS: "); 
        Serial.println(time, DEC);   
        //printCurrentDmxMessage();      
      }else if(ch == ','){
        if(parserTemp>255 || parserTemp<0){
          Serial.println("Warning: DMX-Range"); //(std3); 
        }else{
          currentDmxMessage[i] = parserTemp; 
          DmxSimple.write(channelOffset+i, parserTemp); 
        }
        parserTemp = 0; 
        i++; 
      }else if(isDigit(ch)){
        parserTemp = 10*parserTemp + (ch-'0'); 
      }else{// abort that attempt and reset everything.  
        parserTemp = 0; 
        i = 0; 
        readingPT = false; 
        //Serial.println("Error while parsing a PT.");   
      }
    }else{// all other stuff
      // Arduino sends some info stuff at startup -> need to disable those error messages. 
      // Serial.println("Received Gibberish. That one:"); 
      // Serial.write(ch); 
    } 
  }
}