Skip to content

Commit

Permalink
TFTTerminal
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinyu-Zhao committed Aug 27, 2021
0 parents commit b9af029
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 0 deletions.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
License
-------

Copyright (c) 2021

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# TFTTerminal

## License

[MIT](LICENSE)
16 changes: 16 additions & 0 deletions library.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "TFTTerminal",
"description": "TFTTerminal",
"keywords": "TFTTerminal",
"authors": {
"name": "anonymous",
"url": "http://www.google.com"
},
"repository": {
"type": "git",
"url": "https://github.com/Tinyu-Zhao/TFTTerminal.git"
},
"version": "0.0.1",
"framework": "arduino",
"platforms": "espressif32"
}
10 changes: 10 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name=TFTTerminal
version=0.0.1
author=anonymous
maintainer=anonymous
sentence=Library for TFTTerminal
paragraph=See more on
category=Device Control
url=https://github.com/Tinyu-Zhao/TFTTerminal
architectures=esp32
includes=TFTTerminal.h
162 changes: 162 additions & 0 deletions src/TFTTerminal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#include "TFTTerminal.h"

TFTTerminal::TFTTerminal(TFT_eSprite *dis_buff_ptr)
{
_dis_buff_ptr = dis_buff_ptr;
memset(discharbuff, 0, 55 * 60);
}

TFTTerminal::~TFTTerminal()
{

}


void TFTTerminal::setcolor( uint16_t color, uint16_t bk_color )
{
_color = color;
_bkcolor = bk_color;
}

void TFTTerminal::setGeometry(uint16_t x, uint16_t y, uint16_t w, uint16_t h )
{
_win_x_pos = x;
_win_y_pos = y;
_win_w = w;
_win_h = h;

_line_x_limit = _win_w / _font_x_size;
_line_y_limit = _win_h / _font_y_size;
}

void TFTTerminal::setFontsize(uint8_t size)
{
size = ( size == 0 ) ? 1 : size;
_font_x_size = 5 * size ;
_font_y_size = 6 * size ;
_fontSize = size;

_line_x_limit = _win_w / _font_x_size;
_line_y_limit = _win_h / _font_y_size;
}

size_t TFTTerminal::write(uint8_t chardata)
{

bool flush_page_flag = false;
uint8_t dis_y_pos = 0;

if ((chardata == '\r') || (chardata == '\n'))
{
xpos = 0;
ypos++;
ypos = ypos % 60;
memset(discharbuff[ypos % 60], 0, 55);
return 1;
}
else if(xpos >= _line_x_limit)
{
xpos = 0;
ypos++;
ypos = ypos % 60;
memset(discharbuff[ypos % 60], 0, 55);
}

discharbuff[ypos][xpos] = chardata;
xpos++;

if ((dispos <= ypos) && ((ypos - dispos) > _line_y_limit))
{
dispos = ypos - _line_y_limit;
flush_page_flag = true;
}
else if ((dispos <= ypos) && ((ypos - dispos) <= _line_y_limit))
{
dis_y_pos = ypos - dispos;
flush_page_flag = false;
}
else if ((dispos > ypos) && ((60 - (dispos - ypos)) > _line_y_limit))
{
dispos = 60 - ( _line_y_limit - ypos );
flush_page_flag = true;
}
else if ((dispos > ypos) && ((60 - (dispos - ypos)) > _line_y_limit))
{
dis_y_pos = 60 - ( dispos - ypos );
flush_page_flag = false;
}

dispos = dispos % 60;

_dis_buff_ptr->setTextColor(_color);
_dis_buff_ptr->setTextSize(_fontSize);

if( flush_page_flag )
{
_dis_buff_ptr->fillSprite(_bkcolor);

for (size_t i = 0; i < _line_y_limit; i++)
{
_dis_buff_ptr->drawString((char *)discharbuff[(dispos + i) % 60], 0, i * _font_y_size);
}
}
else
{
_dis_buff_ptr->drawChar(chardata, ( xpos - 1 ) * _font_x_size, dis_y_pos * _font_y_size );
}
_dis_buff_ptr->pushSprite(_win_x_pos, _win_y_pos);

return 1;

}

size_t TFTTerminal::write(const uint8_t *buffer, size_t size)
{

while ((size != 0) && (*buffer != '\0'))
{
if ((*buffer == '\r') || (*buffer == '\n'))
{
xpos = 0;
ypos++;
ypos = ypos % 60;
memset(discharbuff[ypos % 60], 0, 55);
buffer++;
size--;
continue;
}
else if(xpos >= _line_x_limit)
{
xpos = 0;
ypos++;
ypos = ypos % 60;
memset(discharbuff[ypos % 60], 0, 55);
}
discharbuff[ypos][xpos] = *buffer;
xpos++;
buffer++;
size--;
}

if ((dispos <= ypos) && ((ypos - dispos) > _line_y_limit))
{
dispos = ypos - _line_y_limit;
}
else if ((dispos > ypos) && ((60 - (dispos - ypos)) > _line_y_limit))
{
dispos = 60- ( _line_y_limit - ypos );
}

dispos = dispos % 60;

_dis_buff_ptr->setTextColor(_color);
_dis_buff_ptr->setTextSize(_fontSize);
_dis_buff_ptr->fillSprite(_bkcolor);
//_dis_buff_ptr->fillRect(_win_x_pos, _win_y_pos, _win_w, _win_h, _bkcolor);
for (size_t i = 0; i < _line_y_limit; i++)
{
_dis_buff_ptr->drawString((char *)discharbuff[(dispos + i) % 60], 0, i * _font_y_size);
}
_dis_buff_ptr->pushSprite(_win_x_pos, _win_y_pos);
return 1;
}
35 changes: 35 additions & 0 deletions src/TFTTerminal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef _TFTTERMINAL_H_
#define _TFTTERMINAL_H_

#include <M5Stack.h>
#include <Print.h>

class TFTTerminal : public Print
{
private:
TFT_eSprite *disptr;
char discharbuff[60][55];
uint32_t xpos = 0,ypos = 0, dispos = 0;
TFT_eSprite* _dis_buff_ptr = NULL;
uint16_t _bkcolor = TFT_BLACK;
uint16_t _color = TFT_GREEN;

uint16_t _win_x_pos = 0,_win_y_pos = 0,_win_w = 320,_win_h = 240;
uint16_t _font_x_size = 6,_font_y_size = 8;
uint8_t _fontSize = 0;
uint16_t _line_x_limit = 53,_line_y_limit = 30;

public:
TFTTerminal(TFT_eSprite *dis_buff_ptr);
~TFTTerminal();

void setcolor( uint16_t color, uint16_t bk_color );
void setGeometry(uint16_t x, uint16_t y, uint16_t w, uint16_t h );
void setFontsize(uint8_t size);

size_t write(uint8_t) ;
size_t write(const uint8_t *buffer, size_t size);
};


#endif

0 comments on commit b9af029

Please sign in to comment.