📄 fesc_5554_term.c
字号:
/**********************************************************/
void TermActivityScan(void) {
uint8_t RecChar;
if(ESCI_A.SR.B.RDRF == 1)
//if( (ESCI_A.SR.R & 0x2000) !=0)
{
RecChar = ESCI_A.DR.B.D;
ESCI_A.SR.R = 0x20000000;
command_line_interface();
}
}
/*********************************************************************/
unsigned long TermGetNum(void) {
unsigned long temp;
unsigned char ch;
unsigned char base;
unsigned char cnt;
cnt = 0;
temp = 0;
base = 10;
while( (ch = get_c_ESCI_A()) != 0x0D)
{
send_c_ESCIA(ch);
if((cnt < 3)&&((ch == 'x')||(ch == 'X'))) base = 16;
temp = temp*base + char2num(ch);
cnt++;
}
return(temp);
}
/*********************************************************************
printp()
A simplied version of printf() to be used in Embedded system
**********************************************************************
打印时如果 %后面的x,d为大写,打印的数作为无符号数
为小写,作为有符号数
8bit 变量可以直接用 d
32bit的int变量必须加l。
/************************************************************************/
typedef struct Print_Ctrl_Tag
{
uint8_t pad_char;
uint8_t total_len;
uint8_t valid_len;
uint8_t base;
uint8_t negative;
//-----
uint8_t pad_flag;
uint8_t left_flag;
uint8_t long_flag;
} Print_Ctrl_T;
/*---------------------------------------------------*/
/* */
/* This routine puts pad characters into the output */
/* buffer. */
/* */
static void printp_padding(void (*PortToPut)(uint8_t),Print_Ctrl_T* ctrl_t_p )
{
uint8_t i;
if ( (ctrl_t_p->pad_flag == 1) && (ctrl_t_p->valid_len < ctrl_t_p->total_len))
for (i=ctrl_t_p->valid_len; i<ctrl_t_p->total_len; i++)
PortToPut(ctrl_t_p->pad_char);
}
/*---------------------------------------------------*/
/* */
/* This routine moves a string to the output buffer */
/* as directed by the printp_padding and positioning flags. */
/* */
static void printp_outs(void (*PortToPut)(uint8_t), uint8_t* lp, Print_Ctrl_T* ctrl_t_p )
{
/* pad on left if needed */
ctrl_t_p->valid_len = strlen(lp);
if(ctrl_t_p->left_flag != 1) printp_padding(PortToPut, ctrl_t_p);
/* Move string to the buffer */
while (*lp) PortToPut( *lp++);
/* Pad on right if needed */
if(ctrl_t_p->left_flag == 1) printp_padding(PortToPut, ctrl_t_p);
}
/*---------------------------------------------------*/
/* */
/* This routine moves a number to the output buffer */
/* as directed by the printp_padding and positioning flags. */
/* */
static void printp_outnum(void (*PortToPut)(uint8_t),unsigned long num, Print_Ctrl_T* ctrl_t_p )
{
uint8_t* cp;
uint8_t outbuf[32];
const uint8_t digits[] = "0123456789ABCDEF";
/* Build number (backwards) in outbuf */
cp = outbuf;
do {
*cp++ = digits[(uint8_t)(num % ctrl_t_p->base)];
} while ((num /= ctrl_t_p->base) > 0);
if (ctrl_t_p->negative) *cp++ = '-';
*cp-- = 0; //add a 0 in buffer is to use strlen function below
ctrl_t_p->valid_len = strlen(outbuf);
if(ctrl_t_p->left_flag != 1) printp_padding(PortToPut, ctrl_t_p);
while (cp >= outbuf) PortToPut( *cp--);
if(ctrl_t_p->left_flag == 1) printp_padding(PortToPut, ctrl_t_p);
}
/*---------------------------------------------------*/
/* */
/* This routine gets a number from the format */
/* string. */
/* */
static uint32_t printp_getnum( uint8_t** linep) //in this routine ,the return value is the number after %
{ //and the ctrl(pointer to the format string) will change too.
uint32_t n;
uint8_t* cp;
n = 0;
cp = *linep;
while (isdigit(*cp))
n = n*10 + ((*cp++) - '0');
*linep = cp;
return(n);
}
/*---------------------------------------------------*/
/* */
/* This routine operates just like a printf/sprintf */
/* routine. It outputs a set of data under the */
/* control of a formatting string. Not all of the */
/* standard C format control are supported. The ones */
/* provided are primarily those needed for embedded */
/* systems work. */
void printp(void (*PortToPut)(uint8_t), uint8_t* ctrl, ...)
{
Print_Ctrl_T ctrl_t;
uint8_t ch;
long num;
va_list argp;
va_start( argp, ctrl);
for ( ; *ctrl; ctrl++) {
if (*ctrl != '%') {
PortToPut(*ctrl);
continue;
}
/* initialize all the flags for this format. */
ctrl_t.long_flag = 0;
ctrl_t.left_flag = 0;
ctrl_t.pad_flag = 0;
ctrl_t.base = 10;
ctrl_t.negative = 0;
ctrl_t.total_len = 0;
ctrl_t.valid_len = 0;
ctrl_t.pad_char = ' ';
try_next:
ch = *(++ctrl);
if (isdigit(ch))
{
if (ch == '0') ctrl_t.pad_char = '0';
ctrl_t.total_len = printp_getnum(&ctrl);
ctrl_t.pad_flag = 1;
ctrl--;
goto try_next;
}
switch (tolower(ch)) { //the ch is the char after % or %numbers in format string
case '%':
PortToPut( '%');
continue; //if %, putout % and start next for loop
case '-':
ctrl_t.left_flag = 1;
break; //if -, left align out switch and goto try_next to see the next char
case 'l':
ctrl_t.long_flag = 1;
break; //l means long
case 'x': ctrl_t.base = 16;
case 'd':
if(islower(ch)) //signed format
{ if(ctrl_t.long_flag == 1) num = va_arg(argp, long);
else num = va_arg(argp, int);
if(num < 0L) {ctrl_t.negative = 1; num = - num;}
}
else //unsigned format
{ if(ctrl_t.long_flag == 1) num = va_arg(argp, unsigned long);
else num = va_arg(argp, unsigned int);
}
printp_outnum(PortToPut, (unsigned long)num ,&ctrl_t);
continue;
case 's':
printp_outs(PortToPut, va_arg( argp, uint8_t*), &ctrl_t);
continue; //output a string with special format as -10.8s and start next for loop
case 'c':
PortToPut((uint8_t)(va_arg(argp, int)));
continue; //output a char and start next for loop
default:
continue;
}
goto try_next; //execute only if there is '-' or '.' or 'l' in format
} //end of for loop
va_end( argp); //end arguments extraction
}
//end of file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -