#include <stdio.h>
#include <wiringPi.h>
+#include <time.h>
+#define COIN_BUTTON 7
+#define CREDIT_EXTENSION 10
-int main (void)
+int expires = -1;
+int one_second_ago = 0;
+
+void init_gpio()
{
- printf ("Short pin 7 and 9 to see something cool.\n") ;
+ wiringPiSetup();
+ pinMode(COIN_BUTTON, INPUT);
+ pullUpDnControl(COIN_BUTTON, PUD_UP);
+}
- wiringPiSetup ();
+int now()
+{
+ return (unsigned)time(NULL);
+}
- pinMode(7, INPUT) ;
- pullUpDnControl(7, PUD_UP);
+int time_over()
+{
+ if(one_second_ago != now() && expires != -1)
+ {
+ printf("You have %d seconds remaining\n", expires - now());
+ one_second_ago = now();
+ }
- while(1)
+ if(expires == now())
{
- if (digitalRead(7) == LOW) //Low is pressed
- printf("Yay!\n");
+ printf("It's all ogre now\n");
+ expires = -1;
+ return 1;
+ }
- while(digitalRead(7) == LOW) //This just causes the loop to wait til the button is released
- delay(10);
+ return expires == -1;
+}
+
+int read_button()
+{
+ if(digitalRead(COIN_BUTTON) == LOW)
+ {
+ printf("Nice! I just added %d to your time.\n", CREDIT_EXTENSION);
+
+ //Increment timer
+ if(expires == -1) //Initial credit, set the expiry to now plus time
+ {
+ expires = now() + CREDIT_EXTENSION;
+ } else { //expiry already set, simply extend by time
+ expires += CREDIT_EXTENSION;
+ }
+
+ //wait for button to be released
+ while(digitalRead(COIN_BUTTON) == LOW)
+ {
+ //I don't see this happening, but in principle if the button is held down
+ //It would be possible to stop the switch happening after time runs out
+ //So we must check for that here.
+ if(time_over())
+ break;
+
+ delay(10);
+ }
+
+ return 1;
+ }
+
+ return 0;
+}
+
+int main (void)
+{
+ init_gpio();
+ printf ("Short pin 7 and 9 to increment timer.\n") ;
+
+ while(1)
+ {
+ read_button();
+ //I don't really need to call this here but I'm doing it to see output.
+ time_over();
+ delay(10);
}
}