/**
 * @file shell_port.c
 * @author Letter (NevermindZZT@gmail.com)
 * @brief 
 * @version 0.1
 * @date 2019-02-22
 * 
 * @copyright (c) 2019 Letter
 * 
 */

#include "shell.h"
#include "bsp_uart.h"
#include "log.h"
#include "stdbool.h"


#define     SHELL_UART      USART1
#define     SHELL_BUF_LENGTH      1024
Shell shell;

char shellBuffer[SHELL_BUF_LENGTH];

void Uart_Log_Write(char *buffer, short len);


// 定义log对象
Log uartLog = {
    .write = Uart_Log_Write,
    .active = LOG_ENABLE,
    .level = LOG_ALL

};


/**
 * @brief 用户shell写
 * 
 * @param data 数据
 * @param len 数据长度
 * 
 * @return unsigned short 写入实际长度
 */
signed short userShellWrite(char *data, unsigned short len)
{
    BSP_Uart3_Send_Data(data, len);
    return len;
}

// 实现log写buffer函数
void Uart_Log_Write(char *buffer, short len)
{
    if (uartLog.shell)
    {
        // log工具可以结合letter shell的尾行模式，实现log和shell共用一个终端，但不影响shell交互体验
        shellWriteEndLine(uartLog.shell, buffer, len);
    }
        // UART1_SendString((uint8_t *)buffer, len);
}



/**
 * @brief 用户shell初始化
 * 
 */
void userShellInit(void)
{
    shell.write = userShellWrite;

    shellInit(&shell, shellBuffer, SHELL_BUF_LENGTH);

    logRegister(&uartLog, &shell);
}


