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

// IO definitions
#define PIN_LED_ONBOARD     PIN5_bm // PORT B
#define PIN_BUTTON          PIN0_bm // PORT B
#define PIN_LED_BUTTON      PIN1_bm // PORT A
#define PIN_LED_GREEN       PIN2_bm // PORT A
#define PIN_LED_RED         PIN3_bm // PORT A
#define PIN_CHANNEL0        PIN0_bm // PORT C
#define PIN_CHANNEL1        PIN1_bm // PORT C
#define PIN_CHANNEL2        PIN2_bm // PORT C
#define PIN_CHANNEL3        PIN3_bm // PORT C
#define PIN_BCD0            PIN4_bm // PORT A
#define PIN_BCD1            PIN5_bm // PORT A
#define PIN_BCD2            PIN6_bm // PORT A
#define PIN_BCD3            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    // 9600 baud

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 = 0;             // 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_CHANNELS       4
#define GAMEDATA_CHANNEL_SIZE   4
#define GAMEDATA_SOLUTION_POS   0
#define GAMEDATA_ENTRY_POS      1
#define GAMEDATA_LED_POS        2
#define GAMEDATA_MARK_POS       3

#define GAME_LED_GREEN          0x04   // on PA2, so mask is 0b00000100
#define GAME_LED_RED            0x08   // on PA3, so mask is 0b00001000
#define GAME_LED_YELLOW         0x0C   // on PA2 and PA3, so mask is 0b00001100

volatile unsigned int seconds;

volatile unsigned char gameState;   // record the current state of the game
volatile unsigned char gameData[GAMEDATA_CHANNELS * GAMEDATA_CHANNEL_SIZE] = {1, 0, 0, 0,
                                                                              3, 0, 0, 0,
                                                                              3, 0, 0, 0,
                                                                              7, 0, 0, 0};
                                        // the array with solutions, answers and game results
volatile unsigned char gameDataIndex;   // the pointer to the gameData array
volatile unsigned char gameChannelSelect;   // the current selected channel
volatile unsigned char buttonPressed = 0;

#endif