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