📄 ttyio.c
字号:
/*
* Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
* All rights reserved.
*
* This software is copyrighted by and is the sole property of
* VIA Networking Technologies, Inc. This software may only be used
* in accordance with the corresponding license agreement. Any unauthorized
* use, duplication, transmission, distribution, or disclosure of this
* software is expressly forbidden.
*
* This software is provided by VIA Networking Technologies, Inc. "as is"
* and any express or implied warranties, including, but not limited to, the
* implied warranties of merchantability and fitness for a particular purpose
* are disclaimed. In no event shall VIA Networking Technologies, Inc.
* be liable for any direct, indirect, incidental, special, exemplary, or
* consequential damages.
*
*
* File: ttyio.c
*
* Purpose: Terminal basic I/O functions
*
* Author: Jenda Jao
*
* Date: Jan 08, 2002
*
* Functions:
*
* Revision History:
*
*/
#include <ctype.h>
#if !defined(__STR_H__)
#include "str.h"
#endif
#if !defined(__PLATFORM_H__)
#include "platform.h"
#endif
#if !defined(__TTYIO_H__)
#include "ttyio.h"
#endif
/*--------------------- Static Definitions ------------------------*/
#define UINT16_DEC_STR_LEN 5
#define UINT16_HEX_STR_LEN 4
/*--------------------- Static Types ------------------------------*/
/*--------------------- Static Macros -----------------------------*/
/*--------------------- Static Classes ----------------------------*/
/*--------------------- Static Variables --------------------------*/
/*--------------------- Static Functions --------------------------*/
/*--------------------- Export Variables --------------------------*/
//
// replace stdio.h
//
// Because the support of arrow key, the single ESC key will not work.
// In order to solve this problem, added a 0.5-second timer.
// if no key input, PLATbyUartGetKeyMultiTask() would return 0 every 0.5s.
// If there is not any input key after 0.5s timeout, TTYcGetChar() would return ESC key.
//
char TTYcGetChar(void) DIRECT_FUNTYPE_REENT
{
char cKey = 0;
// in order to support arrow key
while (cKey == 0) {
cKey = PLATbyUartGetKeyMultiTask();
}
if (cKey == KEY_ESC)
{
cKey = PLATbyUartGetKeyMultiTask();
if (cKey == 0) {
return KEY_ESC;
}
else if (cKey == '[') {
cKey = PLATbyUartGetKeyMultiTask();
if ('A' <= cKey && cKey <= 'D')
cKey += (KEY_ARROW_UP - 'A');
else
return 0; // if 3-key sequence is broken, return 0
}
else
return 0; // if 3-key sequence is broken, return 0
}
return cKey;
}
// This function will ignore KEY_DISCONNECT, and be used only for login.c
void TTYvGetString(PSTR pstr, UINT8 byStrLen, BOOL bPutStar) DIRECT_FUNTYPE_REENT
{
char cKey;
UINT8 byInLen = 0;
while (1) {
cKey = TTYcGetChar();
if (cKey == KEY_BKSPC) {
if (byInLen > 0) {
byInLen--;
TTYvBackspace();
}
}
else if (cKey == KEY_ENTER) {
pstr[byInLen] = 0;
return;
}
else if (isprint(cKey) && (byInLen < byStrLen)) { // if !isprint(cKey), do nothing, including KEY_DISCONNECT
pstr[byInLen] = cKey;
if (bPutStar)
TTYvPutChar('*');
else
TTYvPutChar(cKey);
byInLen++;
}
}
}
void TTYvPutChar(char c) DIRECT_FUNTYPE_REENT
{
while (RI)
; // (RI==1) indicates a key in SBUF but not has been got.
SBUF = c;
while (TI == 0)
; // wait for the key transmited
TI = 0;
if (c == '\n')
{
SBUF = '\r';
while (TI == 0)
;
TI = 0;
}
}
void TTYvPutString(char* strBuf) DIRECT_FUNTYPE_REENT
{
BYTE ui;
for(ui=0; strBuf[ui]; ui++)
TTYvPutChar(strBuf[ui]);
}
void TTYvSPutDec(char* strBuf, UINT16 u16Value) DIRECT_FUNTYPE_REENT
{
char strDec[UINT16_DEC_STR_LEN + 1] = {0};
BYTE byInStr = UINT16_DEC_STR_LEN;
do {
strDec[--byInStr] = '0' + (u16Value % 10);
} while (u16Value /= 10);
if (strBuf)
STRvStrcpy(strBuf, strDec+byInStr);
else
TTYvPutString(strDec + byInStr);
}
void TTYvSPutHex(char* strBuf, UINT16 u16Value) DIRECT_FUNTYPE_REENT
{
DIRECT_MEMTYPE_CODE char strHexDigit[] = {"0123456789ABCDEF"};
char strHex[UINT16_HEX_STR_LEN + 1] = {0};
BYTE byInStr = UINT16_HEX_STR_LEN;
do {
strHex[--byInStr] = strHexDigit[u16Value & 0xF];
} while (u16Value >>= 4);
if (byInStr % 2)
strHex[--byInStr] = '0';
if (strBuf)
STRvStrcpy(strBuf, strHex+byInStr);
else
TTYvPutString(strHex+byInStr);
}
void TTYvSetCurPos(UINT8 byRow, UINT8 byCol) DIRECT_FUNTYPE_REENT
{
TTYvPutString("\x1B[");
TTYvPutDec(byRow);
TTYvPutChar(';');
TTYvPutDec(byCol);
TTYvPutChar('H');
}
void TTYvDrawLine(UINT8 byRow, UINT8 byCol, char cDrawChar, UINT8 byLen) DIRECT_FUNTYPE_REENT
{
BYTE ui;
TTYvSetCurPos(byRow, byCol); //repeat some char for several times.
for(ui=0; ui<byLen; ui++)
TTYvPutChar(cDrawChar);
}
void TTYvPutPosStr(UINT8 byRow, UINT8 byCol, PSTR szOutput) DIRECT_FUNTYPE_REENT
{
TTYvSetCurPos(byRow, byCol);
TTYvPutString(szOutput);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -