32b928c34651f143268dbedfd8dea09a0bed82de
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 if(digitalRead(COIN_BUTTON
) == LOW
)
81 // Don't do anything until button is released
82 while(digitalRead(COIN_BUTTON
) == LOW
)
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.
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
)
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
);
99 if(config
.sync_switch
== 0) //naively pulse the switch
103 if(config
.external_timer
!= 1)
105 printf("Nice! I just added %d to your time.\n", config
.credit_value
);
108 if(expires
== -1) //Initial credit, set the expiry to now plus time
110 expires
= now() + config
.credit_value
;
111 } else { //expiry already set, simply extend by time
112 expires
+= config
.credit_value
;
115 //wait for button to be released
116 while(digitalRead(COIN_BUTTON
) == LOW
)
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.
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.