MX 2 лет назад
Родитель
Сommit
50bab0a818

+ 113 - 102
non_catalog_apps/prog_calculator/calculation_logic.c

@@ -6,35 +6,46 @@
 
 // global constant lookup table
 static const char* HEX_TO_BINARY_TABLE[16] = {
-    "0000", "0001", "0010", "0011",
-    "0100", "0101", "0110", "0111",
-    "1000", "1001", "1010", "1011",
-    "1100", "1101", "1110", "1111"
-};
+    "0000", 
+    "0001", 
+    "0010", 
+    "0011",
+    "0100", 
+    "0101", 
+    "0110", 
+    "0111",
+    "1000", 
+    "1001", 
+    "1010", 
+    "1011",
+    "1100", 
+    "1101", 
+    "1110", 
+    "1111"};
 
 bool decToBin(const char* decString, char* binaryResult, size_t resultSize) {
-    if (decString == NULL || binaryResult == NULL || resultSize < 2) {
+    if(decString == NULL || binaryResult == NULL || resultSize < 2) {
         return false; // Invalid pointers or insufficient result size
     }
 
     char* end;
     unsigned long num = strtoul(decString, &end, 10);
 
-    if (*end != '\0' || *decString == '\0') {
+    if(*end != '\0' || *decString == '\0') {
         return false; // Invalid decimal
     }
 
     // Calculate the number of bits
     size_t bitsNeeded = 0;
     unsigned long tempNum = num;
-    while (tempNum > 0) {
+    while(tempNum > 0) {
         bitsNeeded++;
         tempNum >>= 1;
     }
 
     // If the number is zero
-    if (num == 0) {
-        if (resultSize < 2) {
+    if(num == 0) {
+        if(resultSize < 2) {
             return false; // buffer
         }
         strcpy(binaryResult, "0");
@@ -42,14 +53,14 @@ bool decToBin(const char* decString, char* binaryResult, size_t resultSize) {
     }
 
     // check buffer
-    if (bitsNeeded >= resultSize) {
+    if(bitsNeeded >= resultSize) {
         return false;
     }
 
     binaryResult[bitsNeeded] = '\0'; // Null-terminate the result
 
     // fill the binary string from the end
-    for (int i = bitsNeeded - 1; i >= 0; i--) {
+    for(int i = bitsNeeded - 1; i >= 0; i--) {
         binaryResult[i] = (num & 1) + '0'; // the least significant bit
         num >>= 1;
     }
@@ -58,14 +69,14 @@ bool decToBin(const char* decString, char* binaryResult, size_t resultSize) {
 }
 
 bool decToHex(const char* decString, char* hexResult, size_t resultSize) {
-    if (decString == NULL || hexResult == NULL || resultSize == 0) {
+    if(decString == NULL || hexResult == NULL || resultSize == 0) {
         return false;
     }
 
     char* end;
     unsigned long num = strtoul(decString, &end, 10);
 
-    if (*end != '\0' || *decString == '\0') {
+    if(*end != '\0' || *decString == '\0') {
         return false;
     }
 
@@ -73,36 +84,36 @@ bool decToHex(const char* decString, char* hexResult, size_t resultSize) {
     size_t requiredSize = 1;
     unsigned long tempNum = num;
 
-    while (tempNum >= 16) {
+    while(tempNum >= 16) {
         requiredSize++;
         tempNum /= 16;
     }
 
-    if (requiredSize + 1 > resultSize) {
+    if(requiredSize + 1 > resultSize) {
         return false;
     }
 
     hexResult[requiredSize] = '\0';
 
     // convert to hexadecimal in reverse order
-    do {
+    do{
         int remainder = num % 16;
         hexResult[--requiredSize] = (remainder < 10) ? ('0' + remainder) : ('A' + (remainder - 10));
         num /= 16;
-    } while (num > 0);
+    } while(num > 0);
 
     return true;
 }
 
 bool decToChar(const char* decString, char* outChar) {
-    if (decString == NULL || outChar == NULL || *decString == '\0') {
+    if(decString == NULL || outChar == NULL || *decString == '\0') {
         return false;
     }
 
     char* end;
     unsigned long num = strtoul(decString, &end, 10);
 
-    if (*end != '\0' || num > 255) {
+    if(*end != '\0' || num > 255) {
         return false;
     }
 
@@ -113,15 +124,15 @@ bool decToChar(const char* decString, char* outChar) {
 bool hexDigitToBinary(char hexDigit, char* binary) {
     const char* hexToBinary = NULL;
 
-    if (hexDigit >= '0' && hexDigit <= '9') {
+    if(hexDigit >= '0' && hexDigit <= '9') {
         hexToBinary = HEX_TO_BINARY_TABLE[hexDigit - '0'];
-    } else if (hexDigit >= 'A' && hexDigit <= 'F') {
+    } else if(hexDigit >= 'A' && hexDigit <= 'F') {
         hexToBinary = HEX_TO_BINARY_TABLE[10 + hexDigit - 'A'];
-    } else if (hexDigit >= 'a' && hexDigit <= 'f') {
+    } else if(hexDigit >= 'a' && hexDigit <= 'f') {
         hexToBinary = HEX_TO_BINARY_TABLE[10 + hexDigit - 'a'];
     }
 
-    if (hexToBinary) {
+    if(hexToBinary) {
         memcpy(binary, hexToBinary, 5);
         return true;
     } else {
@@ -130,28 +141,28 @@ bool hexDigitToBinary(char hexDigit, char* binary) {
 }
 
 bool hexToBin(const char* hexString, char* binaryResult, size_t resultSize) {
-    if (hexString == NULL || binaryResult == NULL || resultSize == 0) {
+    if(hexString == NULL || binaryResult == NULL || resultSize == 0) {
         return false;
     }
 
     size_t requiredSize = strlen(hexString) * 4 + 1;
-    if (requiredSize > resultSize) {
+    if(requiredSize > resultSize) {
         return false;
     }
 
-    while (*hexString) {
+    while(*hexString) {
         const char* hexToBinary;
-        if (*hexString >= '0' && *hexString <= '9') {
+        if(*hexString >= '0' && *hexString <= '9') {
             hexToBinary = HEX_TO_BINARY_TABLE[*hexString - '0'];
-        } else if (*hexString >= 'A' && *hexString <= 'F') {
+        } else if(*hexString >= 'A' && *hexString <= 'F') {
             hexToBinary = HEX_TO_BINARY_TABLE[10 + *hexString - 'A'];
-        } else if (*hexString >= 'a' && *hexString <= 'f') {
+        } else if(*hexString >= 'a' && *hexString <= 'f') {
             hexToBinary = HEX_TO_BINARY_TABLE[10 + *hexString - 'a'];
         } else {
             return false;
         }
 
-        if (resultSize < 4) {
+        if(resultSize < 4) {
             return false;
         }
         memcpy(binaryResult, hexToBinary, 4);
@@ -165,26 +176,26 @@ bool hexToBin(const char* hexString, char* binaryResult, size_t resultSize) {
 }
 
 bool hexToDec(const char* hexString, int* outNum) {
-    if (hexString == NULL || outNum == NULL) {
+    if(hexString == NULL || outNum == NULL) {
         return false;
     }
 
     *outNum = 0;
-    while (*hexString) {
+    while(*hexString) {
         char digit = *hexString;
         int value;
 
-        if (digit >= '0' && digit <= '9') {
+        if(digit >= '0' && digit <= '9') {
             value = digit - '0';
-        } else if (digit >= 'A' && digit <= 'F') {
+        } else if(digit >= 'A' && digit <= 'F') {
             value = digit - 'A' + 10;
-        } else if (digit >= 'a' && digit <= 'f') {
+        } else if(digit >= 'a' && digit <= 'f') {
             value = digit - 'a' + 10;
         } else {
             return false;
         }
 
-        if (*outNum > INT_MAX / 16 || (*outNum == INT_MAX / 16 && value > INT_MAX % 16)) {
+        if(*outNum > INT_MAX / 16 || (*outNum == INT_MAX / 16 && value > INT_MAX % 16)) {
             return false; // check overflow
         }
 
@@ -196,16 +207,16 @@ bool hexToDec(const char* hexString, int* outNum) {
 }
 
 bool binToDec(const char* binaryString, int* decResult) {
-    if (binaryString == NULL || decResult == NULL) {
+    if(binaryString == NULL || decResult == NULL) {
         return false;
     }
 
     *decResult = 0;
-    for (const char* p = binaryString; *p; ++p) {
-        if (*p != '0' && *p != '1') {
+    for(const char* p = binaryString; *p; ++p) {
+        if(*p != '0' && *p != '1') {
             return false;
         }
-        if (*decResult > INT_MAX / 2) {
+        if(*decResult > INT_MAX / 2) {
             return false; // check overflow
         }
         *decResult = (*decResult << 1) | (*p - '0');
@@ -216,7 +227,7 @@ bool binToDec(const char* binaryString, int* decResult) {
 
 char binaryToHexDigit(const char* bin) {
     int value = 0;
-    for (int i = 0; i < 4; ++i) {
+    for(int i = 0; i < 4; ++i) {
         value = (value << 1) | (bin[i] - '0');
     }
 
@@ -224,14 +235,14 @@ char binaryToHexDigit(const char* bin) {
 }
 
 bool binToHex(const char* binaryString, char* hexResult, size_t resultSize) {
-    if (binaryString == NULL || hexResult == NULL || resultSize == 0) {
+    if(binaryString == NULL || hexResult == NULL || resultSize == 0) {
         return false;
     }
 
     size_t binLength = strlen(binaryString);
 
-    for (size_t i = 0; i < binLength; ++i) {
-        if (binaryString[i] != '0' && binaryString[i] != '1') {
+    for(size_t i = 0; i < binLength; ++i) {
+        if(binaryString[i] != '0' && binaryString[i] != '1') {
             snprintf(hexResult, resultSize, "Invalid Binary");
             return false;
         }
@@ -241,7 +252,7 @@ bool binToHex(const char* binaryString, char* hexResult, size_t resultSize) {
     size_t paddingZeros = (4 - (binLength % 4)) % 4;
     size_t totalChunks = (binLength + paddingZeros) / 4;
 
-    if (totalChunks > resultSize - 1) {
+    if(totalChunks > resultSize - 1) {
         return false;
     }
 
@@ -249,14 +260,14 @@ bool binToHex(const char* binaryString, char* hexResult, size_t resultSize) {
     char tempBin[5] = "0000";
     size_t tempIndex = 0;
 
-    for (size_t i = 0; i < binLength + paddingZeros; ++i) {
-        if (i < paddingZeros) {
+    for(size_t i = 0; i < binLength + paddingZeros; ++i) {
+        if(i < paddingZeros) {
             tempBin[tempIndex++] = '0';
         } else {
             tempBin[tempIndex++] = binaryString[i - paddingZeros];
         }
 
-        if (tempIndex == 4) {
+        if(tempIndex == 4) {
             hexResult[hexIndex++] = binaryToHexDigit(tempBin);
             tempIndex = 0;
         }
@@ -267,64 +278,64 @@ bool binToHex(const char* binaryString, char* hexResult, size_t resultSize) {
 }
 
 void calculate(Calculator* calculator_state) {
-    if (calculator_state == NULL) {
+    if(calculator_state == NULL) {
         return;
     }
 
     int num = 0;
     char result = '\0';
 
-    switch (calculator_state->mode) {
-        case ModeDecToBin:
-            if (!decToBin(calculator_state->text, calculator_state->binaryResult, sizeof(calculator_state->binaryResult))) {
-                snprintf(calculator_state->binaryResult, sizeof(calculator_state->binaryResult), "Invalid Dec");
-            }
-            break;
-
-        case ModeDecToHex:
-            if (!decToHex(calculator_state->text, calculator_state->hexResult, sizeof(calculator_state->hexResult))) {
-                snprintf(calculator_state->hexResult, sizeof(calculator_state->hexResult), "Invalid Dec");
-            }
-            break;
-
-        case ModeDecToChar:
-            if (decToChar(calculator_state->text, &result)) {
-                calculator_state->charResult[0] = result;
-                calculator_state->charResult[1] = '\0';
-            } else {
-                snprintf(calculator_state->charResult, sizeof(calculator_state->charResult), "Invalid Dec");
-            }
-            break;
-
-        case ModeHexToBin:
-            if (!hexToBin(calculator_state->text, calculator_state->binaryResult, sizeof(calculator_state->binaryResult))) {
-                snprintf(calculator_state->binaryResult, sizeof(calculator_state->binaryResult), "Invalid Hex");
-            }
-            break;
-
-        case ModeHexToDec:
-            if (hexToDec(calculator_state->text, &num)) {
-                snprintf(calculator_state->decResult, sizeof(calculator_state->decResult), "%d", num);
-            } else {
-                snprintf(calculator_state->decResult, sizeof(calculator_state->decResult), "Invalid Hex");
-            }
-            break;
-
-        case ModeBinToDec:
-            if (binToDec(calculator_state->text, &num)) {
-                snprintf(calculator_state->decResult, sizeof(calculator_state->decResult), "%d", num);
-            } else {
-                snprintf(calculator_state->decResult, sizeof(calculator_state->decResult), "Invalid Binary");
-            }
-            break;
-
-        case ModeBinToHex:
-            if (!binToHex(calculator_state->text, calculator_state->hexResult, sizeof(calculator_state->hexResult))) {
-                snprintf(calculator_state->hexResult, sizeof(calculator_state->hexResult), "Invalid Binary");
-            }
-            break;
-
-        default:
-            break;
+    switch(calculator_state->mode) {
+    case ModeDecToBin:
+        if(!decToBin(calculator_state->text, calculator_state->binaryResult, sizeof(calculator_state->binaryResult))) {
+            snprintf(calculator_state->binaryResult, sizeof(calculator_state->binaryResult), "Invalid Dec");
+        }
+        break;
+
+    case ModeDecToHex:
+        if(!decToHex(calculator_state->text, calculator_state->hexResult, sizeof(calculator_state->hexResult))) {
+            snprintf(calculator_state->hexResult, sizeof(calculator_state->hexResult), "Invalid Dec");
+        }
+        break;
+
+    case ModeDecToChar:
+        if(decToChar(calculator_state->text, &result)) {
+            calculator_state->charResult[0] = result;
+            calculator_state->charResult[1] = '\0';
+        } else {
+            snprintf(calculator_state->charResult, sizeof(calculator_state->charResult), "Invalid Dec");
+        }
+        break;
+
+    case ModeHexToBin:
+        if(!hexToBin(calculator_state->text, calculator_state->binaryResult, sizeof(calculator_state->binaryResult))) {
+            snprintf(calculator_state->binaryResult, sizeof(calculator_state->binaryResult), "Invalid Hex");
+        }
+        break;
+
+    case ModeHexToDec:
+        if(hexToDec(calculator_state->text, &num)) {
+            snprintf(calculator_state->decResult, sizeof(calculator_state->decResult), "%d", num);
+        } else {
+            snprintf(calculator_state->decResult, sizeof(calculator_state->decResult), "Invalid Hex");
+        }
+        break;
+
+    case ModeBinToDec:
+        if(binToDec(calculator_state->text, &num)) {
+            snprintf(calculator_state->decResult, sizeof(calculator_state->decResult), "%d", num);
+        } else {
+            snprintf(calculator_state->decResult, sizeof(calculator_state->decResult), "Invalid Binary");
+        }
+        break;
+
+    case ModeBinToHex:
+        if(!binToHex(calculator_state->text, calculator_state->hexResult, sizeof(calculator_state->hexResult))) {
+            snprintf(calculator_state->hexResult, sizeof(calculator_state->hexResult), "Invalid Binary");
+        }
+        break;
+
+    default:
+        break;
     }
 }

+ 5 - 4
non_catalog_apps/prog_calculator/init_cleanup.c

@@ -19,7 +19,6 @@ Calculator* initialize_calculator_state() {
     return state;
 }
 
-
 ViewPort* initialize_view_port(Calculator* calculator_state, FuriMessageQueue* event_queue) {
     ViewPort* view_port = view_port_alloc();
     view_port_draw_callback_set(view_port, calculator_draw_callback, calculator_state);
@@ -28,15 +27,17 @@ ViewPort* initialize_view_port(Calculator* calculator_state, FuriMessageQueue* e
     return view_port;
 }
 
-
 Gui* initialize_gui(ViewPort* view_port) {
     Gui* gui = furi_record_open(RECORD_GUI);
     gui_add_view_port(gui, view_port, GuiLayerFullscreen);
     return gui;
 }
 
-
-void cleanup_resources(Calculator* calculator_state, ViewPort* view_port, Gui* gui, FuriMessageQueue* event_queue) {
+void cleanup_resources(
+    Calculator* calculator_state, 
+    ViewPort* view_port, 
+    Gui* gui, 
+    FuriMessageQueue* event_queue) {
     gui_remove_view_port(gui, view_port);
     view_port_free(view_port);
     furi_mutex_free(calculator_state->mutex);

+ 5 - 1
non_catalog_apps/prog_calculator/init_cleanup.h

@@ -9,6 +9,10 @@ FuriMessageQueue* initialize_event_queue();
 Calculator* initialize_calculator_state();
 ViewPort* initialize_view_port(Calculator* calculator_state, FuriMessageQueue* event_queue);
 Gui* initialize_gui(ViewPort* view_port);
-void cleanup_resources(Calculator* calculator_state, ViewPort* view_port, Gui* gui, FuriMessageQueue* event_queue);
+void cleanup_resources(
+    Calculator* calculator_state, 
+    ViewPort* view_port, 
+    Gui* gui, 
+    FuriMessageQueue* event_queue);
 
 #endif // INIT_CLEANUP_H

+ 84 - 84
non_catalog_apps/prog_calculator/input_handling.c

@@ -21,60 +21,60 @@ void calculator_input_callback(InputEvent* input_event, void* ctx) {
 
 void handle_short_press(Calculator* calculator_state, ViewPort* view_port, InputEvent* event) {
     switch(event->key) {
-        case InputKeyUp:
-            if(calculator_state->position.y > 0) {
-                if(calculator_state->position.y == 2) {
-                    calculator_state->position.x = 0;
-                }
-                calculator_state->position.y--;
-                if(calculator_state->position.y == 1) { // If cursor moves to row 2, skip it
-                    calculator_state->position.y--;
-                }
-            }   
-            break;
-        case InputKeyDown:
-            if(calculator_state->position.y < 6 - 1) {
-                if(calculator_state->position.y == 6 - 2 && 
-                   (calculator_state->position.x == 3 || calculator_state->position.x == 4)) {
-                    calculator_state->position.y = 6 - 1;
-                    calculator_state->position.x = 3;
-                } else {
-                    calculator_state->position.y++;
-                }
-                if(calculator_state->position.y == 1) { // If cursor moves to row 2, skip it
-                    calculator_state->position.y++;
-                }
-            }
-            break;
-        case InputKeyLeft:
-            if(calculator_state->position.y > 1 && calculator_state->position.x > 0) {
-                calculator_state->position.x--;
+    case InputKeyUp:
+        if(calculator_state->position.y > 0) {
+            if(calculator_state->position.y == 1) {
+                calculator_state->position.x = 0;
             }
-            break;
-        case InputKeyRight:
-            if(calculator_state->position.y < 2) {
-                // Cursor stays in the same column
-            } else if(calculator_state->position.y == 6 - 1) {
-                if(calculator_state->position.x < 3) {
-                    calculator_state->position.x++;
-                }
+            calculator_state->position.y--;
+            // if(calculator_state->position.y == 1) { // If cursor moves to row 2, skip it
+            //     calculator_state->position.y--;
+            // }
+        }   
+        break;
+    case InputKeyDown:
+        if(calculator_state->position.y < 5 - 1) {
+            if(calculator_state->position.y == 5 - 2 && 
+                (calculator_state->position.x == 3 || calculator_state->position.x == 4)) {
+                calculator_state->position.y = 5 - 1;
+                calculator_state->position.x = 3;
             } else {
-                if(calculator_state->position.x < 6 - 1) {
-                    calculator_state->position.x++;
-                }
+                calculator_state->position.y++;
             }
-            break;
-        case InputKeyOk:
-            if(calculator_state->position.y == 0) {
-                toggle_mode(calculator_state);
-            } else {
-                char key = getKeyAtPosition(calculator_state->position.x, calculator_state->position.y);
-                handle_key_press(calculator_state, key);
+            // if(calculator_state->position.y == 1) { // If cursor moves to row 2, skip it
+            //     calculator_state->position.y++;
+            // }
+        }
+        break;
+    case InputKeyLeft:
+        if(calculator_state->position.y > 0 && calculator_state->position.x > 0) {
+            calculator_state->position.x--;
+        }
+        break;
+    case InputKeyRight:
+        if(calculator_state->position.y < 1) {
+            // Cursor stays in the same column
+        } else if(calculator_state->position.y == 5 - 1) {
+            if(calculator_state->position.x < 3) {
+                calculator_state->position.x++;
             }
-            break;
+        } else {
+            if(calculator_state->position.x < 5 - 1) {
+                calculator_state->position.x++;
+            }
+        }
+        break;
+    case InputKeyOk:
+        if(calculator_state->position.y == 0) {
+            toggle_mode(calculator_state);
+        } else {
+            char key = getKeyAtPosition(calculator_state->position.x, calculator_state->position.y);
+            handle_key_press(calculator_state, key);
+        }
+        break;
 
-        default:
-            break;
+    default:
+        break;
     }
 
     view_port_update(view_port);
@@ -99,44 +99,44 @@ void handle_long_press(Calculator* calculator_state, ViewPort* view_port, InputE
 
 void handle_key_press(Calculator* calculator_state, char key) {
     switch(key) {
-        case '=':
-            // Logic for '=' key
-            strncpy(calculator_state->originalInput, calculator_state->text, MAX_TEXT_LENGTH);
-            calculate(calculator_state);
-            // calculator_state->text[0] = '\0';
-            calculator_state->textLength = 0;
-            break;
-        case 'R':
-            // Logic for 'R' key, typically 'Clear'
+    case '=':
+        // Logic for '=' key
+        strncpy(calculator_state->originalInput, calculator_state->text, MAX_TEXT_LENGTH);
+        calculate(calculator_state);
+        // calculator_state->text[0] = '\0';
+        calculator_state->textLength = 0;
+        break;
+    case 'R':
+        // Logic for 'R' key, typically 'Clear'
+        calculator_state->text[0] = '\0';
+        calculator_state->textLength = 0;
+        calculator_state->binaryResult[0] = '\0';  // Clear binary result
+        calculator_state->decResult[0] = '\0';     // Clear binary result
+        calculator_state->charResult[0] = '\0';    // Clear binary result
+        calculator_state->hexResult[0] = '\0';     // Clear hex result
+        calculator_state->newInputStarted = false;
+        break;
+    case '<':
+        // Logic for '<' key, typically 'Backspace'
+        if(calculator_state->textLength > 0) {
+            calculator_state->text[--calculator_state->textLength] = '\0';
+        }
+        calculator_state->newInputStarted = false;
+        break;
+    default:
+        // Default logic for number and operator keys
+        if(calculator_state->newInputStarted) {
+            // Reset the text for a fresh input if new input has started
             calculator_state->text[0] = '\0';
             calculator_state->textLength = 0;
-            calculator_state->binaryResult[0] = '\0';  // Clear binary result
-            calculator_state->decResult[0] = '\0';     // Clear binary result
-            calculator_state->charResult[0] = '\0';    // Clear binary result
-            calculator_state->hexResult[0] = '\0';     // Clear hex result
             calculator_state->newInputStarted = false;
-            break;
-        case '<':
-            // Logic for '<' key, typically 'Backspace'
-            if(calculator_state->textLength > 0) {
-                calculator_state->text[--calculator_state->textLength] = '\0';
-            }
-            calculator_state->newInputStarted = false;
-            break;
-        default:
-            // Default logic for number and operator keys
-            if(calculator_state->newInputStarted) {
-                // Reset the text for a fresh input if new input has started
-                calculator_state->text[0] = '\0';
-                calculator_state->textLength = 0;
-                calculator_state->newInputStarted = false;
-            }
-            // Add the new character to the text, respecting the maximum text length
-            if(calculator_state->textLength < MAX_TEXT_LENGTH - 1) {
-                calculator_state->text[calculator_state->textLength++] = key;
-                calculator_state->text[calculator_state->textLength] = '\0';
-            }
-            break;
+        }
+        // Add the new character to the text, respecting the maximum text length
+        if(calculator_state->textLength < MAX_TEXT_LENGTH - 1) {
+            calculator_state->text[calculator_state->textLength++] = key;
+            calculator_state->text[calculator_state->textLength] = '\0';
+        }
+        break;
     }
 }
 

+ 4 - 1
non_catalog_apps/prog_calculator/input_handling.h

@@ -10,6 +10,9 @@ void calculator_input_callback(InputEvent* input_event, void* ctx);
 void handle_short_press(Calculator* calculator_state, ViewPort* view_port, InputEvent* event);
 void handle_long_press(Calculator* calculator_state, ViewPort* view_port, InputEvent* event);
 void handle_key_press(Calculator* calculator_state, char key);
-void handle_event(Calculator* calculator_state, ViewPort* view_port, InputEvent* event);
+void handle_event(
+    Calculator* calculator_state, 
+    ViewPort* view_port, 
+    InputEvent* event);
 
 #endif // INPUT_HANDLING_H

+ 101 - 80
non_catalog_apps/prog_calculator/ui_drawing.c

@@ -4,7 +4,7 @@
 #include "calculator.h"
 
 void drawElement(Canvas* canvas, const char* str, int x, int y, int width, int height, Font font) {
-    if (str) {
+    if(str) {
         canvas_set_font(canvas, font);
         canvas_draw_str(canvas, x, y, str);
     } else {
@@ -14,32 +14,30 @@ void drawElement(Canvas* canvas, const char* str, int x, int y, int width, int h
 
 void generate_calculator_layout(Canvas* canvas) {
 
-
     // display
-    canvas_draw_frame(canvas, 0, 0, 64, 49);  // display frame 
+    canvas_draw_frame(canvas, 0, 0, 64, 62);  // display frame
     canvas_draw_frame(canvas, 2, 2, 60, 31);  // output  frame
-    canvas_draw_frame(canvas, 2, 34, 60, 13); // input   frame
+    canvas_draw_frame(canvas, 2, 34, 60, 26); // input   frame
 
     // Horizonal and Vertical lines
-    drawElement(canvas, NULL, 0, 48, 64, 2, 0);  // H line 1
-    drawElement(canvas, NULL, 0, 61, 64, 1, 0);  // H line 2
+    // drawElement(canvas, NULL, 0, 48, 64, 2, 0);  // H line 1
+    drawElement(canvas, NULL, 0, 61, 64, 2, 0);  // H line 2
     drawElement(canvas, NULL, 0, 73, 64, 2, 0);  // H line 3
     drawElement(canvas, NULL, 0, 87, 64, 1, 0);  // H line 4
     drawElement(canvas, NULL, 0, 100, 64, 1, 0); // H line 5
     drawElement(canvas, NULL, 0, 113, 64, 1, 0); // H line 6
     drawElement(canvas, NULL, 0, 126, 64, 2, 0); // H line 7
     
-    drawElement(canvas, NULL, 0, 50, 2, 80, 0);  // V line 1
-    drawElement(canvas, NULL, 13, 73, 1, 55, 0); // V line 2
+    drawElement(canvas, NULL, 0, 61, 2, 65, 0);  // V line 1
+    drawElement(canvas, NULL, 13, 61, 1, 65, 0); // V line 2
     drawElement(canvas, NULL, 25, 73, 1, 55, 0); // V line 3
     drawElement(canvas, NULL, 37, 73, 2, 55, 0); // V line 4
     drawElement(canvas, NULL, 50, 73, 1, 41, 0); // V line 5
-    drawElement(canvas, NULL, 62, 48, 2, 81, 0); // V line 6
+    drawElement(canvas, NULL, 62, 61, 2, 65, 0); // V line 6
 
     // MODE key and display
     // row 1 and 2
-    drawElement(canvas, "MODE", 19, 59, 0, 0, FontSecondary);
-    drawElement(canvas, "", 5, 38, 0, 0, FontSecondary);
+    drawElement(canvas, "", 4, 72, 0, 0, FontSecondary);
 
     // Keys
     // row 3
@@ -75,38 +73,40 @@ void draw_highlighted_cell(Canvas* canvas, short x, short y, short width, short
 }
 
 void displayResult(Canvas* canvas, char* result, int x, int y) {
+    const int maxLength = 24; // Maximum length for splitting into lines
+    const int lineLength = 12; // Length of each line
+    const int lineSpacing = 9; // Reduced line spacing
+
     int length = strlen(result);
-    int lineSpacing = 9; // Reduced line spacing
 
-    if (length > 22) {
-        char line1[12];
-        strncpy(line1, result, 11);
-        line1[11] = '\0';
+    if(length > maxLength) {
+        char line1[lineLength + 1];
+        strncpy(line1, result, lineLength);
+        line1[lineLength] = '\0';
 
-        char line2[12];
-        strncpy(line2, result + 11, 11);
-        line2[11] = '\0';
+        char line2[lineLength + 1];
+        strncpy(line2, result + lineLength, lineLength);
+        line2[lineLength] = '\0';
 
-        const char* line3 = result + 22;
+        const char* line3 = result + (2 * lineLength);
 
         canvas_draw_str(canvas, x, y, line1);
         canvas_draw_str(canvas, x, y + lineSpacing, line2);
-        canvas_draw_str(canvas, x, y + 2 * lineSpacing, line3);
-    } else if (length > 11) {
-        char line1[12];
-        strncpy(line1, result, 11);
-        line1[11] = '\0';
+        canvas_draw_str(canvas, x, y + (2 * lineSpacing), line3);
+    } else if(length > lineLength) {
+        char line1[lineLength + 1];
+        strncpy(line1, result, lineLength);
+        line1[lineLength] = '\0';
 
-        const char* line2 = result + 11;
+        const char* line2 = result + lineLength;
 
         canvas_draw_str(canvas, x, y, line1);
         canvas_draw_str(canvas, x, y + lineSpacing, line2);
     } else {
-        canvas_draw_str(canvas, x, y, result); // Single line if 11 characters or less
+        canvas_draw_str(canvas, x, y, result); // Single line if lineLength characters or less
     }
 }
 
-
 void calculator_draw_callback(Canvas* canvas, void* ctx) {
     furi_assert(ctx);
     const Calculator* calculator_state = ctx;
@@ -119,82 +119,103 @@ void calculator_draw_callback(Canvas* canvas, void* ctx) {
     generate_calculator_layout(canvas);
 
     char resultLabel[2 * MAX_TEXT_LENGTH]; // Buffer to hold the result label
+    char modeNumber[3]; // Buffer to hold the mode number
 
     // Check which mode is selected and prepare the label accordingly
-    if(calculator_state->mode == ModeHexToDec) {
-        snprintf(resultLabel, sizeof(resultLabel), "Dec: %s", calculator_state->decResult);
-    } else if(calculator_state->mode == ModeDecToBin) {
-        snprintf(resultLabel, sizeof(resultLabel), "Bin: %s", calculator_state->binaryResult);
-    } else if(calculator_state->mode == ModeDecToChar) {
-        snprintf(resultLabel, sizeof(resultLabel), "Charachter %s", calculator_state->charResult);
-    } else if(calculator_state->mode == ModeHexToBin) {
-        snprintf(resultLabel, sizeof(resultLabel), "Bin: %s", calculator_state->binaryResult);
-    } else if(calculator_state->mode == ModeDecToHex) {
-        snprintf(resultLabel, sizeof(resultLabel), "Hex: %s", calculator_state->hexResult);
-    } else if(calculator_state->mode == ModeBinToDec) {
-        snprintf(resultLabel, sizeof(resultLabel), "Dec: %s", calculator_state->decResult);
-    } else if(calculator_state->mode == ModeBinToHex) {
-        snprintf(resultLabel, sizeof(resultLabel), "Hex: %s", calculator_state->hexResult);
-    } else {
+    switch(calculator_state->mode) {
+    case ModeDecToBin:
+        snprintf(resultLabel, sizeof(resultLabel), "> %s", calculator_state->binaryResult);
+        strcpy(modeNumber, "1");
+        break;
+    case ModeDecToHex:
+        snprintf(resultLabel, sizeof(resultLabel), "> %s", calculator_state->hexResult);
+        strcpy(modeNumber, "2");
+        break;
+    case ModeDecToChar:
+        snprintf(resultLabel, sizeof(resultLabel), "> %s", calculator_state->charResult);
+        strcpy(modeNumber, "3");
+        break;
+    case ModeHexToBin:
+        snprintf(resultLabel, sizeof(resultLabel), "> %s", calculator_state->binaryResult);
+        strcpy(modeNumber, "4");
+        break;
+    case ModeHexToDec:
+        snprintf(resultLabel, sizeof(resultLabel), "> %s", calculator_state->decResult);
+        strcpy(modeNumber, "5");
+        break;
+    case ModeBinToDec:
+        snprintf(resultLabel, sizeof(resultLabel), "> %s", calculator_state->decResult);
+        strcpy(modeNumber, "6");
+        break;
+    case ModeBinToHex:
+        snprintf(resultLabel, sizeof(resultLabel), "> %s", calculator_state->hexResult);
+        strcpy(modeNumber, "7");
+        break;
+    default:
         // If no mode is selected, you can display a default message or leave it empty
-        strncpy(resultLabel, "Click MODE Programmer Calc v0.8", sizeof(resultLabel));
+        strncpy(resultLabel, "Click [M]ODE_________   <PCalc v0.8>", sizeof(resultLabel));
+        strcpy(modeNumber, "M");
+        break;
     }
 
     // Display the result, splitting into two lines if necessary
     displayResult(canvas, resultLabel, 5, 12);
 
     // Draw new input with ">" label or mode selection prompt
-    char inputLabel[MAX_TEXT_LENGTH + 3]; // Adjusted size for "> "
-    
-    snprintf(inputLabel, sizeof(inputLabel), "$> %s", calculator_state->text);
+    char inputLabel[MAX_TEXT_LENGTH + 1]; // Adjusted size for "> "
+    snprintf(inputLabel, sizeof(inputLabel), ">%s", calculator_state->text);
     canvas_draw_str(canvas, 5, 44, inputLabel);
+
+    // Replace "M" with the current mode number
+    canvas_draw_str(canvas, 5, 72, modeNumber); // Position where "M" was originally drawn
+
     // Define the cell dimensions for each row and column
-    const short cellDimensions[6][5][2] = {
+    const short cellDimensions[5][5][2] = {
         // {Width, Height} for each cell
         {{12, 13}, {0, 0}, {0, 0}, {0, 0}, {0, 0}}, // Row 1 (One column)
-        {{9, 13}, {0, 0}, {0, 0}, {0, 0}, {0, 0}}, // Row 2 (One column)
+        {{12, 13}, {12, 13}, {12, 13}, {12, 13}, {12, 13}}, // Row 2
         {{12, 13}, {12, 13}, {12, 13}, {12, 13}, {12, 13}}, // Row 3
         {{12, 13}, {12, 13}, {12, 13}, {12, 13}, {12, 13}}, // Row 4
-        {{12, 13}, {12, 13}, {12, 13}, {12, 13}, {12, 13}}, // Row 5
-        {{12, 13}, {12, 13}, {12, 13}, {24, 13}, {0, 0}}, // Row 6, with different width for column 4
+        {{12, 13}, {12, 13}, {12, 13}, {24, 13}, {0, 0}}, // Row 5, with different width for column 4
     };
 
     const char* modeStr = "";
     switch(calculator_state->mode) {
-        case ModeDecToBin:
-            modeStr = " 1: Dec >> Bin";
-            break;
-        case ModeDecToHex:
-            modeStr = "2: Dec >> Hex";
-            break;
-        case ModeDecToChar:
-            modeStr = "3: Dec >> Char";
-            break;
-        case ModeHexToBin:
-            modeStr = "4: Hex >> Bin";
-            break;
-        case ModeHexToDec:
-            modeStr = "5: Hex >> Dec";
-            break;
-        case ModeBinToDec:
-            modeStr = "6: Bin >> Dec";
-            break;
-        case ModeBinToHex:
-            modeStr = "7: Bin >> Hex";
-            break;
-        default:
-            modeStr = "      waiting ...";
-            break;
+    case ModeDecToBin:
+        modeStr = "Dec > Bin";
+        break;
+    case ModeDecToHex:
+        modeStr = "Dec > Hex";
+        break;
+    case ModeDecToChar:
+        modeStr = "Dec > Char";
+        break;
+    case ModeHexToBin:
+        modeStr = "Hex > Bin";
+        break;
+    case ModeHexToDec:
+        modeStr = "Hex > Dec";
+        break;
+    case ModeBinToDec:
+        modeStr = "Bin > Dec";
+        break;
+    case ModeBinToHex:
+        modeStr = "Bin > Hex";
+        break;
+    default:
+        modeStr = " waiting ...";
+        break;
     }
-    canvas_draw_str(canvas, 4, 71, modeStr);
+    
+    canvas_draw_str(canvas, 15, 72, modeStr);
     short cursorX = 2;
-    short cursorY = 48; // Starting Y position
+    short cursorY = 61; // Starting Y position
 
-    for (int i = 0; i < calculator_state->position.y; i++) {
+    for(int i = 0; i < calculator_state->position.y; i++) {
         cursorY += cellDimensions[i][0][1]; // Add the height of each previous row
     }
 
-    for (int i = 0; i < calculator_state->position.x; i++) {
+    for(int i = 0; i < calculator_state->position.x; i++) {
         cursorX += cellDimensions[calculator_state->position.y][i][0]; // Add the width of each previous column
     }
 
@@ -217,4 +238,4 @@ void calculator_draw_callback(Canvas* canvas, void* ctx) {
     canvas_draw_str(canvas, textX, textY, cellContent);
 
     furi_mutex_release(calculator_state->mutex);
-}
+}

+ 53 - 30
non_catalog_apps/prog_calculator/utilities.c

@@ -3,12 +3,11 @@
 
 char getKeyAtPosition(short x, short y) {
     const char keys[6][5] = {
-        {'*', ' ', ' ', ' ', ' '}, // Row 1 (MODE key)
-        {' ', ' ', ' ', ' ', ' '}, // Row 2 (display row)
-        {'7', '8', '9', 'A', 'B'}, // Row 3
-        {'4', '5', '6', 'C', 'D'}, // Row 4
-        {'1', '2', '3', 'E', 'F'}, // Row 5
-        {'<', '0', '=', 'R', ' '}  // Row 6
+        {'M', ' ', ' ', ' ', ' '}, // Row 1 (MODE key)
+        {'7', '8', '9', 'A', 'B'}, // Row 2
+        {'4', '5', '6', 'C', 'D'}, // Row 3
+        {'1', '2', '3', 'E', 'F'}, // Row 4
+        {'<', '0', '=', 'R', ' '}  // Row 5
     };
 
     return (y < 6 && x < 5) ? keys[y][x] : ' ';
@@ -16,31 +15,55 @@ char getKeyAtPosition(short x, short y) {
 
 short calculateStringWidth(const char* str, short length) {
     short width = 0;
-    for (short i = 0; i < length; i++) {
+    for(short i = 0; i < length; i++) {
         switch (str[i]) {
-            case '*':width += 4;break;
-            case '0':width += 4;break;
-            case '1':width += 3;break;
-            case '2':
-            case '3':
-            case '4':width += 4;break;
-            case '5':width += 4;break;
-            case '6':width += 4;break;
-            case '7':width += 4;break;
-            case '8':width += 4;break;
-            case '9':width += 4;break;
-            case 'A':
-            case 'B':
-            case 'C':
-            case 'D':
-            case 'E':
-            case 'F':
-            case '<':width += 3;break;
-            case '=':width += 4;break;
-            case 'R':width += 9;break;
-            default:
-                width += 3;
-                break;
+        case 'M':
+            width += 5;
+            break;
+        case '0':
+            width += 4;
+            break;
+        case '1':
+            width += 3;
+            break;
+        case '2':
+        case '3':
+        case '4':
+            width += 4;
+            break;
+        case '5':
+            width += 4;
+            break;
+        case '6':
+            width += 4;
+            break;
+        case '7':
+            width += 4;
+            break;
+        case '8':
+            width += 4;
+            break;
+        case '9':
+            width += 4;
+            break;
+        case 'A':
+        case 'B':
+        case 'C':
+        case 'D':
+        case 'E':
+        case 'F':
+        case '<':
+            width += 3;
+            break;
+        case '=':
+            width += 4;
+            break;
+        case 'R':
+            width += 9;
+            break;
+        default:
+            width += 3;
+            break;
         }
         width += 1;
     }