Terminal.c
237 lines
| 7.6 KiB
| text/x-c
|
CLexer
r50 | /*------------------------------------------------------------------------------ | |||
-- This file is a part of the libuc, microcontroler library | ||||
-- Copyright (C) 2013, Alexis Jeandet | ||||
-- | ||||
-- This program is free software; you can redistribute it and/or modify | ||||
-- it under the terms of the GNU General Public License as published by | ||||
-- the Free Software Foundation; either version 3 of the License, or | ||||
-- (at your option) any later version. | ||||
-- | ||||
-- This program is distributed in the hope that it will be useful, | ||||
-- but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
-- GNU General Public License for more details. | ||||
-- | ||||
-- You should have received a copy of the GNU General Public License | ||||
-- along with this program; if not, write to the Free Software | ||||
-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||||
------------------------------------------------------------------------------- | ||||
-- Author : Alexis Jeandet | ||||
Jeandet Alexis
|
r64 | -- Mail : alexis.jeandet@member.fsf.org | ||
r50 | -------------------------------------------------------------------------------*/ | |||
#include <widget.h> | ||||
#include <terminal.h> | ||||
#include <stdint.h> | ||||
#include <stdio.h> | ||||
#include <gpio.h> | ||||
#include <core.h> | ||||
r63 | #include <malloc.h> | |||
r50 | ||||
r51 | #define CHARXPOS(terminal,charwidth) (((terminal)->column * charwidth) + (terminal)->Xpos) | |||
#define CHARYPOS(terminal,charheight) ((terminal)->line * charheight + (terminal)->Ypos+charheight) | ||||
r50 | ||||
r51 | streamdevice_ops TERMINAL_OPS= | |||
{ | ||||
.write = &terminal_write, | ||||
.read = &terminal_read, | ||||
.setpos= &terminal_setpos, | ||||
.close = NULL | ||||
}; | ||||
r50 | ||||
r51 | int terminal_init(terminal_t* terminal,LCD_t* LCD,sFONT* font,streamdevice* strdev) | |||
r50 | { | |||
r51 | if((LCD!=NULL) && (font!=NULL) && (strdev!=NULL) && (terminal!=NULL)) | |||
r50 | { | |||
r51 | terminal->LCD=LCD; | |||
terminal->font=font; | ||||
terminal->line = 0; | ||||
terminal->column = 0; | ||||
r63 | terminal->horizontalSpace = 0; | |||
terminal->verticalSpace = 0; | ||||
r51 | int charw=terminal->font->Width + terminal->horizontalSpace; | |||
int charh=terminal->font->Height + terminal->verticalSpace; | ||||
terminal->textColor=0xFFFF; | ||||
r65 | terminal->backgroundColor=0xF00F; | |||
r51 | terminal_setgeometry(terminal,5,5,terminal->LCD->width-(10),terminal->LCD->height-(10)); | |||
strdev->_stream = (UHANDLE)terminal; | ||||
strdev->ops = &TERMINAL_OPS; | ||||
r50 | strdev->streamPt = 0; | |||
r51 | terminal_clear(terminal); | |||
terminal->LCD->paintFilRect(terminal->LCD,CHARXPOS(terminal, charw),CHARYPOS(terminal, charh)-charh,charw,charh,terminal->backgroundColor,0,terminal->backgroundColor); | ||||
r63 | terminal->buffer = (char*)malloc(terminal->lineCount*(terminal->columnCount + 1)); | |||
terminal->firstLine = 0; | ||||
terminal->lastLine = 0; | ||||
terminal->empty = 1; | ||||
for(int i=1;i<=terminal->lineCount;i++) | ||||
{ | ||||
terminal->buffer[i * terminal->columnCount]='\n'; | ||||
} | ||||
r50 | return 1; | |||
} | ||||
return 0; | ||||
} | ||||
r51 | void terminal_setgeometry(terminal_t* terminal,uint16_t Xpos,uint16_t Ypos,uint16_t width,uint16_t height) | |||
r50 | { | |||
r51 | int charw=terminal->font->Width + terminal->horizontalSpace; | |||
int charh=terminal->font->Height + terminal->verticalSpace; | ||||
if(terminal->LCD->height<(Ypos+height)) | ||||
return; | ||||
if(terminal->LCD->width<(Xpos+width)) | ||||
return; | ||||
terminal->Xpos = Xpos; | ||||
terminal->Ypos = Ypos; | ||||
terminal->height = height; | ||||
terminal->width = width; | ||||
terminal->lineCount = terminal->height/charh; | ||||
terminal->columnCount = (terminal->width/charw)-1; | ||||
terminal_clear(terminal); | ||||
} | ||||
r50 | ||||
r51 | ||||
void terminal_clear(terminal_t* terminal) | ||||
{ | ||||
r63 | terminal->firstLine = 0; | |||
terminal->lastLine = 0; | ||||
terminal->empty = 1; | ||||
terminal->line =0; | ||||
terminal->column=0; | ||||
r51 | terminal->LCD->paintFilRect(terminal->LCD,terminal->Xpos,terminal->Ypos,terminal->width,terminal->height,terminal->backgroundColor,0,terminal->backgroundColor); | |||
} | ||||
void terminal_setbackgroundColor(terminal_t* terminal, uint32_t backgrooundColor) | ||||
{ | ||||
terminal->backgroundColor = backgrooundColor; | ||||
r50 | } | |||
r51 | void terminal_settextColor(terminal_t* terminal, uint32_t textColor) | |||
{ | ||||
terminal->textColor = textColor; | ||||
} | ||||
void terminal_movecursor(terminal_t* terminal,int n) | ||||
r50 | { | |||
r51 | terminal->column += n; | |||
r63 | if(terminal->column>(terminal->columnCount)) | |||
r51 | { | |||
terminal->line += (terminal->column)/terminal->columnCount; | ||||
terminal->line = terminal->line % terminal->lineCount; | ||||
r63 | terminal->column = (terminal->column % terminal->columnCount)-1; | |||
r51 | } | |||
} | ||||
void terminal_clearCurentLine(terminal_t* terminal) | ||||
{ | ||||
int charw=terminal->font->Width + terminal->horizontalSpace; | ||||
int charh=terminal->font->Height + terminal->verticalSpace; | ||||
r65 | terminal->LCD->paintFilRect(terminal->LCD,terminal->Xpos,CHARYPOS(terminal, charh)-charh,terminal->width,charh,terminal->backgroundColor,0,terminal->backgroundColor); | |||
r51 | } | |||
int terminal_writenc(terminal_t* terminal,char* data, int n) | ||||
{ | ||||
int charw=terminal->font->Width + terminal->horizontalSpace; | ||||
int charh=terminal->font->Height + terminal->verticalSpace; | ||||
r50 | int l=0; | |||
char buffer[2]=" "; | ||||
while(l<n) | ||||
{ | ||||
buffer[0]=data[l]; | ||||
if(buffer[0]=='\n') | ||||
{ | ||||
r51 | terminal->line= (terminal->line+1) % terminal->lineCount; | |||
terminal->column = 0; | ||||
r63 | if(terminal->line==0) | |||
terminal_clear(terminal); | ||||
else | ||||
terminal_clearCurentLine(terminal); | ||||
r50 | }else { | |||
if(buffer[0]=='\t') | ||||
{ | ||||
r51 | for(int i=0;i<1;i++)terminal_movecursor(terminal,1); | |||
}else | ||||
if(terminal->column==0)terminal_clearCurentLine(terminal); | ||||
r63 | if(buffer[0]!='\r'){ | |||
Jeandet Alexis
|
r64 | // terminal->LCD->paintFilRect(terminal->LCD,CHARXPOS(terminal,charw),CHARYPOS(terminal, charh)-charh,charw,charh,terminal->backgroundColor,0,terminal->backgroundColor); | ||
r65 | terminal->LCD->paintText(terminal->LCD,buffer,CHARXPOS(terminal,charw),(CHARYPOS(terminal, charh))-(terminal->verticalSpace/2),terminal->font,terminal->textColor); | |||
r63 | terminal_movecursor(terminal,1); | |||
} | ||||
r50 | } | |||
l++; | ||||
} | ||||
return n; | ||||
} | ||||
r63 | void terminal_print_line(terminal_t* terminal,int line) | |||
{ | ||||
int charw=terminal->font->Width + terminal->horizontalSpace; | ||||
int charh=terminal->font->Height + terminal->verticalSpace; | ||||
terminal_clearCurentLine(terminal); | ||||
terminal->LCD->paintText(terminal->LCD,terminal->buffer+(line*(terminal->columnCount + 1)),CHARXPOS(terminal,charw),CHARYPOS(terminal, charh),terminal->font,terminal->textColor); | ||||
} | ||||
int terminal_writenc2(terminal_t* terminal,char* data, int n) | ||||
{ | ||||
int charw=terminal->font->Width + terminal->horizontalSpace; | ||||
int charh=terminal->font->Height + terminal->verticalSpace; | ||||
/* First copy to buffer*/ | ||||
int offset = (terminal->lastLine*(terminal->columnCount + 1)) + terminal->column; | ||||
for(int i=0;i<n;i++) | ||||
{ | ||||
terminal->empty=0; | ||||
terminal->buffer[offset] = data[i]; | ||||
if(data[i]=='\n') | ||||
{ | ||||
offset = ((offset/(terminal->columnCount + 1) + 1) * (terminal->columnCount + 1)); | ||||
} | ||||
else | ||||
{ | ||||
offset++; | ||||
} | ||||
} | ||||
if(!terminal->empty) | ||||
{ | ||||
for(int i=terminal->firstLine;i<=terminal->lastLine;i++) | ||||
{ | ||||
terminal_print_line(terminal,i); | ||||
} | ||||
} | ||||
return n; | ||||
} | ||||
r51 | int terminal_write(streamdevice* device,void* data,int size, int n) | |||
r50 | { | |||
r51 | return terminal_writenc((terminal_t*)device->_stream,(char*) data,size*n); | |||
r50 | } | |||
r51 | int terminal_read(streamdevice* device,void* data,int size, int n) | |||
r50 | { | |||
return n*size; | ||||
} | ||||
r51 | int terminal_setpos(streamdevice* device,int pos) | |||
r50 | { | |||
return 1; | ||||
} | ||||
r51 | int terminal_close( streamdevice* device) | |||
r50 | { | |||
return 1; | ||||
} | ||||