build using platformio
[Arduino] / Diseq_c / Diseq_c.ino
index 733b042..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
@@ -51,13 +59,16 @@ void setup(){
 char restore[10] = "";
 char len = 0;
 
-int receiver_selection( int nr ) {
+int receiver_satelite( int nr ) {
 
   #ifdef DEBUG
   Serial.print("|<");
   #endif
 
-  if ( nr == 0 ) len = 0;
+  if ( nr == 0 ) {
+    len = 0;
+    if ( ! master_enabled() ) restore[len++] = '9';
+  }
 
   int active = 0;
   int selected = -1;
@@ -102,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(){
 
@@ -113,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:");
@@ -126,25 +137,31 @@ void loop(){
     Serial.print(last_sat);
     #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");
+      }
 
     }
  
@@ -161,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;
         }
@@ -261,4 +279,4 @@ void loop(){
   #endif
 }
 
-
+// vim: tabstop=2 shiftwidth=2 expandtab