/****************************************************************************
*
* 文件名: usart_api.c
* 内容简述: 使用中断方式实现串口FIFO，提供易用的函数接口，便于用户程序调用。
	注意：
	(1) 中断程序未对发送缓冲区满做处理.也就是说调用comSendBuf()函数时，如果
		开辟的发送缓冲区不足，将导致最先放入FIFO的数据被冲掉。
		不过对以一般的应用程序，将发送缓冲区开的足够大以保证一个最长的通信帧能够容纳下即可。
*/


/* Includes ------------------------------------------------------------------*/
#include "string.h"
#include "include.h"

#include "usart.h"
#include "delay.h"

#if ARCH_MODE == NRF52832_Slave_Mode
#include "app_uart.h"
#include "ble_nus.h"
#include "ble_advdata.h"

#elif ARCH_MODE == CC254X_Peripheral_Mode
#include "simpleBLE.h"
#elif ARCH_MODE == NRF51822_Peripheral_Mode 
#include "nrf_log.h"
#include "app_uart.h"
#include "ble_nus.h"
#include "ble_advdata.h"
#include "ble_advertising.h"
#include "bsp_btn_ble.h"
#elif ARCH_MODE == NRF51802_Peripheral_Mode
#include "app_uart.h"
#include "ble_nus.h"
#include "ble_advdata.h"

#endif

/*******************************************************/
static uint8_t HexTable[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

//////////////////////////////////////////////////////////////////
#if ARCH_MODE == NRF52832_Slave_Mode        
extern ble_nus_t                        m_nus;                                      /**< Structure to identify the Nordic UART Service. */
#elif ARCH_MODE == NRF51822_Peripheral_Mode
extern ble_nus_t                        m_nus;                                      /**< Structure to identify the Nordic UART Service. */
#elif ARCH_MODE == NRF51802_Peripheral_Mode
extern ble_nus_t                        m_nus;                                      /**< Structure to identify the Nordic UART Service. */
#else

#endif

uint32_t ble_uart_string_send(uint8_t * p_string)
{
#if ARCH_MODE == NRF52832_Slave_Mode        
	return ble_nus_string_send_short(&m_nus, p_string, strlen((const char *)p_string));
#elif ARCH_MODE == CC254X_Peripheral_Mode
	simpleBLE_UartDataMain(p_string, strlen((const char *)p_string));
	return NRF_SUCCESS;
#elif ARCH_MODE == NRF51822_Peripheral_Mode
	return ble_nus_string_send_short(&m_nus, p_string, strlen((const char *)p_string));
#elif ARCH_MODE == NRF51802_Peripheral_Mode
	return ble_nus_string_send_short(&m_nus, p_string, strlen((const char *)p_string));
#endif
}

uint32_t ble_uart_string_len_send(uint8_t * p_string, uint16_t length)
{
#if ARCH_MODE == NRF52832_Slave_Mode        
	return ble_nus_string_send_short(&m_nus, p_string, length);
#elif ARCH_MODE == CC254X_Peripheral_Mode
	simpleBLE_UartDataMain(p_string, length);
	return NRF_SUCCESS;
#elif ARCH_MODE == NRF51822_Peripheral_Mode
	return ble_nus_string_send_short(&m_nus, p_string, length);
#elif ARCH_MODE == NRF51802_Peripheral_Mode
	return ble_nus_string_send_short(&m_nus, p_string, length);
#endif
}

#if UART_ENABLED

/**@brief   Function for handling app_uart events.
 *
 * @details This function will receive a single character from the app_uart module and append it to
 *          a string. The string will be be sent over BLE when the last character received was a
 *          'new line' i.e '\r\n' (hex 0x0D) or if the string has reached a length of
 *          @ref NUS_MAX_DATA_LENGTH.
 */
/**@snippet [Handling the data received over UART] */
static void uart_event_handle(app_uart_evt_t * p_event)
{
    static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
    static uint8_t index = 0;
    //uint32_t       err_code = NRF_SUCCESS;

    switch (p_event->evt_type)
    {
        case APP_UART_DATA_READY:
#if UART_ENABLED
            UNUSED_VARIABLE(app_uart_get(&data_array[index]));
		/*
            index++;

            if ((data_array[index - 1] == '\n') || (index >= (BLE_NUS_MAX_DATA_LEN)))
            {
                //err_code = ble_nus_string_send(&m_nus, data_array, index);
                if (err_code != NRF_ERROR_INVALID_STATE)
                {
                    APP_ERROR_CHECK(err_code);
                }

                index = 0;
            }
		*/
#endif
            break;

        case APP_UART_COMMUNICATION_ERROR:
            //APP_ERROR_HANDLER(p_event->data.error_communication);
            break;

        case APP_UART_FIFO_ERROR:
            //APP_ERROR_HANDLER(p_event->data.error_code);
            break;

        default:
            break;
    }
}
/**@snippet [Handling the data received over UART] */
#endif

/**@brief  Function for initializing the UART module.
 */
/**@snippet [UART Initialization] */
void uart_init(void)
{
#if UART_ENABLED
    uint32_t                     err_code;
    app_uart_comm_params_t const comm_params =
    {
        .rx_pin_no    = RX_PIN_NUMBER,
        .tx_pin_no    = TX_PIN_NUMBER,
        .rts_pin_no   = RTS_PIN_NUMBER,
        .cts_pin_no   = CTS_PIN_NUMBER,
        .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
        .use_parity   = false,
        .baud_rate    = NRF_UART_BAUDRATE_115200
    };

    APP_UART_FIFO_INIT(&comm_params,
                       UART_RX_BUF_SIZE,
                       UART_TX_BUF_SIZE,
                       uart_event_handle,
                       APP_IRQ_PRIORITY_LOWEST,
                       err_code);
    APP_ERROR_CHECK(err_code);
#endif
}
/**@snippet [UART Initialization] */

/****************************************************************************
* 函数名: comSendBuf
* 功  能: 向串口发送一组数据，非阻塞。
* 输  入: 	_ucPort: 端口号(COM1 COM2 COM3)
*			_ucaBuf: 待发送的数据缓冲区
*			_usLen : 数据长度
* 输  出: 无
* 返  回: 无
*/
void comSendBuf(uint8_t _ucPort, uint8_t *_ucaBuf, uint16_t _usLen)
{
#if ARCH_MODE == NRF52832_Slave_Mode
	ble_uart_string_send(_ucaBuf);
#endif	
}

/****************************************************************************
* 函数名: comGetChar
* 功  能: 从串口缓冲区读取1字节，非阻塞。
* 输  入: _pByte 结果存放地址
* 输  出: 无
* 返  回: 0 表示无数据, 1 表示读取到有效字节
*/
uint8_t comGetChar(uint8_t _ucPort, uint8_t *_pByte)
{
	//*_pByte = NRF_LOG_GETCHAR();
	
	return 1;
}

/************************************************************/
void Send(uint8_t dat)
{
#if ARCH_MODE == NRF52832_Slave_Mode
    ble_uart_string_send(&dat);
#endif	
}

void SendNewLine(void)
{    
#if ARCH_MODE == NRF52832_Slave_Mode
	uint8_t chr = 0x0D;
	ble_uart_string_len_send(&chr, 1);
	chr = 0x0A;
	ble_uart_string_len_send(&chr, 1);
#endif	
}

void SendString(uint8_t *dat)
{
#if ARCH_MODE == NRF52832_Slave_Mode
    ble_uart_string_send(dat);//发送TxBuffer1数组的字符
#endif	
}

void SendString_NewLine(uint8_t *dat)
{
#if ARCH_MODE == NRF52832_Slave_Mode
    ble_uart_string_send(dat);//发送TxBuffer1数组的字符
    SendNewLine();//发送新行  0xd 0xa
#endif	
}

void SendUsartxString_NewLine(uint8_t *dat)
{
#if ARCH_MODE == NRF52832_Slave_Mode
    ble_uart_string_send(dat);//发送TxBuffer1数组的字符
    SendNewLine();//发送新行  0xd 0xa
#endif	
}

void UART2_SendChar(uint8_t dat)
{
    Send('0');
    Send('x');
    Send(HexTable[dat>>4]);
    Send(HexTable[dat&0x0f]);
    Send(' ');
}

void UART2_SendString(uint8_t *dat)
{
#if ARCH_MODE == NRF52832_Slave_Mode
    ble_uart_string_send(dat);//发送TxBuffer1数组的字符
#endif	
}

/*********************************************end of file*********************************************/
