Change some stuff to work with external timer properly
authorCameron Ball <c.ball1729@gmail.com>
Sun, 12 Apr 2015 10:59:43 +0000 (10:59 +0000)
committerCameron Ball <c.ball1729@gmail.com>
Sun, 12 Apr 2015 10:59:43 +0000 (10:59 +0000)
main.c

diff --git a/main.c b/main.c
index 32b928c..8c73ae4 100644 (file)
--- a/main.c
+++ b/main.c
@@ -76,32 +76,44 @@ int time_over()
 
 int read_button()
 {
-  if(digitalRead(COIN_BUTTON) == LOW)
+  // In the case of an external timer all we need to do is set the internal switch
+  // state to match the state of the external timer pin. Low means a they can see
+  // the second input, high means show the default video.
+  if(config.external_timer == 1)
   {
-    // Don't do anything until button is released
-    while(digitalRead(COIN_BUTTON) == LOW)
+    if(digitalRead(COIN_BUTTON) == HIGH)
     {
-      //I don't see this happening, but in principle if the button is held down
-      //It would be possible to stop the switch happening after time runs out
-      //So we must check for that here.
-      time_over();
-      delay(10);
+      switch_state = config.default_switch_state;
+    } else {
+      switch_state = 1 - config.default_switch_state;
     }
 
-    // Flip internal switch state, but only if we are in the default state
-    // i.e., waiting for a credit
-    if(config.default_switch_state == switch_state)
+    return 1;
+  } else {
+    if(digitalRead(COIN_BUTTON) == LOW)
     {
-      printf("Changing internal switch state from %d\n", switch_state);
-      switch_state = 1 - switch_state;
-      printf("Switch state is now %d\n", switch_state);
+      // Don't do anything until button is released
+      while(digitalRead(COIN_BUTTON) == LOW)
+      {
+        //I don't see this happening, but in principle if the button is held down
+        //It would be possible to stop the switch happening after time runs out
+        //So we must check for that here.
+        time_over();
+        delay(10);
+      }
 
-      if(config.sync_switch == 0) //naively pulse the switch
-          pulse_switch();
-    }
+      // Flip internal switch state, but only if we are in the default state
+      // i.e., waiting for a credit
+      if(config.default_switch_state == switch_state)
+      {
+        printf("Changing internal switch state from %d\n", switch_state);
+        switch_state = 1 - switch_state;
+        printf("Switch state is now %d\n", switch_state);
+
+        if(config.sync_switch == 0) //naively pulse the switch
+            pulse_switch();
+      }
 
-    if(config.external_timer != 1)
-    {
       printf("Nice! I just added %d to your time.\n", config.credit_value);
 
       //Increment timer
@@ -112,20 +124,8 @@ int read_button()
           expires += config.credit_value;
       }
 
-      //wait for button to be released
-      while(digitalRead(COIN_BUTTON) == LOW)
-      {
-         //I don't see this happening, but in principle if the button is held down
-         //It would be possible to stop the switch happening after time runs out
-         //So we must check for that here.
-         if(time_over())
-           break;
-
-         delay(10);
-      }
+      return 1;
     }
-
-    return 1;
   }
 
   return 0;