Refoctor code. Add version output
authorCameron Ball <c.ball1729@gmail.com>
Fri, 27 Mar 2015 07:20:11 +0000 (07:20 +0000)
committerCameron Ball <c.ball1729@gmail.com>
Fri, 27 Mar 2015 07:20:11 +0000 (07:20 +0000)
main.c
start.sh

diff --git a/main.c b/main.c
index 35519e1..4de6e4b 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,5 +1,6 @@
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 #include <wiringPi.h>
 #include <time.h>
 #include "config.h"
@@ -66,6 +67,25 @@ int read_button()
 {
   if(digitalRead(COIN_BUTTON) == LOW)
   {
+    // 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);
+    }
+
+    // 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.external_timer != 1)
     {
       printf("Nice! I just added %d to your time.\n", config.credit_value);
@@ -91,24 +111,20 @@ int read_button()
       }
     }
 
-    // Flip switch state
-    // But only if we are in the default state (IE 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);
-    }
-
     return 1;
   }
 
   return 0;
 }
 
-int main (void)
+int main (int argc, char *argv[])
 {
-  printf("hdmi-switcher %s\n", VERSION);
+  if(argc == 2 && strcmp("version", argv[1]) == 0)
+  {
+    printf("hdmi-switcher %s\n", VERSION);
+    exit(1);
+  }
+
   init_config(&config);
   init_gpio();
   switch_state = config.default_switch_state;
index ece8387..8f67f44 100755 (executable)
--- a/start.sh
+++ b/start.sh
@@ -1,4 +1,5 @@
 #!/bin/bash
+/home/pi/hdmi-switcher/hdmi-switcher version
 /home/pi/hdmi-switcher/update.sh
 printf "Starting video... "
 sleep 5