My code currently receives characters of a book one by one and preprocesses it so that it displays it in the form of:
I went to the
library to pick up
my favorite
baseball hat
instead of
I went to the libr
art to pick up my
favorite basebal
l hat
which is what the default Adafruit_ST7735.h wrap text option would do. Everything works properly but now I'm struggling to implement pages functionality. I want to be able to enter a page number and the function displays the preprocessed text only of that page (where the pages are determined by dividing the whole book size by the number of characters that the display can fit). It is a pretty complicated system and I've been banging my head for hours but it seems to be way beyond my IQ. Here's the code of my void: (where the characters are read from a file on an SD card) I can't explain how it works but a quick read through the if statements should give an idea of what it happens. I believe that the main issues arise when the go-to-new-line-when-word-doesn't-fit system causes miscalculations of what space of the page is taken and it starts to mess up the text. Another issue I suspect is that it would need to somehow calculate pages it has passed so it can display the current page properly. And also, when the final word doesn't fit at the space left at the end of the page, it goes to the next line, but it's not displayed on the next page. Maybe there's a better way to do this whole system and maybe there's a library or a ready-to-use algorithm somewhere. I'm ready to rewrite the whole thing if I have to.
#define line_size 26
void open_book_page(String file_name, int page) {
tft.fillScreen(ST77XX_BLACK);
tft.setCursor(0, 0);
File myFile = SD.open(file_name);
if (myFile) {
int space_left = line_size;
String current_word = "";
int page_space_debug = 0;
while (myFile.available()) {
char c = myFile.read();
// myFile.size() - myFile.available() gives the characters receieved until now
if(myFile.size() - myFile.available() >= page * 401 && myFile.size() - myFile.available() <= (page * 401) + 401) {
if(current_word.length() == space_left + current_word.length()) {
if(c == ' ') {
tft.print(current_word);
tft.println();
current_word = "";
space_left = line_size;
} else {
tft.println();
current_word += c;
current_word.remove(0, 1);
space_left = line_size - current_word.length();
}
} else {
if(c == ' ') {
tft.print(current_word);
current_word = c;
} else {
current_word += c;
}
space_left--;
}
}
}
if(current_word != "") {
if(space_left < current_word.length()) {
tft.println();
tft.print(current_word);
} else {
tft.print(current_word);
}
}
myFile.close();
} else {
tft.print("Error opening file.");
}
}
If there are any questions, I'd love to answer.
I'm doing this whole thing on a stm32f103c8t6 board, not a computer. I'm limited to memory and storing capacity.
**
**
Not having a stm32f103c8t6 board or any way to debug your exact code best I can give is a psudocode solution.
if you preprocess the file so that each "page" is exactly the amount of characters you can fit on the screen (filling the end of each line with spaces) you should be able to use the page number as the offset into the file.
#define line_size 26
// line_size * 4 lines?
#define page_size 104
void open_book_page(String file_name, int page){
File myFile = SD.open(file_name);
if( myFile.available() ){
if( myFile.seek(page * page_size) ){
// read page_size characters and put on screen
}
myFile.close();
}
}
I hope this is helpful enough