You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

120 regels
3.7 KiB

  1. #include "core/common.h"
  2. #include "string.h"
  3. #include "core/buttons.h"
  4. #include "core/display.h"
  5. #include "reader/reader_storage.h"
  6. #include "ReaderMode.h"
  7. #include "esp_log.h"
  8. static const char *TAG = "ReaderMode";
  9. //bool displaySleeping = false;
  10. void ReaderMode::start()
  11. {
  12. char filename[64];
  13. reader_storage_get_filename(filename, sizeof(filename));
  14. this->textReader = textStorage.open(filename);
  15. this->bookmark_max = reader_storage_get_length();
  16. this->bookmark = reader_storage_get_position();
  17. if (this->bookmark_max < 0) {
  18. this->bookmark_max = 0;
  19. }
  20. if (this->bookmark < 0 || this->bookmark > this->bookmark_max) {
  21. this->bookmark = 0;
  22. }
  23. ESP_LOGI(TAG, "Opening %s for reading at %ld / %ld.",
  24. filename, bookmark, bookmark_max);
  25. }
  26. void ReaderMode::loop()
  27. {
  28. char text[1024];
  29. if (textReader != NULL) {
  30. if (pageCurrent == NULL) {
  31. size_t read = textReader->read(bookmark, text, sizeof(text));
  32. pageCurrent = typesetter.preparePage(text, read);
  33. pageCurrent->start = bookmark;
  34. }
  35. if (pageLast == NULL) {
  36. // align with the start?
  37. if (bookmark < sizeof(text)) {
  38. size_t read = textReader->read(0, text, sizeof(text));
  39. pageLast = typesetter.preparePage(text, read);
  40. pageLast->start = 0;
  41. } else {
  42. size_t read = textReader->read((bookmark - sizeof(text)), text, sizeof(text));
  43. pageLast = typesetter.preparePreviousPage(text, read);
  44. pageLast->start = bookmark - pageLast->len;
  45. }
  46. }
  47. } else {
  48. typesetter.destroyPage(pageCurrent);
  49. strcpy(text, "File could not be opened.");
  50. pageCurrent = typesetter.preparePage(text, sizeof(text));
  51. }
  52. display_clear();
  53. if (bookmark == bookmark_max) {
  54. display_alert("THE END");
  55. } else {
  56. pagePrinter.print(pageCurrent);
  57. }
  58. display_update();
  59. //time_t idleStart = clock();
  60. while (1) {
  61. delay(10);
  62. if (buttons_pressed_ok()) {
  63. ESP_LOGI(TAG, "Exiting reader.");
  64. this->setFinished();
  65. break;
  66. }
  67. if (buttons_pressed_plus()) {
  68. ESP_LOGI(TAG, "Turn page PLUS.");
  69. if (pageCurrent != NULL) {
  70. bookmark = pageCurrent->start + pageCurrent->len;
  71. if (bookmark > bookmark_max) {
  72. bookmark = bookmark_max;
  73. }
  74. reader_storage_set_position(bookmark);
  75. typesetter.destroyPage(pageLast);
  76. pageLast = pageCurrent;
  77. pageCurrent = NULL;
  78. } else {
  79. ESP_LOGW(TAG, "No current page.");
  80. }
  81. break;
  82. }
  83. if (buttons_pressed_minus()) {
  84. ESP_LOGI(TAG, "Turn page MINUS.");
  85. if (pageLast != NULL) {
  86. bookmark = pageLast->start;
  87. reader_storage_set_position(bookmark);
  88. typesetter.destroyPage(pageCurrent);
  89. pageCurrent = pageLast;
  90. pageLast = NULL;
  91. } else {
  92. ESP_LOGW(TAG, "No last page.");
  93. }
  94. break;
  95. }
  96. /*if (!displaySleeping && (clock() - idleStart > DISPLAY_SLEEP_TIMEOUT)) {
  97. displaySleeping = true;
  98. ESP_LOGI(TAG, "Display going to sleep after %d ms.", DISPLAY_SLEEP_TIMEOUT);
  99. display_sleep();
  100. }*/
  101. }
  102. /*if (displaySleeping) {
  103. displaySleeping = false;
  104. ESP_LOGI(TAG, "Display waking up.");
  105. display_wake();
  106. }*/
  107. }
  108. void ReaderMode::finish()
  109. {
  110. textStorage.close(this->textReader);
  111. }