From 683f6e22e2ae621d181472d019579283f34ed669 Mon Sep 17 00:00:00 2001 From: Bertrand Songis Date: Sat, 21 Nov 2015 11:35:09 +0100 Subject: [PATCH] [Horus] MODELSEL screen continued --- radio/src/gui/Horus/lcd.cpp | 2 +- radio/src/gui/Horus/menu_model.cpp | 6 ++-- radio/src/gui/Horus/menu_model_select.cpp | 38 +++++++++++--------- radio/src/gui/Horus/navigation.cpp | 5 +++ radio/src/main_arm.cpp | 7 +++- radio/src/opentx.cpp | 4 ++- radio/src/sdcard.cpp | 10 +++--- radio/src/sdcard.h | 2 +- radio/src/storage/sdcard_raw.cpp | 42 ++++++++++++++++------- radio/src/storage/sdcard_raw.h | 1 + 10 files changed, 76 insertions(+), 41 deletions(-) diff --git a/radio/src/gui/Horus/lcd.cpp b/radio/src/gui/Horus/lcd.cpp index c23e393bd..e2638b3ee 100644 --- a/radio/src/gui/Horus/lcd.cpp +++ b/radio/src/gui/Horus/lcd.cpp @@ -175,7 +175,7 @@ int getTextWidth(const pm_char * s, int len, LcdFlags flags) if (flags & ZCHAR) c = idx2char(*s); else - c = pgm_read_byte(s); + c = *s; if (c == '\0') break; result += getCharWidth(c, specs); diff --git a/radio/src/gui/Horus/menu_model.cpp b/radio/src/gui/Horus/menu_model.cpp index 881384c95..5bb55467d 100644 --- a/radio/src/gui/Horus/menu_model.cpp +++ b/radio/src/gui/Horus/menu_model.cpp @@ -205,10 +205,10 @@ void editName(coord_t x, coord_t y, char * name, uint8_t size, evt_t event, uint } lcdDrawTextWithLen(x, y, name, size, flags); - coord_t w = (editNameCursorPos == 0 ? 0 : getTextWidth(name, editNameCursorPos, flags)); + coord_t left = (editNameCursorPos == 0 ? 0 : getTextWidth(name, editNameCursorPos, flags)); char s[] = { (flags & ZCHAR) ? idx2char(name[editNameCursorPos]) : name[editNameCursorPos], '\0' }; - lcdDrawSolidFilledRect(x+w-1, y-INVERT_VERT_MARGIN, getTextWidth(s, 1, flags)+1, INVERT_LINE_HEIGHT, TEXT_INVERTED_BGCOLOR); - lcdDrawText(x+w, y, s, TEXT_INVERTED_COLOR); + lcdDrawSolidFilledRect(x+left-1, y-INVERT_VERT_MARGIN, getTextWidth(s, 1)+1, INVERT_LINE_HEIGHT, TEXT_INVERTED_BGCOLOR); + lcdDrawText(x+left, y, s, TEXT_INVERTED_COLOR); } else { editNameCursorPos = 0; diff --git a/radio/src/gui/Horus/menu_model_select.cpp b/radio/src/gui/Horus/menu_model_select.cpp index eae453d5c..1c9f9b9f0 100644 --- a/radio/src/gui/Horus/menu_model_select.cpp +++ b/radio/src/gui/Horus/menu_model_select.cpp @@ -36,7 +36,6 @@ #include "../../opentx.h" #define CATEGORIES_WIDTH 140 - #define MODELCELL_WIDTH 153 #define MODELCELL_HEIGHT 61 @@ -54,10 +53,10 @@ char selectedCategory[LEN_MODEL_FILENAME+1]; void drawCategory(coord_t y, const char * name, bool selected) { if (selected) { - lcdDrawSolidFilledRect(0, y-INVERT_VERT_MARGIN, CATEGORIES_WIDTH, INVERT_LINE_HEIGHT, HEADER_BGCOLOR); + lcdDrawSolidFilledRect(0, y-INVERT_VERT_MARGIN, CATEGORIES_WIDTH, INVERT_LINE_HEIGHT+2, HEADER_BGCOLOR); lcdDrawBitmapPattern(CATEGORIES_WIDTH-12, y, LBM_LIBRARY_CURSOR, MENU_TITLE_COLOR); if (selectMode == MODE_SELECT_CATEGORY) { - drawShadow(0, y-INVERT_VERT_MARGIN, CATEGORIES_WIDTH, INVERT_LINE_HEIGHT); + drawShadow(0, y-INVERT_VERT_MARGIN, CATEGORIES_WIDTH, INVERT_LINE_HEIGHT+2); } } lcdDrawText(MENUS_MARGIN_LEFT, y, name, MENU_TITLE_COLOR); @@ -81,7 +80,7 @@ void drawModel(coord_t x, coord_t y, const char * name, bool selected) lcdDrawBitmap(x+5, y+24, modelBitmap, 0, 0, getBitmapScale(modelBitmap, 64, 32)); } lcdDrawSolidHorizontalLine(x+5, y+19, 143, LINE_COLOR); - if (selectMode == MODE_SELECT_MODEL && selected) { + if (selected) { lcdDrawSolidRect(x, y, MODELCELL_WIDTH, MODELCELL_HEIGHT, TITLE_BGCOLOR); drawShadow(x, y, MODELCELL_WIDTH, MODELCELL_HEIGHT); } @@ -89,8 +88,14 @@ void drawModel(coord_t x, coord_t y, const char * name, bool selected) void onCategorySelectMenu(const char * result) { - if (result == STR_CREATE_CATEGORY) { - storageAppendCategory("Category"); + if (result == STR_CREATE_MODEL) { + storageCheck(true); + createModel(currentCategory); + selectMode = MODE_SELECT_MODEL; + m_posVert = 255; + } + else if (result == STR_CREATE_CATEGORY) { + storageInsertCategory("Category", -1); } else if (result == STR_RENAME_CATEGORY) { selectMode = MODE_RENAME_CATEGORY; @@ -105,6 +110,7 @@ void onModelSelectMenu(const char * result) if (result == STR_SELECT_MODEL) { memcpy(g_eeGeneral.currModelFilename, selectedFilename, LEN_MODEL_FILENAME); storageDirty(EE_GENERAL); + storageCheck(true); loadModel(g_eeGeneral.currModelFilename); chainMenu(menuMainView); } @@ -177,6 +183,7 @@ bool menuModelSelect(evt_t event) case EVT_KEY_LONG(KEY_ENTER): if (selectMode == MODE_SELECT_CATEGORY) { killEvents(event); + MENU_ADD_ITEM(STR_CREATE_MODEL); MENU_ADD_ITEM(STR_CREATE_CATEGORY); MENU_ADD_ITEM(STR_RENAME_CATEGORY); if (currentCategory > 0) @@ -244,8 +251,6 @@ bool menuModelSelect(evt_t event) drawCategory(y, line, currentCategory==index); } } - - y += FH; index++; } @@ -261,15 +266,16 @@ bool menuModelSelect(evt_t event) if (!error) { bool result = storageSeekCategory(&storage, currentCategory); coord_t y = MENU_HEADER_HEIGHT+7; - int index = 0; + int count = 0; while (result) { char line[LEN_MODEL_FILENAME+1]; result = storageReadNextModel(&storage, line, LEN_MODEL_FILENAME); - if (!result) + if (!result) { break; - if (index >= s_pgOfs*2 && index < (s_pgOfs+3)*2) { - bool selected = (m_posVert*2+m_posHorz==index); - if (index & 1) { + } + if (count >= s_pgOfs*2 && count < (s_pgOfs+3)*2) { + bool selected = (selectMode==MODE_SELECT_MODEL && m_posVert*2+m_posHorz==count); + if (count & 1) { drawModel(CATEGORIES_WIDTH+MENUS_MARGIN_LEFT+162, y, line, selected); y += 66; } @@ -280,14 +286,14 @@ bool menuModelSelect(evt_t event) memcpy(selectedFilename, line, sizeof(selectedFilename)); } } - index++; + count++; } if (selectMode == MODE_SELECT_MODEL) { - if (navigate(event, index, 3, 2)) { + if (navigate(event, count, 3, 2)) { putEvent(EVT_REFRESH); } } - drawVerticalScrollbar(DEFAULT_SCROLLBAR_X, MENU_HEADER_HEIGHT+7, MENU_FOOTER_TOP-MENU_HEADER_HEIGHT-15, s_pgOfs, (index+1)/2, 3); + drawVerticalScrollbar(DEFAULT_SCROLLBAR_X, MENU_HEADER_HEIGHT+7, MENU_FOOTER_TOP-MENU_HEADER_HEIGHT-15, s_pgOfs, (count+1)/2, 3); } return true; diff --git a/radio/src/gui/Horus/navigation.cpp b/radio/src/gui/Horus/navigation.cpp index a8aca4edd..1a66707d7 100644 --- a/radio/src/gui/Horus/navigation.cpp +++ b/radio/src/gui/Horus/navigation.cpp @@ -231,6 +231,11 @@ bool navigate(evt_t event, int count, int rows, int columns) int maxlastcol = count-maxrow*columns-1; int maxcol = (m_posVert != maxrow ? columns-1 : maxlastcol); + if (m_posVert > maxrow) { + m_posVert = maxrow; + m_posHorz = maxlastcol; + } + switch (event) { case EVT_KEY_FIRST(KEY_RIGHT): case EVT_KEY_REPT(KEY_RIGHT): diff --git a/radio/src/main_arm.cpp b/radio/src/main_arm.cpp index fe9684d41..fe3116c3c 100644 --- a/radio/src/main_arm.cpp +++ b/radio/src/main_arm.cpp @@ -180,7 +180,12 @@ void guiMain(evt_t evt) } } else { - popupDisplayed = false; + if (popupDisplayed) { + if (evt == 0) { + evt = EVT_REFRESH; + } + popupDisplayed = false; + } refreshNeeded = g_menuStack[g_menuStackPtr](evt); } diff --git a/radio/src/opentx.cpp b/radio/src/opentx.cpp index c5b4e81d8..46dfbde52 100644 --- a/radio/src/opentx.cpp +++ b/radio/src/opentx.cpp @@ -461,7 +461,9 @@ void modelDefault(uint8_t id) #endif #if !defined(EEPROM) - strcpy(g_model.header.name, "\015\361\374\373\364\033\034"); + strcpy(g_model.header.name, "\015\361\374\373\364"); + g_model.header.name[5] = '\033' + id/10; + g_model.header.name[6] = '\033' + id%10; #endif } diff --git a/radio/src/sdcard.cpp b/radio/src/sdcard.cpp index 39bb2fa74..04c30b3c1 100644 --- a/radio/src/sdcard.cpp +++ b/radio/src/sdcard.cpp @@ -106,20 +106,20 @@ char * getFileIndex(char * filename, unsigned int & value) return filename; } -bool findNextFileIndex(char * filename, const char * directory) +int findNextFileIndex(char * filename, const char * directory) { unsigned int index; - char * pos = getFileIndex(filename, index); + char * indexPos = getFileIndex(filename, index); char extension[LEN_FILE_EXTENSION+1]; strncpy(extension, getFileExtension(filename), sizeof(extension)); do { - pos = strAppendNumber(pos, ++index); + char * pos = strAppendNumber(indexPos, ++index); strAppend(pos, extension); if (!isFileAvailable(filename, directory)) { - return true; + return index; } } while (index < 99); - return false; + return 0; } bool sdListFiles(const char *path, const char *extension, const uint8_t maxlen, const char *selection, uint8_t flags=0) diff --git a/radio/src/sdcard.h b/radio/src/sdcard.h index cc21ebf40..fc835b1dc 100644 --- a/radio/src/sdcard.h +++ b/radio/src/sdcard.h @@ -101,7 +101,7 @@ inline const pm_char * SDCARD_ERROR(FRESULT result) #endif bool isFileAvailable(const char * filename); -bool findNextFileIndex(char * filename, const char * directory); +int findNextFileIndex(char * filename, const char * directory); const char * sdCopyFile(const char * src, const char * dest); const char * sdCopyFile(const char * filename, const char * srcDir, const char * destDir); diff --git a/radio/src/storage/sdcard_raw.cpp b/radio/src/storage/sdcard_raw.cpp index 996584cd3..652dfed0c 100644 --- a/radio/src/storage/sdcard_raw.cpp +++ b/radio/src/storage/sdcard_raw.cpp @@ -171,18 +171,13 @@ const char * writeGeneralSettings() void storageCheck(bool immediately) { - // if (immediately) { - // eepromWriteWait(); - // } - if (storageDirtyMsk & EE_GENERAL) { TRACE("eeprom write general"); storageDirtyMsk -= EE_GENERAL; - writeGeneralSettings(); - // if (immediately) - // eepromWriteWait(); - // else - return; + const char * error = writeGeneralSettings(); + if (error) { + TRACE("writeGeneralSettings error=%s", error); + } } if (storageDirtyMsk & EE_MODEL) { @@ -192,8 +187,6 @@ void storageCheck(bool immediately) if (error) { TRACE("writeModel error=%s", error); } - // if (immediately) - // eepromWriteWait(); } } @@ -430,9 +423,9 @@ const char * storageModifyLine(unsigned int operation, int category, int positio return NULL; } -const char * storageAppendCategory(const char * name) +const char * storageInsertCategory(const char * name, int position) { - return storageModifyLine(STORAGE_INSERT_CATEGORY, -1, -1, name); + return storageModifyLine(STORAGE_INSERT_CATEGORY, position, -1, name); } const char * storageRenameCategory(int category, const char * name) @@ -459,3 +452,26 @@ const char * storageRenameModel(const char * name, int category, int position) { return storageModifyLine(STORAGE_RENAME_MODEL, category, position, name); } + +const char * createModel(int category) +{ + preModelLoad(); + + char filename[LEN_MODEL_FILENAME+1]; + memset(filename, 0, sizeof(filename)); + strcpy(filename, "Model.bin"); + + int index = findNextFileIndex(filename, MODELS_PATH); + if (index > 0) { + modelDefault(index); + TRACE("filename=%s", filename); + memcpy(g_eeGeneral.currModelFilename, filename, sizeof(g_eeGeneral.currModelFilename)); + storageDirty(EE_GENERAL); + storageDirty(EE_MODEL); + storageCheck(true); + storageInsertModel(filename, category, -1); + } + postModelLoad(true); + + return NULL; +} diff --git a/radio/src/storage/sdcard_raw.h b/radio/src/storage/sdcard_raw.h index f56163159..6ce72093e 100644 --- a/radio/src/storage/sdcard_raw.h +++ b/radio/src/storage/sdcard_raw.h @@ -39,5 +39,6 @@ const char * readModel(const char * filename, uint8_t * buffer, uint32_t size); const char * loadModel(const char * filename); +const char * createModel(int category);