#ifndef _MAIN_H_
#define _MAIN_H_
#include "protocol.h"

// IO definitions
#define PIN_LED_ONBOARD     PIN5_bm // PORT B
#define PIN_LED_BUTTON0     PIN0_bm // PORT C
#define PIN_LED_BUTTON1     PIN1_bm // PORT C
#define PIN_LED_BUTTON2     PIN2_bm // PORT C
#define PIN_LED_BUTTON3     PIN3_bm // PORT C
#define PIN_BUTTON0         PIN4_bm // PORT A
#define PIN_BUTTON1         PIN5_bm // PORT A
#define PIN_BUTTON2         PIN6_bm // PORT A
#define PIN_BUTTON3         PIN7_bm // PORT A

#define ALIASonboadLEDon    (PORTB.OUT |= PIN_LED_ONBOARD)
#define ALIASonboadLEDoff   (PORTB.OUT &= !PIN_LED_ONBOARD)

// RTC definitions
#define RTC_PERIOD  1024    // 32768 Hz / prescaler (32) / 1024 = 1 second

// Serial definitions
#define PIN_TX  PIN2_bm
#define PIN_RX  PIN3_bm
#define PIN_DIR PIN4_bm
#define SERIAL_BAUDRATE 1389

volatile unsigned char serialTXbuffer;                      // the buffer to hold the response byte
volatile unsigned char serialRXbuffer[PROTOCOL_FRAME_SIZE]; // the buffer to hold the received bytes
volatile unsigned char serialRXbufferIndex;                 // the pointer to the current locatin in the buffer

#define ALIASserialGetRS485Mode          (PORTB.IN &= PIN_DIR)      // check the RS485 mode
#define ALIASserialSetRS485ModeSend      (PORTB.OUT &= ~PIN_DIR)    // set the RS485 mode to send
#define ALIASserialSetRS485ModeReceive   (PORTB.OUT |= PIN_DIR)     // set the RS485 mode to receive

// game definitions
#define GAMESTATE_INIT              0
#define GAMESTATE_READY             1
#define GAMESTATE_RUNNING           2
#define GAMESTATE_CALCULATING       3
#define GAMESTATE_FINISHED          4

#define GAMEDATA_PATTERN_LENGTH     8

#define GAME_DIFFICULTY             2
#define GAME_TIMEOUT                5

volatile unsigned int tenthSeconds;     // record elapsed 1/10 seconds
volatile unsigned int seconds;          // record elapsed seconds
volatile unsigned char gameState;       // record the current state of the game
volatile unsigned char gameLevel;       // the current level
volatile unsigned char gameData[2 * GAMEDATA_PATTERN_LENGTH] = {1, 2, 4, 4, 8, 4, 2, 2};
                                        // the array with solutions, answers and game results
volatile unsigned char gameDataIndex;   // the pointer to the gameData array
volatile unsigned char gameAnswerTimer; // the time before the game starts again

#endif