首页 > 解决方案 > 如何编写一个一个一个接收字符并以带页的书的形式显示的算法?

问题描述

我的代码当前一个接一个地接收一本书的字符并对其进行预处理,以便以以下形式显示它:

我去
图书馆取
我最喜欢的
棒球帽

代替

我去图书馆
艺术馆拿我
最喜欢的棒球

这就是默认的 Adafruit_ST7735.h 换行文本选项的作用。一切正常,但现在我正在努力实现页面功能。我希望能够输入页码,并且该函数仅显示该页面的预处理文本(其中页面是通过将整本书的大小除以显示器可以容纳的字符数来确定的)。这是一个非常复杂的系统,我已经敲了好几个小时,但它似乎超出了我的智商。这是我的 void 代码:(从 SD 卡上的文件中读取字符)我无法解释它是如何工作的,但快速阅读 if 语句应该可以了解它发生了什么。我相信主要问题出现在 go-to-new-line-when-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.");
  }
}

如果有任何问题,我很乐意回答。

我在 stm32f103c8t6 板上做这一切,而不是电脑。我受限于内存和存储容量。

**

解决方案!我可以从发送文本的手机应用程序进行所有预处理。

**

标签: c++text-processingword-processor

解决方案


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

我希望这足够有帮助


推荐阅读