/******************************************************************************
 * @file     common.c
 * @brief    Common functions.
 * @version  V4.0.3
 * @date     30-July-2026
 *
 * @note
 * Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd.. All rights reserved.
 *
 * @attention
 * THIS SOFTWARE JUST PROVIDES CUSTOMERS WITH CODING INFORMATION REGARDING
 * THEIR PRODUCTS, WHICH AIMS AT SAVING TIME FOR THEM. SPINTROL SHALL NOT BE
 * LIABLE FOR THE USE OF THE SOFTWARE. SPINTROL DOES NOT GUARANTEE THE
 * CORRECTNESS OF THIS SOFTWARE AND RESERVES THE RIGHT TO MODIFY THE SOFTWARE
 * WITHOUT NOTIFICATION.
 *
 ******************************************************************************/

#include "common.h"




/******************************************************************************
 * @brief      Convert an integer to a string
 *
 * @param[in]  str   :  The string
 * @param[in]  intnum:  The integer to be converted
 *
 * @return     none
 *
 ******************************************************************************/
void Int2Str(uint8_t *str, uint32_t intnum)
{
    uint32_t i, Div = 1000000000, j = 0, Status = 0;

    for (i = 0; i < 10; i++)
    {
        str[j++] = (intnum / Div) + 48;

        intnum = intnum % Div;
        Div /= 10;

        if ((str[j - 1] == '0') & (Status == 0))
        {
            j = 0;
        }
        else
        {
            Status++;
        }
    }
}




/******************************************************************************
 * @brief      Convert a string to an integer
 *
 * @param[in]  inputstr:  The string to be converted
 * @param[in]  intnum  :  The intger value
 *
 * @return     0 - Error
 *             1 - Correct
 *
 ******************************************************************************/
uint32_t Str2Int(uint8_t *inputstr, uint32_t *intnum)
{
    uint32_t i = 0, result = 0;
    uint32_t val = 0;

    if (inputstr[0] == '0' && (inputstr[1] == 'x' || inputstr[1] == 'X'))
    {
        if (inputstr[2] == '\0')
        {
            return 0;
        }

        for (i = 2; i < 11; i++)
        {
            /* End flag */
            if (inputstr[i] == '\0')
            {
                *intnum = val;
                /* return 1; */
                result = 1;
                break;
            }

            if (ISVALIDHEX(inputstr[i]))
            {
                val = (val << 4) + CONVERTHEX(inputstr[i]);
            }
            else
            {
                /* return 0, Invalid input */
                result = 0;
                break;
            }
        }

        /* Over 8 digit hex --invalid */
        if (i >= 11)
        {
            result = 0;
        }
    }
    else /* max 10-digit decimal input */
    {
        for (i = 0; i < 11; i++)
        {
            if (inputstr[i] == '\0')
            {
                *intnum = val;
                /* return 1 */
                result = 1;
                break;
            }
            else if ((inputstr[i] == 'k' || inputstr[i] == 'K') && (i > 0))
            {
                val = val << 10;
                *intnum = val;
                result = 1;
                break;
            }
            else if ((inputstr[i] == 'm' || inputstr[i] == 'M') && (i > 0))
            {
                val = val << 20;
                *intnum = val;
                result = 1;
                break;
            }
            else if (ISVALIDDEC(inputstr[i]))
            {
                val = val * 10 + CONVERTDEC(inputstr[i]);
            }
            else
            {
                /* return 0, Invalid input */
                result = 0;
                break;
            }
        }

        /* Over 10 digit decimal --invalid */
        if (i >= 11)
        {
            result = 0;
        }
    }

    return result;
}




/******************************************************************************
 * @brief      Get a key from the PC Terminal
 *
 * @param[in]  none
 *
 * @return     The key pressed
 *
 ******************************************************************************/
uint8_t GetKey(void)
{
    uint8_t key = 0;

    /* Waiting for user input */
    while (UART_IsRxNotEmpty(UART) == 0)
    {
    }
    key = UART_ReadByte(UART);

    return key;
}




/******************* Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd. ***** END OF FILE ****/
