// AVKrobotics
#include <asf.h>
//#include <pio_samd20j18.h>

#  define PORTA             PORT->Group[0]
#  define PORTB             PORT->Group[1]

// Led0 Oled1planedXpro
#define LED0_PIN                  PIN_PA14
#define LED0_ACTIVE               false
#define LED0_INACTIVE             !LED0_ACTIVE
//Wrap
#define LED_0_NAME                "LED0 (yellow)"
#define LED_0_PIN                 LED0_PIN
#define LED_0_ACTIVE              LED0_ACTIVE
#define LED_0_INACTIVE            LED0_INACTIVE

// Led1 Oled1planedXpro
#define LED1_PIN                  PIN_PA12
#define LED1_ACTIVE               false
#define LED1_INACTIVE             !LED1_ACTIVE
//Wrap
#define LED_1_NAME                "LED1 (yellow)"
#define LED_1_PIN                 LED1_PIN
#define LED_1_ACTIVE              LED1_ACTIVE
#define LED_1_INACTIVE            LED1_INACTIVE

// Led2  Oled1planedXpro
#define LED2_PIN                  PIN_PA13
#define LED2_ACTIVE               false
#define LED2_INACTIVE             !LED2_ACTIVE
//Wrap
#define LED_2_NAME                "LED2 (yellow)"
#define LED_2_PIN                 LED2_PIN
#define LED_2_ACTIVE              LED2_ACTIVE
#define LED_2_INACTIVE            LED2_INACTIVE

// Led3 Oled1planedXpro
#define LED3_PIN                  PIN_PA15
#define LED3_ACTIVE               false
#define LED3_INACTIVE             !LED3_ACTIVE
//Wrap
#define LED_3_NAME                "LED3 (yellow)"
#define LED_3_PIN                 LED3_PIN
#define LED_3_ACTIVE              LED3_ACTIVE
#define LED_3_INACTIVE            LED3_INACTIVE

// Botton 0
#define SW0_PIN                   PIN_PA15
#define SW0_ACTIVE                false
#define SW0_INACTIVE              !SW0_ACTIVE
//#define SW0_EIC_PIN               PIN_PA15A_EIC_EXTINT15
//#define SW0_EIC_MUX               MUX_PA15A_EIC_EXTINT15
//#define SW0_EIC_PINMUX            PINMUX_PA15A_EIC_EXTINT15
//#define SW0_EIC_LINE              15
/* \name Button #0 definitions
*
* Wrapper macros for SW0, to ensure common naming across all Xplained Pro
* boards.
*
*  @{ */
#define BUTTON_0_NAME             "SW0"
#define BUTTON_0_PIN              SW0_PIN
#define BUTTON_0_ACTIVE           SW0_ACTIVE
#define BUTTON_0_INACTIVE         SW0_INACTIVE
//#define BUTTON_0_EIC_PIN          SW0_EIC_PIN
//#define BUTTON_0_EIC_MUX          SW0_EIC_MUX
//#define BUTTON_0_EIC_PINMUX       SW0_EIC_PINMUX
//#define BUTTON_0_EIC_LINE         SW0_EIC_LINE

// Botton 1 Oled1planedXpro
#define SW1_PIN                   PIN_PA28
#define SW1_ACTIVE                false
#define SW1_INACTIVE              !SW1_ACTIVE /*
#define SW1_EIC_PIN               PIN_PA28A_EIC_EXTINT28
#define SW1_EIC_MUX               MUX_PA28_EIC_EXTINT28
#define SW1_IC_PINMUX            PINMUX_PA28EIC_EXTINT28
#define SW_EIC_LINE              28 */
/* \name Button #0 definitions
*
* Wrapper macros for SW1, to ensure common naming across all Xplained Pro
* boards.
*
*  @{ */
	#define BUTTON_1_NAME             "SW1"
	#define BUTTON_1_PIN              SW1_PIN
	#define BUTTON_1_ACTIVE           SW1_ACTIVE
	#define BUTTON_1_INACTIVE         SW1_INACTIVE /*
	#define BUTTON_0_EIC_PIN          SW0_EIC_PIN
	#define BUTTON_0_EIC_MUX          SW0_EIC_MUX
	#define BUTTON_0_EIC_PINMUX       SW0_EIC_PINMUX
	#define BUTTON_0_EIC_LINE         SW0_EIC_LINE  */
	
	// Botton 2 Oled1planedXpro
#define SW2_PIN                   PIN_PA02
#define SW2_ACTIVE                false
#define SW2_INACTIVE              !SW2_ACTIVE /*
#define SW1_EIC_PIN               PIN_PA28A_EIC_EXTINT28
#define SW1_EIC_MUX               MUX_PA28_EIC_EXTINT28
#define SW1_IC_PINMUX            PINMUX_PA28EIC_EXTINT28
#define SW_EIC_LINE              28 */
/* \name Button #0 definitions
*
* Wrapper macros for SW2, to ensure common naming across all Xplained Pro
* boards.
*
*  @{ */
	#define BUTTON_2_NAME             "SW2"
	#define BUTTON_2_PIN              SW2_PIN
	#define BUTTON_2_ACTIVE           SW2_ACTIVE
	#define BUTTON_2_INACTIVE         SW2_INACTIVE /*
	#define BUTTON_0_EIC_PIN          SW0_EIC_PIN
	#define BUTTON_0_EIC_MUX          SW0_EIC_MUX
	#define BUTTON_0_EIC_PINMUX       SW0_EIC_PINMUX
	#define BUTTON_0_EIC_LINE         SW0_EIC_LINE  */

// Botton 3 Oled1planedXpro
#define SW3_PIN                   PIN_PA03
#define SW3_ACTIVE                false
#define SW3_INACTIVE              !SW3_ACTIVE /*
#define SW1_EIC_PIN               PIN_PA28A_EIC_EXTINT28
#define SW1_EIC_MUX               MUX_PA28_EIC_EXTINT28
#define SW1_IC_PINMUX            PINMUX_PA28EIC_EXTINT28
#define SW_EIC_LINE              28 */
/* \name Button #0 definitions
*
* Wrapper macros for SW3, to ensure common naming across all Xplained Pro
* boards.
*
*  @{ */
	#define BUTTON_3_NAME             "SW3"
	#define BUTTON_3_PIN              SW3_PIN
	#define BUTTON_3_ACTIVE           SW3_ACTIVE
	#define BUTTON_3_INACTIVE         SW3_INACTIVE /*
	#define BUTTON_0_EIC_PIN          SW0_EIC_PIN
	#define BUTTON_0_EIC_MUX          SW0_EIC_MUX
	#define BUTTON_0_EIC_PINMUX       SW0_EIC_PINMUX
	#define BUTTON_0_EIC_LINE         SW0_EIC_LINE  */

	

void configure_port_pins(void);

void Delay(unsigned int n)
{while(n)
	{    
		n--;
	};
	return;
};

void configure_port_pins()
{
	struct port_config configure_port_pin;
	port_get_config_defaults(&configure_port_pin);
	
	configure_port_pin.direction = PORT_PIN_DIR_INPUT;
	configure_port_pin.input_pull = PORT_PIN_PULL_UP;
	port_pin_set_config(BUTTON_0_PIN, &configure_port_pin);
	port_pin_set_config(BUTTON_1_PIN, &configure_port_pin);
	port_pin_set_config(BUTTON_2_PIN, &configure_port_pin);
	port_pin_set_config(BUTTON_3_PIN, &configure_port_pin);
	
	configure_port_pin.direction = PORT_PIN_DIR_OUTPUT;
	port_pin_set_config(LED_0_PIN, &configure_port_pin);
	port_pin_set_config(LED_1_PIN, &configure_port_pin);
	port_pin_set_config(LED_2_PIN, &configure_port_pin);
	port_pin_set_config(LED_3_PIN, &configure_port_pin);
	
};

int main (void)
{
	system_init();
	configure_port_pins();
	
	while (true)
	{
		bool pin_state4 = port_pin_get_input_level(BUTTON_0_PIN);
		Delay(65000);
		port_pin_set_output_level(LED_0_PIN, !pin_state4);
		Delay(65000);
		bool pin_state = port_pin_get_input_level(BUTTON_1_PIN);
		Delay(65000);
		port_pin_set_output_level(LED_1_PIN, !pin_state);
		Delay(65000);
		bool pin_state2 = port_pin_get_input_level(BUTTON_2_PIN);
		Delay(65000);
		port_pin_set_output_level(LED_2_PIN, !pin_state2);
		bool pin_state3 = port_pin_get_input_level(BUTTON_3_PIN);
		Delay(65000);
		port_pin_set_output_level(LED_3_PIN, !pin_state3);
		
		
	};
	
};