memcpy

Status
Not open for further replies.
The code in total.

#include <SPI.h>

#include <RA8875.h>

//MISO 8 //12
//SCLK 14 //13
//MOSI 7 //11
#define RA8875_RESET 255
#define RA8875_CS 20 //10
RA8875 tft = RA8875(RA8875_CS, RA8875_RESET, 7, 14, 8);



#include <fonts/liberation_mono_28.c>
char ch;
uint32_t end_micros;
uint32_t begin_micros ;
uint32_t after_print_times;
void setup() {

tft.begin(RA8875_800x480,8);
tft.fillWindow(RA8875_BLACK);
tft.setTextColor(RA8875_YELLOW);
tft.setFont(&liberation_mono_28);
// tft.setFontScale(1);
tft.setCursor(310, 240);
tft.write("PD0LEW");
delay(1000);
tft.fillWindow(RA8875_BLACK);
Serial1.begin(115200);

}

void loop() {


if (Serial1.available() )
{
ch = Serial1.read();
printandscroll_layer_1(); tft.writeTo(L1);tft.layerEffect(LAYER2);
printandscroll_layer_2(); tft.writeTo(L2);tft.layerEffect(LAYER1);

}
}

void printandscroll_layer_1() {

begin_micros = micros();
int count_chars = 0; //*********************


static char line1[43];
static char line2[43];
static char line3[43];
static char line4[43];
static char line5[43];
static char line6[43];
static char line7[43];
static char line8[43];
static char line9[43];

static uint16_t curs; // Cursor position at bottom line
static int8_t x; // current X co-ordinate
static uint8_t last_space; // location of latest space character


if (ch == ' ') last_space = x; // Keep track of word spaces in order to avoid breaking up words
if (x == 42) // End of line. Scroll everything up one line
{

after_print_times = micros(); // ***********
count_chars = strlen(line9) + strlen(line8) + strlen(line7) + strlen(line6)
+ strlen(line5) + strlen(line4)+ strlen(line3) + strlen(line2) + strlen(line1); //*********

memcpy(line9, line8, 42);
memcpy(line8, line7, 42);
memcpy(line7, line6, 42);
memcpy(line6, line5, 42);
memcpy(line5, line4, 42); // Shift all lines up
memcpy(line4, line3, 42);
memcpy(line3, line2, 42);
if (last_space == 0) last_space = 42; // Special case, no space in line1
memcpy(line2, line1, last_space);
line2[last_space] = '\0'; // Terminate string

end_micros = micros();
Serial.print("slow code took text part: ");
Serial.print(after_print_times - begin_micros); //***********
Serial.print(" Chars output: "); //***********
Serial.print(count_chars); //***********
Serial.print(" Total time: "); //***********
Serial.print(end_micros - begin_micros);
Serial.println(" microseconds");


tft.setTextColor(RA8875_WHITE,RA8875_BLACK);
tft.setCursor(10, 25);
tft.print(line9);
tft.setCursor(10, 75);
tft.print(line8);
tft.setCursor(10, 125);
tft.print(line7);
tft.setCursor(10, 175);
tft.print(line6);
tft.setCursor(10, 225); // and print data
tft.print(line5);
tft.setCursor(10, 275);
tft.print(line4);
tft.setCursor(10, 325);
tft.print(line3);
tft.setCursor(10, 375);
tft.print(line2);
tft.setTextColor(RA8875_GREEN);
tft.setCursor(0, 430); // Set cursor at beginning of first (lowest) line
if (last_space == 42) // Special case, no space in line1, therefore nothing to print
{
tft.fillRect(0, 430, 800, 33, RA8875_BLACK);
line1[0] = '\0';
x = 0;
curs = 0;
}
else // Move beginning of unfinished word to front of line
{
x = 42 - (last_space + 1); // Get length of text we want to move to front
memmove(line1, &line1[last_space], x);
line1[x] = '\0'; // Terminate end of string
tft.fillRect(0, 430, 800, 32, RA8875_BLACK); // Clear line
tft.setCursor(0, 430);
tft.print(line1); // Print the moved word-fragment
curs = x * 18 + 18; // And move cursor accordingly
line1[x++] = ch; // Print to string
tft.print(ch);
}
last_space = 0;
}
else // Print new character to string and TFT, one char at a time
{
tft.setTextColor(RA8875_GREEN); // Set GREEN as text Colour
tft.setCursor(curs, 430);
line1[x++] = ch;
tft.println(ch);
curs = curs + 18; //space between characters
}


}
void printandscroll_layer_2() {
static char line1[43];
static char line2[43];
static char line3[43];
static char line4[43];
static char line5[43];
static char line6[43];
static char line7[43];
static char line8[43];
static char line9[43];

static uint16_t curs; // Cursor position at bottom line
static int8_t x; // current X co-ordinate
static uint8_t last_space; // location of latest space character


if (ch == ' ') last_space = x; // Keep track of word spaces in order to avoid breaking up words
if (x == 42) // End of line. Scroll everything up one line
{

memcpy(line9, line8, 42);
memcpy(line8, line7, 42);
memcpy(line7, line6, 42);
memcpy(line6, line5, 42);
memcpy(line5, line4, 42); // Shift all lines up
memcpy(line4, line3, 42);
memcpy(line3, line2, 42);
if (last_space == 0) last_space = 42; // Special case, no space in line1
memcpy(line2, line1, last_space);
line2[last_space] = '\0'; // Terminate string
tft.setTextColor(RA8875_WHITE,RA8875_BLACK);
tft.setCursor(10, 25);
tft.write(line9);
tft.setCursor(10, 75);
tft.write(line8);
tft.setCursor(10, 125);
tft.write(line7);
tft.setCursor(10, 175);
tft.write(line6);
tft.setCursor(10, 225); // and write data
tft.write(line5);
tft.setCursor(10, 275);
tft.write(line4);
tft.setCursor(10, 325);
tft.write(line3);
tft.setCursor(10, 375);
tft.write(line2);
tft.setTextColor(RA8875_GREEN);
tft.setCursor(0, 430); // Set cursor at beginning of first (lowest) line
if (last_space == 42) // Special case, no space in line1, therefore nothing to write
{
tft.fillRect(0, 430, 800, 33, RA8875_BLACK);
line1[0] = '\0';
x = 0;
curs = 0;
}
else // Move beginning of unfinished word to front of line
{
x = 42 - (last_space + 1); // Get length of text we want to move to front
memcpy(line1, &line1[last_space], x);
line1[x] = '\0'; // Terminate end of string
tft.fillRect(0, 430, 800, 32, RA8875_BLACK); // Clear line
tft.setCursor(0, 430);
tft.write(line1); // write the moved word-fragment
curs = x * 18 + 18; // And move cursor accordingly
line1[x++] = ch; // write to string
tft.write(ch);
}
last_space = 0;
}
else // write new character to string and TFT, one char at a time
{

tft.setTextColor(RA8875_GREEN); // Set GREEN as text Colour
tft.setCursor(curs, 430);
line1[x++] = ch;
tft.write(ch);
curs = curs + 18; //space between characters

}

}
 
Hi Kurt and others,
I think the delay is gone .....

But still a small issue, see at the video there are some characters missing when word breaking occurs.

But I not see any delay ... nearly there I think.

Best,
Johan
 
Sorry, I don't have time or the hardware to debug your code. I did a quick look through your changes and again not sure how the layers effect it.

But I do think you will have issues with your just removing the drawing of the text in opaque mode when a shorter line is drawn over a longer line, you will end up with leftover text from previous line drawn there...

I should also point out another approach I would take if I were doing this...

That is I would experiment with seeing how well the scrolling would work with the display. That is you have some methods (setScrollMode, setScrollWindow, scroll), which I have not investigated, but there is a test program scroll that comes with the library.

Again if you assume 9 lines of text, when you need to scroll the lines up to make room. Could you not simply Scroll the top 7 lines up one line width (50 pixels), then draw the text that should go in the 8th line (the text that was in green) on the 8th line, clear out the space after it if necessary,

Then draw the start of the new 9th line of text... With this you only need to hold onto the one line of text... Not sure again if layers help or not. Also not sure how the Scrolling stuff works on this display. Will it simply do the copy of the pixels up for you in this case, what does it do with the new pixels that would be uncovered? Roll it around from the part that scrolled away or ??? Again much easier to say when you can try out a couple of simple examples on the actual hardware.
 
Hi Kurt, for testing I can send you my fancy morse decoder with the Display. Best regards, Johan
 
Sorry, tempting but I don't give out my personal information like where I live on the forums...

At one point I thought of purchasing one of the displays... But Norton gives a lot of warnings a about the site (Phishing and the like) selling the first boards mentioned in the library readme... So there still is the Adafruit ones.

Besides I have plenty of my own current projects to work on.
 
Hi Kurt,

I respect your privacy and time. Remember Adafruit tft's are working but they library's are never optimized. For my project in fact a simple terminal monitor it looks easy as that, but it's not.

Scrolling fonts as Liberation_Mono 28 on a 5" is real pain in the bud, and I not see anyone who did this in a perfect way. We need two layers without it will never happen with this font.

But there is always hope and I will find a way to accomplish my wish for the very nice and cheap RA8875 from Buy display.

We did a lot of tests with the scrolling function but also this is not working because you will lost the x and y position.


Best regards,

Johan
 
In your version that worked you had
memmove(line1, &line1[last_space], x);

And in the new version that drops characters you have
memcpy(line1, &line1[last_space], x);

Edit: Well maybe not. You seem to have 3 different versions posted.
Edit2: But memmove is the safe function. So what are you using for the copy within the same string?
 
Last edited:
Hi rcarr, I tried a lot to see what is the fasted way. The speed is ok now but still an issue to solve see video.

Thanks for helping!

Johan

The code till now.


#include <SPI.h>
#include <RA8875.h>
//MISO 8 //12
//SCLK 14 //13
//MOSI 7 //11
#define RA8875_RESET 255
#define RA8875_CS 20 //10
RA8875 tft = RA8875(RA8875_CS, RA8875_RESET, 7, 14, 8);
#include <fonts/liberation_mono_28.c>
char ch;
uint32_t end_micros;
uint32_t begin_micros ;
uint32_t after_print_times;

void setup() {

tft.begin(RA8875_800x480,8);
tft.fillWindow(RA8875_BLACK);
tft.setTextColor(RA8875_YELLOW);
tft.setFont(&liberation_mono_28);
tft.setCursor(320, 240);
tft.write("Welkom");
tft.setCursor(250, 290);
tft.write("Feitze van Zwol");

delay(2000);
tft.fillWindow(RA8875_BLACK);
Serial1.begin(115200);
}

void loop() {

if (Serial1.available() )
{

ch = Serial1.read();
printandscroll_layer_1(); tft.writeTo(L1);tft.layerEffect(LAYER2);
printandscroll_layer_2(); tft.writeTo(L2);tft.layerEffect(LAYER1);
Serial.print(ch);
}
}

void printandscroll_layer_1() {

begin_micros = micros();
int count_chars = 0; //*********************
static char line1[43];
static char line2[43];
static char line3[43];
static char line4[43];
static char line5[43];
static char line6[43];
static char line7[43];
static char line8[43];
static char line9[43];
static uint16_t curs; // Cursor position at bottom line
static int8_t x; // current X co-ordinate
static uint8_t last_space; // location of latest space character
if (ch == ' ') last_space = x; // Keep track of word spaces in order to avoid breaking up words
if (x == 42) // End of line. Scroll everything up one line
{
// after_print_times = micros(); // ***********
//count_chars = strlen(line9) + strlen(line8) + strlen(line7) + strlen(line6)
// + strlen(line5) + strlen(line4)+ strlen(line3) + strlen(line2) + strlen(line1); //*********
memcpy(line9, line8, 42);
memcpy(line8, line7, 42);
memcpy(line7, line6, 42);
memcpy(line6, line5, 42);
memcpy(line5, line4, 42); // Shift all lines up
memcpy(line4, line3, 42);
memcpy(line3, line2, 42);
if (last_space == 0) last_space = 42; // Special case, no space in line1
memcpy(line2, line1, last_space);
line2[last_space] = '\0'; // Terminate string
// end_micros = micros();
//Serial.print("slow code took text part: ");
// Serial.print(after_print_times - begin_micros); //***********
// Serial.print(" Chars output: "); //***********
// Serial.print(count_chars); //***********
//Serial.print(" Total time: "); //***********
//Serial.print(end_micros - begin_micros);
// Serial.println(" microseconds");

tft.setTextColor(RA8875_WHITE,RA8875_BLACK);
tft.setCursor(10, 25);
tft.fillRect(10, 25, 800, 33, RA8875_BLACK);
tft.print(line9);
tft.setCursor(10, 75);
tft.print(line8);
tft.setCursor(10, 125);
tft.print(line7);
tft.setCursor(10, 175);
tft.print(line6);
tft.setCursor(10, 225); // and print data
tft.print(line5);
tft.setCursor(10, 275);
tft.print(line4);
tft.setCursor(10, 325);
tft.print(line3);
tft.setCursor(10, 375);
tft.print(line2);
tft.setTextColor(RA8875_GREEN,RA8875_BLACK);
tft.setCursor(0, 430); // Set cursor at beginning of first (lowest) line
if (last_space == 42) // Special case, no space in line1, therefore nothing to print
{
tft.fillRect(0, 430, 800, 33, RA8875_BLACK);
line1[0] = '\0';
x = 0;
curs = 0;
}
else // Move beginning of unfinished word to front of line
{
x = 42 - (last_space + 0); // Get length of text we want to move to front
memmove(line1, &line1[last_space], x);
line1[x] = '\0'; // Terminate end of string
tft.fillRect(0, 430, 800, 32, RA8875_BLACK); // Clear line
tft.setCursor(0, 430);
tft.print(line1); // Print the moved word-fragment
curs = x * 18 + 18; // And move cursor accordingly
line1[x++] = ch; // Print to string
tft.print(ch);
}
last_space = 0;
}
else // Print new character to string and TFT, one char at a time
{
tft.setTextColor(RA8875_GREEN,RA8875_BLACK); // Set GREEN as text Colour
tft.setCursor(curs, 430);
line1[x++] = ch;
tft.println(ch);
curs = curs + 18; //space between characters
}
}

void printandscroll_layer_2() {
static char line1[43];
static char line2[43];
static char line3[43];
static char line4[43];
static char line5[43];
static char line6[43];
static char line7[43];
static char line8[43];
static char line9[43];
static uint16_t curs; // Cursor position at bottom line
static int8_t x; // current X co-ordinate
static uint8_t last_space; // location of latest space character
if (ch == ' ') last_space = x; // Keep track of word spaces in order to avoid breaking up words
if (x == 42) // End of line. Scroll everything up one line
{
memcpy(line9, line8, 42);
memcpy(line8, line7, 42);
memcpy(line7, line6, 42);
memcpy(line6, line5, 42);
memcpy(line5, line4, 42); // Shift all lines up
memcpy(line4, line3, 42);
memcpy(line3, line2, 42);
if (last_space == 0) last_space = 42; // Special case, no space in line1
memcpy(line2, line1, last_space);
line2[last_space] = '\0'; // Terminate string
tft.setTextColor(RA8875_WHITE,RA8875_BLACK);
tft.setCursor(10, 25);
tft.write(line9);
tft.setCursor(10, 75);
tft.write(line8);
tft.setCursor(10, 125);
tft.write(line7);
tft.setCursor(10, 175);
tft.write(line6);
tft.setCursor(10, 225); // and write data
tft.write(line5);
tft.setCursor(10, 275);
tft.write(line4);
tft.setCursor(10, 325);
tft.write(line3);
tft.setCursor(10, 375);
tft.write(line2);
tft.setTextColor(RA8875_GREEN,RA8875_BLACK );
tft.setCursor(0, 430); // Set cursor at beginning of first (lowest) line
if (last_space == 42) // Special case, no space in line1, therefore nothing to write
{
tft.fillRect(0, 430, 800, 33, RA8875_BLACK);
line1[0] = '\0';
x = 0;
curs = 0;
}
else // Move beginning of unfinished word to front of line
{
x = 42 - (last_space + 0); // Get length of text we want to move to front
memcpy(line1, &line1[last_space], x);
line1[x] = '\0'; // Terminate end of string
tft.fillRect(0, 430, 800, 32, RA8875_BLACK); // Clear line
tft.setCursor(0, 430);
tft.write(line1); // write the moved word-fragment
curs = x * 18 + 18; // And move cursor accordingly
line1[x++] = ch; // write to string
tft.write(ch);
}
last_space = 0;
}
else // write new character to string and TFT, one char at a time
{
tft.setTextColor(RA8875_GREEN); // Set GREEN as text Colour
tft.setCursor(curs, 430);
line1[x++] = ch;
tft.write(ch);
curs = curs + 18; //space between characters
}
}
 
I do not see it dropping letters now, but it does have a problem with erasing the right hand end of all the strings. Maybe you could try padding them out to 42 characters with spaces?
 
Hi rcarr, thanks for the reply. on this moment I have no clue do do that..... also the text is not lined out at the left side of the display. If you have something in mind please edit the file. Thank you.
 
Here is my try. I get a compile error on the screen constructor, so I must have a different library. If it doesn't show any promise, just delete it. But you can search for the word 'pad' to see how I pad out the string with spaces.
Code:
#include <SPI.h>
#include <RA8875.h>
//MISO 8 //12
//SCLK 14 //13
//MOSI 7 //11
#define RA8875_RESET 255
#define RA8875_CS 20 //10
RA8875 tft = RA8875(RA8875_CS, RA8875_RESET, 7, 14, 8);
#include <fonts/liberation_mono_28.c>
char ch;
static char line1[43];
static char line2[43];
static char line3[43];
static char line4[43];
static char line5[43];
static char line6[43];
static char line7[43];
static char line8[43];
static char line9[43];

uint32_t end_micros;
uint32_t begin_micros ;
uint32_t after_print_times;


void setup() {

  tft.begin(RA8875_800x480,8);
  tft.fillWindow(RA8875_BLACK);
  tft.setTextColor(RA8875_YELLOW);
  tft.setFont(&liberation_mono_28);
  tft.setCursor(320, 240);
  tft.write("Welkom");
  tft.setCursor(250, 290);
  tft.write("Feitze van Zwol");

  delay(2000);
  tft.fillWindow(RA8875_BLACK);
  tft.layerEffect(LAYER1);
  Serial1.begin(115200);
}

void loop() {

  if (Serial1.available() )
  {
   ch = Serial1.read();
   printandscroll_layers(ch);
   Serial.print(ch);
  }
}


// scroll lines, print both layers, return new x position
int8_t scroll_lines( uint8_t last_space ){    
int x;

    memcpy(line9, line8, 42);
    memcpy(line8, line7, 42);
    memcpy(line7, line6, 42);
    memcpy(line6, line5, 42);
    memcpy(line5, line4, 42); // Shift all lines up
    memcpy(line4, line3, 42);
    memcpy(line3, line2, 42);
    if (last_space == 0) last_space = 42; // Special case, no space in line1
    memcpy(line2, line1, last_space);
    x = last_space;
    while( x < 42 ) line2[x++] = ' ';      // pad out the line with spaces
    line2[42] = '\0'; // Terminate string

   tft.layerEffect(LAYER2); tft.writeTo(L1);       // update layer 1 while displaying layer 2
    tft.setTextColor(RA8875_WHITE,RA8875_BLACK);
    tft.setCursor(10, 25);
    tft.fillRect(10, 25, 800, 33, RA8875_BLACK);
    tft.print(line9);
    tft.setCursor(10, 75);
    tft.print(line8);
    tft.setCursor(10, 125);
    tft.print(line7);
    tft.setCursor(10, 175);
    tft.print(line6);
    tft.setCursor(10, 225); // and print data
    tft.print(line5);
    tft.setCursor(10, 275);
    tft.print(line4);
    tft.setCursor(10, 325);
    tft.print(line3);
    tft.setCursor(10, 375);
    tft.print(line2);
    tft.fillRect(0, 430, 800, 33, RA8875_BLACK);

   tft.layerEffect(LAYER1); tft.writeTo(L2);       // update layer 2 while displaying layer 1
    tft.setTextColor(RA8875_WHITE,RA8875_BLACK);
    tft.setCursor(10, 25);
    tft.fillRect(10, 25, 800, 33, RA8875_BLACK);
    tft.print(line9);
    tft.setCursor(10, 75);
    tft.print(line8);
    tft.setCursor(10, 125);
    tft.print(line7);
    tft.setCursor(10, 175);
    tft.print(line6);
    tft.setCursor(10, 225); // and print data
    tft.print(line5);
    tft.setCursor(10, 275);
    tft.print(line4);
    tft.setCursor(10, 325);
    tft.print(line3);
    tft.setCursor(10, 375);
    tft.print(line2);
    tft.fillRect(0, 430, 800, 33, RA8875_BLACK);

    if( last_space == 42 ){
      line1[0] = '\0';
      return 0;     // nothing to print on line1 special case
    }

    x = 42 - (last_space + 0); // Get length of text we want to move to front
    memmove(line1, &line1[last_space], x);
    line1[x] = '\0'; // Terminate end of string

    tft.layerEffect(LAYER2); tft.writeTo(L1); 
    tft.setTextColor(RA8875_GREEN,RA8875_BLACK);
    tft.setCursor(0, 430);
    tft.print(line1); // Print the moved word-fragment
    tft.layerEffect(LAYER1); tft.writeTo(L2);       // update layer 2 while displaying layer 1
    tft.setTextColor(RA8875_GREEN,RA8875_BLACK);
    tft.setCursor(0, 430);
    tft.print(line1); // Print the moved word-fragment
    
    return x + 1;   
}

void printandscroll_layers(char ch){
// begin_micros = micros();
// static int count_chars = 0;
static uint16_t curs; // Cursor position at bottom line
static int8_t x; // current X co-ordinate
static uint8_t last_space; // location of latest space character

  if (ch == ' ') last_space = x; // Keep track of word spaces in order to avoid breaking up words
  if( x == 42 ){
    x = scroll_lines(last_space);
    last_space = 0;
  }

  if( x == 0 && ch == ' ' ) return;  // avoid spaces on left edge of the screen

  curs = x * 18;    
  tft.writeTo(L2);
  tft.setTextColor(RA8875_GREEN,RA8875_BLACK); // Set GREEN as text Colour
  tft.setCursor(curs, 430);
  tft.print(ch);
  tft.writeTo(L1);
  tft.setTextColor(RA8875_GREEN,RA8875_BLACK); // Set GREEN as text Colour
  tft.setCursor(curs, 430);
  tft.print(ch);
  
  line1[x++] = ch;
}
 
As @rcarr mentioned you can pad all of the lines out to 42 spaces which will slow things down as you are drawing more stuff.

Alternatively like I mentioned back in posting #24,
which is you remember how long each line is and you use fillRect to fill after that point. You could fill to the right edge or simply remember where the previous line ended and the new line ends and if necessary fill between the two...

FYI - I did order one of the 5" touch screens from buydisplay... So maybe in a month it will arrive... Who knows if I like the display I might use it instead of the ili9341...
 
Very appreciated, when it works then a lot of radio hams are very happy because this is a cheap and very sharp tft display and in combination with my fancy decoder one of a kind.

Thanks rcarr and Kurt, I will test this today.

Best regards,
Johan
 
Hi rcarr, looks perfect only one small issue is the text aligning left side of display..

thanks a lot for helping!

Video in the dropbox.
 
Yes, it seems the space at position last_space is also copied to the beginning of the next line. I think this section

Code:
   x = 42 - (last_space + 0); // Get length of text we want to move to front
    memmove(line1, &line1[last_space], x);
    line1[x] = '\0'; // Terminate end of string

changed to something like this may be the fix. But it is very easy to get this wrong without the hardware for testing available.

Code:
    x = 42 - (last_space + 1);                    // Get length of text we want to move to front
    memmove(line1, &line1[last_space + 1], x);
    line1[x] = '\0';                                // Terminate end of string

Edit : or maybe that should still be line1[x] = '\0'; as the last line instead of line1[++x] as you will see in the email.
 
Hi rcarr, still not perfect.... see both video's in the dropbox the last video is the changed code
x = 42 - (last_space + 1); // Get length of text we want to move to front
memmove(line1, &line1[last_space + 1], x);
line1[x] = '\0'; // Terminate end of string
 
Hi rcarr, we are nearly there ......this works however the very first line is no aligned with the other lines.
If this can also be solved then we have a nice terminal program with very fast scrolling and usable for many applications such as my fancy CW program and also for terminal programs with no word breaking.... thank you for the effort.
See new video in the dropbox.

// This code is ok except the first line should be aligned also then we are there....

x = 42 - (last_space + 1); // Get length of text we want to move to front
memmove(line1, &line1[last_space + 1], x);
line1[x] = '\0'; // Terminate end of string
 
All the scrolled lines are set in 10 pixels from the left edge with 16 statements like this one as an example: tft.setCursor(10, 175);
The bottom line is not set in 10 pixels. If you want all the scrolled lines to be aligned with the bottom line then change all the 10's to 0's for example: tft.setCursor(0, 175);

If you want the bottom line set in 10 pixels, then you would add 10 to the calculated cursor position on this line: curs = x * 18;
Change to: curs = x * 18 + 10;
 
Hi rcarr, perfect everything works how it should be, now everyone may use the terminal program for many purposes.

THANK YOU.

Hereby the complete code:

//** Copyright (c) 2018 J.G. Holstein PD0LEW
//** Version 1.0 monitor program for eight lines of text, RA8875 SPI TFT from Buydisplay.com platform Teensy 3.2
//** 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.

#include <SPI.h>
#include <RA8875.h>
//Teensy 3.2 TFT connections
//MISO 8
//SCLK 14
//MOSI 7
#define RA8875_RESET 255 //Not used
#define RA8875_CS 20
RA8875 tft = RA8875(RA8875_CS, RA8875_RESET, 7, 14, 8);
#include <fonts/Liberation_Mono_28.c>
char ch;
static char line1[43];
static char line2[43];
static char line3[43];
static char line4[43];
static char line5[43];
static char line6[43];
static char line7[43];
static char line8[43];

void setup() {

tft.begin(RA8875_800x480);
tft.fillWindow(RA8875_BLACK);
tft.setTextColor(RA8875_YELLOW);
tft.setFont(&Liberation_Mono_28);
tft.setCursor(320, 240);
tft.write("Thank you");
tft.setCursor(270, 290);
tft.write("for all the help");

delay(2000);
tft.fillWindow(RA8875_BLACK);
tft.layerEffect(LAYER1);
Serial1.begin(115200);
}

void loop() {

if (Serial1.available() )
{
ch = Serial1.read(); //Serial1 pin 2 (RX) Teensy
printandscroll_layers(ch);
Serial.print(ch); //Used for monitoring
}
}

//Scroll lines, print both layers, return new x position
int8_t scroll_lines( uint8_t last_space ){

int x;
memcpy(line8, line7, 42); //Shift all lines up
memcpy(line7, line6, 42);
memcpy(line6, line5, 42);
memcpy(line5, line4, 42);
memcpy(line4, line3, 42);
memcpy(line3, line2, 42);
if (last_space == 0) last_space = 42; //Special case, no space in line1
memcpy(line2, line1, last_space);
x = last_space;
while( x < 42 ) line2[x++] = ' '; //Pad out the line with spaces
line2[42] = '\0'; //Terminate string

tft.layerEffect(LAYER2); tft.writeTo(L1); //Update layer 1 while displaying layer 2
tft.setTextColor(RA8875_WHITE,RA8875_BLACK);
tft.setCursor(0, 25);
tft.fillRect(0, 25, 800, 33, RA8875_BLACK);
tft.setCursor(0, 75);
tft.print(line8);
tft.setCursor(0, 125);
tft.print(line7);
tft.setCursor(0, 175);
tft.print(line6);
tft.setCursor(0, 225);
tft.print(line5);
tft.setCursor(0, 275);
tft.print(line4);
tft.setCursor(0, 325);
tft.print(line3);
tft.setCursor(0, 375);
tft.print(line2);
tft.fillRect(0, 430, 800, 33, RA8875_BLACK);

tft.layerEffect(LAYER1); tft.writeTo(L2); //Update layer 2 while displaying layer 1
tft.setTextColor(RA8875_WHITE,RA8875_BLACK);
tft.setCursor(0, 25);
tft.fillRect(0, 25, 800, 33, RA8875_BLACK);
tft.setCursor(0, 75);
tft.print(line8);
tft.setCursor(0, 125);
tft.print(line7);
tft.setCursor(0, 175);
tft.print(line6);
tft.setCursor(0, 225);
tft.print(line5);
tft.setCursor(0, 275);
tft.print(line4);
tft.setCursor(0, 325);
tft.print(line3);
tft.setCursor(0, 375);
tft.print(line2);
tft.fillRect(0, 430, 800, 33, RA8875_BLACK);

if( last_space == 42 ){
line1[0] = '\0';
return 0; //Nothing to print on line1 special case
}
x = 42 - (last_space + 1); //Get length of text we want to move to front
memmove(line1, &line1[last_space + 1], x);
line1[x] = '\0'; //Terminate end of string

tft.layerEffect(LAYER2); tft.writeTo(L1);
tft.setTextColor(RA8875_GREEN,RA8875_BLACK);
tft.setCursor(0, 430);
tft.print(line1); //Print the moved word-fragment
tft.layerEffect(LAYER1); tft.writeTo(L2); //Update layer 2 while displaying layer 1
tft.setTextColor(RA8875_GREEN,RA8875_BLACK);
tft.setCursor(0, 430);
tft.print(line1); // Print the moved word-fragment
return x ;
}

void printandscroll_layers(char ch){
static uint16_t curs; //Cursor position at bottom line
static int8_t x; //Current X co-ordinate
static uint8_t last_space; //Location of latest space character

if (ch == ' ') last_space = x; //Keep track of word spaces in order to avoid breaking up words
if( x == 42 ){
x = scroll_lines(last_space);
last_space = 0;
}

if( x == 0 && ch == ' ' ) //Avoid spaces on left edge of the screen
return;
curs = x * 18 ;
tft.writeTo(L2);
tft.setTextColor(RA8875_GREEN,RA8875_BLACK); //Set GREEN as text Colour
tft.setCursor(curs, 430);
tft.print(ch);
tft.writeTo(L1);
tft.setTextColor(RA8875_GREEN,RA8875_BLACK); //Set GREEN as text Colour
tft.setCursor(curs, 430);
tft.print(ch);
line1[x++] = ch;
}
 
Status
Not open for further replies.
Back
Top