8 #define COIN_BUTTON 7 //physically pin 7
9 #define SYNC_PIN 4 //physically pin 16
10 #define PULSE_PIN 1 //physically pin 12
14 int one_second_ago
= 0;
15 int switch_state
; //Keeps track of what state the software thinks the switch should be in
20 pinMode(COIN_BUTTON
, INPUT
);
21 pinMode(SYNC_PIN
, INPUT
);
22 pinMode(PULSE_PIN
, OUTPUT
);
23 digitalWrite(PULSE_PIN
, 0); //just to be sure
25 pullUpDnControl(COIN_BUTTON
, PUD_UP
);
26 pullUpDnControl(SYNC_PIN
, PUD_UP
);
31 delay(config
.sync_switch_delay
); //wait
32 digitalWrite(PULSE_PIN
, 1);
34 digitalWrite(PULSE_PIN
, 0);
38 int validate_switch_state()
40 if(config
.sync_switch
== 0 || digitalRead(SYNC_PIN
) == switch_state
)
43 printf("Sync pin is not what I expected, attempting to switch signal\n");
46 return digitalRead(SYNC_PIN
) == switch_state
;
51 return (unsigned)time(NULL
);
56 if(one_second_ago
!= now() && expires
!= -1)
58 printf("You have %d seconds remaining\n", expires
- now());
59 one_second_ago
= now();
62 if(expires
!= -1 && expires
<= now())
64 printf("It's all ogre now\n");
66 switch_state
= 1 - switch_state
;
68 if(config
.sync_switch
== 0) //naively pulse
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)
84 if(digitalRead(COIN_BUTTON
) == HIGH
)
86 switch_state
= config
.default_switch_state
;
88 switch_state
= 1 - config
.default_switch_state
;
93 if(digitalRead(COIN_BUTTON
) == LOW
)
95 // Don't do anything until button is released
96 while(digitalRead(COIN_BUTTON
) == LOW
)
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.
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
)
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
);
113 if(config
.sync_switch
== 0) //naively pulse the switch
117 printf("Nice! I just added %d to your time.\n", config
.credit_value
);
120 if(expires
== -1) //Initial credit, set the expiry to now plus time
122 expires
= now() + config
.credit_value
;
123 } else { //expiry already set, simply extend by time
124 expires
+= config
.credit_value
;
134 int main (int argc
, char *argv
[])
136 if(argc
== 2 && strcmp("version", argv
[1]) == 0)
138 printf("hdmi-switcher %s\n", VERSION
);
142 init_config(&config
);
144 switch_state
= config
.default_switch_state
;
145 printf ("Short pin 7 and 9 to increment timer.\n") ;
149 if(validate_switch_state() == 0)
151 printf("Unable to switch video signal. Exiting.\n");
156 //I don't really need to call this here but I'm doing it to see output.