/******************** (C) COPYRIGHT 2015 STMicroelectronics ********************
* File Name          : main.c
* Author             : AMS - VMA RF Application Team
* Version            : V1.0.0
* Date               : 05-October-2015
* Description        : BlueNRG-1 HID/HOGP main
********************************************************************************
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/
/** @addtogroup BlueNRG1_demonstrations_applications
 *  BlueNRG-1 HID Peripheral demo \see main.c for documentation.
 *
 *@{
 */

/** @} */
/** \cond DOXYGEN_SHOULD_SKIP_THIS
 */

/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#ifdef __ICCARM__
#include <yfuns.h>
#endif
#include <string.h>
#include "hw_config.h"
#include "BlueNRG1_it.h"
#include "BlueNRG1_conf.h"
#include "sleep.h"
#include "ble_const.h"
#include "bluenrg1_stack.h"
#include "SDK_EVAL_Com.h"
#include "hid_stack_config.h"
#include "clock.h"

/* Include the correct HID device header file */
#ifdef HID_KEYBOARD
#include "keyboard.h"
#else
#include "mouse.h"
#endif

//#define DEBUG 1
#ifdef DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#else
#define PRINTF(...)
#endif

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
int main(void)
{
  uint8_t ret;

  /* System Init */
  SystemInit();
  
  /* Identify BlueNRG1 platform */
  SdkEvalIdentification();

  /* Application demo Led Init 
     LED1 = Sensor configuration led 
     LED3 = Connection led
  */
  SdkEvalLedInit(LED1);
  SdkEvalLedInit(LED3);
  SdkEvalLedOn(LED1);
  SdkEvalLedOff(LED3);

  /* Serial Init 115200-8-N-1*/
  SerialInit();

  /* Clock Init */
  Clock_Init();

  /* BlueNRG-1 stack init */
  ret = BlueNRG_Stack_Initialization(&BlueNRG_Stack_Init_params);
  if (ret != BLE_STATUS_SUCCESS) {
    PRINTF("Error in BlueNRG_Stack_Initialization() 0x%02x\r\n", ret);
    while(1);
  }

  /* Configure HID Peripheral and put the device in discoverable mode */
  ret = Configure_HidPeripheral();
  if (ret != BLE_STATUS_SUCCESS) {
    PRINTF("Error in configure_hid_peripheral() 0x%02x\n", ret);
  }
  
  while(1) {
    /* BLE Tick */
    BTLE_StackTick();
    
    /* Application Tick */
    APP_Tick();
    
    /* Device Power Save Management Procedures */
    DevicePowerSaveProcedure();
  }
}

#ifdef  USE_FULL_ASSERT

/**
* @brief  Reports the name of the source file and the source line number
*         where the assert_param error has occurred.
* @param  file: pointer to the source file name
* @param  line: assert_param error line source number
*/
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
  ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  
  /* Infinite loop */
  while (1)
  {
  }
}

#endif
