text: /**
   * \file
   *
   * \brief User Interface
   *
   * Copyright (c) 2012-2018 Microchip Technology Inc. and its subsidiaries.
   *
   * \asf_license_start
   *
   * \page License
   *
   * Subject to your compliance with these terms, you may use Microchip
   * software and any derivatives exclusively with Microchip products.
   * It is your responsibility to comply with third party license terms applicable
   * to your use of third party software (including open source software) that
   * may accompany Microchip software.
   *
   * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
   * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
   * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
   * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
   * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
   * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
   * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
   * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE.  TO THE FULLEST EXTENT
   * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
   * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
   * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
   *
   * \asf_license_stop
   *
   */
  /*
   * Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
   */
  
  #include <asf.h>
  #include "ui.h"
  
  //! Sequence process running each \c SEQUENCE_PERIOD ms
  #define SEQUENCE_PERIOD 150
  
  static struct {
  	bool b_modifier;
  	bool b_down;
  	uint8_t u8_value;
  } ui_sequence[] = {
  	// Display windows menu
  	{true,true,HID_MODIFIER_LEFT_UI},
  	// Launch Windows Command line
  	{false,true,HID_R},
  	{false,false,HID_R},
  	// Clear modifier
  	{true,false,HID_MODIFIER_LEFT_UI},
  	// Tape sequence "notepad" + return
  	{false,true,HID_N},
  	{false,false,HID_N},
  	{false,true,HID_O},
  	{false,false,HID_O},
  	{false,true,HID_T},
  	{false,false,HID_T},
  	{false,true,HID_E},
  	{false,false,HID_E},
  	{false,true,HID_P},
  	{false,false,HID_P},
  	{false,true,HID_A},
  	{false,false,HID_A},
  	{false,true,HID_D},
  	{false,false,HID_D},
  	{false,true,HID_ENTER},
  	{false,false,HID_ENTER},
  	// Delay to wait "notepad" focus
  	{false,false,0}, // No key (= SEQUENCE_PERIOD delay)
  	{false,false,0}, // No key (= SEQUENCE_PERIOD delay)
  	{false,false,0}, // No key (= SEQUENCE_PERIOD delay)
  	{false,false,0}, // No key (= SEQUENCE_PERIOD delay)
  	{false,false,0}, // No key (= SEQUENCE_PERIOD delay)
  	{false,false,0}, // No key (= SEQUENCE_PERIOD delay)
  	{false,false,0}, // No key (= SEQUENCE_PERIOD delay)
  	// Display "Atmel "
  	{true,true,HID_MODIFIER_RIGHT_SHIFT}, // Enable Maj
  	{false,true,HID_A},
  	{false,false,HID_A},
  	{true,false,HID_MODIFIER_RIGHT_SHIFT}, // Disable Maj
  	{false,true,HID_T},
  	{false,false,HID_T},
  	{false,true,HID_M},
  	{false,false,HID_M},
  	{false,true,HID_E},
  	{false,false,HID_E},
  	{false,true,HID_L},
  	{false,false,HID_L},
  	{false,true,HID_SPACEBAR},
  	{false,false,HID_SPACEBAR},
  	// Display "ARM "
  	{false,true,HID_CAPS_LOCK}, // Enable caps lock
  	{false,false,HID_CAPS_LOCK},
  	{false,true,HID_A},
  	{false,false,HID_A},
  	{false,true,HID_R},
  	{false,false,HID_R},
  	{false,true,HID_M},
  	{false,false,HID_M},
  	{false,true,HID_CAPS_LOCK}, // Disable caps lock
  	{false,false,HID_CAPS_LOCK},
  };
  
  // Wakeup pin is PA15 (fast wakeup 14)
  #define  WAKEUP_PMC_FSTT (PMC_FSMR_FSTT14)
  #define  WAKEUP_PIN      (PIO_PA15_IDX)
  #define  WAKEUP_PIO      (PIOA)
  #define  WAKEUP_PIO_ID   (ID_PIOA)
  #define  WAKEUP_PIO_MASK (PIO_PA15)
  #define  WAKEUP_PIO_ATTR (PIO_INPUT | PIO_PULLUP | PIO_DEBOUNCE | PIO_IT_LOW_LEVEL)
  
  // Interrupt on "pin change" from PA15 to do wakeup on USB
  // Note:
  // This interrupt is enable when the USB host enable remotewakeup feature
  // This interrupt wakeup the CPU if this one is in idle mode
  static void ui_wakeup_handler(uint32_t id, uint32_t mask)
  {
  	if (WAKEUP_PIO_ID == id && WAKEUP_PIO_MASK == mask) {
  		// It is a wakeup then send wakeup USB
  		udc_remotewakeup();
  	}
  }
  
  void ui_init(void)
  {
  	// Enable PIO clock for button inputs
  	pmc_enable_periph_clk(ID_PIOA);
  	pmc_enable_periph_clk(ID_PIOB);
  	pmc_enable_periph_clk(ID_PIOC);
  	// Set handler for PA15
  	pio_handler_set(WAKEUP_PIO, WAKEUP_PIO_ID, WAKEUP_PIO_MASK, WAKEUP_PIO_ATTR, ui_wakeup_handler);
  	// Enable IRQ for button (PIOA)
  	NVIC_EnableIRQ((IRQn_Type) WAKEUP_PIO_ID);
  	// Initialize LEDs
  	LED_On(LED0_GPIO);
  	LED_Off(LED1_GPIO);
  }
  
  void ui_powerdown(void)
  {
  	LED_Off(LED0_GPIO);
  	LED_Off(LED1_GPIO);
  }
  
  
  void ui_wakeup_enable(void)
  {
  	// Configure PA15 as PIO input
  	pio_configure_pin(WAKEUP_PIN, WAKEUP_PIO_ATTR);
  	// Enable interrupt for PA15
  	pio_enable_pin_interrupt(WAKEUP_PIN);
  	// Enable fastwakeup for button pin (WKUP14 for PA15)
  	pmc_set_fast_startup_input(WAKEUP_PMC_FSTT);
  }
  
  void ui_wakeup_disable(void)
  {
  	// Disable interrupt for button pin
  	pio_disable_pin_interrupt(WAKEUP_PIN);
  	// Disable fastwakeup for button pin (WKUP14 for PA15)
  	pmc_clr_fast_startup_input(WAKEUP_PMC_FSTT);
  }
  
  void ui_wakeup(void)
  {
  	LED_On(LED0_GPIO);
  }
  
  void ui_process(uint16_t framenumber)
  {
  	bool b_btn_state, sucess;
  	static bool btn_last_state = false;
  	static bool sequence_running = false;
  	static uint8_t u8_sequence_pos = 0;
  	uint8_t u8_value;
  	static uint16_t cpt_sof = 0;
  
  	if ((framenumber % 1000) == 0) {
  		LED_On(LED0_GPIO);
  	}
  	if ((framenumber % 1000) == 500) {
  		LED_Off(LED0_GPIO);
  	}
  	// Scan process running each 2ms
  	cpt_sof++;
  	if ((cpt_sof % 2) == 0) {
  		return;
  	}
  
  	// Scan buttons on switch 0 to send keys sequence
  	b_btn_state = (!gpio_pin_is_high(GPIO_PUSH_BUTTON_1)) ? true : false;
  	if (b_btn_state != btn_last_state) {
  		btn_last_state = b_btn_state;
  		sequence_running = true;
  	}
  
  	// Sequence process running each period
  	if (SEQUENCE_PERIOD > cpt_sof) {
  		return;
  	}
  	cpt_sof = 0;
  
  	if (sequence_running) {
  		// Send next key
  		u8_value = ui_sequence[u8_sequence_pos].u8_value;
  		if (u8_value!=0) {
  			if (ui_sequence[u8_sequence_pos].b_modifier) {
  				if (ui_sequence[u8_sequence_pos].b_down) {
  					sucess = udi_hid_kbd_modifier_down(u8_value);
  				} else {
  					sucess = udi_hid_kbd_modifier_up(u8_value);
  				}
  			} else {
  				if (ui_sequence[u8_sequence_pos].b_down) {
  					sucess = udi_hid_kbd_down(u8_value);
  				} else {
  					sucess = udi_hid_kbd_up(u8_value);
  				}
  			}
  			if (!sucess) {
  				return; // Retry it on next schedule
  			}
  		}
  		// Valid sequence position
  		u8_sequence_pos++;
  		if (u8_sequence_pos >=
  			sizeof(ui_sequence) / sizeof(ui_sequence[0])) {
  			u8_sequence_pos = 0;
  			sequence_running = false;
  		}
  	}
  }
  
  void ui_start_read(void)
  {
  	LED_On(LED1_GPIO);
  }
  
  void ui_stop_read(void)
  {
  	LED_Off(LED1_GPIO);
  }
  
  void ui_start_write(void)
  {
  	LED_On(LED1_GPIO);
  }
  
  void ui_stop_write(void)
  {
  	LED_Off(LED1_GPIO);
  }
  
  void ui_kbd_led(uint8_t value)
  {
  	UNUSED(value);
  	/*
  	if (value & HID_LED_NUM_LOCK) {
  	} else {
  	}
  	if (value & HID_LED_CAPS_LOCK) {
  	} else {
  	}
  	*/
  }
  
  
  /**
   * \defgroup UI User Interface
   *
   * Human interface on SAM4S-EK2:
   * - Led 0 (D2) blinks when USB host has checked and enabled MSC interface
   * - The USRPB1 (BP2) opens a notepad application on Windows O.S.
   *   and sends key sequence "Atmel ARM"
   * - Led 1 (D3) is on during read/write operation
   * - Only a low level on J13.PA15 will generate a wakeup to USB Host in remote wakeup mode.
   *
   */
  