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
}
}
#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
}
}