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