From a32fcd8785587fd6bec5d71909927583c1e4bfaa Mon Sep 17 00:00:00 2001 From: dejvino Date: Sun, 2 Feb 2020 01:04:10 +0100 Subject: [PATCH] Added app modes, extracted into mode controllers. --- main/component.mk | 2 +- main/{ => core}/buttons.c | 0 main/{ => core}/buttons.h | 8 ++ main/core/common.c | 7 ++ main/core/common.h | 14 +++ main/{ => core}/display.c | 0 main/{ => core}/display.h | 10 +- main/{ => core}/storage.c | 0 main/core/storage.h | 9 ++ main/main.cpp | 114 ++---------------- main/modes/AppMode.h | 12 ++ main/modes/BootMode.cpp | 19 +++ main/modes/BootMode.h | 9 ++ main/modes/MainMenuMode.cpp | 25 ++++ main/modes/MainMenuMode.h | 9 ++ main/modes/ModeRunner.cpp | 55 +++++++++ main/modes/ModeRunner.h | 20 +++ main/modes/ReaderMode.cpp | 106 ++++++++++++++++ main/modes/ReaderMode.h | 9 ++ main/{ => modes/reader}/Page.cpp | 0 main/{ => modes/reader}/Page.h | 0 main/{ => modes/reader}/PagePrinter.cpp | 0 main/{ => modes/reader}/PagePrinter.h | 0 .../reader}/PageSettingsProvider.cpp | 0 .../{ => modes/reader}/PageSettingsProvider.h | 0 main/{ => modes/reader}/TextReader.cpp | 0 main/{ => modes/reader}/TextReader.h | 0 main/{ => modes/reader}/TextStorage.cpp | 0 main/{ => modes/reader}/TextStorage.h | 0 main/{ => modes/reader}/Typesetter.cpp | 0 main/{ => modes/reader}/Typesetter.h | 0 main/storage.h | 2 - 32 files changed, 323 insertions(+), 107 deletions(-) rename main/{ => core}/buttons.c (100%) rename main/{ => core}/buttons.h (60%) create mode 100644 main/core/common.c create mode 100644 main/core/common.h rename main/{ => core}/display.c (100%) rename main/{ => core}/display.h (66%) rename main/{ => core}/storage.c (100%) create mode 100644 main/core/storage.h create mode 100644 main/modes/AppMode.h create mode 100644 main/modes/BootMode.cpp create mode 100644 main/modes/BootMode.h create mode 100644 main/modes/MainMenuMode.cpp create mode 100644 main/modes/MainMenuMode.h create mode 100644 main/modes/ModeRunner.cpp create mode 100644 main/modes/ModeRunner.h create mode 100644 main/modes/ReaderMode.cpp create mode 100644 main/modes/ReaderMode.h rename main/{ => modes/reader}/Page.cpp (100%) rename main/{ => modes/reader}/Page.h (100%) rename main/{ => modes/reader}/PagePrinter.cpp (100%) rename main/{ => modes/reader}/PagePrinter.h (100%) rename main/{ => modes/reader}/PageSettingsProvider.cpp (100%) rename main/{ => modes/reader}/PageSettingsProvider.h (100%) rename main/{ => modes/reader}/TextReader.cpp (100%) rename main/{ => modes/reader}/TextReader.h (100%) rename main/{ => modes/reader}/TextStorage.cpp (100%) rename main/{ => modes/reader}/TextStorage.h (100%) rename main/{ => modes/reader}/Typesetter.cpp (100%) rename main/{ => modes/reader}/Typesetter.h (100%) delete mode 100644 main/storage.h diff --git a/main/component.mk b/main/component.mk index 0f76ee5..8c1f6ae 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 := . +COMPONENT_SRCDIRS := . core modes COMPONENT_ADD_INCLUDEDIRS := . diff --git a/main/buttons.c b/main/core/buttons.c similarity index 100% rename from main/buttons.c rename to main/core/buttons.c diff --git a/main/buttons.h b/main/core/buttons.h similarity index 60% rename from main/buttons.h rename to main/core/buttons.h index 344ed54..2e61bd0 100644 --- a/main/buttons.h +++ b/main/core/buttons.h @@ -1,4 +1,12 @@ +#ifdef __cplusplus +extern "C" { +#endif + void buttons_init(); bool buttons_pressed_ok(); bool buttons_pressed_plus(); bool buttons_pressed_minus(); + +#ifdef __cplusplus +} +#endif diff --git a/main/core/common.c b/main/core/common.c new file mode 100644 index 0000000..7f9e387 --- /dev/null +++ b/main/core/common.c @@ -0,0 +1,7 @@ +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" + +void delay(unsigned int ms) +{ + vTaskDelay(ms / portTICK_RATE_MS); +} diff --git a/main/core/common.h b/main/core/common.h new file mode 100644 index 0000000..64c0467 --- /dev/null +++ b/main/core/common.h @@ -0,0 +1,14 @@ +#ifndef _COMMON_H_ +#define _COMMON_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +void delay(unsigned int ms); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/main/display.c b/main/core/display.c similarity index 100% rename from main/display.c rename to main/core/display.c diff --git a/main/display.h b/main/core/display.h similarity index 66% rename from main/display.h rename to main/core/display.h index 6edc2a9..4a59a06 100644 --- a/main/display.h +++ b/main/core/display.h @@ -1,3 +1,7 @@ +#ifdef __cplusplus +extern "C" { +#endif + void display_init(); void spi_init(); void display_connect(); @@ -8,4 +12,8 @@ void display_refresh(); void display_update(); void display_wake(); -void display_sleep(); \ No newline at end of file +void display_sleep(); + +#ifdef __cplusplus +} +#endif diff --git a/main/storage.c b/main/core/storage.c similarity index 100% rename from main/storage.c rename to main/core/storage.c diff --git a/main/core/storage.h b/main/core/storage.h new file mode 100644 index 0000000..0ac7078 --- /dev/null +++ b/main/core/storage.h @@ -0,0 +1,9 @@ +#ifdef __cplusplus +extern "C" { +#endif + +int storage_init(); + +#ifdef __cplusplus +} +#endif diff --git a/main/main.cpp b/main/main.cpp index 0f4ab82..12d4254 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -4,8 +4,6 @@ #include #include #include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" #include "esp_system.h" #include "driver/gpio.h" #include "esp_system.h" @@ -14,16 +12,15 @@ #include "esp_log.h" static const char *TAG = "main"; -extern "C" { +#include "core/common.h" #include "config.h" -#include "buttons.h" -#include "storage.h" -#include "display.h" -} +#include "core/buttons.h" +#include "core/storage.h" +#include "core/display.h" + +#include "modes/ModeRunner.h" +#include "modes/BootMode.h" -#include "Typesetter.h" -#include "TextStorage.h" -#include "PagePrinter.h" static struct tm* tm_info; static char tmp_buff[128]; @@ -32,11 +29,6 @@ static const char *file_fonts[3] = {"/spiffs/fonts/DotMatrix_M.fon", "/spiffs/fo static const char tag[] = "[LilyBook]"; esp_err_t ret; -void sleep(unsigned int ms) -{ - vTaskDelay(ms / portTICK_RATE_MS); -} - extern "C" void app_main() { printf("\n LilyBook v%s\n\n", APP_VERSION); @@ -46,99 +38,15 @@ extern "C" void app_main() display_init(); spi_init(); - sleep(500); + delay(500); display_connect(); - display_splash_screen(); - - sleep(500); printf("==== START ====\r\n\n"); - 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; - + getModeRunner()->init(); + getModeRunner()->startMainMode(new BootMode()); while (1) { - char text[1024]; - if (textReader != NULL) { - if (pageCurrent == NULL) { - size_t read = textReader->read(bookmark, text, sizeof(text)); - pageCurrent = typesetter.preparePage(text, read); - pageCurrent->start = bookmark; - } - if (pageLast == NULL) { - // align with the start? - if (bookmark < sizeof(text)) { - size_t read = textReader->read(0, text, sizeof(text)); - pageLast = typesetter.preparePage(text, read); - pageLast->start = 0; - } else { - size_t read = textReader->read((bookmark - sizeof(text)), text, sizeof(text)); - pageLast = typesetter.preparePreviousPage(text, read); - pageLast->start = bookmark - pageLast->len; - } - } - } else { - typesetter.destroyPage(pageCurrent); - strcpy(text, "File could not be opened."); - pageCurrent = typesetter.preparePage(text, sizeof(text)); - } - - display_clear(); - pagePrinter.print(pageCurrent); - display_update(); - - //time_t idleStart = clock(); - while (1) { - sleep(10); - if (buttons_pressed_ok()) { - ESP_LOGI(TAG, "Clear page."); - display_refresh(); - break; - } - if (buttons_pressed_plus()) { - ESP_LOGI(TAG, "Turn page PLUS."); - if (pageCurrent != NULL) { - bookmark = pageCurrent->start + pageCurrent->len; - // TODO: limit bookmark to file size - typesetter.destroyPage(pageLast); - pageLast = pageCurrent; - pageCurrent = NULL; - } else { - ESP_LOGW(TAG, "No current page."); - } - break; - } - if (buttons_pressed_minus()) { - ESP_LOGI(TAG, "Turn page MINUS."); - if (pageLast != NULL) { - bookmark = pageLast->start; - typesetter.destroyPage(pageCurrent); - pageCurrent = pageLast; - pageLast = NULL; - } else { - ESP_LOGW(TAG, "No last page."); - } - break; - } - /*if (!displaySleeping && (clock() - idleStart > DISPLAY_SLEEP_TIMEOUT)) { - displaySleeping = true; - ESP_LOGI(TAG, "Display going to sleep after %d ms.", DISPLAY_SLEEP_TIMEOUT); - display_sleep(); - }*/ - } - /*if (displaySleeping) { - displaySleeping = false; - ESP_LOGI(TAG, "Display waking up."); - display_wake(); - }*/ + getModeRunner()->loop(); } - } diff --git a/main/modes/AppMode.h b/main/modes/AppMode.h new file mode 100644 index 0000000..edd7f69 --- /dev/null +++ b/main/modes/AppMode.h @@ -0,0 +1,12 @@ +#ifndef _APPMODE_H_ +#define _APPMODE_H_ + +class AppMode +{ +public: + virtual void start(); + virtual void loop(); + virtual void finish(); +}; + +#endif diff --git a/main/modes/BootMode.cpp b/main/modes/BootMode.cpp new file mode 100644 index 0000000..0d64cab --- /dev/null +++ b/main/modes/BootMode.cpp @@ -0,0 +1,19 @@ +#include "core/common.h" +#include "core/display.h" +#include "ModeRunner.h" +#include "MainMenuMode.h" +#include "BootMode.h" + +void BootMode::start() +{} + +void BootMode::loop() +{ + display_splash_screen(); + delay(500); + + getModeRunner()->startMainMode(new MainMenuMode()); +} + +void BootMode::finish() +{} diff --git a/main/modes/BootMode.h b/main/modes/BootMode.h new file mode 100644 index 0000000..13e3662 --- /dev/null +++ b/main/modes/BootMode.h @@ -0,0 +1,9 @@ +#include "AppMode.h" + +class BootMode : public AppMode +{ +public: + virtual void start(); + virtual void loop(); + virtual void finish(); +}; diff --git a/main/modes/MainMenuMode.cpp b/main/modes/MainMenuMode.cpp new file mode 100644 index 0000000..33479ca --- /dev/null +++ b/main/modes/MainMenuMode.cpp @@ -0,0 +1,25 @@ +#include "core/common.h" +#include "core/buttons.h" +#include "core/display.h" +#include +#include "MainMenuMode.h" + +void MainMenuMode::start() +{} + +void MainMenuMode::loop() +{ + display_clear(); + EPD_print("Main Menu", CENTER, 0); + display_update(); + + while(1) { + delay(5); + if (buttons_pressed_ok()) { + break; + } + } +} + +void MainMenuMode::finish() +{} diff --git a/main/modes/MainMenuMode.h b/main/modes/MainMenuMode.h new file mode 100644 index 0000000..f8cc305 --- /dev/null +++ b/main/modes/MainMenuMode.h @@ -0,0 +1,9 @@ +#include "AppMode.h" + +class MainMenuMode : public AppMode +{ +public: + virtual void start(); + virtual void loop(); + virtual void finish(); +}; diff --git a/main/modes/ModeRunner.cpp b/main/modes/ModeRunner.cpp new file mode 100644 index 0000000..f4ad680 --- /dev/null +++ b/main/modes/ModeRunner.cpp @@ -0,0 +1,55 @@ +#include "ModeRunner.h" + +#include "esp_log.h" +static const char *TAG = "ModeRunner"; + +void ModeRunner::init() +{ + this->modeStackDepth = -1; +} + +void ModeRunner::loop() +{ + if (this->modeStackDepth < 0) { + ESP_LOGE(TAG, "Cannot run app mode. None activated (depth %d).", this->modeStackDepth); + return; + } + this->modeStack[this->modeStackDepth]->loop(); +} + +void ModeRunner::startMainMode(AppMode* mode) +{ + this->finishMode(NULL); + this->modeStackDepth = -1; + this->startInnerMode(mode); +} + +void ModeRunner::startInnerMode(AppMode* mode) +{ + if (this->modeStackDepth + 1 >= MODE_STACK_SIZE) { + ESP_LOGE(TAG, "Cannot start inner mode. Stack overflow."); + return; + } + this->modeStackDepth++; + this->modeStack[this->modeStackDepth] = mode; + ESP_LOGI(TAG, "Starting new mode (depth %d).", this->modeStackDepth); + mode->start(); +} + +void ModeRunner::finishMode(AppMode* mode) +{ + for (int i = this->modeStackDepth; i >= 0; i--) { + ESP_LOGI(TAG, "Finishing mode (depth %d).", this->modeStackDepth); + this->modeStack[i]->finish(); + this->modeStackDepth--; + if (mode == this->modeStack[i]) { + break; + } + } +} + +ModeRunner modeRunner; +ModeRunner* getModeRunner() +{ + return &modeRunner; +} diff --git a/main/modes/ModeRunner.h b/main/modes/ModeRunner.h new file mode 100644 index 0000000..325773e --- /dev/null +++ b/main/modes/ModeRunner.h @@ -0,0 +1,20 @@ +#include "AppMode.h" +#define MODE_STACK_SIZE 5 + +class ModeRunner +{ +public: + void init(); + void loop(); + + void startMainMode(AppMode* mode); + void startInnerMode(AppMode* mode); + + void finishMode(AppMode* mode); + +private: + AppMode* modeStack[MODE_STACK_SIZE]; + int modeStackDepth = -1; +}; + +ModeRunner* getModeRunner(); diff --git a/main/modes/ReaderMode.cpp b/main/modes/ReaderMode.cpp new file mode 100644 index 0000000..7292d27 --- /dev/null +++ b/main/modes/ReaderMode.cpp @@ -0,0 +1,106 @@ +#include "core/common.h" +#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() +{ +} + +void ReaderMode::loop() +{ + char text[1024]; + if (textReader != NULL) { + if (pageCurrent == NULL) { + size_t read = textReader->read(bookmark, text, sizeof(text)); + pageCurrent = typesetter.preparePage(text, read); + pageCurrent->start = bookmark; + } + if (pageLast == NULL) { + // align with the start? + if (bookmark < sizeof(text)) { + size_t read = textReader->read(0, text, sizeof(text)); + pageLast = typesetter.preparePage(text, read); + pageLast->start = 0; + } else { + size_t read = textReader->read((bookmark - sizeof(text)), text, sizeof(text)); + pageLast = typesetter.preparePreviousPage(text, read); + pageLast->start = bookmark - pageLast->len; + } + } + } else { + typesetter.destroyPage(pageCurrent); + strcpy(text, "File could not be opened."); + pageCurrent = typesetter.preparePage(text, sizeof(text)); + } + + display_clear(); + pagePrinter.print(pageCurrent); + display_update(); + + //time_t idleStart = clock(); + while (1) { + delay(10); + if (buttons_pressed_ok()) { + ESP_LOGI(TAG, "Clear page."); + display_refresh(); + break; + } + if (buttons_pressed_plus()) { + ESP_LOGI(TAG, "Turn page PLUS."); + if (pageCurrent != NULL) { + bookmark = pageCurrent->start + pageCurrent->len; + // TODO: limit bookmark to file size + typesetter.destroyPage(pageLast); + pageLast = pageCurrent; + pageCurrent = NULL; + } else { + ESP_LOGW(TAG, "No current page."); + } + break; + } + if (buttons_pressed_minus()) { + ESP_LOGI(TAG, "Turn page MINUS."); + if (pageLast != NULL) { + bookmark = pageLast->start; + typesetter.destroyPage(pageCurrent); + pageCurrent = pageLast; + pageLast = NULL; + } else { + ESP_LOGW(TAG, "No last page."); + } + break; + } + /*if (!displaySleeping && (clock() - idleStart > DISPLAY_SLEEP_TIMEOUT)) { + displaySleeping = true; + ESP_LOGI(TAG, "Display going to sleep after %d ms.", DISPLAY_SLEEP_TIMEOUT); + display_sleep(); + }*/ + } + /*if (displaySleeping) { + displaySleeping = false; + ESP_LOGI(TAG, "Display waking up."); + display_wake(); + }*/ +} + +void ReaderMode::finish() +{} diff --git a/main/modes/ReaderMode.h b/main/modes/ReaderMode.h new file mode 100644 index 0000000..d1faf2b --- /dev/null +++ b/main/modes/ReaderMode.h @@ -0,0 +1,9 @@ +#include "AppMode.h" + +class ReaderMode : AppMode +{ +public: + virtual void start(); + virtual void loop(); + virtual void finish(); +}; diff --git a/main/Page.cpp b/main/modes/reader/Page.cpp similarity index 100% rename from main/Page.cpp rename to main/modes/reader/Page.cpp diff --git a/main/Page.h b/main/modes/reader/Page.h similarity index 100% rename from main/Page.h rename to main/modes/reader/Page.h diff --git a/main/PagePrinter.cpp b/main/modes/reader/PagePrinter.cpp similarity index 100% rename from main/PagePrinter.cpp rename to main/modes/reader/PagePrinter.cpp diff --git a/main/PagePrinter.h b/main/modes/reader/PagePrinter.h similarity index 100% rename from main/PagePrinter.h rename to main/modes/reader/PagePrinter.h diff --git a/main/PageSettingsProvider.cpp b/main/modes/reader/PageSettingsProvider.cpp similarity index 100% rename from main/PageSettingsProvider.cpp rename to main/modes/reader/PageSettingsProvider.cpp diff --git a/main/PageSettingsProvider.h b/main/modes/reader/PageSettingsProvider.h similarity index 100% rename from main/PageSettingsProvider.h rename to main/modes/reader/PageSettingsProvider.h diff --git a/main/TextReader.cpp b/main/modes/reader/TextReader.cpp similarity index 100% rename from main/TextReader.cpp rename to main/modes/reader/TextReader.cpp diff --git a/main/TextReader.h b/main/modes/reader/TextReader.h similarity index 100% rename from main/TextReader.h rename to main/modes/reader/TextReader.h diff --git a/main/TextStorage.cpp b/main/modes/reader/TextStorage.cpp similarity index 100% rename from main/TextStorage.cpp rename to main/modes/reader/TextStorage.cpp diff --git a/main/TextStorage.h b/main/modes/reader/TextStorage.h similarity index 100% rename from main/TextStorage.h rename to main/modes/reader/TextStorage.h diff --git a/main/Typesetter.cpp b/main/modes/reader/Typesetter.cpp similarity index 100% rename from main/Typesetter.cpp rename to main/modes/reader/Typesetter.cpp diff --git a/main/Typesetter.h b/main/modes/reader/Typesetter.h similarity index 100% rename from main/Typesetter.h rename to main/modes/reader/Typesetter.h diff --git a/main/storage.h b/main/storage.h deleted file mode 100644 index 71612c8..0000000 --- a/main/storage.h +++ /dev/null @@ -1,2 +0,0 @@ - -int storage_init();