Announcement

Collapse
No announcement yet.

Call Function From flash_Exec() Call

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Call Function From flash_Exec() Call

    Hello,

    I'm looking to see if it's possible to call a function from FLASHBANK_0 (what I'm referring to as the "main flashbank," which loads when the display is power), from a function that was called in a separate flashback via flash_Exec(). The reason I am looking to do this is because I have developed some semi-universal support functions that I have thrown into other flashbanks that I use throughout a variety of my applications to reduce the code size of each main FLASHBANK_0 application. These support functions are things like drawing keypads, drawing keyboards, drawing icons, etc. Some of these are blocking support functions, for example drawing a keypad and responding to touch input, only returning to the main function once the enter key is pressed.

    This has worked well for me since it keeps my development modular, but now I've run into an instance where I'd like to run maintenance functions from within those flash_Exec() calls. A typical example would be responding to serial communications while the keypad is displayed. I've used function pointers within a flashbank successfully (see example below), but I'm struggling to understand how to operate them within a flash_Exec() call - or if it's even possible. The help documentation on flash_Exec seems to imply that the program in the current bank remains in memory, so I assumed I wouldn't have a limitation in doing this.

    Operational code (function pointers within single flashbank):
    Code:
    func main()
    
        gfx_ScreenMode(LANDSCAPE_R) ;
        var f1, f2;
        f1 := printFunction;
        f2 := printIntFunction;
        f1();
        f2(20);
        tryPointerToPointer(f1);
        repeat
        forever
    
    endfunc
    
    func printFunction()
        print("PrintFunction\n");
    endfunc
    
    func printIntFunction(var inputVal)
        print("PrintIntFunction: ", inputVal,"\n");
    endfunc
    
    func tryPointerToPointer(var functionPtr)
        functionPtr();
    endfunc
    Then, what I'm attempting, but failing. This seems to get to the point where I would expect it to call via a function pointer, but then repeats back on the FLASHBANK_3 main function, and eventually results in a stack overflow.
    Flashbank 3 code:
    Code:
    #MODE FLASHBANK_3
    func main(var functionPointer)
    
        print("Started Flashbank 3 Main\n");
        print("Starting Flashbank 0 Function Call\n");
        functionPointer();
        print("Completed Flashbank 0 Function Call\n");
        return;
    endfunc
    Main code:
    Code:
    func main()
    
        gfx_ScreenMode(LANDSCAPE_R) ;
        runFlashBank3Function();
        repeat
        forever
    
    endfunc
    
    func runFlashBank3Function()
        var kargs[2];
        kargs[0] := 1;
        kargs[1] := printFunction;
        flash_Exec(FLASHBANK_3,kargs);
    endfunc
    
    func printFunction()
        print("PrintFunction\n");
    endfunc
    What am I missing? Is this even possible?

  • #2
    Hi,

    Welcome to the forum.

    You cannot directly call functions of a flash bank from one to another. But fortunately, you can send value/s which you can utilize for calling a function. The key function to be used is the flash_Exec(flashbank, arglistptr).
    where ,
    flashbank - name of the bank to be executed
    arglistptr - pointer to the list of arguments to pass to the selected bank or 0 if no arguments

    With the use of this function, you can create flags from the argument values to be used in processing every function from different banks.

    To further understand, attached are sample programs for its implementation. Also, kindly read the comments on the code to be guided.

    Note: Run the program in "calledBank" first before running program in "MainProgram"

    Hope this helps,

    Best regards,
    Attached Files
    Vince_4D

    Comment


    • #3
      Vince,

      Thank you for the response regarding flash_Exec(), although I'm not certain I communicated my core question correctly. I am already using flash_Exec() to run helper functions in other banks. What I'm wondering is if there is a way to pass a function pointer or callback to that flash_Exec() from the main bank and be able to call that. I'm assuming from my testing and based on your response "You cannot directly call functions of a flash bank from one to another..." that this is a no.

      I'm assuming my best option then is to pull my maintenance functions into yet another flashbank that any flashbank can call. For example:
      Flashbank_0 calls Flashbank_1 functions for serial comms, encoder reads, output controls, etc.
      Flashbank_0 calls Flashbank_2 for one of my modules, for example to get keyboard input
      while the keyboard is displayed, Flashbank_2 continues to call Flashbank_1 to continue operating serial, encoders, etc.
      control returns to Flashbank_0 main, which then continues to use Flashbank_1 functions...

      And so on. That's not quite ideal, since the maintenance functions vary by application, but if I really need to do it this way, I believe that is a solution. Thank you for getting back to me!

      Comment


      • #4
        Hi,

        The code snippets below somehow execute what you want to implement, that is, as far as I understood, to call a function in the main bank from a different bank after executing flash_Exec(). What I did mainly is, execute the flash_exec() with callback data as one of the parameters and used the sent data in the bank by setting the callback parameter in "return" command. Then utilize the returned value in the main bank.
        Below are the codes to be implemented.

        Main Flash Bank
        Code:
        func main()
        
            bankCallBack(FLASHBANK_2, 1); // execute flashbank2 with callback parameter
            bankProceedTask(FLASHBANK_2);
            repeat
            forever
        
        endfunc
        
        func bankProceedTask(var bank)
            var kargs[2];
            kargs[0] := 1;
            kargs[1] := 2;
            flash_Exec(bank,kargs);
        endfunc
        
        
        func bankCallBack(var bank, var callBackFunc)
            var kargs[2];
            var i;
        
            kargs[0] := 2;
            kargs[1] := 1;              // cmd 1 for callBack
            kargs[2] := callBackFunc;
        
            i := flash_Exec(bank,kargs);
        
            switch(i)
                case 1:
                    func1();
                    break;
                case 2:
                    func2();
                    break;
            endswitch
        
        endfunc
        
        func func1()
            print("Routine in Main function 1\n");
        endfunc
        
        func func2()
            print("Routine in Main function 2\n");
        endfunc
        Called Flash Bank (In this case flash bank2)
        Code:
        #MODE FLASHBANK_2
        
        func main(var cmd, var callBack)
            var RetValue;
            switch(cmd)
                case 1:         // let cmd = 1 be a value to execute callback functions in main bank
                    RetValue := callBack;
                    break;
                case 2:         // let cmd = 2 be a value for running functions in current bank
                    myFunc();
                    break;
            endswitch
        
            return RetValue;    // will be catched from flash_Exec() func in main bank
        endfunc
        
        
        func myFunc()
            txt_FGcolour(WHITE);
            print("Proceed ...\n");
            print("Routin2 in bank 2 func\n");
        endfunc
        With regards to your routine, logically it's possible.

        Hope this will help and best regards.
        Vince_4D

        Comment

        Working...
        X