Update pulse width to 100ms
[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(100);
34 digitalWrite(PULSE_PIN, 0);
35 delay(100);
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 // In the case of an external timer all we need to do is set the internal switch
80 // state to match the state of the external timer pin. Low means a they can see
81 // the second input, high means show the default video.
82 if(config.external_timer == 1)
83 {
84 if(digitalRead(COIN_BUTTON) == HIGH)
85 {
86 switch_state = config.default_switch_state;
87 } else {
88 switch_state = 1 - config.default_switch_state;
89 }
90
91 return 1;
92 } else {
93 if(digitalRead(COIN_BUTTON) == LOW)
94 {
95 // Don't do anything until button is released
96 while(digitalRead(COIN_BUTTON) == LOW)
97 {
98 //I don't see this happening, but in principle if the button is held down
99 //It would be possible to stop the switch happening after time runs out
100 //So we must check for that here.
101 time_over();
102 delay(10);
103 }
104
105 // Flip internal switch state, but only if we are in the default state
106 // i.e., waiting for a credit
107 if(config.default_switch_state == switch_state)
108 {
109 printf("Changing internal switch state from %d\n", switch_state);
110 switch_state = 1 - switch_state;
111 printf("Switch state is now %d\n", switch_state);
112
113 if(config.sync_switch == 0) //naively pulse the switch
114 pulse_switch();
115 }
116
117 printf("Nice! I just added %d to your time.\n", config.credit_value);
118
119 //Increment timer
120 if(expires == -1) //Initial credit, set the expiry to now plus time
121 {
122 expires = now() + config.credit_value;
123 } else { //expiry already set, simply extend by time
124 expires += config.credit_value;
125 }
126
127 return 1;
128 }
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 }