##// END OF EJS Templates
Factorised stream device structure to reduce memory usage....
Factorised stream device structure to reduce memory usage. Improved Graphical terminal for multi instances.

File last commit:

r51:1ef095ac0ee3 dev_alexis
r51:1ef095ac0ee3 dev_alexis
Show More
Terminal.c
186 lines | 6.3 KiB | text/x-c | CLexer
/*------------------------------------------------------------------------------
-- 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
-- Mail : alexis.jeandet@gmail.com
-------------------------------------------------------------------------------*/
#include <widget.h>
#include <terminal.h>
#include <stdint.h>
#include <stdio.h>
#include <gpio.h>
#include <core.h>
#define CHARXPOS(terminal,charwidth) (((terminal)->column * charwidth) + (terminal)->Xpos)
#define CHARYPOS(terminal,charheight) ((terminal)->line * charheight + (terminal)->Ypos+charheight)
streamdevice_ops TERMINAL_OPS=
{
.write = &terminal_write,
.read = &terminal_read,
.setpos= &terminal_setpos,
.close = NULL
};
int terminal_init(terminal_t* terminal,LCD_t* LCD,sFONT* font,streamdevice* strdev)
{
if((LCD!=NULL) && (font!=NULL) && (strdev!=NULL) && (terminal!=NULL))
{
terminal->LCD=LCD;
terminal->font=font;
terminal->line = 0;
terminal->column = 0;
terminal->horizontalSpace=-2;
terminal->verticalSpace=2;
int charw=terminal->font->Width + terminal->horizontalSpace;
int charh=terminal->font->Height + terminal->verticalSpace;
terminal->textColor=0xFFFF;
terminal->backgroundColor=0x0000;
terminal_setgeometry(terminal,5,5,terminal->LCD->width-(10),terminal->LCD->height-(10));
strdev->_stream = (UHANDLE)terminal;
strdev->ops = &TERMINAL_OPS;
strdev->streamPt = 0;
terminal_clear(terminal);
terminal->LCD->paintFilRect(terminal->LCD,CHARXPOS(terminal, charw),CHARYPOS(terminal, charh)-charh,charw,charh,terminal->backgroundColor,0,terminal->backgroundColor);
return 1;
}
return 0;
}
void terminal_setgeometry(terminal_t* terminal,uint16_t Xpos,uint16_t Ypos,uint16_t width,uint16_t height)
{
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);
}
void terminal_clear(terminal_t* terminal)
{
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;
}
void terminal_settextColor(terminal_t* terminal, uint32_t textColor)
{
terminal->textColor = textColor;
}
void terminal_movecursor(terminal_t* terminal,int n)
{
int charw=terminal->font->Width + terminal->horizontalSpace;
int charh=terminal->font->Height + terminal->verticalSpace;
terminal->column += n;
if(terminal->column>terminal->columnCount)
{
terminal->line += (terminal->column)/terminal->columnCount;
terminal->LCD->paintFilRect(terminal->LCD,CHARXPOS(terminal, charw),CHARYPOS(terminal,charh)-charh,charw*(terminal->columnCount-terminal->column),charh,terminal->backgroundColor,0,terminal->backgroundColor);
terminal->line = terminal->line % terminal->lineCount;
terminal->column = terminal->column % terminal->columnCount;
}
terminal->LCD->paintFilRect(terminal->LCD,CHARXPOS(terminal,charw),CHARYPOS(terminal,charh)-charh,charw,charh,terminal->backgroundColor,0,terminal->backgroundColor);
}
void terminal_clearCurentLine(terminal_t* terminal)
{
int charw=terminal->font->Width + terminal->horizontalSpace;
int charh=terminal->font->Height + terminal->verticalSpace;
terminal->LCD->paintFilRect(terminal->LCD,terminal->Xpos,CHARYPOS(terminal, charh)-charh,terminal->width,charh,terminal->backgroundColor,0,terminal->backgroundColor);
}
int terminal_writenc(terminal_t* terminal,char* data, int n)
{
int charw=terminal->font->Width + terminal->horizontalSpace;
int charh=terminal->font->Height + terminal->verticalSpace;
int l=0;
char buffer[2]=" ";
while(l<n)
{
buffer[0]=data[l];
if(buffer[0]=='\n')
{
terminal->line= (terminal->line+1) % terminal->lineCount;
terminal->column = 0;
}else {
if(buffer[0]=='\t')
{
for(int i=0;i<1;i++)terminal_movecursor(terminal,1);
}else
if(terminal->column==0)terminal_clearCurentLine(terminal);
if(buffer[0]!='\r'){
terminal->LCD->paintFilRect(terminal->LCD,CHARXPOS(terminal,charw),CHARYPOS(terminal, charh)-charh,charw,charh,terminal->backgroundColor,0,terminal->backgroundColor);
terminal->LCD->paintText(terminal->LCD,buffer,CHARXPOS(terminal,charw),CHARYPOS(terminal, charh),terminal->font,terminal->textColor);
terminal_movecursor(terminal,1);
}
}
l++;
}
return n;
}
int terminal_write(streamdevice* device,void* data,int size, int n)
{
return terminal_writenc((terminal_t*)device->_stream,(char*) data,size*n);
}
int terminal_read(streamdevice* device,void* data,int size, int n)
{
return n*size;
}
int terminal_setpos(streamdevice* device,int pos)
{
return 1;
}
int terminal_close( streamdevice* device)
{
return 1;
}