Remove bad meme
authorCameron Ball <c.ball1729@gmail.com>
Thu, 26 Mar 2015 07:42:45 +0000 (07:42 +0000)
committerCameron Ball <c.ball1729@gmail.com>
Thu, 26 Mar 2015 07:42:45 +0000 (07:42 +0000)
main.c
usb-config.sh [deleted file]

diff --git a/main.c b/main.c
index 9227a5a..5524e79 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,18 +1,41 @@
+#include <stdlib.h>
 #include <stdio.h>
 #include <wiringPi.h>
 #include <time.h>
 #include "config.h"
-#define COIN_BUTTON 7
+
+#define COIN_BUTTON 7 //physically pin 7
+#define SYNC_PIN 4 //physically pin 16
 
 configuration config;
 int expires = -1;
 int one_second_ago = 0;
+int switch_state; //Keeps track of what state the software thinks the switch should be in
 
 void init_gpio()
 {
   wiringPiSetup();
   pinMode(COIN_BUTTON, INPUT);
+  pinMode(SYNC_PIN, INPUT);
   pullUpDnControl(COIN_BUTTON, PUD_UP);
+  pullUpDnControl(SYNC_PIN, PUD_UP);
+}
+
+void pulse_switch()
+{
+  delay(config.sync_switch_delay); //wait
+  // do the biz
+}
+
+int validate_switch_state()
+{
+  if(config.sync_switch == 0 || digitalRead(SYNC_PIN) == switch_state)
+    return 1;
+
+  printf("Sync pin is not what I expected, attempting to switch signal\n");
+  pulse_switch();
+
+  return digitalRead(SYNC_PIN) == switch_state;
 }
 
 int now()
@@ -28,10 +51,11 @@ int time_over()
     one_second_ago = now();
   }
 
-  if(expires == now())
+  if(expires != -1 && expires <= now())
   {
     printf("It's all ogre now\n");
     expires = -1;
+    switch_state = 1 - switch_state;
     return 1;
   }
 
@@ -42,26 +66,38 @@ int read_button()
 {
   if(digitalRead(COIN_BUTTON) == LOW)
   {
-    printf("Nice! I just added %d to your time.\n", config.credit_value);
-
-    //Increment timer
-    if(expires == -1) //Initial credit, set the expiry to now plus time
+    if(config.external_timer != 1)
     {
-        expires = now() + config.credit_value;
-    } else { //expiry already set, simply extend by time
-        expires += config.credit_value;
+      printf("Nice! I just added %d to your time.\n", config.credit_value);
+
+      //Increment timer
+      if(expires == -1) //Initial credit, set the expiry to now plus time
+      {
+          expires = now() + config.credit_value;
+      } else { //expiry already set, simply extend by time
+          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);
+      }
     }
 
-    //wait for button to be released
-    while(digitalRead(COIN_BUTTON) == LOW)
+    // Flip switch state
+    // But only if we are in the default state (IE waiting for a credit)
+    if(config.default_switch_state == switch_state)
     {
-       //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);
+      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;
@@ -74,14 +110,22 @@ int main (void)
 {
   init_config(&config);
   init_gpio();
-
+  switch_state = config.default_switch_state;
   printf ("Short pin 7 and 9 to increment timer.\n") ;
 
   while(1)
   {
+    if(validate_switch_state() == 0)
+    {
+      printf("Unable to switch video signal. Exiting.\n");
+      exit(1);
+    }
+
     read_button();
     //I don't really need to call this here but I'm doing it to see output.
     time_over();
     delay(10);
   }
+
+  exit(0);
 }
diff --git a/usb-config.sh b/usb-config.sh
deleted file mode 100755 (executable)
index 8d89ad4..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-#!/bin/bash
-
-clear
-
-if [[ ! -e "/dev/sda1" ]]; then
- exit 1
-fi
-
-printf "Found USB... "
-
-umount /dev/sda1 > /dev/null 2>&1
-mount /dev/sda1 > /dev/null 2>&1 || (printf "Mount failure\n" && exit 1)
-
-printf "Mount OK\n"
-
-set -e
-
-if [[ -e "/mnt/usb-config/config.ini" ]]; then
-  printf "Found config.ini... "
-  cp /mnt/usb-config/config.ini /home/pi/hdmi-switcher || (printf "Copy failure\n" && exit 0)
-  printf "Copy OK\n"
-fi
-
-if [[ -e "/mnt/usb-config/video.mp4" ]]; then
-  printf "Found video.mp4... "
-  cp /mnt/usb-config/video.mp4 /home/pi/hdmi-switcher || (printf "Copy failure\n" && exit 0)
-  printf "Copy OK\n"
-fi
-
-umount /dev/sda1 > /dev/null 2>&1