📄 console.c
字号:
/*
Text console driver
----------------------------
(c) motoprogger 2008
*/
#include "../libs/motolibs.h"
#include "display.h"
#include "backlight.h"
#include "console.h"
#define CHAR_WIDTH 5
#define CHAR_HEIGHT 8
#define CHAR_XSPACE 1
#define CHAR_YSPACE 1
#define CHAR_XSHIFT 0
#define CHAR_YSHIFT 0
#define CHAR_XDIM (CHAR_WIDTH+CHAR_XSPACE)
#define CHAR_YDIM (CHAR_HEIGHT+CHAR_YSPACE)
#define FONT_CHARSIZE 5
#define TAB_SIZE 8
byte x,y;
byte x1, y1, x2, y2;
short x0,y0;
byte dx,dy;
hword bgcolor, textcolor;
extern byte font_5x8;
void console_gotoxy(byte xp, byte yp)
{
x=xp;
y=yp;
}
void console_clearscreen()
{
display_set_fill_mode(true);
display_rectangle(x1, y1, x2, y2, bgcolor, bgcolor);
console_gotoxy(0,0);
}
void console_setviewport(byte nx1, byte ny1, byte nx2, byte ny2)
{
byte dxp, dyp, xsh, ysh;
dxp=nx2-nx1+1+CHAR_XSPACE;
dyp=ny2-ny1+1+CHAR_YSPACE;
dx=dxp/CHAR_XDIM;
xsh=dxp%CHAR_XDIM;
dy=dyp/CHAR_YDIM;
ysh=dyp%CHAR_YDIM;
x1=nx1; y1=ny1; x2=nx2; y2=ny2;
x0=x1+(xsh>>1);
y0=y1+(ysh>>1);
}
void console_setcolors(hword bgcol, hword forecol)
{
bgcolor=bgcol;
textcolor=forecol;
}
void console_init()
{
display_init();
console_setviewport(0,0,DISPLAY_XDIM-1, DISPLAY_YDIM-1);
console_setcolors(0x0000,0xFFFF);
console_clearscreen();
backlight_set(true, true);
}
void console_getcharimg(unsigned char ch, hword * img, hword bgcol, hword chcol, byte chw, byte chh)
{
byte x,y;
hword * pixel_ptr;
byte * char_ptr;
char_ptr=(&font_5x8)+(ch-0x20)*FONT_CHARSIZE;
pixel_ptr=img;
for (y=0; y<chh; y++)
for (x=0; x<chw; x++)
if (y>=CHAR_YSHIFT && x>=CHAR_XSHIFT && x<CHAR_YSHIFT+CHAR_WIDTH && char_ptr[x-CHAR_XSHIFT]&1<<(y-CHAR_YSHIFT)) *(pixel_ptr++)=chcol;
else *(pixel_ptr++)=bgcol;
}
void console_putchar(unsigned char ch, byte xp, byte yp, hword bgcol, hword chcol)
{
byte chx1, chy1, chx2, chy2, chw, chh;
word i;
hword charimg[CHAR_XDIM*CHAR_YDIM];
chx1=x0+CHAR_XDIM*xp;
chy1=y0+CHAR_YDIM*yp;
chw=CHAR_XDIM;
chh=CHAR_YDIM;
chx2=chx1+chw-1;
chy2=chy1+chh-1;
if (chx2>x2) chw-=(chx2-x2);
if (chy2>y2) chh-=(chy2-y2);
chx2=chx1+chw-1;
chy2=chy1+chh-1;
if (ch>=0x20 && ch<0x80) console_getcharimg(ch, charimg, bgcol, chcol, chw, chh);
else for (i=0; i<chw*chh; i++) charimg[i]=bgcol;
display_put_image(chx1, chy1, chx2, chy2, (void * ) charimg);
}
void console_scroll(byte lines)
{
byte sx1, sy1, sx2, sy2;
if (lines>=dy)
{
console_clearscreen();
return;
}
sx1=x0;
sx2=x0+dx*CHAR_XDIM-1;
sy1=y0+lines*CHAR_YDIM;
sy2=y0+dy*CHAR_YDIM-CHAR_YSPACE+CHAR_YSHIFT-1;
display_copy(sx1,sy1,sx2,sy2,x0,y0);
display_set_fill_mode(true);
display_rectangle(sx1,y0+(dy-lines)*CHAR_YDIM,sx2,sy2,bgcolor,bgcolor);
y-=lines;
}
void console_printstr(char * str)
{
while (*str)
{
switch(*str)
{
case 0x8:
if (x>0) x--;
else if (y>0)
{
y--;
x=dx-1;
}
console_putchar(' ',x,y,bgcolor,textcolor);
break;
case 0x9:
x=x-(x%TAB_SIZE)+TAB_SIZE;
if (x>=dx)
{
x=0;
if (++y>=dy) console_scroll(1);
}
break;
case 0xA:
x=0;
if (++y>=dy) console_scroll(1);
break;
case 0xD:
x=0;
default:
console_putchar(*str,x++,y,bgcolor,textcolor);
if (x>=dx)
{
x=0;
if (++y>=dy) console_scroll(1);
}
}
str++;
}
}
void console_printui32_hex(word num, byte chars, bool prefix)
{
char numstr[12];
if (prefix)
{
numstr[0]='0';
numstr[1]='x';
util_ui32_to_hexasc(num,numstr+2,chars);
numstr[chars+2]=0;
}
else {
util_ui32_to_hexasc(num,numstr,chars);
numstr[chars]=0;
}
console_printstr(numstr);
}
void console_printui32_dec(word num)
{
char numstr[12];
numstr[11]=0;
console_printstr(util_ui32_to_decasc(num,numstr+10));
}
void console_newline()
{
console_printstr("\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -