diff --git a/radio/src/gui/colorlcd/view_text.cpp b/radio/src/gui/colorlcd/view_text.cpp index de7f2cc6f..e7fb60673 100644 --- a/radio/src/gui/colorlcd/view_text.cpp +++ b/radio/src/gui/colorlcd/view_text.cpp @@ -26,17 +26,17 @@ #include "sdcard.h" #define CASE_EVT_KEY_NEXT_LINE \ - case EVT_ROTARY_RIGHT: \ + case EVT_ROTARY_RIGHT: \ case EVT_KEY_BREAK(KEY_PGDN) // case EVT_KEY_BREAK(KEY_DOWN) #define CASE_EVT_KEY_PREVIOUS_LINE \ - case EVT_ROTARY_LEFT: \ - case EVT_KEY_BREAK(KEY_PGUP): \ + case EVT_ROTARY_LEFT: \ + case EVT_KEY_BREAK(KEY_PGUP): \ case EVT_KEY_BREAK(KEY_UP) -#define CASE_EVT_START \ - case EVT_ENTRY: \ +#define CASE_EVT_START \ + case EVT_ENTRY: \ case EVT_KEY_BREAK(KEY_ENTER): \ case EVT_KEY_BREAK(KEY_TELEM) @@ -54,138 +54,6 @@ void ViewTextWindow::extractNameSansExt() name.substr(nameLength); } -#if READ_FILE_BY_LINE -void ViewTextWindow::buildBody(Window *window) -{ - GridLayout grid(window); - grid.spacer(); - int i; - - // assume average characte is 10 pixels wide, round the string length to tens. - // Font is not fixed width, so this is for the worst case... - const int maxLineLength = static_cast(window->width() / 10 / 10) * 10 - 2; - window->setFocus(); - - FIL file; - if (FR_OK != - f_open(&file, (TCHAR *)fullPath.c_str(), FA_OPEN_EXISTING | FA_READ)) { - return; - } - readCount = 0; - longestLine = 0; - lastLine = false; - for (i = 0; i < TEXT_FILE_MAXSIZE && !lastLine; i++) { - lastLine = - sdReadTextLine(&file, reusableBuffer.viewText.lines[0], maxLineLength); - - new StaticText(window, grid.getSlot(), reusableBuffer.viewText.lines[0]); - grid.nextLine(); - } - - f_close(&file); - - window->setInnerWidth((longestLine + 4) * 10); - window->setInnerHeight(grid.getWindowHeight()); -} - -void ViewTextWindow::checkEvents() -{ - if (&body == Window::focusWindow) { - const int step = PAGE_LINE_HEIGHT + PAGE_LINE_SPACING; - coord_t currentPos = body.getScrollPositionY(); - coord_t deltaY; - event_t event = getWindowEvent(); - -#if defined(ROTARY_ENCODER_NAVIGATION) - if (event == EVT_ROTARY_LEFT || event == EVT_ROTARY_RIGHT) { - deltaY = ROTARY_ENCODER_SPEED() * step; - } else { - deltaY = step; - } -#else - deltaY = step; -#endif - switch (event) { - CASE_EVT_KEY_NEXT_LINE: - currentPos += deltaY; - break; - - CASE_EVT_KEY_PREVIOUS_LINE: - currentPos -= deltaY; - break; - - default: - Page::onEvent(event); - return; - } - body.setScrollPositionY(currentPos); - } - Page::checkEvents(); -} - -bool ViewTextWindow::sdReadTextLine(FIL *file, char line[], - const uint8_t maxLineLength) -{ - char c; - unsigned int sz; - uint8_t line_length = 0; - uint8_t escape = 0; - char escape_chars[4] = {0}; - int current_line = 0; - - memclear(line, maxLineLength + 1); - line[line_length++] = 0x20; - - for (uint8_t i = 0; i < maxLineLength && readCount < (int)TEXT_FILE_MAXSIZE; - ++i) { - if ((f_read(file, &c, 1, &sz) != FR_OK || !sz) && - line_length < maxLineLength) { - return true; - } - readCount++; - - if (c == '\n') { - ++current_line; - escape = 0; - return false; - } else if (c != '\r') { - if (c == '\\' && escape == 0) { - escape = 1; - continue; - } else if (c != '\\' && escape > 0 && escape < sizeof(escape_chars)) { - escape_chars[escape - 1] = c; - if (escape == 2 && !strncmp(escape_chars, "up", 2)) { - c = CHAR_UP; - } else if (escape == 2 && !strncmp(escape_chars, "dn", 2)) { - c = CHAR_DOWN; - } else if (escape == 3) { - int val = atoi(escape_chars); - if (val >= 200 && val < 225) { - c = '\200' + val - 200; - } - } else { - escape++; - continue; - } - } else if (c == '~') { - c = 'z' + 1; - } else if (c == '\t') { - c = 0x1D; // tab - } - escape = 0; - line[line_length++] = c; - if (longestLine < line_length) longestLine = line_length; - } - } - if (c != '\n') { - current_line += 1; - return false; - } - - return false; -} -#else - void ViewTextWindow::buildBody(Window *window) { GridLayout grid(window); @@ -199,27 +67,25 @@ void ViewTextWindow::buildBody(Window *window) window->setFocus(); readLinesCount = 0; lastLoadedLine = 0; - - lines = new char*[maxScreenLines]; - for(i = 0 ; i < maxScreenLines; i++) - { + + lines = new char *[maxScreenLines]; + for (i = 0; i < maxScreenLines; i++) { lines[i] = new char[maxLineLength + 1]; memclear(lines[i], maxLineLength + 1); - } - + } + longestLine = 0; loadFirstScreen(); - - if(isInSetup == true) - { + + if (isInSetup == true) { textBottom = false; - while(!textBottom) - { + while (!textBottom) { sdReadTextFileBlock(fullPath.c_str(), readLinesCount); textVerticalOffset += 10; } - maxPos = (maxLines - maxScreenLines) * (PAGE_LINE_HEIGHT + PAGE_LINE_SPACING); - if(maxPos < body.getRect().h) maxPos = body.getRect().h; + maxPos = + (maxLines - maxScreenLines) * (PAGE_LINE_HEIGHT + PAGE_LINE_SPACING); + if (maxPos < body.getRect().h) maxPos = body.getRect().h; } isInSetup = false; @@ -227,7 +93,8 @@ void ViewTextWindow::buildBody(Window *window) for (i = 0; i < maxScreenLines; i++) { new DynamicText(window, grid.getSlot(), [=]() { - std::string str = (lines[i][0]) ? std::string(lines[i]) : std::string(" "); + std::string str = + (lines[i][0]) ? std::string(lines[i]) : std::string(" "); return std::string(str); }); grid.nextLine(); @@ -247,8 +114,8 @@ bool ViewTextWindow::onTouchSlide(coord_t x, coord_t y, coord_t startX, textVerticalOffset += lineStep; if (textVerticalOffset < 0) textVerticalOffset = 0; - // if (textBottom && lineStep > 0) textVerticalOffset -= lineStep; - if(textVerticalOffset > maxLines) textVerticalOffset = maxLines; + // if (textBottom && lineStep > 0) textVerticalOffset -= lineStep; + if (textVerticalOffset > maxLines) textVerticalOffset = maxLines; sdReadTextFileBlock(fullPath.c_str(), readLinesCount); } return Page::onTouchSlide(x, y, startX, startY, slideX, slideY); @@ -262,27 +129,19 @@ void ViewTextWindow::checkEvents() coord_t deltaY; event_t event = getWindowEvent(); -#if defined(ROTARY_ENCODER_NAVIGATION) +#if defined(ROTARY_ENCODER_NAVIGATION) if (event == EVT_ROTARY_LEFT || event == EVT_ROTARY_RIGHT) { deltaY = ROTARY_ENCODER_SPEED() * step; } else { deltaY = step; } #else - deltaY = step; + deltaY = step; #endif - + int lineStep = deltaY / step; - if(lineStep > (maxScreenLines >> 1)) lineStep = maxScreenLines >> 1; -/* - if(event) { - sprintf(lines[0], "Received event %d", event); - if(!readLinesCount) - readLinesCount = 1; - Page::onEvent(event); - return; - } -*/ + if (lineStep > (maxScreenLines >> 1)) lineStep = maxScreenLines >> 1; + switch (event) { CASE_EVT_START: textVerticalOffset = 0; @@ -291,11 +150,11 @@ void ViewTextWindow::checkEvents() break; CASE_EVT_KEY_NEXT_LINE: - if(textBottom && textVerticalOffset) //(textVerticalOffset + maxScreenLines - 1 >= readLinesCount) + if (textBottom && textVerticalOffset) break; else { textVerticalOffset += lineStep; - if(textVerticalOffset > maxLines) textVerticalOffset = maxLines; + if (textVerticalOffset > maxLines) textVerticalOffset = maxLines; } sdReadTextFileBlock(fullPath.c_str(), readLinesCount); break; @@ -305,7 +164,7 @@ void ViewTextWindow::checkEvents() break; else { textVerticalOffset -= lineStep; - if(textVerticalOffset < 0) textVerticalOffset = 0; + if (textVerticalOffset < 0) textVerticalOffset = 0; } sdReadTextFileBlock(fullPath.c_str(), readLinesCount); @@ -338,7 +197,7 @@ void ViewTextWindow::sdReadTextFileBlock(const char *filename, int &lines_count) int current_line = 0; textBottom = false; - for(int i = 0; i < maxScreenLines; i++) { + for (int i = 0; i < maxScreenLines; i++) { memclear(lines[i], maxLineLength + 1); lines[i][0] = ' '; } @@ -354,10 +213,10 @@ void ViewTextWindow::sdReadTextFileBlock(const char *filename, int &lines_count) ++current_line; line_length = 1; escape = 0; - } + } if (c != '\r' && c != '\n' && current_line >= textVerticalOffset && - current_line - textVerticalOffset < maxScreenLines && - line_length < maxLineLength) { + current_line - textVerticalOffset < maxScreenLines && + line_length < maxLineLength) { if (c == '\\' && escape == 0) { escape = 1; continue; @@ -376,17 +235,16 @@ void ViewTextWindow::sdReadTextFileBlock(const char *filename, int &lines_count) escape++; continue; } - } - else if (c == '~') { + } else if (c == '~') { c = 'z' + 1; } else if (c == '\t') { c = 0x1D; // tab } escape = 0; - + lines[current_line - textVerticalOffset][line_length++] = c; if (longestLine < line_length) longestLine = line_length; - } else if(current_line < textVerticalOffset) { + } else if (current_line < textVerticalOffset) { ++line_length; } } @@ -402,8 +260,7 @@ void ViewTextWindow::sdReadTextFileBlock(const char *filename, int &lines_count) f_close(&file); } - if(lastLoadedLine < textVerticalOffset) - lastLoadedLine = textVerticalOffset; + if (lastLoadedLine < textVerticalOffset) lastLoadedLine = textVerticalOffset; if (lines_count == 0) { lines_count = current_line; @@ -414,7 +271,7 @@ void ViewTextWindow::drawVerticalScrollbar(BitmapBuffer *dc) { int readPos = textVerticalOffset * (PAGE_LINE_HEIGHT + PAGE_LINE_SPACING); - if(readPos < header.getRect().h << 1) readPos = header.getRect().h << 1; + if (readPos < header.getRect().h << 1) readPos = header.getRect().h << 1; coord_t yofs = divRoundClosest(body.getRect().h * readPos, maxPos); coord_t yhgt = divRoundClosest(body.getRect().h * body.getRect().h, maxPos); @@ -424,34 +281,31 @@ void ViewTextWindow::drawVerticalScrollbar(BitmapBuffer *dc) SCROLLBAR_WIDTH, yhgt, COLOR_THEME_PRIMARY3); } -#endif - #include "datastructs.h" -static void replaceSpaceWithUnderscore(std::string& name) +static void replaceSpaceWithUnderscore(std::string &name) { size_t index; do { index = name.find(' '); - if(index != std::string::npos) - name[index] = '_'; + if (index != std::string::npos) name[index] = '_'; } while (index != std::string::npos); } #if defined(SDCARD_YAML) -#define MODEL_FILE_EXT YAML_EXT +#define MODEL_FILE_EXT YAML_EXT #else -#define MODEL_FILE_EXT MODELS_EXT +#define MODEL_FILE_EXT MODELS_EXT #endif bool openNotes(const char buf[], std::string modelNotesName) { - if(isFileAvailable(modelNotesName.c_str())) { - new ViewTextWindow(std::string(buf), modelNotesName, ICON_MODEL); + if (isFileAvailable(modelNotesName.c_str())) { + new ViewTextWindow(std::string(buf), modelNotesName, ICON_MODEL); return true; - } else { - return false; - } + } else { + return false; + } } void readModelNotes() { @@ -461,25 +315,24 @@ void readModelNotes() std::string modelNotesName(g_model.header.name); modelNotesName.append(TEXT_EXT); const char buf[] = {MODELS_PATH}; - f_chdir((TCHAR*)buf); + f_chdir((TCHAR *)buf); notesFound = openNotes(buf, modelNotesName); - if(!notesFound) { + if (!notesFound) { replaceSpaceWithUnderscore(modelNotesName); notesFound = openNotes(buf, modelNotesName); } -#if !defined(EEPROM) - if(!notesFound) { +#if !defined(EEPROM) + if (!notesFound) { modelNotesName.assign(g_eeGeneral.currModelFilename); size_t index = modelNotesName.find(MODEL_FILE_EXT); - if(index != std::string::npos) - { + if (index != std::string::npos) { modelNotesName.erase(index); modelNotesName.append(TEXT_EXT); notesFound = openNotes(buf, modelNotesName); } - if(!notesFound) { + if (!notesFound) { replaceSpaceWithUnderscore(modelNotesName); notesFound = openNotes(buf, modelNotesName); } diff --git a/radio/src/gui/colorlcd/view_text.h b/radio/src/gui/colorlcd/view_text.h index 99617019f..ee6754b7f 100644 --- a/radio/src/gui/colorlcd/view_text.h +++ b/radio/src/gui/colorlcd/view_text.h @@ -20,24 +20,20 @@ #pragma once -#include "page.h" -#include "static.h" +#include "ff.h" #include "lcd.h" #include "menus.h" -#include "ff.h" - -#define READ_FILE_BY_LINE 0 +#include "page.h" +#include "static.h" constexpr uint16_t TEXT_FILE_MAXSIZE = 20480; class ViewTextWindow : public Page { public: - ViewTextWindow(const std::string iPath, const std::string iName, unsigned int icon = ICON_RADIO_SD_MANAGER) : - Page(icon), - path(std::move(iPath)), - name(std::move(iName)), - icon(icon) + ViewTextWindow(const std::string iPath, const std::string iName, + unsigned int icon = ICON_RADIO_SD_MANAGER) : + Page(icon), path(std::move(iPath)), name(std::move(iName)), icon(icon) { fullPath = path + std::string("/") + name; extractNameSansExt(); @@ -46,41 +42,35 @@ class ViewTextWindow : public Page maxPos = 0; maxLines = 0; isInSetup = true; - //body.setWindowFlags(FORWARD_SCROLL); header.setWindowFlags(NO_SCROLLBAR); - + buildHeader(&header); buildBody(&body); }; -#if READ_FILE_BY_LINE - bool sdReadTextLine(FIL* file, char lines[], - const uint8_t lineLength = LCD_COLS); -#else - void sdReadTextFileBlock(const char * filename, int& lines_count); + void sdReadTextFileBlock(const char* filename, int& lines_count); void loadFirstScreen(void); - #if defined(HARDWARE_TOUCH) - bool onTouchSlide(coord_t x, coord_t y, coord_t startX, coord_t startY, coord_t slideX, coord_t slideY) override; -#endif - void drawVerticalScrollbar(BitmapBuffer * dc); +#if defined(HARDWARE_TOUCH) + bool onTouchSlide(coord_t x, coord_t y, coord_t startX, coord_t startY, + coord_t slideX, coord_t slideY) override; +#endif + void drawVerticalScrollbar(BitmapBuffer* dc); ~ViewTextWindow() { - if(lines != nullptr) { - for(int i = 0; i < maxScreenLines; i++) - { + if (lines != nullptr) { + for (int i = 0; i < maxScreenLines; i++) { delete[] lines[i]; } delete[] lines; } - } + } void paint(BitmapBuffer* dc) override { Page::paint(dc); drawVerticalScrollbar(dc); - } -#endif + } void checkEvents() override; #if defined(DEBUG_WINDOWS) @@ -98,23 +88,20 @@ class ViewTextWindow : public Page uint16_t readCount; int longestLine; -#if !READ_FILE_BY_LINE -char** lines = nullptr; -//char lines[15][80]; -int maxScreenLines; -int maxLineLength; -int textVerticalOffset; -int readLinesCount; -int lastLoadedLine; -int maxPos; -int maxLines; -bool textBottom; -bool isInSetup; -#endif + char** lines = nullptr; + int maxScreenLines; + int maxLineLength; + int textVerticalOffset; + int readLinesCount; + int lastLoadedLine; + int maxPos; + int maxLines; + bool textBottom; + bool isInSetup; void extractNameSansExt(void); - void buildBody(Window *window); - void buildHeader(Window *window) + void buildBody(Window* window); + void buildHeader(Window* window) { new StaticText(window, {PAGE_TITLE_LEFT, PAGE_TITLE_TOP + 10,