build using platformio
[Arduino] / Diseq_c / Diseq_c.ino
index cb587a8..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
+//                  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,40 +56,53 @@ void setup(){
 #endif
 }
 
-int receiver_selection( int nr ) {
-  int StateA = digitalRead( receiver[nr][0] ) == HIGH;
-  int StateB = digitalRead( receiver[nr][1] ) == HIGH;
-  int StateC = digitalRead( receiver[nr][2] ) == HIGH;
-  int StateD = digitalRead( receiver[nr][3] ) == HIGH;
-
-#ifdef DEBUG
-  Serial.print("<");
-  Serial.print(StateA);
-  Serial.print(StateB);
-  Serial.print(StateC);
-  Serial.print(StateD);
+char restore[10] = "";
+char len = 0;
+
+int receiver_satelite( int nr ) {
+
+  #ifdef DEBUG
+  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;
+      restore[len++] = char('1' + (nr * 4) + i);
+    }
+
+    //Serial.print(a);
+    //Serial.print("~");
+    Serial.print(on_off);
+    //Serial.print(" ");
+  }
+  restore[len] = 0;
+
   Serial.print(">");
-#endif
 
-  if ( StateA + StateB + StateC + StateD == 0 ) { // no inputs active
+  if ( active == 0 ) { // no inputs active
     return 0;
   }
 
-  if ( StateA + StateB + StateC + StateD != 1 ) { // only one active at a time
+  if ( active != 1 ) { // only one active at a time
     return -1; // error
   }
 
-  if ( StateA ) {
-    return 1;
-  } else if ( StateB ) {
-    return 2;
-  } else if ( StateC ) {
-    return 3;
-  } else if ( StateD ) {
-    return 4;
-  }
+  Serial.print(" selected=");
+  Serial.print(selected);
 
-  return 0; // receiver off
+  return selected;
 }
 
 #define LED_NO_ACTIVE_INPUTS 2000
@@ -92,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(){
 
@@ -103,22 +124,46 @@ 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(" r");
+    Serial.print(" nr:");
     Serial.print(nr);
     Serial.print("=");
     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;
+    int prefer_master = master_enabled();
+    if ( ! prefer_master ) {
+
+      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");
+      }
+
+    }
  
     if ( sat == 0 ) { // slow blink
       blink_interval = LED_NO_ACTIVE_INPUTS;
@@ -130,30 +175,36 @@ void loop(){
       blink_interval = LED_OFF;
   
       if ( prefer_master ) {
-        nr = 0; // stop
         #if DEBUG
-        Serial.print(" M ");
+        Serial.print("[M]");
         #endif
+        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;
+        }
+        nr = 0; // stop
       } else {
         if ( last_sat != sat ) {
-          Serial.print(" C ");
+          Serial.print("[C]");
           last_changed_nr = current_nr;
           nr=0;
         } else {
-          Serial.print(" S ");
+          Serial.print("[S]");
           nr++;
         }
 
         if ( last_changed_nr != current_nr ) {
           sat = current_sat; // ignore, last not changed
-          Serial.print(" I ");
+          Serial.print("[I]");
         }
   
       }
 
       if ( current_sat != sat ) {
 
-  last_changed_nr = current_nr;
+        last_changed_nr = current_nr;
         current_sat = sat;
         nr = 0; // stop
 
@@ -180,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
@@ -226,4 +279,4 @@ void loop(){
   #endif
 }
 
-
+// vim: tabstop=2 shiftwidth=2 expandtab