📄 console.c
字号:
#include "console.h"
/* input a hex digital from serial console */
int InputHex (UartContextT *psCtx, unsigned int *pu32Hex)
{
int i;
char c;
int iChars = 0;
char acCharBuf[8]; /* 32-bit max */
unsigned int u32Hex = 0;
unsigned int u32Grade = 1;
do /* input a string from serial console */
{
psCtx->readUartFnP(psCtx, &c); /* get a char */
if ((c == '\r') || (c == '\n')) /* carriage return or newline */
{
psCtx->writeUartFnP(psCtx, "\n\r", 2);
break;
}
if ((c == '\b') && (iChars > 0)) /* backspace */
{
psCtx->writeUartFnP(psCtx, "\b \b", 3);
iChars --;
continue;
}
acCharBuf[iChars ++] = c; /* save the char to buffer */
psCtx->writeUartFnP(psCtx, &c, 1); /* echo the char to serial console */
if (iChars >= 8) /* 8 chars max */
{
psCtx->writeUartFnP(psCtx, "\n\r", 2);
break;
}
} while (1);
psCtx->clearRxUartFnP(psCtx); /* empty uart input buffer to avoid problems */
if (iChars <= 0)
{
DM_Message("Input Empty!");
return 0; /* no chars gotten */
}
iChars --; /* point to the last char */
for (i=iChars; i>=0; i--) /* convert the string to hex digital */
{
if ((acCharBuf[i] >= 'A') && (acCharBuf[i] <= 'F')) c = acCharBuf[i] - 55;
else if ((acCharBuf[i] >= 'a') && (acCharBuf[i] <= 'f')) c = acCharBuf[i] - 87;
else if ((acCharBuf[i] >= '0') && (acCharBuf[i] <= '9')) c = acCharBuf[i] - 48;
else /* invalid char */
{
DM_Message("Input Error!");
return 0; /* input error */
}
u32Hex += c * u32Grade;
u32Grade <<= 4; /* multiply by 16 */
}
*pu32Hex = u32Hex; /* result */
return 1; /* successful */
}
/* input a decimal digital from serial console */
int InputDec (UartContextT *psCtx, unsigned int *pu32Dec)
{
int i;
char c;
int iChars = 0;
char acCharBuf[8]; /* 32-bit max */
unsigned int u32Dec = 0;
unsigned int u32Grade = 1;
do /* input a string from serial console */
{
psCtx->readUartFnP(psCtx, &c); /* get a char */
if ((c == '\r') || (c == '\n')) /* carriage return or newline */
{
psCtx->writeUartFnP(psCtx, "\n\r", 2);
break;
}
if ((c == '\b') && (iChars > 0)) /* backspace */
{
psCtx->writeUartFnP(psCtx, "\b \b", 3);
iChars --;
continue;
}
acCharBuf[iChars ++] = c; /* save the char to buffer */
psCtx->writeUartFnP(psCtx, &c, 1); /* echo the char to serial console */
if (iChars >= 8) /* 8 chars max */
{
psCtx->writeUartFnP(psCtx, "\n\r", 2);
break;
}
} while (1);
psCtx->clearRxUartFnP(psCtx); /* empty uart input buffer to avoid problems */
if (iChars <= 0)
{
DM_Message("Input Empty!");
return 0; /* no chars gotten */
}
iChars --; /* point to the last char */
for (i=iChars; i>=0; i--) /* convert the string to decimal digital */
{
if ((acCharBuf[i] >= '0') && (acCharBuf[i] <= '9')) c = acCharBuf[i] - 48;
else /* invalid char */
{
DM_Message("Input Error!");
return 0; /* input error */
}
u32Dec += c * u32Grade;
u32Grade *= 10; /* multiply by 10 */
}
*pu32Dec = u32Dec; /* result */
return 1; /* successful */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -