📄 tty.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: tty.c
*
* Purpose: TTY basic I/O functions
*
* Author: Jenda Jao
*
* Date: Jan 08, 2002
*
* Functions:
*
* Revision History:
*
*/
#if !defined(__KEY_H__)
#include "key.h"
#endif
#if !defined(__STRTYPE_H__)
#include "strtype.h"
#endif
#if !defined(__STR_H__)
#include "str.h"
#endif
#if !defined(__UART_H__)
#include "uart.h"
#endif
#if !defined(__TTY_H__)
#include "tty.h"
#endif
#if !defined(__SPRINTF_H__)
#include "sprintf.h"
#endif
/*--------------------- Static Definitions ------------------------*/
#define UINT16_DEC_STR_LEN 5
#define UINT16_HEX_STR_LEN 4
#define UINT32_DEC_STR_LEN 10
#define UINT32_HEX_STR_LEN 8
// time ticks for successive escape sequence time-out value
#define TICKS_ESC_SEQ_TIMEOUT 5
/*--------------------- Static Types ------------------------------*/
/*--------------------- Static Macros -----------------------------*/
/*--------------------- Static Classes ----------------------------*/
/*--------------------- Static Variables --------------------------*/
/*--------------------- Static Functions --------------------------*/
/*--------------------- Export Variables --------------------------*/
//
// 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, UART_i16GetCharRawTimed() would return 0 every 0.5s.
// If there is not any input key after 0.5s timeout, TTY_cGetChar() would return ESC key.
//
char TTY_cGetChar(void)
{
INT16 i16Key;
// in order to support arrow key
do {
i16Key = UART_i16GetCharRawTimed(50);
} while (i16Key == FAILED);
if (i16Key == MK_ESCAPE) {
i16Key = UART_i16GetCharRawTimed(TICKS_ESC_SEQ_TIMEOUT);
if (i16Key == FAILED) {
return MK_ESCAPE;
}
else if (i16Key == '[') {
i16Key = UART_i16GetCharRawTimed(TICKS_ESC_SEQ_TIMEOUT);
if (('A' <= i16Key) && (i16Key <= 'D'))
i16Key += (MK_V_UP - 'A');
else{
/* PageUp and PageDown is 4-key */
if (i16Key == '3'){
UART_i16GetCharRawTimed(TICKS_ESC_SEQ_TIMEOUT);
i16Key = MK_V_PAGE_UP;
}
else if (i16Key == '4'){
UART_i16GetCharRawTimed(TICKS_ESC_SEQ_TIMEOUT);
i16Key = MK_V_DELETE;
}
else if (i16Key == '6'){
UART_i16GetCharRawTimed(TICKS_ESC_SEQ_TIMEOUT);
i16Key = MK_V_PAGE_DOWN;
}
else if (i16Key == '1'){
/* F1 is 5-key */
i16Key = UART_i16GetCharRawTimed(TICKS_ESC_SEQ_TIMEOUT);
if (i16Key == 0x7e){
i16Key = MK_V_INSERT;
}
else if (i16Key == '1'){
UART_i16GetCharRawTimed(TICKS_ESC_SEQ_TIMEOUT);
i16Key = MK_V_F1;
}
else if (i16Key == '3'){
UART_i16GetCharRawTimed(TICKS_ESC_SEQ_TIMEOUT);
i16Key = MK_V_F3;
}
else if (i16Key == '5'){
UART_i16GetCharRawTimed(TICKS_ESC_SEQ_TIMEOUT);
i16Key = MK_V_F5;
}
else if (i16Key == '7'){
UART_i16GetCharRawTimed(TICKS_ESC_SEQ_TIMEOUT);
i16Key = MK_V_F6;
}
}
else
return 0;
}
}
else {
return 0; // if 3-key sequence is broken, return 0
}
}
return (char)i16Key;
}
// This function be used only for login.c
// Mode: 0, put char
// : 1, put star
// : 2, put nothing
void TTY_vGetStr(PSTR pstr, UINT8 byStrLen, UINT8 byMode)
{
char cKey;
UINT8 byInLen = 0;
while (1) {
cKey = TTY_cGetChar();
if (cKey == MK_BACKSPACE) {
if (byInLen > 0) {
byInLen--;
TTY_vBackspace();
}
}
else if (cKey == MK_ENTER) {
pstr[byInLen] = 0;
return;
}
else if (STR_bIsPrint(cKey) && (byInLen < byStrLen)) {
// if !STR_bIsPrint(cKey), do nothing
pstr[byInLen] = cKey;
if (byMode == 1)
UART_vPutChar('*');
else if (byMode == 0)
UART_vPutChar(cKey);
byInLen++;
}
}
}
void TTY_vPutStr(char* strBuf)
{
UINT16 u16;
for (u16 = 0; strBuf[u16]; u16++)
UART_vPutChar(strBuf[u16]);
}
void TTY_vPutDec(UINT16 u16Value)
{
char strDec[UINT16_DEC_STR_LEN + 1];
UINT8 byInStr = UINT16_DEC_STR_LEN;
STR_pvMemset(strDec, 0 , UINT16_DEC_STR_LEN + 1);
do {
strDec[--byInStr] = '0' + (u16Value % 10);
} while (u16Value /= 10);
TTY_vPutStr(strDec + byInStr);
}
void TTY_vPutU32Dec(UINT32 u32Value)
{
char strDec[UINT32_DEC_STR_LEN + 1] = {0};
UINT8 byInStr = UINT32_DEC_STR_LEN;
do {
strDec[--byInStr] = '0' + (u32Value % 10);
} while (u32Value /= 10);
TTY_vPutStr(strDec + byInStr);
}
void TTY_vPutHex(UINT16 u16Value)
{
char strHexDigit[] = "0123456789ABCDEF";
char strHex[UINT16_HEX_STR_LEN + 1];
UINT8 byInStr = UINT16_HEX_STR_LEN;
STR_pvMemset(strHex, 0 , UINT16_HEX_STR_LEN + 1);
do {
strHex[--byInStr] = strHexDigit[u16Value & 0xF];
} while (u16Value >>= 4);
if ((byInStr % 2) != 0)
strHex[--byInStr] = '0';
TTY_vPutStr(strHex + byInStr);
}
void TTY_vPutU32Hex(UINT32 u32Value)
{
char strHexDigit[] = "0123456789ABCDEF";
char strHex[UINT32_HEX_STR_LEN + 1] = {0};
UINT8 byInStr = UINT32_HEX_STR_LEN;
do {
strHex[--byInStr] = strHexDigit[u32Value & 0xF];
} while (u32Value >>= 4);
if (byInStr % 2)
strHex[--byInStr] = '0';
TTY_vPutStr(strHex + byInStr);
}
void TTY_vSetCurPos(UINT8 byRow, UINT8 byCol)
{
TTY_vPutStr("\x1B[");
TTY_vPutDec(byRow);
UART_vPutChar(';');
TTY_vPutDec(byCol);
UART_vPutChar('H');
}
void TTY_vDrawLine(UINT8 byRow, UINT8 byCol, char cDrawChar, UINT8 byLen)
{
UINT8 ui;
TTY_vSetCurPos(byRow, byCol); //repeat some char for several times.
for (ui = 0; ui < byLen; ui++)
UART_vPutChar(cDrawChar);
}
void TTY_vPutStrOnPos(UINT8 byRow, UINT8 byCol, PSTR szOutput)
{
TTY_vSetCurPos(byRow, byCol);
TTY_vPutStr(szOutput);
}
void TTY_vPrintfHex (PSTR pszStr, UINT32 u32HexVal)
{
CHAR abyStr[MAX_FILENAME_LEN];
PSTR psz = abyStr;
psz += STR_iStrcpy(psz, pszStr);
psz += STR_iU32ToStrHexPad(psz, u32HexVal, 8);
psz += STR_iStrcat(psz, "\n");
TTY_vPutStr(abyStr);
}
void TTY_vPrintf(const char *fmt, ...)
{
#if 0 //TBD
static char aszBuffer[256];
PSTR piVarg = (PSTR)fmt;
PSTR pStr = aszBuffer;
//PRNT_iPrintFmt((PSTR*)&aszBuffer, NULL, (PINT)piVarg); //Is complier bug?
PRNT_iPrintFmt((PSTR*)&pStr, NULL, (PINT)piVarg);
TTY_vPutStr(aszBuffer);
#endif
TTY_vPutStr((char*)fmt);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -