build using platformio
[Arduino] / Diseq_c / Diseq_c.ino
index f6587f0..8e784e7 100644 (file)
@@ -7,20 +7,28 @@
 // serial commands for loop-back test
 #define TEST 1
 
-// pins, 2 recorders 4 inputs each 
+// pins, 2 receivers 4 satelites signals from Diseqc for each 
 int receiver[2][4] = {
   { A0, A1, A2, A3 },
   { A4, A5, A6, A7 },
 };
 
+// pin for master disable if shorted to ground (GND is next to D2, so jumpers nicely)
 #define REC_MASTER 2
 
+// normal arduino LED on D13
 #define LED 13
 
 //                  rec1        rec2     master
+// keys in serial:   1  2  3 4  5 6 7 8  9
 int test_pins[] = { 12,11,10,9, 8,7,6,5, 4 };
 int test_out[]  = {  0, 0, 0,0, 0,0,0,0, 1};
 int nr_test_pins = sizeof(test_pins)/sizeof(int);
+// loop test pins to receiver pins above for testing
+
+int master_enabled() {
+  return digitalRead(REC_MASTER) == HIGH ? 1 : 0;
+}
 
 void setup(){
   Serial.begin(9600); // FIXME check speed
@@ -37,7 +45,7 @@ void setup(){
 
 #if DEBUG
   Serial.print("boot master=");
-  Serial.println(digitalRead(REC_MASTER) == HIGH );
+  Serial.println(master_enabled());
 #endif
 
 #if TEST
@@ -48,28 +56,40 @@ void setup(){
 #endif
 }
 
-int receiver_selection( int nr ) {
+char restore[10] = "";
+char len = 0;
+
+int receiver_satelite( int nr ) {
 
   #ifdef DEBUG
-  Serial.print("<");
+  Serial.print("|<");
   #endif
 
+  if ( nr == 0 ) {
+    len = 0;
+    if ( ! master_enabled() ) restore[len++] = '9';
+  }
+
   int active = 0;
   int selected = -1;
+
   for(int i=0; i<4; i++) {
     int a = analogRead( receiver[nr][i] );
     int on_off = a > 512 ? 1 : 0;
     active += on_off;
-    if ( on_off ) selected = i+1;
+    if ( on_off ) {
+      selected = i+1;
+      restore[len++] = char('1' + (nr * 4) + i);
+    }
 
     //Serial.print(a);
     //Serial.print("~");
     Serial.print(on_off);
     //Serial.print(" ");
   }
+  restore[len] = 0;
 
   Serial.print(">");
-  Serial.print(selected);
 
   if ( active == 0 ) { // no inputs active
     return 0;
@@ -79,6 +99,9 @@ int receiver_selection( int nr ) {
     return -1; // error
   }
 
+  Serial.print(" selected=");
+  Serial.print(selected);
+
   return selected;
 }
 
@@ -90,7 +113,7 @@ int receiver_selection( int nr ) {
 int current_sat = 0;
 int blink_interval = LED_NO_ACTIVE_INPUTS; // 2 sec on/off no receivers turned on
 int last_changed_nr = -1;
-int last_receiver_selection[2] = { -1, -1 };
+int last_receiver_satelite[2] = { -1, -1 };
 
 void loop(){
 
@@ -101,9 +124,9 @@ void loop(){
 
     int current_nr = nr;
 
-    int sat = receiver_selection(i);
-    int last_sat = last_receiver_selection[i];
-    last_receiver_selection[i] = sat;
+    int sat = receiver_satelite(i);
+    int last_sat = last_receiver_satelite[i];
+    last_receiver_satelite[i] = sat;
 
     #if DEBUG
     Serial.print(" nr:");
@@ -112,28 +135,33 @@ void loop(){
     Serial.print(sat);
     Serial.print(" last:");
     Serial.print(last_sat);
-    Serial.print("|");
     #endif
 
-    int prefer_master = 0;
-    if ( digitalRead(REC_MASTER) == HIGH ) {
-      prefer_master = 1;
-    } else {
-        if ( last_sat != sat ) {
-          Serial.print("C");
-          last_changed_nr = current_nr;
-          nr=0; // stop
-        } else {
-          Serial.print("=");
-          nr++; // next
-          continue;
-        }
+    int prefer_master = master_enabled();
+    if ( ! prefer_master ) {
 
-        if ( last_changed_nr != current_nr ) {
-          sat = current_sat; // ignore, last not changed
-          Serial.print("I");
+      if ( last_sat != sat ) {
+
+        Serial.print("C");
+        last_changed_nr = current_nr;
+        if ( sat == 0 ) {
+          Serial.print("off");
+          sat = receiver_satelite( (i+1)%2 ); // check other receiver
         }
+        nr=0; // stop
+
+      } else {
+
+        Serial.print("=");
+        nr++; // next
+        continue;
+
+      }
 
+      if ( last_changed_nr != current_nr ) {
+        sat = current_sat; // ignore, last not changed
+        Serial.print("I");
+      }
 
     }
  
@@ -150,8 +178,9 @@ void loop(){
         #if DEBUG
         Serial.print("[M]");
         #endif
-        if ( nr == 1 ) { // need to check 2nd recorder error
-          int error = receiver_selection(i + 1);
+        if ( nr == 1 ) { // need to check 2nd receiver error and report it
+          int error = receiver_satelite(i + 1);
+          Serial.print(" E?");
           Serial.print(error);
           if ( error == -1 ) blink_interval = LED_MULTIPLE_INPUTS;
         }
@@ -175,7 +204,7 @@ void loop(){
 
       if ( current_sat != sat ) {
 
-  last_changed_nr = current_nr;
+        last_changed_nr = current_nr;
         current_sat = sat;
         nr = 0; // stop
 
@@ -202,10 +231,12 @@ void loop(){
 
 
   #if DEBUG
-  Serial.print(" sat=");
+  Serial.print("| sat=");
   Serial.print(current_sat);
   Serial.print(" from:");
   Serial.print(last_changed_nr);
+  Serial.print(" restore>");
+  Serial.print(restore);
   Serial.print(" blink=");
   Serial.println(blink_interval);
   #endif
@@ -248,4 +279,4 @@ void loop(){
   #endif
 }
 
-
+// vim: tabstop=2 shiftwidth=2 expandtab