Text config file support
[hdmi-switcher.git] / main.c
1 #include <stdio.h>
2 #include <wiringPi.h>
3 #include <time.h>
4 #include "config.h"
5 #define COIN_BUTTON 7
6
7 configuration config;
8 int expires = -1;
9 int one_second_ago = 0;
10
11 void init_gpio()
12 {
13 wiringPiSetup();
14 pinMode(COIN_BUTTON, INPUT);
15 pullUpDnControl(COIN_BUTTON, PUD_UP);
16 }
17
18 int now()
19 {
20 return (unsigned)time(NULL);
21 }
22
23 int time_over()
24 {
25 if(one_second_ago != now() && expires != -1)
26 {
27 printf("You have %d seconds remaining\n", expires - now());
28 one_second_ago = now();
29 }
30
31 if(expires == now())
32 {
33 printf("It's all ogre now\n");
34 expires = -1;
35 return 1;
36 }
37
38 return expires == -1;
39 }
40
41 int read_button()
42 {
43 if(digitalRead(COIN_BUTTON) == LOW)
44 {
45 printf("Nice! I just added %d to your time.\n", config.credit_value);
46
47 //Increment timer
48 if(expires == -1) //Initial credit, set the expiry to now plus time
49 {
50 expires = now() + config.credit_value;
51 } else { //expiry already set, simply extend by time
52 expires += config.credit_value;
53 }
54
55 //wait for button to be released
56 while(digitalRead(COIN_BUTTON) == LOW)
57 {
58 //I don't see this happening, but in principle if the button is held down
59 //It would be possible to stop the switch happening after time runs out
60 //So we must check for that here.
61 if(time_over())
62 break;
63
64 delay(10);
65 }
66
67 return 1;
68 }
69
70 return 0;
71 }
72
73 int main (void)
74 {
75 init_config(&config);
76 init_gpio();
77
78 printf ("Short pin 7 and 9 to increment timer.\n") ;
79
80 while(1)
81 {
82 read_button();
83 //I don't really need to call this here but I'm doing it to see output.
84 time_over();
85 delay(10);
86 }
87 }