Make scripts and binary executable after updating
[hdmi-switcher.git] / main.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <wiringPi.h>
5 #include <time.h>
6 #include "config.h"
7
8 #define COIN_BUTTON 7 //physically pin 7
9 #define SYNC_PIN 4 //physically pin 16
10
11 configuration config;
12 int expires = -1;
13 int one_second_ago = 0;
14 int switch_state; //Keeps track of what state the software thinks the switch should be in
15
16 void init_gpio()
17 {
18 wiringPiSetup();
19 pinMode(COIN_BUTTON, INPUT);
20 pinMode(SYNC_PIN, INPUT);
21 pullUpDnControl(COIN_BUTTON, PUD_UP);
22 pullUpDnControl(SYNC_PIN, PUD_UP);
23 }
24
25 void pulse_switch()
26 {
27 delay(config.sync_switch_delay); //wait
28 // do the biz
29 }
30
31 int validate_switch_state()
32 {
33 if(config.sync_switch == 0 || digitalRead(SYNC_PIN) == switch_state)
34 return 1;
35
36 printf("Sync pin is not what I expected, attempting to switch signal\n");
37 pulse_switch();
38
39 return digitalRead(SYNC_PIN) == switch_state;
40 }
41
42 int now()
43 {
44 return (unsigned)time(NULL);
45 }
46
47 int time_over()
48 {
49 if(one_second_ago != now() && expires != -1)
50 {
51 printf("You have %d seconds remaining\n", expires - now());
52 one_second_ago = now();
53 }
54
55 if(expires != -1 && expires <= now())
56 {
57 printf("It's all ogre now\n");
58 expires = -1;
59 switch_state = 1 - switch_state;
60 return 1;
61 }
62
63 return expires == -1;
64 }
65
66 int read_button()
67 {
68 if(digitalRead(COIN_BUTTON) == LOW)
69 {
70 // Don't do anything until button is released
71 while(digitalRead(COIN_BUTTON) == LOW)
72 {
73 //I don't see this happening, but in principle if the button is held down
74 //It would be possible to stop the switch happening after time runs out
75 //So we must check for that here.
76 time_over();
77 delay(10);
78 }
79
80 // Flip internal switch state, but only if we are in the default state
81 // i.e., waiting for a credit
82 if(config.default_switch_state == switch_state)
83 {
84 printf("Changing internal switch state from %d\n", switch_state);
85 switch_state = 1 - switch_state;
86 printf("Switch state is now %d\n", switch_state);
87 }
88
89 if(config.external_timer != 1)
90 {
91 printf("Nice! I just added %d to your time.\n", config.credit_value);
92
93 //Increment timer
94 if(expires == -1) //Initial credit, set the expiry to now plus time
95 {
96 expires = now() + config.credit_value;
97 } else { //expiry already set, simply extend by time
98 expires += config.credit_value;
99 }
100
101 //wait for button to be released
102 while(digitalRead(COIN_BUTTON) == LOW)
103 {
104 //I don't see this happening, but in principle if the button is held down
105 //It would be possible to stop the switch happening after time runs out
106 //So we must check for that here.
107 if(time_over())
108 break;
109
110 delay(10);
111 }
112 }
113
114 return 1;
115 }
116
117 return 0;
118 }
119
120 int main (int argc, char *argv[])
121 {
122 if(argc == 2 && strcmp("version", argv[1]) == 0)
123 {
124 printf("hdmi-switcher %s\n", VERSION);
125 exit(1);
126 }
127
128 init_config(&config);
129 init_gpio();
130 switch_state = config.default_switch_state;
131 printf ("Short pin 7 and 9 to increment timer.\n") ;
132
133 while(1)
134 {
135 if(validate_switch_state() == 0)
136 {
137 printf("Unable to switch video signal. Exiting.\n");
138 exit(1);
139 }
140
141 read_button();
142 //I don't really need to call this here but I'm doing it to see output.
143 time_over();
144 delay(10);
145 }
146
147 exit(0);
148 }