added restore output for current state
[Arduino] / Diseq_c / Diseq_c.ino
1 /*
2  */
3
4 // debug on serial
5 #define DEBUG 1
6
7 // serial commands for loop-back test
8 #define TEST 1
9
10 // pins, 2 recorders 4 inputs each 
11 int receiver[2][4] = {
12   { A0, A1, A2, A3 },
13   { A4, A5, A6, A7 },
14 };
15
16 #define REC_MASTER 2
17
18 #define LED 13
19
20 //                  rec1        rec2     master
21 int test_pins[] = { 12,11,10,9, 8,7,6,5, 4 };
22 int test_out[]  = {  0, 0, 0,0, 0,0,0,0, 1};
23 int nr_test_pins = sizeof(test_pins)/sizeof(int);
24
25 void setup(){
26   Serial.begin(9600); // FIXME check speed
27
28   pinMode(REC_MASTER, INPUT_PULLUP); // pullup for jumper/button
29
30   for(int nr=0; nr<2; nr++) {
31     for(int input=0; input<4; input++) {
32       pinMode(receiver[nr][input], INPUT);
33     }
34   }
35
36   pinMode(LED, OUTPUT);
37
38 #if DEBUG
39   Serial.print("boot master=");
40   Serial.println(digitalRead(REC_MASTER) == HIGH );
41 #endif
42
43 #if TEST
44   for(int i=0;i<nr_test_pins;i++) {
45     pinMode(test_pins[i], OUTPUT);
46     digitalWrite(test_pins[i], test_out[i]);
47   }
48 #endif
49 }
50
51 char restore[10] = "";
52 char len = 0;
53
54 int receiver_selection( int nr ) {
55
56   #ifdef DEBUG
57   Serial.print("<");
58   #endif
59
60   if ( nr == 0 ) len = 0;
61
62   int active = 0;
63   int selected = -1;
64
65   for(int i=0; i<4; i++) {
66     int a = analogRead( receiver[nr][i] );
67     int on_off = a > 512 ? 1 : 0;
68     active += on_off;
69     if ( on_off ) {
70       selected = i+1;
71       restore[len++] = char('1' + (nr * 4) + i);
72     }
73
74     //Serial.print(a);
75     //Serial.print("~");
76     Serial.print(on_off);
77     //Serial.print(" ");
78   }
79   restore[len] = 0;
80
81   Serial.print(">");
82   Serial.print(" selected:");
83   Serial.print(selected);
84
85   if ( active == 0 ) { // no inputs active
86     return 0;
87   }
88
89   if ( active != 1 ) { // only one active at a time
90     return -1; // error
91   }
92
93   return selected;
94 }
95
96 #define LED_NO_ACTIVE_INPUTS 2000
97 #define LED_MULTIPLE_INPUTS  125
98 #define LED_SERIAL           200 // < 250
99 #define LED_OFF              0
100
101 int current_sat = 0;
102 int blink_interval = LED_NO_ACTIVE_INPUTS; // 2 sec on/off no receivers turned on
103 int last_changed_nr = -1;
104 int last_receiver_selection[2] = { -1, -1 };
105
106 void loop(){
107
108   int nr = 1;
109
110   while(nr && nr <= 2) {
111     int i = nr - 1;
112
113     int current_nr = nr;
114
115     int sat = receiver_selection(i);
116     int last_sat = last_receiver_selection[i];
117     last_receiver_selection[i] = sat;
118
119     #if DEBUG
120     Serial.print(" nr:");
121     Serial.print(nr);
122     Serial.print("=");
123     Serial.print(sat);
124     Serial.print(" last:");
125     Serial.print(last_sat);
126     Serial.print("|");
127     #endif
128
129     int prefer_master = 0;
130     if ( digitalRead(REC_MASTER) == HIGH ) {
131       prefer_master = 1;
132     } else {
133         if ( last_sat != sat ) {
134           Serial.print("C");
135           last_changed_nr = current_nr;
136           nr=0; // stop
137         } else {
138           Serial.print("=");
139           nr++; // next
140           continue;
141         }
142
143         if ( last_changed_nr != current_nr ) {
144           sat = current_sat; // ignore, last not changed
145           Serial.print("I");
146         }
147
148
149     }
150  
151     if ( sat == 0 ) { // slow blink
152       blink_interval = LED_NO_ACTIVE_INPUTS;
153       nr++; // next
154     } else if ( sat < 0 ) { // error
155       blink_interval = LED_MULTIPLE_INPUTS;
156       nr = 0;
157     } else {
158       blink_interval = LED_OFF;
159   
160       if ( prefer_master ) {
161         #if DEBUG
162         Serial.print("[M]");
163         #endif
164         if ( nr == 1 ) { // need to check 2nd recorder error
165           int error = receiver_selection(i + 1);
166           Serial.print(error);
167           if ( error == -1 ) blink_interval = LED_MULTIPLE_INPUTS;
168         }
169         nr = 0; // stop
170       } else {
171         if ( last_sat != sat ) {
172           Serial.print("[C]");
173           last_changed_nr = current_nr;
174           nr=0;
175         } else {
176           Serial.print("[S]");
177           nr++;
178         }
179
180         if ( last_changed_nr != current_nr ) {
181           sat = current_sat; // ignore, last not changed
182           Serial.print("[I]");
183         }
184   
185       }
186
187       if ( current_sat != sat ) {
188
189   last_changed_nr = current_nr;
190         current_sat = sat;
191         nr = 0; // stop
192
193         for(int repeat=0; repeat<2; repeat++) {
194   
195           Serial.print("@L,");
196           Serial.println( char('A' + sat - 1) );
197   
198           for(int i=0; i<sat; i++) {
199             digitalWrite(LED, HIGH);
200             delay(LED_SERIAL);
201             digitalWrite(LED, LOW);
202             delay(LED_SERIAL);
203           }
204           delay( ( 5 - sat ) * LED_SERIAL * 2 ); // sleep up to 2s
205     //assert(4 * LED_SERIAL < 2000);
206         }
207
208       }
209
210     } // while
211
212   }
213
214
215   #if DEBUG
216   Serial.print(" sat=");
217   Serial.print(current_sat);
218   Serial.print(" from:");
219   Serial.print(last_changed_nr);
220   Serial.print(" restore>");
221   Serial.print(restore);
222   Serial.print(" blink=");
223   Serial.println(blink_interval);
224   #endif
225
226   // handle next led blink event in time
227   int m = millis() % ( blink_interval * 2 );
228   if ( blink_interval != 0 ) {
229     if ( m > blink_interval ) {
230       digitalWrite(LED, HIGH);
231     } else {
232       digitalWrite(LED, LOW);
233     }
234   } else {
235     digitalWrite(LED, LOW);
236   }
237
238   #if TEST
239   while ( Serial.available() ) {
240     int serial = Serial.read();
241     Serial.print(serial, HEX);
242     if ( serial >= '1' && serial <= ( '1' + nr_test_pins ) ) {
243       int nr = serial - '1';
244       Serial.print(" toggle pin ");
245       test_out[nr] = ! test_out[nr];
246       int pin = test_pins[nr];
247       Serial.print(pin);
248       Serial.print("=");
249       int out = test_out[nr];
250       Serial.print(out);
251       digitalWrite( pin, out );
252
253     }
254     Serial.print(" <");
255     for(int i=0;i<nr_test_pins;i++) {
256       if ( i == 4 || i == 8 ) Serial.print(" ");
257       Serial.print(test_out[i]);
258     }
259     Serial.println(">");
260   }
261   #endif
262 }
263
264