/*
* Copyright (c) 2015, Freescale Semiconductor, Inc.
* Copyright 2016-2017 NXP
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice, this list
*   of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice, this
*   list of conditions and the following disclaimer in the documentation and/or
*   other materials provided with the distribution.
*
* o Neither the name of the copyright holder nor the names of its
*   contributors may be used to endorse or promote products derived from this
*   software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "board.h"
#include "fsl_uart.h"
//#include "fsl_debug_console.h"
#include "pin_mux.h"
#include "composite.h"

/*******************************************************************************
* Definitions
******************************************************************************/


/* UART instance and clock */
#define DEMO_UART UART1
#define DEMO_UART_CLKSRC BUS_CLK
#define DEMO_UART_CLK_FREQ CLOCK_GetFreq(BUS_CLK)
#define DEMO_UART_IRQn UART1_IRQn
#define DEMO_UART_IRQHandler UART1_IRQHandler

/*! @brief Ring buffer size (Unit: Byte). */
#define DATA_BUFFER_SIZE 50


extern volatile struct hid_composite_t hid_report;
/*******************************************************************************
* Prototypes
******************************************************************************/


/*******************************************************************************
* Variables
******************************************************************************/

uint8_t dataBuffer[DATA_BUFFER_SIZE];
volatile uint16_t rxIndex; /* Index of the memory to save new arrived data. */
/*******************************************************************************
* Code
******************************************************************************/

volatile uint8_t packet_ok = 0;
void DEMO_UART_IRQHandler(void)
{
  volatile static uint8_t data;
  static volatile  uint8_t start = 0;
  /* If new data arrived. */
  if ((kUART_RxDataRegFullFlag | kUART_RxOverrunFlag) & UART_GetStatusFlags(DEMO_UART))
  {
    data = UART_ReadByte(DEMO_UART);
    if (data == '?' &&  packet_ok == 0) // PAcket start 
      start = 1;
    
    if(start)
    {
      dataBuffer[rxIndex++] = data;
      if (data == '@') //Packed edn
      {
        dataBuffer[rxIndex++] = '\0';
        rxIndex =0;
        packet_ok = 1;
        start = 0;
      }
      if (rxIndex > 26) // If some mistake arrived = reset 
      {
        rxIndex = 0;
        start = 0;
      }
    } 
  }
}


int main(void)
{
  uart_config_t config;
  
  /* Board pin, clock, debug console init */
  BOARD_InitPins();
  BOARD_BootClockRUN();
  //BOARD_InitDebugConsole();
  USB_DeviceApplicationInit();
  
  /*
  * config.baudRate_Bps = 115200U;
  * config.parityMode = kUART_ParityDisabled;
  * config.stopBitCount = kUART_OneStopBit;
  * config.txFifoWatermark = 0;
  * config.rxFifoWatermark = 1;
  * config.enableTx = false;
  * config.enableRx = false;
  */
  UART_GetDefaultConfig(&config);
  config.baudRate_Bps = BOARD_DEBUG_UART_BAUDRATE;
  config.enableTx = true;
  config.enableRx = true;
  UART_Init(DEMO_UART, &config, DEMO_UART_CLK_FREQ);

  /* Enable RX interrupt. */
  UART_EnableInterrupts(DEMO_UART, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable);
  EnableIRQ(DEMO_UART_IRQn);
  
  int32_t result = -6;
  while (1)
  {
    /* Process data only when correct packet was received */
    if (packet_ok == 1)
    {
      result = sscanf(dataBuffer,"?L%4ldU%4ldS%4ldB%3huK%3hu@", &hid_report.moveLR,&hid_report.moveUD,&hid_report.Scrool,&hid_report.mouse_button,&hid_report.keyboard_modifier_button);
      packet_ok = 0;
    }
  }
}
