+
 
 #include <avr/pgmspace.h>
-//#include <TimerOne.h>
 #include <Time.h>
 #include "english_v2.h"
 
-#define TiMPIN			A0
-#define BUTTON_L		A1
-#define BUTTON_R		A2
+#define TiMPIN				A0
+#define BUTTON_L			8
+#define BUTTON_R			12
+#define PIN_LOW				10
 
-#define BUTTON_L_IDX	1
-#define BUTTON_R_IDX	2
+#define BUTTON_L_IDX		1
+#define BUTTON_R_IDX		2
 
-/*// Stores the current time, in half-seconds, since the beginning of the day.
-uint16_t g_time2Hz = 0;
-uint16_t g_time1Hz = 0;
-uint16_t g_timeMinutes = 0;*/
+#define LONG_PRESS_MILLIS	2000
+
+typedef struct KeyStates{
+	uint8_t justPressed;	// Buttons that have just been pressed
+	uint8_t longPressed;	// Buttons that have been pressed for a long time
+	uint8_t up;				// Buttons that are pressed
+	uint8_t down;			// Buttons that are released
+};
 
 void setup() {
 	// Setup the GPIO

	pinMode(BUTTON_R, INPUT);
	digitalWrite(BUTTON_R, HIGH);
	
	// Start the serial port
	Serial.begin(9600);
	
	// Initialise the display
	disp_init();
	
/*	// Interrupt timer 1 every 1/2 a second
	Timer1.initialize(500000);
	Timer1.attachInterrupt(twiceASecond);*/
}

void loop() {
	uint16_t ledStates[8] = { 0 };
	uint8_t keys;
		
	keys = getKeys();
	
	// Depending on the keys just pressed, take the necessary action
	
	displayWord(ledStates, g_timeMinutes >> 1);

	// Make the bottom right LED blink every second
	ledStates[7] |= second() & 0x01;

	disp_display(ledStates);
}

void displayWord(uint16_t *ledStates, int time) {
	// Loop through every line
	Serial.print("Time: ");
	Serial.println(time);

	for(int dispIdx = 0; dispIdx < 4; dispIdx++) {


	debounce[debounceIdx++] = pressed;
	debounceIdx &= 3;
	
	keys.down = debounce[0] & debounce[1] & debounce[2] & debounce[3];
	keys.up = debounce[0] | debounce[1] | debounce[2] | debounce[3];
	
	// Determine which buttons have freshly been pressed and return the result
	uint8_t prevStates = states;
	states &= keys.up;
	states |= keys.down;
	keys.justPressed = states & (states ^ prevStates);
	
	if(states != 0) {
		if(states != prevStates) {
			timeInStates = millis();
		} else {
			if(millis() - timeInStates > LONG_PRESS_MILLIS) {
				keys.longPressed = keys.up;
			}
		}
	} else {
		keys.longPressed = 0;
	}
	
	return &keys;
}


