如何写一个逐个接收字符并以书页的形式显示的算法?

问题描述 投票:0回答:1

我的代码目前是将一本书的字符一个个接收,并进行预处理,使其以书页的形式显示。

我去图书馆拿了一顶我最喜欢的棒球帽

而不是

我去图书馆拿了我最喜欢的棒球帽。

这就是默认的Adafruit_ST7735.h wrap text选项的作用。一切都能正常工作,但现在我正在努力实现页面功能。我希望能够输入一个页码,然后函数只显示该页的预处理文本(其中页数是由整本书的大小除以显示器能够容纳的字符数来确定的)。这是个相当复杂的系统,我敲了几个小时的脑袋,但似乎已经超出了我的智商。下面是我的虚空的代码。(从SD卡上的文件中读取字符)我无法解释它是如何工作的,但快速阅读if语句应该可以了解它的工作原理。我相信主要的问题出现在当word-doesn't-fit系统导致错误计算页面的空间时,它开始扰乱文本。我怀疑的另一个问题是,它需要以某种方式计算它已经通过的页面,这样它就可以正确显示当前页面。还有,当最后一个字在页面末尾留下的空格处不合适时,它会进入下一行,但它没有在下一页显示。也许有更好的方法来完成这整个系统,也许某处有一个库或一个现成的算法。如果有必要,我准备重写整个系统。

#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.");
  }
}

如果有什么问题,我很乐意回答。

我是在tm32f103c8t6板上做这件事,不是在电脑上做。我的内存和存储容量有限。

**

解决! 我可以在手机应用上进行所有的预处理,从哪里发短信就从哪里发。

**

c++ text-processing word-processor
1个回答
0
投票

不具备stm32f103c8t6板,也没有任何办法调试你的准确代码,我可以给的最好的是psudocode解决方案。

如果你对文件进行预处理,使每个 "页 "正好是你能在屏幕上容纳的字符量(用空格填充每行的末尾),你应该能够使用页码作为偏移到文件中。

#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();
    }
}

我希望这对你有足够的帮助

© www.soinside.com 2019 - 2024. All rights reserved.