From 8917b91a036c9e5fc3d309e224625f2c6f5dcde5 Mon Sep 17 00:00:00 2001 From: dejvino Date: Sun, 2 Feb 2020 16:08:37 +0100 Subject: [PATCH] Core mode trainsitions work. Boot > Menu > Reading > repeat --- main/component.mk | 2 +- main/main.cpp | 2 ++ main/modes/AppMode.h | 11 +++++++ main/modes/BootMode.cpp | 2 +- main/modes/MainMenuMode.cpp | 54 ++++++++++++++++++++++++++++++- main/modes/MainMenuMode.h | 3 ++ main/modes/ModeRunner.cpp | 7 +++- main/modes/ReaderMode.cpp | 21 ++++-------- main/modes/ReaderMode.h | 14 +++++++- main/modes/reader/PagePrinter.cpp | 2 +- 10 files changed, 97 insertions(+), 21 deletions(-) diff --git a/main/component.mk b/main/component.mk index 8c1f6ae..ee9b33c 100644 --- a/main/component.mk +++ b/main/component.mk @@ -3,5 +3,5 @@ # # (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) -COMPONENT_SRCDIRS := . core modes +COMPONENT_SRCDIRS := . core modes modes/reader COMPONENT_ADD_INCLUDEDIRS := . diff --git a/main/main.cpp b/main/main.cpp index 12d4254..4a0b098 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -20,6 +20,7 @@ static const char *TAG = "main"; #include "modes/ModeRunner.h" #include "modes/BootMode.h" +#include "modes/MainMenuMode.h" static struct tm* tm_info; @@ -46,6 +47,7 @@ extern "C" void app_main() getModeRunner()->init(); getModeRunner()->startMainMode(new BootMode()); + while (1) { getModeRunner()->loop(); } diff --git a/main/modes/AppMode.h b/main/modes/AppMode.h index edd7f69..3443342 100644 --- a/main/modes/AppMode.h +++ b/main/modes/AppMode.h @@ -7,6 +7,17 @@ public: virtual void start(); virtual void loop(); virtual void finish(); + bool isFinished() { + return this->finished; + } + +protected: + void setFinished() { + this->finished = true; + } + +private: + bool finished = false; }; #endif diff --git a/main/modes/BootMode.cpp b/main/modes/BootMode.cpp index 0d64cab..376aa46 100644 --- a/main/modes/BootMode.cpp +++ b/main/modes/BootMode.cpp @@ -10,7 +10,7 @@ void BootMode::start() void BootMode::loop() { display_splash_screen(); - delay(500); + delay(200); getModeRunner()->startMainMode(new MainMenuMode()); } diff --git a/main/modes/MainMenuMode.cpp b/main/modes/MainMenuMode.cpp index 33479ca..1558cbe 100644 --- a/main/modes/MainMenuMode.cpp +++ b/main/modes/MainMenuMode.cpp @@ -2,20 +2,72 @@ #include "core/buttons.h" #include "core/display.h" #include +#include "ModeRunner.h" +#include "ReaderMode.h" #include "MainMenuMode.h" +static int baseY = 35; +static int menuSkip = 20; +static int x = 40; +static int menu_options_size = 4; + +void draw_menu_option(char* text, int line, bool selected) +{ + EPD_print(text, CENTER, baseY + line * menuSkip); +} + +void draw_menu_cursor(int cursor) +{ + EPD_drawRect(x, baseY + cursor * menuSkip, + EPD_DISPLAY_WIDTH - 2*x, menuSkip + 1, 0x0F); +} + void MainMenuMode::start() -{} +{ + display_refresh(); +} void MainMenuMode::loop() { display_clear(); + EPD_setFont(COMIC24_FONT, NULL); EPD_print("Main Menu", CENTER, 0); + + int line = 0; + EPD_setFont(DEJAVU18_FONT, NULL); + draw_menu_option("Continue Reading", line, this->cursor == line); line++; + draw_menu_option("Internal Memory", line, this->cursor == line); line++; + draw_menu_option("SD Card", line, this->cursor == line); line++; + draw_menu_option("Settings", line, this->cursor == line); line++; + draw_menu_cursor(this->cursor); display_update(); while(1) { delay(5); if (buttons_pressed_ok()) { + switch (this->cursor) { + case 0: // reading + display_refresh(); + getModeRunner()->startInnerMode(new ReaderMode()); + return; + case 1: // memory + // TODO + break; + case 2: // sd card + // TODO + break; + case 3: // settings + // TODO + break; + } + break; + } + if (buttons_pressed_minus()) { + this->cursor = (this->cursor + menu_options_size - 1) % menu_options_size; + break; + } + if (buttons_pressed_plus()) { + this->cursor = (this->cursor + 1) % menu_options_size; break; } } diff --git a/main/modes/MainMenuMode.h b/main/modes/MainMenuMode.h index f8cc305..7381b19 100644 --- a/main/modes/MainMenuMode.h +++ b/main/modes/MainMenuMode.h @@ -6,4 +6,7 @@ public: virtual void start(); virtual void loop(); virtual void finish(); + +private: + int cursor = 0; }; diff --git a/main/modes/ModeRunner.cpp b/main/modes/ModeRunner.cpp index f4ad680..4b3e495 100644 --- a/main/modes/ModeRunner.cpp +++ b/main/modes/ModeRunner.cpp @@ -14,7 +14,12 @@ void ModeRunner::loop() ESP_LOGE(TAG, "Cannot run app mode. None activated (depth %d).", this->modeStackDepth); return; } - this->modeStack[this->modeStackDepth]->loop(); + AppMode* appMode = this->modeStack[this->modeStackDepth]; + if (appMode->isFinished()) { + this->finishMode(appMode); + } else { + appMode->loop(); + } } void ModeRunner::startMainMode(AppMode* mode) diff --git a/main/modes/ReaderMode.cpp b/main/modes/ReaderMode.cpp index 7292d27..bcfe642 100644 --- a/main/modes/ReaderMode.cpp +++ b/main/modes/ReaderMode.cpp @@ -2,27 +2,16 @@ #include "string.h" #include "core/buttons.h" #include "core/display.h" -#include "reader/Typesetter.h" -#include "reader/TextStorage.h" -#include "reader/PagePrinter.h" #include "ReaderMode.h" #include "esp_log.h" static const char *TAG = "ReaderMode"; -// TODO: class members -Typesetter typesetter; -PagePrinter pagePrinter; -TextStorage textStorage; -TextReader* textReader = textStorage.open("/sdcard/book.txt"); -Page* pageLast = NULL; -Page* pageCurrent = NULL; - -long bookmark = 0; //bool displaySleeping = false; void ReaderMode::start() { + this->textReader = textStorage.open("/sdcard/book.txt"); } void ReaderMode::loop() @@ -60,8 +49,8 @@ void ReaderMode::loop() while (1) { delay(10); if (buttons_pressed_ok()) { - ESP_LOGI(TAG, "Clear page."); - display_refresh(); + ESP_LOGI(TAG, "Exiting reader."); + this->setFinished(); break; } if (buttons_pressed_plus()) { @@ -103,4 +92,6 @@ void ReaderMode::loop() } void ReaderMode::finish() -{} +{ + textStorage.close(this->textReader); +} diff --git a/main/modes/ReaderMode.h b/main/modes/ReaderMode.h index d1faf2b..1278ee9 100644 --- a/main/modes/ReaderMode.h +++ b/main/modes/ReaderMode.h @@ -1,9 +1,21 @@ #include "AppMode.h" +#include "reader/Typesetter.h" +#include "reader/TextStorage.h" +#include "reader/PagePrinter.h" -class ReaderMode : AppMode +class ReaderMode : public AppMode { public: virtual void start(); virtual void loop(); virtual void finish(); + +private: + long bookmark = 0; + Typesetter typesetter; + PagePrinter pagePrinter; + TextStorage textStorage; + TextReader* textReader = NULL; + Page* pageLast = NULL; + Page* pageCurrent = NULL; }; diff --git a/main/modes/reader/PagePrinter.cpp b/main/modes/reader/PagePrinter.cpp index a4f0169..e69222d 100644 --- a/main/modes/reader/PagePrinter.cpp +++ b/main/modes/reader/PagePrinter.cpp @@ -1,4 +1,4 @@ -#include "display.h" +#include "core/display.h" #include "EPD.h" #include "PagePrinter.h"