Obsidean_VM/04-InLavoro/9..... MASTER Transport/Standard Transport/FB495 - LIFO HMI Page.md

4.7 KiB


IF #CurrentPage <> #LastPage THEN
    #LastPage := #CurrentPage;
    
    #found := False;
    #index := -1;
    
    // Search if the current page is already in the history
    FOR #i := 1 TO #Length DO
        IF #PageHistory[#i] = #CurrentPage THEN
            #found := True;
            #index := #i; // Exit the loop as we found the page
            EXIT;
        END_IF;
    END_FOR;
    IF #found THEN
        // If the page is found, move the history to remove loops
        FOR #i := #index TO #Length DO
            #PageHistory[#i-#index] := #PageHistory[#i];
        END_FOR;
        #Length := #Length - #index;
    ELSE
        // If the page is new, add it to the history
        IF #Length < #MaxHistory THEN
            #Length := #Length + 1;
            FOR #i := #Length TO 1 BY -1 DO
                #PageHistory[#i] := #PageHistory[#i - 1];
            END_FOR;
            #PageHistory[0] := #CurrentPage;
        END_IF;
    END_IF;
    // Return the previous page
    #ReturnPage := #PageHistory[1];
ELSE
    // Special initial case if history is empty
    IF #Length=0 THEN
        #PageHistory[0] := #CurrentPage;
        #Length := 1;
        #ReturnPage := 1;
    END_IF;
END_IF;

IF #ReturnPage = 0 THEN
    #ReturnPage := 1;
END_IF;

Then on the FC10:

!Pasted image 20240919154404.png

we save the last page on the DBW66

Description:


This script manages the navigation history of Human-Machine Interface (HMI) pages using a Last-In-First-Out (LIFO) approach. It ensures efficient page tracking and allows users to navigate back to previous pages without encountering loops or redundant entries.

Key Functionalities:


  1. Page Change Detection:
    • The script checks if the current page (#CurrentPage) is different from the last visited page (#LastPage).
    • If there is a change, it updates the navigation history accordingly.
  2. Updating the Last Visited Page:
    • Assigns the value of #CurrentPage to #LastPage to keep track of the latest page.
  3. Searching the Current Page in History:
    • Initializes a flag #found to False and an index #index to -1.
    • Iterates through the #PageHistory array to check if the current page already exists in the history.
    • If found, sets #found to True, records the index, and exits the loop.
  4. Removing Loops in History:
    • If the current page is found in the history (#found is True):
      • Shifts the pages after the found index to the beginning of the #PageHistory array.
      • Updates the #Length of the history to reflect the removed pages, effectively eliminating loops.
  5. Adding a New Page to History:
    • If the current page is not found in the history:
      • Checks if the history length is less than the maximum allowed (#MaxHistory).
      • Increments the #Length of the history.
      • Shifts existing pages one position backward to make space at the top of the history stack.
      • Adds the current page to the top of the history (#PageHistory[0]).
  6. Setting the Return Page:
    • Assigns the value of #PageHistory[1] to #ReturnPage, which represents the previous page for navigation.
  7. Handling Initial Case:
    • If there is no page change and the history is empty (#Length = 0):
      • Initializes the history with the current page.
      • Sets the #ReturnPage to 1 as a default.
  8. Default Return Page:
    • Ensures that #ReturnPage is not zero; if it is, sets it to 1 to avoid invalid page references.

Usage Guidelines:


  • Navigation History Management:
    • Utilize this script to keep an accurate and loop-free history of HMI page navigation.
    • It is particularly useful for implementing "Back" functionality in HMI applications.
  • Customizing History Size:
    • Adjust the #MaxHistory variable to change the maximum number of pages stored in the history.
  • Understanding the History Stack:
    • The #PageHistory array operates as a stack where #PageHistory[0] is the most recently visited page.
    • Pages are pushed onto the stack when new pages are visited and popped off when navigating back.
  • Avoiding Navigation Errors:
    • The script ensures that there is always a valid page to return to by setting a default #ReturnPage if necessary.

Tips:


  • Preventing Loops:
    • The script removes any pages between the current position and a previously visited page to prevent navigation loops.

Conclusion:


This script effectively manages HMI page navigation by maintaining a clean history stack, allowing users to navigate backward efficiently. By preventing loops and redundant entries, it enhances the user experience in HMI applications.