I present you with: General Purpose Terminal Function Calling (GPTFC for short)

Have you ever wondered how to use the terminal Widget to call any function you want in your code?
Now you can!!!

Write your function:

void function_MEMO(void) { 
    int memo = System.freeMemory();
    terminal.println("\nMEMORIA LIVRE: " + String(memo) + " (" + String((memo/1310.72), 0) + "%)");
    terminal.flush();
}

And just call it in the terminal:


You just need a few steps before doing it tho…

Here is a tutorial showing how to set up a terminal that run it’s own functions using a HASH algorithm to make it faster than just nested if statments, it makes way easier to add new functions to your terminal and not that giant nested if statments that is so hard to keep track of what you are doing and that you problaly will end up with a giant mess. First, you just need to copy and paste this header file (you can just open it on github and copy and paste LITERALLY, because it’s made in the old C language) https://github.com/troydhanson/uthash/blob/master/src/uthash.h or add the header file the normal way http://troydhanson.github.io/uthash/index.html

Now you create a structure to hold the funtions like this:

struct funcoes {
    unsigned int id; // <- It's HASH address or id
    void (*func)(void); // <- The pointer that will hold the function
    UT_hash_handle hh; // <- Necessary to HASH
};

And them you create a null pointer of that structure:

struct funcoes * FUNCOES = NULL;

Here is the “add function” function

void add_func(struct funcoes *f) {
    HASH_ADD_INT( FUNCOES, id, f );
} 

And here is the “find function” function

struct funcoes *find_func(unsigned int func_id) {
    struct funcoes *f;

    HASH_FIND_INT( FUNCOES, &func_id, f );  
    return f;
}

Now you just need a Hash function to generate the Hash addresses that you can make it your own, or use any other, but i used this one:

unsigned int hashAddressGen(String input) {
    char word[1000];
    input.toCharArray(word, input.length() + 1);
    unsigned int hashAddress = 5381;
    for (unsigned int counter = 0; word[counter]!='\0'; counter++){
        hashAddress = ((hashAddress << 5) + hashAddress) + word[counter];
    }
    return hashAddress;
}

Boom, now you have every thing that you need (remember to put all of this as global variables and functions).

The next steps are now just to add and use the hash:

Create your function:

void function_LIMP(void) { 
    terminal.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    terminal.flush();
}

Declare globaly a struct that holds that function and give it a Hash address:

struct funcoes FUNCAOLIMP = {
    hashAddressGen(String("LIMP")), // <- this is what is needed to be typed on the terminal
    function_LIMP
};

Now, on setup() you add this line:

add_func(&FUNCAOLIMP);

And your function is now ready to be called on the terminal:

String terminalInput;
bool recebeuInput;

BLYNK_WRITE(V14) { // <- Terminal virtual pin
    terminalInput = param.asStr();
    recebeuInput = true;
}

void loop() {
    Blynk.run();
    if(recebeuInput) {
        struct funcoes *f = find_func(hashAddressGen(terminalInput.toUpperCase())); // <- Find the function (NULL if not found)
        
        if(f != NULL) {
            f->func(); // <- Run the function
        } else {
            terminal.println("\nCOMANDO INVALIDO"); // <- Invalid command
            terminal.flush();
        }

        recebeuInput = false;
    }
}

And there you go, way easier to do code maintenance and add new functions to use in the terminal.

2 Likes

A very helpful post! Great work! I will be using this no doubt! Thank you!

1 Like

You are wellcome! :blush: