4de6e4bbccf5a47bb291ab43755fdcbe44077a14
8 #define COIN_BUTTON 7 //physically pin 7
9 #define SYNC_PIN 4 //physically pin 16
13 int one_second_ago
= 0;
14 int switch_state
; //Keeps track of what state the software thinks the switch should be in
19 pinMode(COIN_BUTTON
, INPUT
);
20 pinMode(SYNC_PIN
, INPUT
);
21 pullUpDnControl(COIN_BUTTON
, PUD_UP
);
22 pullUpDnControl(SYNC_PIN
, PUD_UP
);
27 delay(config
.sync_switch_delay
); //wait
31 int validate_switch_state()
33 if(config
.sync_switch
== 0 || digitalRead(SYNC_PIN
) == switch_state
)
36 printf("Sync pin is not what I expected, attempting to switch signal\n");
39 return digitalRead(SYNC_PIN
) == switch_state
;
44 return (unsigned)time(NULL
);
49 if(one_second_ago
!= now() && expires
!= -1)
51 printf("You have %d seconds remaining\n", expires
- now());
52 one_second_ago
= now();
55 if(expires
!= -1 && expires
<= now())
57 printf("It's all ogre now\n");
59 switch_state
= 1 - switch_state
;
68 if(digitalRead(COIN_BUTTON
) == LOW
)
70 // Don't do anything until button is released
71 while(digitalRead(COIN_BUTTON
) == LOW
)
73 //I don't see this happening, but in principle if the button is held down
74 //It would be possible to stop the switch happening after time runs out
75 //So we must check for that here.
80 // Flip internal switch state, but only if we are in the default state
81 // i.e., waiting for a credit
82 if(config
.default_switch_state
== switch_state
)
84 printf("Changing internal switch state from %d\n", switch_state
);
85 switch_state
= 1 - switch_state
;
86 printf("Switch state is now %d\n", switch_state
);
89 if(config
.external_timer
!= 1)
91 printf("Nice! I just added %d to your time.\n", config
.credit_value
);
94 if(expires
== -1) //Initial credit, set the expiry to now plus time
96 expires
= now() + config
.credit_value
;
97 } else { //expiry already set, simply extend by time
98 expires
+= config
.credit_value
;
101 //wait for button to be released
102 while(digitalRead(COIN_BUTTON
) == LOW
)
104 //I don't see this happening, but in principle if the button is held down
105 //It would be possible to stop the switch happening after time runs out
106 //So we must check for that here.
120 int main (int argc
, char *argv
[])
122 if(argc
== 2 && strcmp("version", argv
[1]) == 0)
124 printf("hdmi-switcher %s\n", VERSION
);
128 init_config(&config
);
130 switch_state
= config
.default_switch_state
;
131 printf ("Short pin 7 and 9 to increment timer.\n") ;
135 if(validate_switch_state() == 0)
137 printf("Unable to switch video signal. Exiting.\n");
142 //I don't really need to call this here but I'm doing it to see output.