From 97381249339add583563ca64d12ac062d280c90c Mon Sep 17 00:00:00 2001 From: kisslorand Date: Sun, 16 Apr 2023 13:27:36 +0300 Subject: [PATCH 1/5] MeshEditor mods --- TFT/src/User/Menu/MeshEditor.c | 319 +++++++++++++++------------------ 1 file changed, 148 insertions(+), 171 deletions(-) diff --git a/TFT/src/User/Menu/MeshEditor.c b/TFT/src/User/Menu/MeshEditor.c index eb08da2320..68a2c02a67 100644 --- a/TFT/src/User/Menu/MeshEditor.c +++ b/TFT/src/User/Menu/MeshEditor.c @@ -10,14 +10,6 @@ #define MESH_LINE_EDGE_DISTANCE 4 // data structures -typedef struct -{ - const uint8_t colsToSkip; - const uint8_t rowsToSkip; - const bool rowsInverted; - const char * const echoMsg; -} MESH_DATA_FORMAT; - typedef enum { ME_DATA_IDLE = 0, @@ -32,29 +24,29 @@ typedef struct float oriData[MESH_GRID_SIZE]; // data grid of original Z height float curData[MESH_GRID_SIZE]; // data grid of current Z height - uint16_t dataSize; // number of data present in the data grid - uint16_t colsNum; // number of points per X axis (number of columns in the data grid) - uint16_t rowsNum; // number of points per Y axis (number of rows in the data grid) - - uint16_t index; // current index in the data grid - uint16_t col; // current column in the data grid - uint16_t row; // current row in the data grid - - float valueMin; // minimum data value in the data grid - float valueMax; // maximum data value in the data grid - float valueDelta; // valueMax - valueMin - - uint16_t rStart; // RGB red component for drawing the minimum data value in the data grid - uint16_t gStart; // RGB green component for drawing the minimum data value in the data grid - uint16_t bStart; // RGB blue component for drawing the minimum data value in the data grid - uint16_t rEnd; // RGB green component for drawing the maximum data value in the data grid - uint16_t gEnd; // RGB red component for drawing the maximum data value in the data grid - uint16_t bEnd; // RGB blue component for drawing the maximum data value in the data grid - int16_t rDelta; // rEnd - rStart - int16_t gDelta; // gEnd - gStart - int16_t bDelta; // bEnd - bStart - - MESH_DATA_STATUS status; // current status of oriData/curData + uint16_t dataSize; // number of data present in the data grid + uint16_t colsNum; // number of points per X axis (number of columns in the data grid) + uint16_t rowsNum; // number of points per Y axis (number of rows in the data grid) + + uint16_t index; // current index in the data grid + uint16_t col; // current column in the data grid + uint16_t row; // current row in the data grid + + float valueMin; // minimum data value in the data grid + float valueMax; // maximum data value in the data grid + float valueDelta; // valueMax - valueMin + + uint16_t rStart; // RGB red component for drawing the minimum data value in the data grid + uint16_t gStart; // RGB green component for drawing the minimum data value in the data grid + uint16_t bStart; // RGB blue component for drawing the minimum data value in the data grid + uint16_t rEnd; // RGB green component for drawing the maximum data value in the data grid + uint16_t gEnd; // RGB red component for drawing the maximum data value in the data grid + uint16_t bEnd; // RGB blue component for drawing the maximum data value in the data grid + int16_t rDelta; // rEnd - rStart + int16_t gDelta; // gEnd - gStart + int16_t bDelta; // bEnd - bStart + + MESH_DATA_STATUS status; // current status of oriData/data uint8_t parsedRows; uint8_t colsToSkip; @@ -82,8 +74,8 @@ typedef enum ME_KEY_HOME, ME_KEY_EDIT, ME_KEY_UP, - ME_KEY_PREV, - ME_KEY_NEXT, + ME_KEY_LEFT, + ME_KEY_RIGHT, ME_KEY_DOWN, ME_KEY_NUM, // number of keys ME_KEY_INCREASE = KEY_INCREASE, @@ -174,21 +166,21 @@ const GUI_RECT meshInfoRect[ME_INFO_NUM] = { // current value #ifdef PORTRAIT_MODE - {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 4 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 5 * MESH_KEY_HEIGHT}, // current value + {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 4 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 5 * MESH_KEY_HEIGHT}, // current value #else - {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 2 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 2 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 3 * MESH_KEY_HEIGHT}, // current value + {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 2 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 2 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 3 * MESH_KEY_HEIGHT}, // current value #endif }; const GUI_RECT meshKeyRect[ME_KEY_NUM] = { #ifdef PORTRAIT_MODE - {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 1 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 2 * MESH_KEY_HEIGHT}, // SAVE - {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 0 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 1 * MESH_KEY_HEIGHT}, // OK - {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 2 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 3 * MESH_KEY_HEIGHT}, // RESET - {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 3 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 4 * MESH_KEY_HEIGHT}, // HOME + {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 1 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 2 * MESH_KEY_HEIGHT}, // SAVE + {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 0 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 1 * MESH_KEY_HEIGHT}, // OK + {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 2 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 3 * MESH_KEY_HEIGHT}, // RESET + {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 3 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 4 * MESH_KEY_HEIGHT}, // HOME // current value - {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 4 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 5 * MESH_KEY_HEIGHT}, // current value + {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 4 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 5 * MESH_KEY_HEIGHT}, // current value // arrow keys {MESH_ARROW_X0 + 0 * MESH_ARROW_WIDTH, MESH_ARROW_Y0 + 0 * MESH_ARROW_HEIGHT, MESH_ARROW_X0 + 2 * MESH_ARROW_WIDTH, MESH_ARROW_Y0 + 1 * MESH_ARROW_HEIGHT}, // UP @@ -197,25 +189,25 @@ const GUI_RECT meshKeyRect[ME_KEY_NUM] = { {MESH_ARROW_X0 + 0 * MESH_ARROW_WIDTH, MESH_ARROW_Y0 + 2 * MESH_ARROW_HEIGHT, MESH_ARROW_X0 + 2 * MESH_ARROW_WIDTH, MESH_ARROW_Y0 + 3 * MESH_ARROW_HEIGHT}, // DOWN #else #ifdef KEYBOARD_ON_LEFT - {MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 0 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 2 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 1 * MESH_KEY_HEIGHT}, // SAVE - {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 0 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 1 * MESH_KEY_HEIGHT}, // OK - {MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 1 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 2 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 2 * MESH_KEY_HEIGHT}, // RESET - {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 1 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 2 * MESH_KEY_HEIGHT}, // HOME + {MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 0 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 2 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 1 * MESH_KEY_HEIGHT}, // SAVE + {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 0 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 1 * MESH_KEY_HEIGHT}, // OK + {MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 1 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 2 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 2 * MESH_KEY_HEIGHT}, // RESET + {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 1 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 2 * MESH_KEY_HEIGHT}, // HOME #else - {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 0 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 1 * MESH_KEY_HEIGHT}, // SAVE - {MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 0 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 2 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 1 * MESH_KEY_HEIGHT}, // OK - {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 1 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 2 * MESH_KEY_HEIGHT}, // RESET - {MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 1 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 2 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 2 * MESH_KEY_HEIGHT}, // HOME + {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 0 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 1 * MESH_KEY_HEIGHT}, // SAVE + {MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 0 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 2 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 1 * MESH_KEY_HEIGHT}, // OK + {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 1 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 2 * MESH_KEY_HEIGHT}, // RESET + {MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 1 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 2 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 2 * MESH_KEY_HEIGHT}, // HOME #endif // current value - {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 2 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 2 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 3 * MESH_KEY_HEIGHT}, // EDIT + {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 2 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 2 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 3 * MESH_KEY_HEIGHT}, // EDIT // arrow keys - {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 3 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 2 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 4 * MESH_KEY_HEIGHT}, // UP - {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 4 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 5 * MESH_KEY_HEIGHT}, // PREV - {MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 4 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 2 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 5 * MESH_KEY_HEIGHT}, // NEXT - {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 5 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 2 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 6 * MESH_KEY_HEIGHT}, // DOWN + {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 3 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 2 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 4 * MESH_KEY_HEIGHT}, // UP + {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 4 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 5 * MESH_KEY_HEIGHT}, // PREV + {MESH_KEY_X0 + 1 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 4 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 2 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 5 * MESH_KEY_HEIGHT}, // NEXT + {MESH_KEY_X0 + 0 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 5 * MESH_KEY_HEIGHT, MESH_KEY_X0 + 2 * MESH_KEY_WIDTH, MESH_KEY_Y0 + 6 * MESH_KEY_HEIGHT}, // DOWN #endif }; @@ -240,27 +232,33 @@ const char * const meshKeyString[ME_KEY_NUM] = { "\u02C5", // DOWN }; -const MESH_DATA_FORMAT meshDataFormat[] = { - // columns to skip, rows to skip, rows inverted, bed leveling data type - { 1, 4, true, "Mesh Bed Level data:"}, // MBL - { 0, 2, false, "Bed Topography Report for CSV:"}, // UBL - { 1, 2, true, "Bilinear Leveling Grid:"}, // ABL Bilinear - { 0, 1, true, "Bed Level Correction Matrix:"}, // ABL Linear or 3-Point +const struct +{ + const uint8_t colsToSkip; + const uint8_t rowsToSkip; + const bool rowsInverted; + const char *const echoMsg; +} meshDataFormat[] = { +/* columns to skip, rows to skip, rows inverted, bed leveling data type */ + { 1 , 4 , true , "Mesh Bed Level data:" }, // MBL + { 0 , 2 , false , "Bed Topography Report for CSV:"}, // UBL + { 1 , 2 , true , "Bilinear Leveling Grid:" }, // ABL Bilinear + { 0 , 1 , true , "Bed Level Correction Matrix:" }, // ABL Linear or 3-Point }; const char * meshErrorMsg[] = {"Invalid mesh"}; // list of possible error responses to "M420 V1 T1" command static MESH_DATA * meshData = NULL; -static inline void meshInitData(void) +static void meshInitData(void) { if (meshData == NULL) return; memset(meshData, 0, sizeof(MESH_DATA)); - uint16_t rgbStart = infoSettings.mesh_min_color; // RGB color in RGB 565 16 bit format - uint16_t rgbEnd = infoSettings.mesh_max_color; // RGB color in RGB 565 16 bit format + const uint16_t rgbStart = infoSettings.mesh_min_color; // RGB color in RGB 565 16 bit format + const uint16_t rgbEnd = infoSettings.mesh_max_color; // RGB color in RGB 565 16 bit format meshData->rStart = (rgbStart >> 11) & 0b0000000000011111; meshData->gStart = (rgbStart >> 5) & 0b0000000000111111; @@ -275,7 +273,7 @@ static inline void meshInitData(void) // meshData->status = ME_DATA_IDLE; // init already done by memset() (ME_DATA_IDLE is 0 as the value used by memset()) } -static inline void meshAllocData(void) +static void meshAllocData(void) { // if data doesn't already exist (e.g. when the menu is loaded for the first time), initialize data. // Otherwise, if data already exist (e.g. when the menu is reloaded), continue to use the existing data @@ -288,7 +286,7 @@ static inline void meshAllocData(void) } } -void meshDeallocData(void) +static void meshDeallocData(void) { if (meshData != NULL) { @@ -299,9 +297,9 @@ void meshDeallocData(void) } } -static inline bool processKnownErrorMessage(const char * dataRow) +static bool processKnownErrorMessage(const char *dataRow) { - uint8_t dataCount = COUNT(meshErrorMsg); + const uint8_t dataCount = COUNT(meshErrorMsg); for (uint8_t i = 0; i < dataCount; i++ ) { @@ -312,9 +310,9 @@ static inline bool processKnownErrorMessage(const char * dataRow) return false; } -static inline bool processKnownDataFormat(const char * dataRow) +static bool processKnownDataFormat(const char *dataRow) { - uint8_t dataCount = COUNT(meshDataFormat); + const uint8_t dataCount = COUNT(meshDataFormat); for (uint8_t i = 0; i < dataCount; i++ ) { @@ -361,10 +359,10 @@ void meshSaveCallback(void) saveEepromSettings(); if (meshData != NULL) // if data have not been released (e.g. data are released when mesh editor menu is forced to exit) - memcpy(meshData->oriData, meshData->curData, sizeof(meshData->curData)); // align data only after save confirmation + memcpy(meshData->oriData, meshData->curData, sizeof(meshData->oriData)); // sync mesh data } -static inline void meshUpdateIndex(const MESH_KEY_VALUES key_num) +static void meshUpdateIndex(MESH_KEY_VALUES key_num) { switch (key_num) { @@ -376,22 +374,23 @@ static inline void meshUpdateIndex(const MESH_KEY_VALUES key_num) meshData->row = (meshData->row + 1) % meshData->rowsNum; break; - case ME_KEY_PREV: case ME_KEY_DECREASE: - case ME_KEY_NEXT: - case ME_KEY_INCREASE: - { - uint16_t index; + // in case of decreasing with the rotary encoder at the start of column the row must change also + if (meshData->col == 0) + meshData->row = (meshData->rowsNum + meshData->row - 1) % meshData->rowsNum; - if (key_num == ME_KEY_PREV || key_num == ME_KEY_DECREASE) - index = meshData->index > 0 ? meshData->index - 1 : meshData->dataSize - 1; - else - index = meshData->index < meshData->dataSize - 1 ? meshData->index + 1 : 0; + case ME_KEY_LEFT: + meshData->col = (meshData->colsNum + meshData->col - 1) % meshData->colsNum; + break; - meshData->col = index % meshData->colsNum; - meshData->row = index / meshData->colsNum; + case ME_KEY_INCREASE: + // in case of increasing with the rotary encoder at the end of column the row must change also + if (meshData->col == meshData->colsNum - 1) + meshData->row = (meshData->row + 1) % meshData->rowsNum; + + case ME_KEY_RIGHT: + meshData->col = (meshData->col + 1) % meshData->colsNum; break; - } default: break; @@ -400,62 +399,46 @@ static inline void meshUpdateIndex(const MESH_KEY_VALUES key_num) meshData->index = meshData->row * meshData->colsNum + meshData->col; } -uint16_t meshGetJ(void) +static bool meshSetValue(const float newMeshData) { - // J index (independent by data format) to be used by G42 (mesh tuner menu) and M421 (meshSetValue() function). - // Bed's top left point -> J = max row index - // Bed's bottom left point -> J = 0 - return (meshData->rowsNum - 1) - meshData->row; -} - -bool meshSetValue(const float value) -{ - if (meshData->curData[meshData->index] != value) // if mesh value is changed + if (meshData->curData[meshData->index] != newMeshData) { - meshData->curData[meshData->index] = value; // set new mesh value - - mustStoreCmd("M421 I%d J%d Z%.3f\n", meshData->col, meshGetJ(), value); // send new mesh value - + mustStoreCmd("M421 I%d J%d Z%.3f\n", (meshData->rowsNum - 1) - meshData->row, newMeshData); // (meshData->rowsNum - 1) - meshData->row -> real row in eeprom + meshData->curData[meshData->index] = newMeshData; return true; } return false; } -static inline void meshUpdateValueMinMax(void) +static void meshUpdateValueMinMax(void) { - float value; - - meshData->valueMin = meshData->valueMax = meshData->curData[0]; // init initial min/max values + meshData->valueMin = meshData->valueMax = meshData->curData[0]; for (uint16_t i = 0; i < meshData->dataSize; i++) { - value = meshData->curData[i]; - - if (value < meshData->valueMin) - meshData->valueMin = value; - else if (value > meshData->valueMax) - meshData->valueMax = value; + if(meshData->curData[i] < meshData->valueMin) + meshData->valueMin = meshData->curData[i]; + else if(meshData->curData[i] > meshData->valueMax) + meshData->valueMax = meshData->curData[i]; } meshData->valueDelta = meshData->valueMax - meshData->valueMin; } -uint16_t mapRGBdata(const uint16_t * rgbStart, const int16_t * rgbDelta, const float * valueDiff, const float * valueDelta) +static uint16_t __attribute__ ((noinline)) mapRGBdata(const uint16_t * rgbStart, const int16_t * rgbDelta, const float * valueDiff, const float * valueDelta) { return *rgbStart + (*valueDiff * *rgbDelta) / *valueDelta; } -uint16_t meshGetRGBColor(const float value) +static uint16_t meshGetRGBColor(const float value) { if (meshData->valueDelta == 0) return (meshData->rStart << 11) | (meshData->gStart << 5) | (meshData->bStart); - float valueDiff; + const float valueDiff = value - meshData->valueMin; uint16_t r, g, b; - valueDiff = value - meshData->valueMin; - r = mapRGBdata(&meshData->rStart, &meshData->rDelta, &valueDiff, &meshData->valueDelta); g = mapRGBdata(&meshData->gStart, &meshData->gDelta, &valueDiff, &meshData->valueDelta); b = mapRGBdata(&meshData->bStart, &meshData->bDelta, &valueDiff, &meshData->valueDelta); @@ -463,16 +446,16 @@ uint16_t meshGetRGBColor(const float value) return (r << 11) | (g << 5) | (b); } -void meshDrawGridCell(const uint16_t index, const bool clearBgColor) +static void meshDrawGridCell(const uint16_t index, const bool clearBgColor) { - float value = meshData->curData[index]; - uint16_t col = index % meshData->colsNum; - uint16_t row = index / meshData->colsNum; - uint16_t cellWidth = MESH_GRID_WIDTH / meshData->colsNum; - uint16_t cellHeight = MESH_GRID_HEIGHT / meshData->rowsNum; + const float value = meshData->curData[index]; + const uint16_t col = index % meshData->colsNum; + const uint16_t row = index / meshData->colsNum; + const uint16_t cellWidth = MESH_GRID_WIDTH / meshData->colsNum; + const uint16_t cellHeight = MESH_GRID_HEIGHT / meshData->rowsNum; uint16_t radius = MIN(cellWidth, cellHeight); - uint16_t rect_x0 = meshGridRect.x0 + col * cellWidth; - uint16_t rect_y0 = meshGridRect.y0 + row * cellHeight; + const uint16_t rect_x0 = meshGridRect.x0 + col * cellWidth; + const uint16_t rect_y0 = meshGridRect.y0 + row * cellHeight; if (index == meshData->index) radius /= MESH_POINT_MIN_RATIO; @@ -500,7 +483,7 @@ void meshDrawGridCell(const uint16_t index, const bool clearBgColor) GUI_SetColor(MESH_FONT_COLOR); } -void meshDrawGrid(void) +static void meshDrawGrid(void) { meshUpdateValueMinMax(); @@ -512,7 +495,7 @@ void meshDrawGrid(void) } } -void meshDrawInfo(const bool drawMinMax) +static void meshDrawInfo(const bool drawMinMax) { if (drawMinMax) { @@ -524,7 +507,7 @@ void meshDrawInfo(const bool drawMinMax) drawStandardValue(&meshInfoRect[ME_INFO_CUR], VALUE_FLOAT, &meshData->curData[meshData->index], FONT_SIZE_LARGE, MESH_FONT_COLOR, MESH_BORDER_COLOR, 4, true); } -static inline void meshDrawButton(const uint8_t index, const uint8_t isPressed) +static void meshDrawButton(const uint8_t index, const uint8_t isPressed) { if (index >= ME_KEY_NUM) return; @@ -533,17 +516,17 @@ static inline void meshDrawButton(const uint8_t index, const uint8_t isPressed) if (isPressed) { - if (index != ME_KEY_EDIT) - color = MESH_BORDER_COLOR; - else + if (index == ME_KEY_EDIT) color = MESH_FONT_COLOR; + else + color = MESH_BORDER_COLOR; } else { - if (index != ME_KEY_EDIT) - color = MESH_BG_COLOR; - else + if (index == ME_KEY_EDIT) color = MESH_BORDER_COLOR; + else + color = MESH_BG_COLOR; } drawBorder(&meshKeyRect[index], color, 1); @@ -552,7 +535,7 @@ static inline void meshDrawButton(const uint8_t index, const uint8_t isPressed) GUI_SetColor(MESH_FONT_COLOR); } -static inline void meshDrawKeyboard(void) +static void meshDrawKeyboard(void) { // draw buttons GUI_SetTextMode(GUI_TEXTMODE_TRANS); @@ -575,7 +558,7 @@ static inline void meshDrawKeyboard(void) GUI_RestoreColorDefault(); } -void meshDrawMenu(void) +static void meshDrawMenu(void) { setMenu(MENU_TYPE_FULLSCREEN, NULL, COUNT(meshKeyRect), meshKeyRect, meshDrawButton, &meshDrawMenu); @@ -609,13 +592,13 @@ void meshDrawMenu(void) meshDrawInfo(true); } -void meshSave(void) +static void meshSave(void) { if (infoMachineSettings.EEPROM == 1) popupDialog(DIALOG_TYPE_QUESTION, (uint8_t *) meshData->saveTitle, LABEL_EEPROM_SAVE_INFO, LABEL_CONFIRM, LABEL_CANCEL, meshSaveCallback, NULL, NULL); } -static inline bool meshIsWaitingFirstData(void) +static bool meshIsWaitingFirstData(void) { // just avoid to merge on the same "if" statement a check on NULL value and an access // to attributes of the data structure meshData due to different compiler optimization // settings (that could evaluate all the conditions in the "if" statement, causing a crash) @@ -641,7 +624,7 @@ bool meshIsWaitingData(void) return true; } -uint16_t meshParseDataRow(char * dataRow, float * dataGrid, const uint16_t maxCount) +static uint16_t meshParseDataRow(char * dataRow, float * dataGrid, const uint16_t maxCount) { if (meshData->parsedRows < meshData->rowsToSkip) return 0; @@ -673,7 +656,7 @@ uint16_t meshParseDataRow(char * dataRow, float * dataGrid, const uint16_t maxCo return count; } -static inline void processGridData(void) +static void processGridData(void) { if (meshData->rowsInverted) // store grid data with the rows in inverse order { @@ -686,7 +669,7 @@ static inline void processGridData(void) memcpy(meshData->oriData, meshData->curData, sizeof(meshData->curData)); // copy the inversed rows data back to oriData } - else // copy oriData to curData + else // store grid data with the rows in normal order { memcpy(meshData->curData, meshData->oriData, sizeof(meshData->oriData)); } @@ -727,7 +710,7 @@ void meshUpdateData(char * dataRow) if (meshData->status == ME_DATA_EMPTY) // if data grid is empty, parse the data row and set the data grid columns number { - count = meshParseDataRow(dataRow, &(meshData->oriData[0]), MESH_GRID_MAX_POINTS_X); + count = meshParseDataRow(dataRow, meshData->oriData, MESH_GRID_MAX_POINTS_X); if (count > 0) // if number of columns in the parsed data row is at least 1, set the data grid columns number { @@ -771,12 +754,11 @@ void meshUpdateData(char * dataRow) if (failed) { - meshData->status = ME_DATA_FAILED; - char tempMsg[MAX_STRING_LENGTH]; - snprintf(tempMsg, MAX_STRING_LENGTH, "%s\n-> %s", textSelect(LABEL_PROCESS_ABORTED), dataRow); + meshData->status = ME_DATA_FAILED; + snprintf(tempMsg, MAX_STRING_LENGTH, "%s\n-> %s", textSelect(LABEL_PROCESS_ABORTED), dataRow); popupReminder(DIALOG_TYPE_ERROR, LABEL_MESH_EDITOR, (uint8_t *) tempMsg); // trigger exit from mesh editor menu. It avoids to loop in case of persistent error @@ -787,7 +769,6 @@ void meshUpdateData(char * dataRow) void menuMeshEditor(void) { MESH_KEY_VALUES key_num = ME_KEY_IDLE; - bool forceExit = false; uint16_t oldIndex; uint16_t curIndex; @@ -818,25 +799,26 @@ void menuMeshEditor(void) { case ME_KEY_UP: case ME_KEY_DOWN: - case ME_KEY_PREV: + case ME_KEY_LEFT: case ME_KEY_DECREASE: - case ME_KEY_NEXT: + case ME_KEY_RIGHT: case ME_KEY_INCREASE: meshUpdateIndex(key_num); break; case ME_KEY_EDIT: - if (coordinateIsKnown() == false) - probeHeightHome(); // home, disable ABL and raise nozzle + { + if (coordinateIsKnown() == false) + probeHeightHome(); // home, disable ABL and raise nozzle - // call mesh tuner menu and set current mesh value, if changed - meshSetValue(menuMeshTuner(meshData->col, meshGetJ(), meshData->curData[curIndex])); + meshSetValue(menuMeshTuner(meshData->col, (meshData->rowsNum - 1) - meshData->row, meshData->curData[curIndex])); // (meshData->rowsNum - 1) - meshData->row -> real row in eeprom - meshDrawMenu(); + meshDrawMenu(); + } break; case ME_KEY_RESET: - if (meshSetValue(meshData->oriData[curIndex])) // set current mesh value to original value, if changed + if (meshSetValue(meshData->oriData[curIndex])) { meshDrawGrid(); meshDrawInfo(true); @@ -848,41 +830,36 @@ void menuMeshEditor(void) break; case ME_KEY_SAVE: - // we unconditionally allow to save the meshes on EEPROM (e.g. just in case we previously edited some - // meshes but we closed the mesh editor menu refusing to save) - meshSave(); - break; - - case ME_KEY_OK: - forceExit = true; - - CLOSE_MENU(); + meshSave(); // save mesh changes to eeprom (if eeprom exists) break; default: break; } - if (curIndex != meshData->index) + if (key_num == ME_KEY_OK) { - curIndex = meshData->index; - - meshDrawGridCell(oldIndex, true); // draw point with old index - meshDrawGridCell(curIndex, true); // draw point with new index - meshDrawInfo(false); + if (memcmp(meshData->oriData, meshData->curData, sizeof(meshData->oriData))) + meshSave(); // save mesh changes to eeprom (if eeprom exists) - oldIndex = curIndex; + meshDeallocData(); + CLOSE_MENU(); } + else + { + if (curIndex != meshData->index) + { + curIndex = meshData->index; - loopProcess(); - } + meshDrawGridCell(oldIndex, true); // draw point with old index + meshDrawGridCell(curIndex, true); // draw point with new index + meshDrawInfo(false); - if (forceExit) - { - if (memcmp(meshData->oriData, meshData->curData, sizeof(meshData->curData))) // check for changes - meshSave(); + oldIndex = curIndex; + } - meshDeallocData(); // finally, deallocate mesh data (meshData no more accessible) + loopProcess(); + } } // restore default From a610440bfac5ffe45d95c696f061bc99c57d9c7b Mon Sep 17 00:00:00 2001 From: kisslorand Date: Tue, 18 Apr 2023 19:12:12 +0300 Subject: [PATCH 2/5] Restoration --- TFT/src/User/API/BabystepControl.c | 2 +- TFT/src/User/API/BabystepControl.h | 2 +- TFT/src/User/API/Notification.c | 6 +- TFT/src/User/API/Vfs/vfs.c | 10 +-- TFT/src/User/API/interfaceCmd.c | 6 +- TFT/src/User/API/parseACK.c | 6 +- TFT/src/User/Fatfs/myfatfs.c | 2 +- TFT/src/User/Menu/ABL.c | 4 +- TFT/src/User/Menu/BedLeveling.c | 4 +- TFT/src/User/Menu/MBL.c | 4 +- TFT/src/User/Menu/Pid.c | 6 +- TFT/src/User/Menu/Popup.c | 16 ++--- TFT/src/User/Menu/PrintingMenu.c | 8 +-- TFT/src/User/Menu/StatusScreen.c | 8 +-- TFT/src/User/Menu/ZOffset.c | 4 +- TFT/src/User/my_misc.c | 110 ++++++++++++++++------------- TFT/src/User/my_misc.h | 23 +++--- 17 files changed, 116 insertions(+), 105 deletions(-) diff --git a/TFT/src/User/API/BabystepControl.c b/TFT/src/User/API/BabystepControl.c index b033b19ae3..e6d337ff5f 100644 --- a/TFT/src/User/API/BabystepControl.c +++ b/TFT/src/User/API/BabystepControl.c @@ -7,7 +7,7 @@ static float babystep_value = BABYSTEP_DEFAULT_VALUE; #define BABYSTEP_CMD_SMW "G43.2 Z%.2f\n" // Set current babystep value -void babystepSetValue(float value) +void babystepSetValue(const float value) { babystep_value = value; } diff --git a/TFT/src/User/API/BabystepControl.h b/TFT/src/User/API/BabystepControl.h index d30bce8a08..9a6952f327 100644 --- a/TFT/src/User/API/BabystepControl.h +++ b/TFT/src/User/API/BabystepControl.h @@ -8,7 +8,7 @@ extern "C" { #include // Set current babystep value -void babystepSetValue(float value); +void babystepSetValue(const float value); // Get current babystep value float babystepGetValue(void); diff --git a/TFT/src/User/API/Notification.c b/TFT/src/User/API/Notification.c index dededf3055..e28cb90754 100644 --- a/TFT/src/User/API/Notification.c +++ b/TFT/src/User/API/Notification.c @@ -25,7 +25,7 @@ void addToast(DIALOG_TYPE style, char * text) LCD_WAKE(); TOAST t; - strncpy_no_pad(t.text, text, TOAST_MSG_LENGTH); + strscpy(t.text, text, TOAST_MSG_LENGTH); t.style = style; t.isNew = true; toastlist[nextToastIndex] = t; @@ -146,8 +146,8 @@ void addNotification(DIALOG_TYPE style, char *title, char *text, bool ShowDialog // store message msglist[nextMsgIndex].style = style; - strncpy_no_pad(msglist[nextMsgIndex].text, text, MAX_MSG_LENGTH); - strncpy_no_pad(msglist[nextMsgIndex].title, title, MAX_MSG_TITLE_LENGTH); + strscpy(msglist[nextMsgIndex].text, text, MAX_MSG_LENGTH); + strscpy(msglist[nextMsgIndex].title, title, MAX_MSG_TITLE_LENGTH); if (ShowDialog && MENU_IS_NOT(menuNotification)) popupReminder(style, (uint8_t *)title, (uint8_t *)msglist[nextMsgIndex].text); diff --git a/TFT/src/User/API/Vfs/vfs.c b/TFT/src/User/API/Vfs/vfs.c index ca84e07391..9d90ea8a6c 100644 --- a/TFT/src/User/API/Vfs/vfs.c +++ b/TFT/src/User/API/Vfs/vfs.c @@ -217,7 +217,7 @@ bool addFile(bool isFile, const char * shortName, const char * longName) if (sName == NULL) // in case of error, exit return false; - strncpy_pad(sName, shortName, sNameLen); // copy to "sName" and set to NULL the flag for filename extension check, if any + strxcpy(sName, shortName, sNameLen); // copy to "sName" and set to NULL the flag for filename extension check, if any // // get long name, if any @@ -237,7 +237,7 @@ bool addFile(bool isFile, const char * shortName, const char * longName) return false; } - strncpy_pad(lName, longName, lNameLen); // copy to "lName" and set to NULL the flag for filename extension check, if any + strxcpy(lName, longName, lNameLen); // copy to "lName" and set to NULL the flag for filename extension check, if any } // @@ -286,8 +286,8 @@ char * hideExtension(char * filename) // if filename provides a supported filename extension then // check extra byte for filename extension check. If 0, no filename extension was previously hidden - if (extPos != NULL && filename[strlen(filename) + 1] == '\0') - filename[extPos - filename] = 0; // temporary hide filename extension + if (extPos != NULL && strchr(filename, '\0')[1] == '\0') + *extPos = 0; // temporary hide filename extension } return filename; @@ -357,7 +357,7 @@ bool getPrintTitle(char * buf, uint8_t len) return false; } - strncpy_pad(buf, getFS(), len); // set source and set the flag for filename extension check + strxcpy(buf, getFS(), len); // set source and set the flag for filename extension check strcat(buf, strPtr); // append filename hideExtension(buf); // hide filename extension if filename extension feature is disabled diff --git a/TFT/src/User/API/interfaceCmd.c b/TFT/src/User/API/interfaceCmd.c index cfe3522a92..9ef89f90ab 100644 --- a/TFT/src/User/API/interfaceCmd.c +++ b/TFT/src/User/API/interfaceCmd.c @@ -161,7 +161,7 @@ bool storeCmdFromUART(const CMD cmd, const SERIAL_PORT_INDEX portIndex) return false; } - strncpy_no_pad(cmdQueue.queue[cmdQueue.index_w].gcode, cmd, CMD_MAX_SIZE); + strscpy(cmdQueue.queue[cmdQueue.index_w].gcode, cmd, CMD_MAX_SIZE); cmdQueue.queue[cmdQueue.index_w].port_index = portIndex; cmdQueue.index_w = (cmdQueue.index_w + 1) % CMD_QUEUE_SIZE; @@ -845,7 +845,7 @@ void sendQueueCmd(void) bool hasE, hasA; // make a copy to work on - strncpy_no_pad(rawMsg, &cmd_ptr[cmd_base_index + 4], CMD_MAX_SIZE); + strscpy(rawMsg, &cmd_ptr[cmd_base_index + 4], CMD_MAX_SIZE); // retrieve message text and flags of M118 gcode msgText = parseM118(rawMsg, &hasE, &hasA); @@ -992,7 +992,7 @@ void sendQueueCmd(void) const char * msgText; // make a copy to work on - strncpy_no_pad(rawMsg, &cmd_ptr[cmd_base_index + 4], CMD_MAX_SIZE); + strscpy(rawMsg, &cmd_ptr[cmd_base_index + 4], CMD_MAX_SIZE); // retrieve message text stripChecksum(rawMsg); diff --git a/TFT/src/User/API/parseACK.c b/TFT/src/User/API/parseACK.c index d43a332caf..621228ca88 100644 --- a/TFT/src/User/API/parseACK.c +++ b/TFT/src/User/API/parseACK.c @@ -764,11 +764,11 @@ void parseACK(void) sprintf(tmpMsg, "Mean: %0.5f", ack_value()); if (ack_continue_seen("Min: ")) - sprintf(&tmpMsg[strlen(tmpMsg)], "\nMin: %0.5f", ack_value()); + sprintf(strchr(tmpMsg, '\0'), "\nMin: %0.5f", ack_value()); if (ack_continue_seen("Max: ")) - sprintf(&tmpMsg[strlen(tmpMsg)], "\nMax: %0.5f", ack_value()); + sprintf(strchr(tmpMsg, '\0'), "\nMax: %0.5f", ack_value()); if (ack_continue_seen("Range: ")) - sprintf(&tmpMsg[strlen(tmpMsg)], "\nRange: %0.5f", ack_value()); + sprintf(strchr(tmpMsg, '\0'), "\nRange: %0.5f", ack_value()); popupReminder(DIALOG_TYPE_INFO, (uint8_t *)"Repeatability Test", (uint8_t *)tmpMsg); } diff --git a/TFT/src/User/Fatfs/myfatfs.c b/TFT/src/User/Fatfs/myfatfs.c index 7e79bb917b..4acf99e710 100644 --- a/TFT/src/User/Fatfs/myfatfs.c +++ b/TFT/src/User/Fatfs/myfatfs.c @@ -211,7 +211,7 @@ bool f_remove_full_dir(const TCHAR* path) char dirBuffer[BUFFER_SIZE]; FILINFO tmpInfo; - strncpy_no_pad(dirBuffer, path, BUFFER_SIZE); + strscpy(dirBuffer, path, BUFFER_SIZE); if (f_remove_node(dirBuffer, BUFFER_SIZE, &tmpInfo) == FR_OK) { return true; diff --git a/TFT/src/User/Menu/ABL.c b/TFT/src/User/Menu/ABL.c index c71e201433..6ccabe526c 100644 --- a/TFT/src/User/Menu/ABL.c +++ b/TFT/src/User/Menu/ABL.c @@ -24,7 +24,7 @@ void ablUpdateStatus(bool succeeded) savingEnabled = false; tempTitle.index = LABEL_ABL_SETTINGS_UBL; - sprintf(&tempMsg[strlen(tempMsg)], "\n %s", textSelect(LABEL_BL_SMART_FILL)); + sprintf(strchr(tempMsg, '\0'), "\n %s", textSelect(LABEL_BL_SMART_FILL)); break; default: @@ -37,7 +37,7 @@ void ablUpdateStatus(bool succeeded) if (savingEnabled && infoMachineSettings.EEPROM == 1) { - sprintf(&tempMsg[strlen(tempMsg)], "\n %s", textSelect(LABEL_EEPROM_SAVE_INFO)); + sprintf(strchr(tempMsg, '\0'), "\n %s", textSelect(LABEL_EEPROM_SAVE_INFO)); popupDialog(DIALOG_TYPE_SUCCESS, tempTitle.index, (uint8_t *) tempMsg, LABEL_CONFIRM, LABEL_CANCEL, saveEepromSettings, NULL, NULL); } diff --git a/TFT/src/User/Menu/BedLeveling.c b/TFT/src/User/Menu/BedLeveling.c index 3f69099e07..f2284cffe0 100644 --- a/TFT/src/User/Menu/BedLeveling.c +++ b/TFT/src/User/Menu/BedLeveling.c @@ -123,8 +123,8 @@ void menuBedLeveling(void) case KEY_ICON_3: if (levelStateNew != UNDEFINED) storeCmd((levelStateNew == ENABLED) ? - (infoMachineSettings.firmwareType != FW_REPRAPFW ? "M420 S0\n" : "G29 S2\n") : - (infoMachineSettings.firmwareType != FW_REPRAPFW ? "M420 S1\n" : "G29 S1\n")); + (infoMachineSettings.firmwareType == FW_MARLIN ? "M420 S0\n" : "G29 S2\n") : + (infoMachineSettings.firmwareType == FW_MARLIN ? "M420 S1\n" : "G29 S1\n")); break; diff --git a/TFT/src/User/Menu/MBL.c b/TFT/src/User/Menu/MBL.c index 51a1ebb5a5..47a518e948 100644 --- a/TFT/src/User/Menu/MBL.c +++ b/TFT/src/User/Menu/MBL.c @@ -63,7 +63,7 @@ void mblUpdateStatus(bool succeeded) if (infoMachineSettings.EEPROM == 1) { - sprintf(&tempMsg[strlen(tempMsg)], "\n %s", textSelect(LABEL_EEPROM_SAVE_INFO)); + sprintf(strchr(tempMsg, '\0'), "\n %s", textSelect(LABEL_EEPROM_SAVE_INFO)); popupDialog(DIALOG_TYPE_SUCCESS, LABEL_MBL_SETTINGS, (uint8_t *) tempMsg, LABEL_CONFIRM, LABEL_CANCEL, saveEepromSettings, NULL, NULL); } @@ -85,7 +85,7 @@ void mblNotifyError(bool isStarted) { LABELCHAR(tempMsg, LABEL_MBL); - sprintf(&tempMsg[strlen(tempMsg)], " %s", isStarted ? textSelect(LABEL_ON) : textSelect(LABEL_OFF)); + sprintf(strchr(tempMsg, '\0'), " %s", isStarted ? textSelect(LABEL_ON) : textSelect(LABEL_OFF)); addToast(DIALOG_TYPE_ERROR, tempMsg); } diff --git a/TFT/src/User/Menu/Pid.c b/TFT/src/User/Menu/Pid.c index 62c5399361..4faf41d79a 100644 --- a/TFT/src/User/Menu/Pid.c +++ b/TFT/src/User/Menu/Pid.c @@ -64,7 +64,7 @@ void pidResultAction(void) memset(pidHeaterTarget, 0, sizeof(pidHeaterTarget)); // reset pidHeaterTarget[] to 0 LABELCHAR(tempMsg, LABEL_TIMEOUT_REACHED); - sprintf(&tempMsg[strlen(tempMsg)], "\n %s", textSelect(LABEL_BUSY)); + sprintf(strchr(tempMsg, '\0'), "\n %s", textSelect(LABEL_BUSY)); BUZZER_PLAY(SOUND_NOTIFY); popupReminder(DIALOG_TYPE_ALERT, LABEL_PID_TITLE, (uint8_t *) tempMsg); } @@ -87,7 +87,7 @@ void pidResultAction(void) if (infoMachineSettings.EEPROM == 1) { - sprintf(&tempMsg[strlen(tempMsg)], "\n %s", textSelect(LABEL_EEPROM_SAVE_INFO)); + sprintf(strchr(tempMsg, '\0'), "\n %s", textSelect(LABEL_EEPROM_SAVE_INFO)); popupDialog(DIALOG_TYPE_SUCCESS, LABEL_PID_TITLE, (uint8_t *) tempMsg, LABEL_CONFIRM, LABEL_CANCEL, saveEepromSettings, NULL, NULL); } @@ -101,7 +101,7 @@ void pidResultAction(void) #ifdef ENABLE_PID_STATUS_UPDATE_NOTIFICATION LABELCHAR(tempMsg, LABEL_PID_TITLE); - sprintf(&tempMsg[strlen(tempMsg)], " %s", textSelect(LABEL_PROCESS_COMPLETED)); + sprintf(strchr(tempMsg, '\0'), " %s", textSelect(LABEL_PROCESS_COMPLETED)); BUZZER_PLAY(SOUND_NOTIFY); addToast(DIALOG_TYPE_INFO, tempMsg); #endif diff --git a/TFT/src/User/Menu/Popup.c b/TFT/src/User/Menu/Popup.c index ca07b9ad17..9aba602688 100644 --- a/TFT/src/User/Menu/Popup.c +++ b/TFT/src/User/Menu/Popup.c @@ -126,12 +126,12 @@ void menuDialog(void) void _setDialogTitleStr(uint8_t * str) { - strncpy_no_pad((char *)popup_title, (char *)str, sizeof(popup_title)); + strscpy((char *)popup_title, (char *)str, sizeof(popup_title)); } void _setDialogMsgStr(uint8_t * str) { - strncpy_no_pad((char *)popup_msg, (char *)str, sizeof(popup_msg)); + strscpy((char *)popup_msg, (char *)str, sizeof(popup_msg)); } uint8_t *getDialogMsgStr() @@ -141,40 +141,40 @@ uint8_t *getDialogMsgStr() void _setDialogOkTextStr(uint8_t * str) { - strncpy_no_pad((char *)popup_ok, (char *)str, sizeof(popup_ok)); + strscpy((char *)popup_ok, (char *)str, sizeof(popup_ok)); } void _setDialogCancelTextStr(uint8_t * str) { - strncpy_no_pad((char *)popup_cancel, (char *)str, sizeof(popup_cancel)); + strscpy((char *)popup_cancel, (char *)str, sizeof(popup_cancel)); } void _setDialogTitleLabel(int16_t index) { uint8_t tempstr[MAX_LANG_LABEL_LENGTH] = {0}; loadLabelText(tempstr, index); - strncpy_no_pad((char *)popup_title, (char *)tempstr, sizeof(popup_title)); + strscpy((char *)popup_title, (char *)tempstr, sizeof(popup_title)); } void _setDialogMsgLabel(int16_t index) { uint8_t tempstr[MAX_LANG_LABEL_LENGTH] = {0}; loadLabelText(tempstr, index); - strncpy_no_pad((char *)popup_msg, (char *)tempstr, sizeof(popup_msg)); + strscpy((char *)popup_msg, (char *)tempstr, sizeof(popup_msg)); } void _setDialogOkTextLabel(int16_t index) { uint8_t tempstr[MAX_LANG_LABEL_LENGTH] = {0}; loadLabelText(tempstr, index); - strncpy_no_pad((char *)popup_ok, (char *)tempstr, sizeof(popup_ok)); + strscpy((char *)popup_ok, (char *)tempstr, sizeof(popup_ok)); } void _setDialogCancelTextLabel(int16_t index) { uint8_t tempstr[MAX_LANG_LABEL_LENGTH] = {0}; loadLabelText(tempstr, index); - strncpy_no_pad((char *)popup_cancel, (char *)tempstr, sizeof(popup_cancel)); + strscpy((char *)popup_cancel, (char *)tempstr, sizeof(popup_cancel)); } /** diff --git a/TFT/src/User/Menu/PrintingMenu.c b/TFT/src/User/Menu/PrintingMenu.c index ad8338d2dd..b12b6e4118 100644 --- a/TFT/src/User/Menu/PrintingMenu.c +++ b/TFT/src/User/Menu/PrintingMenu.c @@ -230,7 +230,7 @@ static void reDrawPrintingValue(uint8_t icon_pos, uint8_t draw_type) if ((getPrintRemainingTime() == 0) || (progDisplayType != ELAPSED_REMAINING)) snprintf(tempstrTop, 9, "%d%% ", getPrintProgress()); else - time_2_string(tempstrTop, TIME_FORMAT_STR, getPrintTime()); + timeToString(tempstrTop, TIME_FORMAT_STR, getPrintTime()); break; case ICON_POS_Z: @@ -287,9 +287,9 @@ static void reDrawPrintingValue(uint8_t icon_pos, uint8_t draw_type) case ICON_POS_TIM: if ((getPrintRemainingTime() == 0) || (progDisplayType == PERCENTAGE_ELAPSED)) - time_2_string(tempstrBottom, TIME_FORMAT_STR, getPrintTime()); + timeToString(tempstrBottom, TIME_FORMAT_STR, getPrintTime()); else - time_2_string(tempstrBottom, TIME_FORMAT_STR, getPrintRemainingTime()); + timeToString(tempstrBottom, TIME_FORMAT_STR, getPrintRemainingTime()); break; case ICON_POS_Z: @@ -440,7 +440,7 @@ void printSummaryPopup(void) char showInfo[300]; char tempstr[60]; - time_2_string(showInfo, (char *)textSelect(LABEL_PRINT_TIME), infoPrintSummary.time); + timeToString(showInfo, (char *)textSelect(LABEL_PRINT_TIME), infoPrintSummary.time); if (isAborted() == true) { diff --git a/TFT/src/User/Menu/StatusScreen.c b/TFT/src/User/Menu/StatusScreen.c index 8d703aceae..1c3e36f787 100644 --- a/TFT/src/User/Menu/StatusScreen.c +++ b/TFT/src/User/Menu/StatusScreen.c @@ -202,17 +202,17 @@ void drawStatus(void) void statusScreen_setMsg(const uint8_t *title, const uint8_t *msg) { - strncpy_no_pad(msgTitle, (char *)title, sizeof(msgTitle)); - strncpy_no_pad(msgBody, (char *)msg, sizeof(msgBody)); + strscpy(msgTitle, (char *)title, sizeof(msgTitle)); + strscpy(msgBody, (char *)msg, sizeof(msgBody)); msgNeedRefresh = true; } void statusScreen_setReady(void) { - strncpy_no_pad(msgTitle, (char *)textSelect(LABEL_STATUS), sizeof(msgTitle)); + strscpy(msgTitle, (char *)textSelect(LABEL_STATUS), sizeof(msgTitle)); if (infoHost.connected == false) - strncpy_no_pad(msgBody, (char *)textSelect(LABEL_UNCONNECTED), sizeof(msgBody)); + strscpy(msgBody, (char *)textSelect(LABEL_UNCONNECTED), sizeof(msgBody)); else snprintf(msgBody, sizeof(msgBody), "%s %s", (char *)machine_type, (char *)textSelect(LABEL_READY)); diff --git a/TFT/src/User/Menu/ZOffset.c b/TFT/src/User/Menu/ZOffset.c index 9af7dc88c4..2e1c3530f1 100644 --- a/TFT/src/User/Menu/ZOffset.c +++ b/TFT/src/User/Menu/ZOffset.c @@ -14,9 +14,9 @@ void zOffsetNotifyError(bool isStarted) sprintf(tempMsg, "%s", textSelect(LABEL_HOME_OFFSET)); if (!isStarted) - sprintf(&tempMsg[strlen(tempMsg)], " %s", textSelect(LABEL_OFF)); + sprintf(strchr(tempMsg, '\0'), " %s", textSelect(LABEL_OFF)); else - sprintf(&tempMsg[strlen(tempMsg)], " %s", textSelect(LABEL_ON)); + sprintf(strchr(tempMsg, '\0'), " %s", textSelect(LABEL_ON)); addToast(DIALOG_TYPE_ERROR, tempMsg); } diff --git a/TFT/src/User/my_misc.c b/TFT/src/User/my_misc.c index 3aa686c38e..352e05471b 100644 --- a/TFT/src/User/my_misc.c +++ b/TFT/src/User/my_misc.c @@ -42,7 +42,58 @@ uint32_t calculateCRC16(const uint8_t *data, uint32_t length) return crc; } -// string convert to uint8, MSB ("2C" to 0x2C) +/* + * - always copy num-1 characters from source to destination + * regardless of null terminating character found in source + * - to avoid overflows, the size of "destination" should be at least "num" bytes + * and the size of "source" should be at least "num-1" bytes, in both cases + * including the '\0' terminal character + * - destination always ends with '\0' + */ +void strxcpy(char * destination, const char * source, size_t num) +{ + num -= !!num; + + memcpy(destination, source, num); + destination[num] ='\0'; +} + +/* + * - copy source to destination but no more than width-1 characters + * - if null terminating character found in source the rest is padded with 0 + * - destination always ends with '\0' + */ +void strwcpy(char * destination, const char * source, size_t width) +{ + width -= !!width; + while (width > 0 && *source != '\0') + { + *destination++ = *source++; + width--; + } + + memset(destination, '\0', width + 1); +} + +/* + * - copy source to destination but no more than size-1 characters + * - if null terminating character found in source the copy stops there + * - destination always ends with '\0' + */ +void strscpy(char * destination, const char * source, size_t size) +{ + size -= !!size; + while (size > 0 && *source != '\0') + { + *destination++ = *source++; + size--; + } + + *destination = '\0'; +} + +// string convert to uint8, MSB +// "2C" to 0x2C uint8_t string_2_uint8(const uint8_t *str) { uint8_t rtv = 0; @@ -112,19 +163,8 @@ uint8_t *uint32_2_string(uint32_t num, uint8_t bytes_num, uint8_t *str) return str; } -// convert time to string with given formatting -void time_2_string(char *buf, char *str_format, uint32_t time) -{ - uint8_t hour = HOURS(time); - uint8_t min = MINUTES(time); - uint8_t sec = SECONDS(time); - - sprintf(buf, str_format, hour, min, sec); -} - -// light weight strtod() function without exponential support. -// Convert string to double (without exponential support) -double strtod_ligth(char *str, char **endptr) +// convert string to double (without exponential support) +double stringToDouble(char *str, char **endptr) { char *p = str; double val = 0.0; @@ -174,44 +214,14 @@ double strtod_ligth(char *str, char **endptr) return val * sign; } -// light weight and safe strncpy() function with padding: -// - copy "src" to "dest" for a maximum of "n-1" characters -// - if null terminating character is found in "src" the rest in "dest" is padded with '\0' -// - "dest" always ends with '\0' -void strncpy_pad(char *dest, const char *src, size_t n) -{ - // if "src" is not NULL, proceed first with the copy. - // Otherwise, proceed only padding "dest" with '\0' - if (src != NULL) - { - while (n > 1 && *src != '\0') - { - *dest++ = *src++; - n--; - } - } - - memset(dest, '\0', n); // NOTE: safe even in case value 0 was passed for "n" (memset() function will do nothing) -} - -// light weight and safe strncpy() function without padding: -// - copy "src" to "dest" for a maximum of "n-1" characters -// - if null terminating character is found in "src" the copy stops there -// - "dest" always ends with '\0' -void strncpy_no_pad(char *dest, const char *src, size_t n) +// convert time to string with given formatting +void timeToString(char *buf, char *strFormat, uint32_t time) { - // if "src" is not NULL, proceed with the copy - if (src != NULL) - { - while (n > 1 && *src != '\0') - { - *dest++ = *src++; - n--; - } - } + uint8_t hour = HOURS(time); + uint8_t min = MINUTES(time); + uint8_t sec = SECONDS(time); - if (n != 0) // safe in case value 0 was passed for "n" - *dest = '\0'; + sprintf(buf, strFormat, hour, min, sec); } // strip out any leading " ", "/" or ":" character that might be in the string diff --git a/TFT/src/User/my_misc.h b/TFT/src/User/my_misc.h index 56d3c929ef..f9be09cc30 100644 --- a/TFT/src/User/my_misc.h +++ b/TFT/src/User/my_misc.h @@ -7,7 +7,7 @@ extern "C" { #include #include -#include // for size_t +#include // Menu Macros #define OPEN_MENU(x) infoMenu.menu[++infoMenu.cur] = x @@ -60,11 +60,11 @@ extern "C" { #define MINUTES(t) (t % (60 * 60) / 60) // minutes remaining to next hour #define SECONDS(t) (t % 60) // seconds remaining to next minute -#define strtod strtod_ligth // light weight strtod() function without exponential support +#define strtod stringToDouble // light weight strtod() function without exponential support #define strncpy(...) \ do { \ - _Pragma("GCC error \"Error: strncpy() is deprecated! Use the alternatives like strncpy_pad() or strncpy_no_pad()\""); \ + _Pragma("GCC error \"Error: strncpy() is deprecated! Use the alternatives like strxcpy(), strwcpy() or strscpy() according to your needs.\""); \ } while (0) // call processes from the argument and than loopProcess() while condition is true @@ -81,15 +81,16 @@ long map(long x, long in_min, long in_max, long out_min, long out_max); uint32_t calculateCRC16(const uint8_t *data, uint32_t length); // calculate CRC16 checksum -uint8_t string_2_uint8(const uint8_t *str); // string convert to uint8, MSB ("2C" to 0x2C) -uint8_t *uint8_2_string(uint8_t num, uint8_t *str); // uint8 convert to string, MSB (0x2C to "2C") -uint32_t string_2_uint32(const uint8_t *str, const uint8_t bytes_num); // string convert to uint32, MSB -uint8_t *uint32_2_string(uint32_t num, uint8_t bytes_num, uint8_t *str); // uint32 convert to string, MSB -void time_2_string(char *buf, char *str_format, uint32_t time); // convert time to string with given formatting +void strxcpy(char * destination, const char * source, size_t num); +void strwcpy(char * destination, const char * source, size_t num); // lightweight and safe strncpy() function with padding +void strscpy(char * destination, const char * source, size_t num); // lightweight and safe strncpy() function without padding -double strtod_ligth(char *str, char **endptr); // light weight strtod() function without exponential support -void strncpy_pad(char *dest, const char *src, size_t n); // light weight and safe strncpy() function with padding -void strncpy_no_pad(char *dest, const char *src, size_t n); // light weight and safe strncpy() function without padding +uint8_t string_2_uint8(const uint8_t *string); // string convert to uint8, MSB ("2C" to 0x2C) +uint8_t *uint8_2_string(uint8_t num, uint8_t *string); // uint8 convert to string, MSB (0x2C to "2C") +uint32_t string_2_uint32(const uint8_t *string, const uint8_t bytes_num); // string convert to uint32, MSB +uint8_t *uint32_2_string(uint32_t num, uint8_t bytes_num, uint8_t *string); // uint32 convert to string, MSB +void timeToString(char *buf, char *strFormat, uint32_t time); // convert time to string with given formatting +double stringToDouble(char *str, char **endptr); // light weight strtod() function without exponential support const char *stripHead(const char *str); // strip out any leading " ", "/" or ":" character that might be in the string void stripChecksum(char *str); // strip out any trailing checksum that might be in the string From 151ce1b2fa747d35df60ddd368cdff72197ce8c2 Mon Sep 17 00:00:00 2001 From: kisslorand Date: Wed, 19 Apr 2023 00:11:22 +0300 Subject: [PATCH 3/5] Extension rework --- TFT/src/User/API/Printing.c | 2 +- TFT/src/User/API/Vfs/vfs.c | 71 +++++--------------------------- TFT/src/User/API/Vfs/vfs.h | 7 ++-- TFT/src/User/Menu/Print.c | 9 ++-- TFT/src/User/Menu/PrintingMenu.c | 12 +++++- TFT/src/User/Menu/Terminal.c | 13 +++--- TFT/src/User/my_misc.c | 32 +++++++------- TFT/src/User/my_misc.h | 28 ++++++------- 8 files changed, 64 insertions(+), 110 deletions(-) diff --git a/TFT/src/User/API/Printing.c b/TFT/src/User/API/Printing.c index 1e5ca80900..a46c29c44a 100644 --- a/TFT/src/User/API/Printing.c +++ b/TFT/src/User/API/Printing.c @@ -303,7 +303,7 @@ void initPrintSummary(void) infoPrintSummary = (PRINT_SUMMARY){.name[0] = '\0', 0, 0, 0, 0, false}; // save print filename (short or long filename) - sprintf(infoPrintSummary.name, "%." STRINGIFY(SUMMARY_NAME_LEN) "s", getPrintFilename()); + strscpy(infoPrintSummary.name, getPrintFilename(), SUMMARY_NAME_LEN); } void preparePrintSummary(void) diff --git a/TFT/src/User/API/Vfs/vfs.c b/TFT/src/User/API/Vfs/vfs.c index 9d90ea8a6c..3e21a77cd4 100644 --- a/TFT/src/User/API/Vfs/vfs.c +++ b/TFT/src/User/API/Vfs/vfs.c @@ -175,14 +175,11 @@ bool isRootFolder(void) } // check if filename provides a supported filename extension -char * isSupportedFile(const char * filename) +static char * isSupportedFile(const char * filename) { char * extPos = strrchr(filename, '.'); // check last "." in the name where extension is supposed to start - if (extPos != NULL && extPos[1] != 'g' && extPos[1] != 'G') - extPos = NULL; - - return extPos; + return (extPos != NULL && (extPos[1] == 'g' || extPos[1] == 'G')) ? extPos : NULL; } // add a file name or folder name to file list @@ -270,7 +267,7 @@ char * getFoldername(uint8_t index) } // return the long file name if exists, otherwise the short one -char * getFilename(uint8_t index) +char * getFilename(uint16_t index) { if (infoFile.longFile[index] != NULL) return infoFile.longFile[index]; @@ -280,14 +277,13 @@ char * getFilename(uint8_t index) char * hideExtension(char * filename) { - if (infoSettings.filename_extension == 0) // if filename extension is disabled + if (infoSettings.filename_extension == 0 && strchr(filename, '\0')[1] == '\0') // if showing the filename extension is disabled and extension not already hidden { char * extPos = isSupportedFile(filename); // if filename provides a supported filename extension then - // check extra byte for filename extension check. If 0, no filename extension was previously hidden - if (extPos != NULL && strchr(filename, '\0')[1] == '\0') - *extPos = 0; // temporary hide filename extension + if (extPos != NULL) + *extPos = '\0'; // temporary hide filename extension } return filename; @@ -295,41 +291,18 @@ char * hideExtension(char * filename) char * restoreExtension(char * filename) { - if (infoSettings.filename_extension == 0) // if filename extension is disabled + if (infoSettings.filename_extension == 0) // if showing the filename extension is disabled { + char * extPos = strchr(filename, '\0'); + // check extra byte for filename extension check. If 0, no filename extension was previously hidden - if (filename[strlen(filename) + 1] != '\0') - filename[strlen(filename)] = '.'; // restore filename extension + if (extPos[1] != '\0') + *extPos = '.'; // restore filename extension } return filename; } -// hide the extension of the file name and return a pointer to that file name -// (the long one if exists, otherwise the short one). -// The hide of the extension is not temporary so do not forget to restore it afterwards! -char * hideFilenameExtension(uint8_t index) -{ - char * filename = hideExtension(infoFile.file[index]); - - if (infoFile.longFile[index] != NULL) - filename = hideExtension(infoFile.longFile[index]); - - return filename; -} - -// restore the extension of the file name and return a pointer to that file name -// (the long one if exists, otherwise the short one) -char * restoreFilenameExtension(uint8_t index) -{ - char * filename = restoreExtension(infoFile.file[index]); - - if (infoFile.longFile[index] != NULL) - filename = restoreExtension(infoFile.longFile[index]); - - return filename; -} - // get print filename according to print originator (remote or local to TFT) char * getPrintFilename(void) { @@ -342,28 +315,6 @@ char * getPrintFilename(void) return getFilename(infoFile.fileIndex); } -// get print title according to print originator (remote or local to TFT) -bool getPrintTitle(char * buf, uint8_t len) -{ - // example: "SD:/test/cap2.gcode" -> "SD:cap2.gcode" - - char * strPtr = getPrintFilename(); - - // "+ 2": space for terminating null character and the flag for filename extension check - if (strlen(getFS()) + strlen(strPtr) + 2 > len) - { - *buf = '\0'; // set buffer to empty string - - return false; - } - - strxcpy(buf, getFS(), len); // set source and set the flag for filename extension check - strcat(buf, strPtr); // append filename - hideExtension(buf); // hide filename extension if filename extension feature is disabled - - return true; -} - // volume exist detect static bool volumeSrcStatus[FF_VOLUMES] = {false, false}; diff --git a/TFT/src/User/API/Vfs/vfs.h b/TFT/src/User/API/Vfs/vfs.h index 4a4d45e014..9e74188389 100644 --- a/TFT/src/User/API/Vfs/vfs.h +++ b/TFT/src/User/API/Vfs/vfs.h @@ -65,13 +65,12 @@ bool addFile(bool isFile, const char * shortName, const char * longName); // ad // called in Print.c char * getFoldername(uint8_t index); // return the long folder name if exists, otherwise the short one -char * getFilename(uint8_t index); // return the long file name if exists, otherwise the short one -char * hideFilenameExtension(uint8_t index); // hide the extension of the file name and return a pointer to that file name -char * restoreFilenameExtension(uint8_t index); // restore the extension of the file name and return a pointer to that file name +char * getFilename(uint16_t index); // return the long file name if exists, otherwise the short one +char * hideExtension(char * filename); // hide the extension of the file name and return a pointer to that file name +char * restoreExtension(char * filename); // restore the extension of the file name and return a pointer to that file name // called in PrintingMenu.c char * getPrintFilename(void); // get print filename according to print originator (remote or local to TFT) -bool getPrintTitle(char * buf, uint8_t len); // get print title according to print originator (remote or local to TFT) // called in menu.c bool volumeExists(uint8_t src); diff --git a/TFT/src/User/Menu/Print.c b/TFT/src/User/Menu/Print.c index 6d6de7b5fd..cd7a3fedc6 100644 --- a/TFT/src/User/Menu/Print.c +++ b/TFT/src/User/Menu/Print.c @@ -85,8 +85,6 @@ void gocdeIconDraw(void) // draw gcode files for (; (baseIndex + i < infoFile.folderCount + infoFile.fileCount) && (i < NUM_PER_PAGE); i++) { - restoreFilenameExtension(baseIndex + i - infoFile.folderCount); // restore filename extension if filename extension feature is disabled - if (enterFolder(infoFile.file[baseIndex + i - infoFile.folderCount]) == false) // always use short filename for file path break; @@ -99,8 +97,7 @@ void gocdeIconDraw(void) exitFolder(); - hideFilenameExtension(baseIndex + i - infoFile.folderCount); // hide filename extension if filename extension feature is disabled - normalNameDisp(&gcodeRect[i], (uint8_t*)infoFile.file[baseIndex + i - infoFile.folderCount]); // always use short filename + normalNameDisp(&gcodeRect[i], (uint8_t *)hideExtension(infoFile.file[baseIndex + i - infoFile.folderCount])); // always use short filename } // clear blank icons @@ -126,7 +123,7 @@ void gocdeListDraw(LISTITEM * item, uint16_t index, uint8_t itemPos) item->icon = CHARICON_FILE; item->itemType = LIST_LABEL; item->titlelabel.index = LABEL_DYNAMIC; - setDynamicLabel(itemPos, hideFilenameExtension(index - infoFile.folderCount)); // hide filename extension if filename extension feature is disabled + setDynamicLabel(itemPos, hideExtension(getFilename(index - infoFile.folderCount))); // hide filename extension if filename extension feature is disabled } } @@ -150,7 +147,7 @@ bool printPageItemSelected(uint16_t index) else if (index < infoFile.folderCount + infoFile.fileCount) // gcode file { infoFile.fileIndex = index - infoFile.folderCount; - char * filename = restoreFilenameExtension(infoFile.fileIndex); // restore filename extension if filename extension feature is disabled + char * filename = restoreExtension(getFilename(infoFile.fileIndex)); // restore filename extension if filename extension feature is disabled if (infoHost.connected != true || enterFolder(infoFile.file[infoFile.fileIndex]) == false) // always use short filename for file path { diff --git a/TFT/src/User/Menu/PrintingMenu.c b/TFT/src/User/Menu/PrintingMenu.c index b12b6e4118..96833eadd2 100644 --- a/TFT/src/User/Menu/PrintingMenu.c +++ b/TFT/src/User/Menu/PrintingMenu.c @@ -122,11 +122,19 @@ static void setLayerNumberTxt(char * layer_number_txt) } } +// set the print title according to the print originator (remote or local to TFT) +static void setPrintTitle(void) +{ + snprintf(title, MAX_TITLE_LEN, "%s%s%c", getFS(), getPrintFilename(), '\0'); + + hideExtension(title); +} + // initialize printing info before opening Printing menu static void initMenuPrinting(void) { - getPrintTitle(title, MAX_TITLE_LEN); // get print title according to print originator (remote or local to TFT) - clearInfoFile(); // as last, clear and free memory for file list + setPrintTitle(); // set the print title according to the print originator (remote or local to TFT) + clearInfoFile(); // as last, clear and free memory for file list progDisplayType = infoSettings.prog_disp_type; diff --git a/TFT/src/User/Menu/Terminal.c b/TFT/src/User/Menu/Terminal.c index 33adbb8b0a..11098c91b8 100644 --- a/TFT/src/User/Menu/Terminal.c +++ b/TFT/src/User/Menu/Terminal.c @@ -24,12 +24,6 @@ typedef struct uint8_t lastSrc; } TERMINAL_DATA; -typedef enum -{ - KEYBOARD_VIEW = 0, - TERMINAL_VIEW -} TERMINAL_WINDOW; - typedef enum { GKEY_PREV = 0, @@ -316,7 +310,12 @@ const uint16_t fontSrcColor[3][3] = { KEYBOARD_DATA * keyboardData; TERMINAL_DATA * terminalData; char * terminalBuf; -TERMINAL_WINDOW curView = KEYBOARD_VIEW; + +enum +{ + KEYBOARD_VIEW = 0, + TERMINAL_VIEW +} curView = KEYBOARD_VIEW; bool numpad = #if defined(KB_TYPE_QWERTY) diff --git a/TFT/src/User/my_misc.c b/TFT/src/User/my_misc.c index 352e05471b..7ae8779a5d 100644 --- a/TFT/src/User/my_misc.c +++ b/TFT/src/User/my_misc.c @@ -45,10 +45,12 @@ uint32_t calculateCRC16(const uint8_t *data, uint32_t length) /* * - always copy num-1 characters from source to destination * regardless of null terminating character found in source + * - destination always ends with '\0' + * + * Note: * - to avoid overflows, the size of "destination" should be at least "num" bytes * and the size of "source" should be at least "num-1" bytes, in both cases * including the '\0' terminal character - * - destination always ends with '\0' */ void strxcpy(char * destination, const char * source, size_t num) { @@ -62,6 +64,9 @@ void strxcpy(char * destination, const char * source, size_t num) * - copy source to destination but no more than width-1 characters * - if null terminating character found in source the rest is padded with 0 * - destination always ends with '\0' + * Note: + * - to avoid overflows, the size of "destination" should be at least + * "width" bytes, including the '\0' terminal character */ void strwcpy(char * destination, const char * source, size_t width) { @@ -79,6 +84,10 @@ void strwcpy(char * destination, const char * source, size_t width) * - copy source to destination but no more than size-1 characters * - if null terminating character found in source the copy stops there * - destination always ends with '\0' + * Note: + * - to avoid overflows, the dimension of "destination" should be + * at least the "size" bytes or the size of "source", which one + * is smaller (including the '\0' terminal character) */ void strscpy(char * destination, const char * source, size_t size) { @@ -245,25 +254,16 @@ void stripChecksum(char *str) // "/test/cap2.gcode *36\n\0" -> "/test/cap2.gcode" // "/test/cap2.gcode \n\0" -> "/test/cap2.gcode" - char *strPtr = strrchr(str, '*'); // e.g. "/test/cap2.gcode *36\n\0" -> "*36\n\0" + char *strPtr = strrchr(str, '*'); + if (strPtr == NULL) strPtr = strchr(str, '\0'); - if (strPtr == NULL) - strPtr = str + strlen(str); // e.g. "/test/cap2.gcode \n\0" -> "\0" - - while (strPtr != str) + // Move strPtr back until it points to the last unwanted character + while (strPtr != str && (*--strPtr == ' ' || *strPtr == '\t' || *strPtr == '\n' || *strPtr == '\r')) { - // e.g. "*36\n\0" -> " *36\n\0" - // e.g. "\0" -> "\n\0" - // - --strPtr; - - if (*strPtr != ' ' && *strPtr != '\t' && *strPtr != '\n' && *strPtr != '\r') - { - strPtr++; // next char has to be set to "\0" - break; - } + // The loop body is intentionally empty } + // Add the null terminator to remove any trailing unwanted characters // e.g. " *36\n\0" -> "\0 *36\n\0" // e.g. " \n\0" -> "\0 \n\0" // diff --git a/TFT/src/User/my_misc.h b/TFT/src/User/my_misc.h index f9be09cc30..46c673ccf1 100644 --- a/TFT/src/User/my_misc.h +++ b/TFT/src/User/my_misc.h @@ -60,7 +60,7 @@ extern "C" { #define MINUTES(t) (t % (60 * 60) / 60) // minutes remaining to next hour #define SECONDS(t) (t % 60) // seconds remaining to next minute -#define strtod stringToDouble // light weight strtod() function without exponential support +#define strtod stringToDouble // lightweight strtod() function without exponential support #define strncpy(...) \ do { \ @@ -79,21 +79,21 @@ extern "C" { uint8_t inRange(int cur, int tag , int range); long map(long x, long in_min, long in_max, long out_min, long out_max); -uint32_t calculateCRC16(const uint8_t *data, uint32_t length); // calculate CRC16 checksum +uint32_t calculateCRC16(const uint8_t *data, uint32_t length); void strxcpy(char * destination, const char * source, size_t num); -void strwcpy(char * destination, const char * source, size_t num); // lightweight and safe strncpy() function with padding -void strscpy(char * destination, const char * source, size_t num); // lightweight and safe strncpy() function without padding - -uint8_t string_2_uint8(const uint8_t *string); // string convert to uint8, MSB ("2C" to 0x2C) -uint8_t *uint8_2_string(uint8_t num, uint8_t *string); // uint8 convert to string, MSB (0x2C to "2C") -uint32_t string_2_uint32(const uint8_t *string, const uint8_t bytes_num); // string convert to uint32, MSB -uint8_t *uint32_2_string(uint32_t num, uint8_t bytes_num, uint8_t *string); // uint32 convert to string, MSB -void timeToString(char *buf, char *strFormat, uint32_t time); // convert time to string with given formatting -double stringToDouble(char *str, char **endptr); // light weight strtod() function without exponential support - -const char *stripHead(const char *str); // strip out any leading " ", "/" or ":" character that might be in the string -void stripChecksum(char *str); // strip out any trailing checksum that might be in the string +void strwcpy(char * destination, const char * source, size_t num); +void strscpy(char * destination, const char * source, size_t num); + +uint8_t string_2_uint8(const uint8_t *string); +uint8_t *uint8_2_string(uint8_t num, uint8_t *string); +uint32_t string_2_uint32(const uint8_t *string, const uint8_t bytes_num); +uint8_t *uint32_2_string(uint32_t num, uint8_t bytes_num, uint8_t *string); +void timeToString(char *buf, char *strFormat, uint32_t time); +double stringToDouble(char *str, char **endptr); + +const char *stripHead(const char *str); +void stripChecksum(char *str); uint8_t getChecksum(char *str); bool validateChecksum(char *str); From 70ca66972a3a77e756ef24d9fd30f881e6a1e207 Mon Sep 17 00:00:00 2001 From: kisslorand Date: Tue, 30 May 2023 13:47:53 +0300 Subject: [PATCH 4/5] Size reduction --- TFT/src/User/API/Gcode/gcode.c | 2 +- TFT/src/User/API/ModeSwitching.c | 3 +- TFT/src/User/API/Notification.c | 102 ++++++++++++-------------- TFT/src/User/API/Notification.h | 7 -- TFT/src/User/API/ProbeOffsetControl.c | 3 +- TFT/src/User/API/RRFParseACK.cpp | 6 +- TFT/src/User/API/interfaceCmd.c | 2 +- TFT/src/User/API/parseACK.c | 14 +++- TFT/src/User/Menu/LevelCorner.c | 58 +++++---------- TFT/src/User/Menu/Move.c | 9 +-- TFT/src/User/Menu/ScreenSettings.c | 2 +- TFT/src/User/Menu/SettingsMenu.c | 66 ++++++----------- TFT/src/User/Menu/SettingsMenu.h | 10 +-- TFT/src/User/Menu/StatusScreen.c | 8 +- TFT/src/User/Menu/StatusScreen.h | 4 +- TFT/src/User/Menu/TuneExtruder.c | 3 +- TFT/src/User/my_misc.h | 12 +-- 17 files changed, 131 insertions(+), 180 deletions(-) diff --git a/TFT/src/User/API/Gcode/gcode.c b/TFT/src/User/API/Gcode/gcode.c index e20c742a55..d6940fcf45 100644 --- a/TFT/src/User/API/Gcode/gcode.c +++ b/TFT/src/User/API/Gcode/gcode.c @@ -260,7 +260,7 @@ void request_M98(const char * filename) mustStoreCmd(command); // prevent a race condition when rrfStatusQuery returns !busy before executing the macro - TASK_LOOP_WHILE(isEnqueued(command)) + TASK_LOOP_WHILE(isEnqueued(command)); rrfStatusQueryFast(); diff --git a/TFT/src/User/API/ModeSwitching.c b/TFT/src/User/API/ModeSwitching.c index 2f7e37559a..a5b4b4c54d 100644 --- a/TFT/src/User/API/ModeSwitching.c +++ b/TFT/src/User/API/ModeSwitching.c @@ -35,7 +35,8 @@ void Mode_Switch(void) LOGO_ReadDisplay(); updateNextHeatCheckTime(); // send "M105" after a delay, because of mega2560 will be hanged when received data at startup - TASK_LOOP_WHILE(OS_GetTimeMs() - startUpTime < BTT_BOOTSCREEN_TIME) // display logo BTT_BOOTSCREEN_TIME ms + // display logo BTT_BOOTSCREEN_TIME ms + TASK_LOOP_WHILE(OS_GetTimeMs() - startUpTime < BTT_BOOTSCREEN_TIME); heatSetUpdateSeconds(TEMPERATURE_QUERY_SLOW_SECONDS); modeFreshBoot = false; diff --git a/TFT/src/User/API/Notification.c b/TFT/src/User/API/Notification.c index e28cb90754..2ece528020 100644 --- a/TFT/src/User/API/Notification.c +++ b/TFT/src/User/API/Notification.c @@ -6,8 +6,12 @@ const GUI_RECT toastRect = {START_X + TITLE_END_Y - (TOAST_Y_PAD * 2), TOAST_Y_PAD, LCD_WIDTH - START_X, TITLE_END_Y - TOAST_Y_PAD}; const GUI_RECT toastIconRect = {START_X, TOAST_Y_PAD, START_X + TITLE_END_Y - (TOAST_Y_PAD * 2), TITLE_END_Y - TOAST_Y_PAD}; -// toast notification variables -static TOAST toastlist[TOAST_MSG_COUNT]; +static struct +{ + DIALOG_TYPE style; + uint8_t isNew; + char text[TOAST_MSG_LENGTH]; +} toastlist[TOAST_MSG_COUNT]; static uint8_t nextToastIndex = 0; // next index to store new toast static uint8_t curToastDisplay = 0; // current toast notification being displayed @@ -24,11 +28,9 @@ void addToast(DIALOG_TYPE style, char * text) { LCD_WAKE(); - TOAST t; - strscpy(t.text, text, TOAST_MSG_LENGTH); - t.style = style; - t.isNew = true; - toastlist[nextToastIndex] = t; + strscpy(toastlist[nextToastIndex].text, text, TOAST_MSG_LENGTH); + toastlist[nextToastIndex].style = style; + toastlist[nextToastIndex].isNew = true; nextToastIndex = (nextToastIndex + 1) % TOAST_MSG_COUNT; } @@ -41,7 +43,7 @@ bool toastRunning(void) // check if any new notification is available bool toastAvailable(void) { - for (int i = 0; i < TOAST_MSG_COUNT; i++) + for (uint8_t i = 0; i < TOAST_MSG_COUNT; i++) { if (toastlist[i].isNew == true) return true; @@ -62,29 +64,35 @@ void drawToast(bool redraw) // draw icon uint8_t *icon; - uint8_t cursound; - if (toastlist[curToastDisplay].style == DIALOG_TYPE_ERROR) - { - GUI_SetColor(NOTIF_ICON_ERROR_BG_COLOR); - icon = IconCharSelect(CHARICON_ERROR); - cursound = SOUND_ERROR; - } - else if (toastlist[curToastDisplay].style == DIALOG_TYPE_SUCCESS) + SOUND cursound; + + switch (toastlist[curToastDisplay].style) { - GUI_SetColor(NOTIF_ICON_SUCCESS_BG_COLOR); - icon = IconCharSelect(CHARICON_OK_ROUND); - cursound = SOUND_SUCCESS; + case DIALOG_TYPE_ERROR: + GUI_SetColor(NOTIF_ICON_ERROR_BG_COLOR); + icon = IconCharSelect(CHARICON_ERROR); + cursound = SOUND_ERROR; + break; + + case DIALOG_TYPE_SUCCESS: + GUI_SetColor(NOTIF_ICON_SUCCESS_BG_COLOR); + icon = IconCharSelect(CHARICON_OK_ROUND); + cursound = SOUND_SUCCESS; + break; + + default: + GUI_SetColor(NOTIF_ICON_INFO_BG_COLOR); + icon = IconCharSelect(CHARICON_INFO); + cursound = SOUND_TOAST; + break; } - else + + if (!redraw) // if notification is new { - GUI_SetColor(NOTIF_ICON_INFO_BG_COLOR); - icon = IconCharSelect(CHARICON_INFO); - cursound = SOUND_TOAST; + BUZZER_PLAY(cursound); // play sound + nextToastTime = OS_GetTimeMs() + SEC_TO_MS(TOAST_DURATION); // set new timer } - if (cursound >= 0 && !redraw) - BUZZER_PLAY(cursound); - GUI_SetTextMode(GUI_TEXTMODE_TRANS); GUI_FillPrect(&toastIconRect); GUI_SetColor(NOTIF_ICON_FG_COLOR); @@ -99,10 +107,6 @@ void drawToast(bool redraw) // set current toast notification as old/completed toastlist[curToastDisplay].isNew = false; - // set new timer if notification is new - if (!redraw) - nextToastTime = OS_GetTimeMs() + SEC_TO_MS(TOAST_DURATION); - GUI_RestoreColorDefault(); } } @@ -110,10 +114,7 @@ void drawToast(bool redraw) // check and control toast notification display void loopToast(void) { - if (getMenuType() == MENU_TYPE_FULLSCREEN) - return; - - if (OS_GetTimeMs() > nextToastTime) + if (getMenuType() != MENU_TYPE_FULLSCREEN && OS_GetTimeMs() > nextToastTime) { if (toastAvailable()) { @@ -130,36 +131,32 @@ void loopToast(void) } // add new message to notification queue -void addNotification(DIALOG_TYPE style, char *title, char *text, bool ShowDialog) +void addNotification(DIALOG_TYPE style, char *title, char *text, bool draw_dialog) { LCD_WAKE(); - if (nextMsgIndex > MAX_MSG_COUNT - 1) + if (nextMsgIndex >= MAX_MSG_COUNT) { // remove oldest message and move all messages up one step - for (int i = 0; i < MAX_MSG_COUNT - 1; i++) - { - memcpy(&msglist[i], &msglist[i + 1], sizeof(NOTIFICATION)); - } + memmove(msglist, &msglist[1], (MAX_MSG_COUNT - 1) * (sizeof(NOTIFICATION))); nextMsgIndex = MAX_MSG_COUNT - 1; } // store message - msglist[nextMsgIndex].style = style; + msglist[nextMsgIndex].style = style; strscpy(msglist[nextMsgIndex].text, text, MAX_MSG_LENGTH); strscpy(msglist[nextMsgIndex].title, title, MAX_MSG_TITLE_LENGTH); + nextMsgIndex++; - if (ShowDialog && MENU_IS_NOT(menuNotification)) - popupReminder(style, (uint8_t *)title, (uint8_t *)msglist[nextMsgIndex].text); - - if (nextMsgIndex < MAX_MSG_COUNT) nextMsgIndex += 1; //(nextMsgIndex + 1) % MAX_MSG_COUNT; + if (draw_dialog && MENU_IS_NOT(menuNotification)) + popupReminder(style, (uint8_t *)title, (uint8_t *)text); if (notificationHandler != NULL) notificationHandler(); notificationDot(); - statusScreen_setMsg((uint8_t *)title, (uint8_t *)text); + statusScreenSetMsg((uint8_t *)title, (uint8_t *)text); } // replay a notification @@ -172,7 +169,7 @@ void replayNotification(uint8_t index) // retrieve a stored notification NOTIFICATION *getNotification(uint8_t index) { - if (strlen(msglist[index].title) > 0 && strlen(msglist[index].text) > 0) + if (msglist[index].title[0] != '\0' && msglist[index].text[0] != '\0') return &msglist[index]; else return NULL; @@ -180,10 +177,7 @@ NOTIFICATION *getNotification(uint8_t index) bool hasNotification(void) { - if (nextMsgIndex == 0) - return false; - else - return true; + return (nextMsgIndex != 0); } void clearNotification(void) @@ -191,11 +185,11 @@ void clearNotification(void) nextMsgIndex = 0; for (int i = 0; i < MAX_MSG_COUNT; i++) { - msglist[i].text[0] = 0; - msglist[i].title[0] = 0; + msglist[i].text[0] = '\0'; + msglist[i].title[0] = '\0'; } notificationDot(); - statusScreen_setReady(); + statusScreenSetReady(); } // check if pressed on titlebar area diff --git a/TFT/src/User/API/Notification.h b/TFT/src/User/API/Notification.h index dfebd867ba..169192805a 100644 --- a/TFT/src/User/API/Notification.h +++ b/TFT/src/User/API/Notification.h @@ -20,13 +20,6 @@ extern "C" { #define MAX_MSG_TITLE_LENGTH 15 #define MAX_MSG_LENGTH 70 -typedef struct -{ - DIALOG_TYPE style; - uint8_t isNew; - char text[TOAST_MSG_LENGTH]; -} TOAST; - typedef struct { DIALOG_TYPE style; diff --git a/TFT/src/User/API/ProbeOffsetControl.c b/TFT/src/User/API/ProbeOffsetControl.c index 1c2b370c9e..45eab01280 100644 --- a/TFT/src/User/API/ProbeOffsetControl.c +++ b/TFT/src/User/API/ProbeOffsetControl.c @@ -20,7 +20,8 @@ void probeOffsetEnable(float shim) { levelingProbePoint(LEVEL_CENTER); // probe center of bed - TASK_LOOP_WHILE(levelingGetProbedPoint() == LEVEL_NO_POINT) // if probed Z is set, exit from loop and read probed Z + // if probed Z is set, exit from loop and read probed Z + TASK_LOOP_WHILE(levelingGetProbedPoint() == LEVEL_NO_POINT); probedZ = levelingGetProbedZ(); levelingResetProbedPoint(); // reset to check for new updates diff --git a/TFT/src/User/API/RRFParseACK.cpp b/TFT/src/User/API/RRFParseACK.cpp index db707d1085..0cfa7ad6d2 100644 --- a/TFT/src/User/API/RRFParseACK.cpp +++ b/TFT/src/User/API/RRFParseACK.cpp @@ -209,19 +209,19 @@ void ParseACKJsonParser::value(const char *value) { setupMachine(FW_REPRAPFW); string_end = strstr(string_start, "ELECTRONICS"); - infoSetFirmwareName((uint8_t *)string_start, string_end-string_start); + infoSetFirmwareName(string_start, string_end-string_start); } else if ((string_start = strstr(value, (char *)"access point")) != NULL) // parse M552 { string_end = strstr(string_start, ","); string_start += 13; - infoSetAccessPoint((uint8_t *)string_start, string_end-string_start); + infoSetAccessPoint(string_start, string_end-string_start); if ((string_start = strstr(string_start, (char *)"IP address")) != NULL) { string_end = strstr(string_start, "\\n"); string_start += 11; - infoSetIPAddress((uint8_t *)string_start, string_end-string_start); + infoSetIPAddress(string_start, string_end-string_start); } } else if ((string_start = strstr(value, (char *)"printing byte")) != NULL) // parse M27 {"seq":21,"resp":"SD printing byte 1226/5040433\n"} diff --git a/TFT/src/User/API/interfaceCmd.c b/TFT/src/User/API/interfaceCmd.c index 9ef89f90ab..f98816d02f 100644 --- a/TFT/src/User/API/interfaceCmd.c +++ b/TFT/src/User/API/interfaceCmd.c @@ -998,7 +998,7 @@ void sendQueueCmd(void) stripChecksum(rawMsg); msgText = stripHead(rawMsg); - statusScreen_setMsg((uint8_t *)"M117", (uint8_t *)msgText); + statusScreenSetMsg((uint8_t *)"M117", (uint8_t *)msgText); if (MENU_IS_NOT(menuStatus)) addToast(DIALOG_TYPE_INFO, (char *)msgText); diff --git a/TFT/src/User/API/parseACK.c b/TFT/src/User/API/parseACK.c index 621228ca88..32964c240d 100644 --- a/TFT/src/User/API/parseACK.c +++ b/TFT/src/User/API/parseACK.c @@ -262,7 +262,7 @@ void hostActionCommands(void) } else { - statusScreen_setMsg((uint8_t *)magic_echo, (uint8_t *)ack_cache + index); // always display the notification on status screen + statusScreenSetMsg((uint8_t *)magic_echo, (uint8_t *)ack_cache + index); // always display the notification on status screen if (!ack_continue_seen("Ready.")) // avoid to display unneeded/frequent useless notifications (e.g. "My printer Ready.") { @@ -882,6 +882,12 @@ void parseACK(void) if (ack_continue_seen("Z: ")) levelingSetProbedPoint(x, y, ack_value()); // save probed Z value } + // parse G30 coordinate unreachable message + else if (ack_seen("Z Probe Past Bed")) + { + levelingSetProbedPoint(infoSettings.machine_size_max[1] + 1, infoSettings.machine_size_max[2] + 1, 0); // cancel waiting for coordinates + BUZZER_PLAY(SOUND_ERROR); + } #if DELTA_PROBE_TYPE != 0 // parse and store Delta calibration settings else if (ack_seen("Calibration OK")) @@ -1160,7 +1166,7 @@ void parseACK(void) // parse M115 capability report else if (ack_seen("FIRMWARE_NAME:")) { - uint8_t * string = (uint8_t *)&ack_cache[ack_index]; + char * string = &ack_cache[ack_index]; uint16_t string_start = ack_index; uint16_t string_end = string_start; @@ -1182,7 +1188,7 @@ void parseACK(void) if (ack_seen("MACHINE_TYPE:")) { - string = (uint8_t *)&ack_cache[ack_index]; + string = &ack_cache[ack_index]; string_start = ack_index; if (ack_seen("EXTRUDER_COUNT:")) @@ -1193,7 +1199,7 @@ void parseACK(void) string_end = ack_index - sizeof("EXTRUDER_COUNT:"); } - infoSetMachineType(string, string_end - string_start); // set firmware name + infoSetMachineType(string, string_end - string_start); // set printer name } } else if (ack_starts_with("Cap:")) diff --git a/TFT/src/User/Menu/LevelCorner.c b/TFT/src/User/Menu/LevelCorner.c index 6a9188268a..e322932cc2 100644 --- a/TFT/src/User/Menu/LevelCorner.c +++ b/TFT/src/User/Menu/LevelCorner.c @@ -1,7 +1,7 @@ #include "LevelCorner.h" #include "includes.h" -const uint8_t valIconIndex[LEVELING_POINT_COUNT] = {4, 5, 1, 0, 3}; +const uint8_t valIconIndex[LEVELING_POINT_COUNT] = {KEY_ICON_4, KEY_ICON_5, KEY_ICON_1, KEY_ICON_0, KEY_ICON_3}; // buffer current Z value measured in Level Corner = {position 1, position 2, position 3, position 4, probe accuracy(M48)} float levelCornerPosition[LEVELING_POINT_COUNT] = {0}; @@ -37,19 +37,6 @@ void refreshValue(MENUITEMS * levelItems, uint8_t index) menuDrawIconText(&levelItems->items[valIconIndex[index]], valIconIndex[index]); } -void checkRefreshValue(MENUITEMS * levelItems) -{ - LEVELING_POINT levelingPoint = levelingGetProbedPoint(); - - if (levelingPoint != LEVEL_NO_POINT) - { - levelCornerPosition[levelingPoint] = levelingGetProbedZ(); - refreshValue(levelItems, levelingPoint); - - levelingResetProbedPoint(); // reset to check for new updates - } -} - // show M48 on icon void drawProbeAccuracyIcon(MENUITEMS * levelItems) { @@ -129,11 +116,25 @@ void menuLevelCorner(void) switch (key_num) { case KEY_ICON_0: - levelingProbePoint(LEVEL_TOP_LEFT); - break; - case KEY_ICON_1: - levelingProbePoint(LEVEL_TOP_RIGHT); + case KEY_ICON_4: + case KEY_ICON_5: + case KEY_ICON_6: + for (int lvlPoint = LEVEL_BOTTOM_LEFT; lvlPoint <= LEVEL_TOP_LEFT; lvlPoint++) + { + if (key_num < KEY_ICON_6 && key_num != valIconIndex[lvlPoint]) + continue; + + levelingProbePoint(lvlPoint); + + // wait until point probing is executed + TASK_LOOP_WHILE(levelingGetProbedPoint() == LEVEL_NO_POINT); + + levelCornerPosition[lvlPoint] = levelingGetProbedZ(); + refreshValue(&levelCornerItems, lvlPoint); + levelingResetProbedPoint(); // reset to check for new updates + } + break; case KEY_ICON_2: @@ -155,25 +156,6 @@ void menuLevelCorner(void) drawProbeAccuracyIcon(&levelCornerItems); break; - case KEY_ICON_4: - levelingProbePoint(LEVEL_BOTTOM_LEFT); - break; - - case KEY_ICON_5: - levelingProbePoint(LEVEL_BOTTOM_RIGHT); - break; - - case KEY_ICON_6: - for (int i = LEVEL_BOTTOM_LEFT; i <= LEVEL_TOP_LEFT; i++) - { - levelingProbePoint(i); - - // following loop needed to guarantee the value for each point beeing probed is updated at least one time on the menu - TASK_LOOP_WHILE(isNotEmptyCmdQueue(), checkRefreshValue(&levelCornerItems)) - } - - break; - case KEY_ICON_7: infoSettings.level_edge = origLevelEdge; // restore original leveling edge value origLevelEdge = -1; @@ -185,7 +167,5 @@ void menuLevelCorner(void) } loopProcess(); - - checkRefreshValue(&levelCornerItems); } } diff --git a/TFT/src/User/Menu/Move.c b/TFT/src/User/Menu/Move.c index ca23c3757e..39c32a7dd0 100644 --- a/TFT/src/User/Menu/Move.c +++ b/TFT/src/User/Menu/Move.c @@ -5,9 +5,9 @@ moveItems.items[p0].label.index = LABEL_##axis##_##dir0; \ moveItems.items[p1].label.index = LABEL_##axis##_##dir1; \ } while(0) -#define X_MOVE_GCODE "G1 X%.2f F%d\n" -#define Y_MOVE_GCODE "G1 Y%.2f F%d\n" -#define Z_MOVE_GCODE "G1 Z%.2f F%d\n" +#define X_MOVE_GCODE "G0 X%.2f F%d\n" +#define Y_MOVE_GCODE "G0 Y%.2f F%d\n" +#define Z_MOVE_GCODE "G0 Z%.2f F%d\n" #define GANTRY_UPDATE_DELAY 500 // 1 seconds is 1000 #ifdef PORTRAIT_MODE @@ -47,7 +47,7 @@ void drawXYZ(void) GUI_SetColor(infoSettings.font_color); } -void updateGantry(void) +static void updateGantry(void) { if (nextScreenUpdate(GANTRY_UPDATE_DELAY)) { @@ -168,7 +168,6 @@ void menuMove(void) moveItems.items[key_num] = itemMoveLen[item_moveLen_index]; menuDrawItem(&moveItems.items[key_num], key_num); - amount = moveLenSteps[item_moveLen_index]; break; diff --git a/TFT/src/User/Menu/ScreenSettings.c b/TFT/src/User/Menu/ScreenSettings.c index 8100fef9ff..4808ac970c 100644 --- a/TFT/src/User/Menu/ScreenSettings.c +++ b/TFT/src/User/Menu/ScreenSettings.c @@ -268,7 +268,7 @@ void menuLanguage(void) if (memcmp(&now, &infoSettings, sizeof(SETTINGS))) { - statusScreen_setReady(); // restore msg buffer when language is changed + statusScreenSetReady(); // restore msg buffer when language is changed storePara(); } } diff --git a/TFT/src/User/Menu/SettingsMenu.c b/TFT/src/User/Menu/SettingsMenu.c index 6d854c344e..adfb2f053c 100644 --- a/TFT/src/User/Menu/SettingsMenu.c +++ b/TFT/src/User/Menu/SettingsMenu.c @@ -25,58 +25,34 @@ const MENUITEMS settingsItems = { // {1 * LCD_WIDTH / 3, 1 * BYTE_HEIGHT}, // {2 * LCD_WIDTH / 3, 1 * BYTE_HEIGHT},}; -static uint8_t firmare_name[64] = "Unknown system"; // Marlin firmware version -uint8_t machine_type[64] = "3D Printer"; // Marlin machine type -uint8_t access_point[64] = "Connecting..."; // Access point for RepRapFirmware -uint8_t ip_address[20] = "0.0.0.0"; // IP address for RepRapFirmware +static char firmware_name[64] = "Unknown system"; // Marlin firmware version +char machine_type[64] = "3D Printer"; // Marlin machine type +static char access_point[64] = "Connecting..."; // Access point for RepRapFirmware +static char ip_address[20] = "0.0.0.0"; // IP address for RepRapFirmware -void infoSetFirmwareName(uint8_t *name, uint8_t name_len) +void infoSetFirmwareName(char * name, uint8_t name_len) { - if (name_len > sizeof(firmare_name) - 1) - name_len = sizeof(firmare_name) - 1; - uint8_t i; - for (i = 0; i < name_len; i++) - { - firmare_name[i] = name[i]; - } - firmare_name[i] = 0; + name_len++; // add space for terminal '\0' character + strxcpy(firmware_name, name, MIN(sizeof(firmware_name), name_len)); } -void infoSetMachineType(uint8_t *machine, uint8_t type_len) +void infoSetMachineType(char * machine, uint8_t type_len) { - if (type_len > sizeof(machine_type) - 1) - type_len = sizeof(machine_type) - 1; - uint8_t i; - for (i = 0; i < type_len; i++) - { - machine_type[i] = machine[i]; - } - machine_type[i] = 0; - statusScreen_setReady(); + type_len++; // add space for terminal '\0' character + strxcpy(machine_type, machine, MIN(sizeof(machine_type), type_len)); + statusScreenSetReady(); } -void infoSetAccessPoint(uint8_t *ssid, uint8_t ssid_len) +void infoSetAccessPoint(char * ssid, uint8_t ssid_len) { - if (ssid_len > sizeof(access_point) - 1) - ssid_len = sizeof(access_point) - 1; - uint8_t i; - for (i = 0; i < ssid_len; i++) - { - access_point[i] = ssid[i]; - } - access_point[i] = 0; + ssid_len++; // add space for terminal '\0' character + strxcpy(access_point, ssid, MIN(sizeof(access_point), ssid_len)); } -void infoSetIPAddress(uint8_t *ip, uint8_t ip_len) +void infoSetIPAddress(char * ip, uint8_t ip_len) { - if (ip_len > sizeof(ip_address) - 1) - ip_len = sizeof(ip_address) - 1; - uint8_t i; - for (i = 0; i < ip_len; i++) - { - ip_address[i] = ip[i]; - } - ip_address[i] = 0; + ip_len++; // add space for terminal '\0' character + strxcpy(ip_address, ip, MIN(sizeof(ip_address), ip_len)); } // Version infomation @@ -135,8 +111,8 @@ void menuInfo(void) // draw info GUI_SetColor(0xDB40); - GUI_DispStringInPrectEOL(&version[0], firmare_name); - GUI_DispStringInPrectEOL(&version[1], machine_type); + GUI_DispStringInPrectEOL(&version[0], (uint8_t *)firmware_name); + GUI_DispStringInPrectEOL(&version[1], (uint8_t *)machine_type); GUI_DispStringInPrectEOL(&version[2], (uint8_t *)hardware); sprintf(buf, "V"STRINGIFY(SOFTWARE_VERSION) " " __DATE__ " in %dMhz", mcuClocks.rccClocks.SYSCLK_Frequency / 1000000); GUI_DispStringInPrectEOL(&version[3], (uint8_t *)buf); @@ -156,9 +132,9 @@ void menuInfo(void) GUI_DispStringInRect(20, LCD_HEIGHT - (BYTE_HEIGHT*2), LCD_WIDTH-20, LCD_HEIGHT, textSelect(LABEL_TOUCH_TO_EXIT)); - while (!isPress()) loopBackEnd(); + while (!isPress()) { loopBackEnd(); } BUZZER_PLAY(SOUND_KEYPRESS); - while (isPress()) loopBackEnd(); + while (isPress()) { loopBackEnd(); } GUI_RestoreColorDefault(); CLOSE_MENU(); diff --git a/TFT/src/User/Menu/SettingsMenu.h b/TFT/src/User/Menu/SettingsMenu.h index 4336796c94..9f295ffe85 100644 --- a/TFT/src/User/Menu/SettingsMenu.h +++ b/TFT/src/User/Menu/SettingsMenu.h @@ -7,12 +7,12 @@ extern "C" { #include -extern uint8_t machine_type[64]; +extern char machine_type[64]; -void infoSetFirmwareName(uint8_t *name, uint8_t name_len); -void infoSetMachineType(uint8_t *machine, uint8_t type_len); -void infoSetAccessPoint(uint8_t *ssid, uint8_t ssid_len); -void infoSetIPAddress(uint8_t *ip, uint8_t ip_len); +void infoSetFirmwareName(char * name, uint8_t name_len); +void infoSetMachineType(char * machine, uint8_t type_len); +void infoSetAccessPoint(char * ssid, uint8_t ssid_len); +void infoSetIPAddress(char * ip, uint8_t ip_len); void menuInfo(void); void menuSettings(void); diff --git a/TFT/src/User/Menu/StatusScreen.c b/TFT/src/User/Menu/StatusScreen.c index 1c3e36f787..32656a45fa 100644 --- a/TFT/src/User/Menu/StatusScreen.c +++ b/TFT/src/User/Menu/StatusScreen.c @@ -200,21 +200,21 @@ void drawStatus(void) GUI_RestoreColorDefault(); } -void statusScreen_setMsg(const uint8_t *title, const uint8_t *msg) +void statusScreenSetMsg(const uint8_t *title, const uint8_t *msg) { strscpy(msgTitle, (char *)title, sizeof(msgTitle)); strscpy(msgBody, (char *)msg, sizeof(msgBody)); msgNeedRefresh = true; } -void statusScreen_setReady(void) +void statusScreenSetReady(void) { strscpy(msgTitle, (char *)textSelect(LABEL_STATUS), sizeof(msgTitle)); if (infoHost.connected == false) strscpy(msgBody, (char *)textSelect(LABEL_UNCONNECTED), sizeof(msgBody)); else - snprintf(msgBody, sizeof(msgBody), "%s %s", (char *)machine_type, (char *)textSelect(LABEL_READY)); + snprintf(msgBody, sizeof(msgBody), "%s %s", machine_type, (char *)textSelect(LABEL_READY)); msgNeedRefresh = true; } @@ -296,7 +296,7 @@ void menuStatus(void) { if (infoHost.connected != lastConnectionStatus) { - statusScreen_setReady(); + statusScreenSetReady(); lastConnectionStatus = infoHost.connected; } diff --git a/TFT/src/User/Menu/StatusScreen.h b/TFT/src/User/Menu/StatusScreen.h index a4f00074a3..92cad0c879 100644 --- a/TFT/src/User/Menu/StatusScreen.h +++ b/TFT/src/User/Menu/StatusScreen.h @@ -13,8 +13,8 @@ extern const GUI_RECT msgRect; void menuStatus(void); void drawTemperature(void); void storegantry(int n, float val); -void statusScreen_setMsg(const uint8_t *title,const uint8_t *msg); -void statusScreen_setReady(void); +void statusScreenSetMsg(const uint8_t *title,const uint8_t *msg); +void statusScreenSetReady(void); void drawStatusScreenMsg(void); float getAxisLocation(uint8_t n); void gantry_dec(int n, float val); diff --git a/TFT/src/User/Menu/TuneExtruder.c b/TFT/src/User/Menu/TuneExtruder.c index 8c309fcd11..bed7cdf619 100644 --- a/TFT/src/User/Menu/TuneExtruder.c +++ b/TFT/src/User/Menu/TuneExtruder.c @@ -127,7 +127,8 @@ static inline void extrudeFilament(void) mustStoreCmd("M92\n"); setParameter(P_STEPS_PER_MM, E_AXIS, 0.0f); // reset E-steps value - TASK_LOOP_WHILE(getParameter(P_STEPS_PER_MM, E_AXIS) == 0.0f) // wait until E-steps is updated + // wait until E-steps is updated + TASK_LOOP_WHILE(getParameter(P_STEPS_PER_MM, E_AXIS) == 0.0f); // home extruder and set absolute positioning mustStoreScript("G28\nG90\n"); diff --git a/TFT/src/User/my_misc.h b/TFT/src/User/my_misc.h index 46c673ccf1..0356c23b19 100644 --- a/TFT/src/User/my_misc.h +++ b/TFT/src/User/my_misc.h @@ -69,12 +69,12 @@ extern "C" { // call processes from the argument and than loopProcess() while condition is true // tasks from argument must be separated by ";" ("TASK_LOOP_WHILE(condition, task1(); task2(); ...)) -#define TASK_LOOP_WHILE(condition, ...) \ - while (condition) \ - { \ - __VA_ARGS__; \ - loopProcess(); \ - } +#define TASK_LOOP_WHILE(condition, ...) \ + while(condition) \ + { \ + __VA_ARGS__; \ + loopProcess(); \ + } uint8_t inRange(int cur, int tag , int range); long map(long x, long in_min, long in_max, long out_min, long out_max); From d7a90e498ecabfacdb293dc0fe29ed7a21fdd51f Mon Sep 17 00:00:00 2001 From: kisslorand Date: Tue, 13 Jun 2023 16:40:42 +0300 Subject: [PATCH 5/5] Little mesheditor optimization --- TFT/src/User/API/ProbeHeightControl.c | 2 +- TFT/src/User/Menu/MeshEditor.c | 62 +++++++++++---------------- 2 files changed, 26 insertions(+), 38 deletions(-) diff --git a/TFT/src/User/API/ProbeHeightControl.c b/TFT/src/User/API/ProbeHeightControl.c index 4d6349a84f..d1e1fa075a 100644 --- a/TFT/src/User/API/ProbeHeightControl.c +++ b/TFT/src/User/API/ProbeHeightControl.c @@ -3,7 +3,7 @@ #define ENDSTOP_CMD "M211 S%d\n" #define ENDSTOP_CMD_RRF "M564 S%d H%d\n" // for RRF -#define MOVE_Z_CMD "G1 Z%.2f F%d\n" +#define MOVE_Z_CMD "G0 Z%.2f F%d\n" #define PROBE_UPDATE_DELAY 200 // 1 seconds is 1000 diff --git a/TFT/src/User/Menu/MeshEditor.c b/TFT/src/User/Menu/MeshEditor.c index 68a2c02a67..e51dcf8d52 100644 --- a/TFT/src/User/Menu/MeshEditor.c +++ b/TFT/src/User/Menu/MeshEditor.c @@ -184,8 +184,8 @@ const GUI_RECT meshKeyRect[ME_KEY_NUM] = { // arrow keys {MESH_ARROW_X0 + 0 * MESH_ARROW_WIDTH, MESH_ARROW_Y0 + 0 * MESH_ARROW_HEIGHT, MESH_ARROW_X0 + 2 * MESH_ARROW_WIDTH, MESH_ARROW_Y0 + 1 * MESH_ARROW_HEIGHT}, // UP - {MESH_ARROW_X0 + 0 * MESH_ARROW_WIDTH, MESH_ARROW_Y0 + 1 * MESH_ARROW_HEIGHT, MESH_ARROW_X0 + 1 * MESH_ARROW_WIDTH, MESH_ARROW_Y0 + 2 * MESH_ARROW_HEIGHT}, // PREV - {MESH_ARROW_X0 + 1 * MESH_ARROW_WIDTH, MESH_ARROW_Y0 + 1 * MESH_ARROW_HEIGHT, MESH_ARROW_X0 + 2 * MESH_ARROW_WIDTH, MESH_ARROW_Y0 + 2 * MESH_ARROW_HEIGHT}, // NEXT + {MESH_ARROW_X0 + 0 * MESH_ARROW_WIDTH, MESH_ARROW_Y0 + 1 * MESH_ARROW_HEIGHT, MESH_ARROW_X0 + 1 * MESH_ARROW_WIDTH, MESH_ARROW_Y0 + 2 * MESH_ARROW_HEIGHT}, // LEFT + {MESH_ARROW_X0 + 1 * MESH_ARROW_WIDTH, MESH_ARROW_Y0 + 1 * MESH_ARROW_HEIGHT, MESH_ARROW_X0 + 2 * MESH_ARROW_WIDTH, MESH_ARROW_Y0 + 2 * MESH_ARROW_HEIGHT}, // RIGHT {MESH_ARROW_X0 + 0 * MESH_ARROW_WIDTH, MESH_ARROW_Y0 + 2 * MESH_ARROW_HEIGHT, MESH_ARROW_X0 + 2 * MESH_ARROW_WIDTH, MESH_ARROW_Y0 + 3 * MESH_ARROW_HEIGHT}, // DOWN #else #ifdef KEYBOARD_ON_LEFT @@ -362,7 +362,7 @@ void meshSaveCallback(void) memcpy(meshData->oriData, meshData->curData, sizeof(meshData->oriData)); // sync mesh data } -static void meshUpdateIndex(MESH_KEY_VALUES key_num) +static void meshUpdateIndex(const MESH_KEY_VALUES key_num) { switch (key_num) { @@ -403,7 +403,7 @@ static bool meshSetValue(const float newMeshData) { if (meshData->curData[meshData->index] != newMeshData) { - mustStoreCmd("M421 I%d J%d Z%.3f\n", (meshData->rowsNum - 1) - meshData->row, newMeshData); // (meshData->rowsNum - 1) - meshData->row -> real row in eeprom + mustStoreCmd("M421 I%d J%d Z%.3f\n", meshData->col, (meshData->rowsNum - 1) - meshData->row, newMeshData); // (meshData->rowsNum - 1) - meshData->row -> real row in eeprom meshData->curData[meshData->index] = newMeshData; return true; } @@ -417,9 +417,9 @@ static void meshUpdateValueMinMax(void) for (uint16_t i = 0; i < meshData->dataSize; i++) { - if(meshData->curData[i] < meshData->valueMin) + if (meshData->curData[i] < meshData->valueMin) meshData->valueMin = meshData->curData[i]; - else if(meshData->curData[i] > meshData->valueMax) + else if (meshData->curData[i] > meshData->valueMax) meshData->valueMax = meshData->curData[i]; } @@ -769,11 +769,10 @@ void meshUpdateData(char * dataRow) void menuMeshEditor(void) { MESH_KEY_VALUES key_num = ME_KEY_IDLE; - uint16_t oldIndex; uint16_t curIndex; meshAllocData(); // allocates and initialize mesh data if not already allocated and initialized - oldIndex = curIndex = meshData->index; + curIndex = meshData->index; mustStoreCmd("M420 V1 T1\n"); // retrieve the mesh data @@ -804,17 +803,20 @@ void menuMeshEditor(void) case ME_KEY_RIGHT: case ME_KEY_INCREASE: meshUpdateIndex(key_num); + meshDrawGridCell(curIndex, true); // draw point with old index + curIndex = meshData->index; + meshDrawGridCell(curIndex, true); // draw point with new index + meshDrawInfo(false); break; case ME_KEY_EDIT: - { - if (coordinateIsKnown() == false) - probeHeightHome(); // home, disable ABL and raise nozzle + if (coordinateIsKnown() == false) + probeHeightHome(); // home, disable ABL and raise nozzle - meshSetValue(menuMeshTuner(meshData->col, (meshData->rowsNum - 1) - meshData->row, meshData->curData[curIndex])); // (meshData->rowsNum - 1) - meshData->row -> real row in eeprom + // call mesh tuner menu and set current mesh value, if changed + meshSetValue(menuMeshTuner(meshData->col, (meshData->rowsNum - 1) - meshData->row, meshData->curData[curIndex])); // (meshData->rowsNum - 1) - meshData->row -> real row in eeprom - meshDrawMenu(); - } + meshDrawMenu(); break; case ME_KEY_RESET: @@ -833,33 +835,19 @@ void menuMeshEditor(void) meshSave(); // save mesh changes to eeprom (if eeprom exists) break; - default: - break; - } + case ME_KEY_OK: + if (memcmp(meshData->oriData, meshData->curData, sizeof(meshData->oriData))) + meshSave(); // save mesh changes to eeprom (if eeprom exists) - if (key_num == ME_KEY_OK) - { - if (memcmp(meshData->oriData, meshData->curData, sizeof(meshData->oriData))) - meshSave(); // save mesh changes to eeprom (if eeprom exists) + meshDeallocData(); + CLOSE_MENU(); + break; - meshDeallocData(); - CLOSE_MENU(); + default: + break; } - else - { - if (curIndex != meshData->index) - { - curIndex = meshData->index; - - meshDrawGridCell(oldIndex, true); // draw point with old index - meshDrawGridCell(curIndex, true); // draw point with new index - meshDrawInfo(false); - - oldIndex = curIndex; - } - loopProcess(); - } + loopProcess(); } // restore default