Page 1 of 1
Macro calling
Posted: Sat Aug 22, 2026 5:44 pm
by alanwms
I would like to call a macro based on a table entry. So if I select table_string[7] which indicates the value of "macro1" for instance, I would like to call macro1.
Currently, calling a macro is singular and not based on a variable finding. Do I need C for this? I can't simulate C in my simulator.
Running FC version 9
Re: Macro calling
Posted: Sat Aug 22, 2026 5:49 pm
by mnfisher
It is certainly do-able using a little C - but no it won't simulate
You need to create a table (using an array) of the macros you want to call addresses:
I used this technique at
viewtopic.php?p=398#p398
Can do a demo if it helps...
Martin
Re: Macro calling
Posted: Sat Aug 22, 2026 6:15 pm
by mnfisher
I did a small demo - I have two macros (which take an integer argument) - and these are called on .i % 2 (so 0, 1, 0, 1 as .i increments)
There are some advantages doing this compared to (for example) a switch - constant speed and smaller code size....
I used a small (10 entry) macro table - and I cast the macro addresses to a UINT32 (on an Arduino 16 bits are sufficient - other MCUs (esp32) need 32 bits...
The key is the function signature - and I would probably use a table (in supplementary code)
void (*fn[10])(MX_UINT32);
fn[0] = FCM_MACRO1;
...
or (more succinctly)
void (*fn[10])(MX_UINT32) = {FCM_Macro1, FCM_Macro2, ...};
then I can call the functions using:
lookup[index_value](argument); Where index could be FCL_I, and argument FCL_ARG for example.
macro_table_2.fcfx demonstrates using supplementary code.
Something to watch - never allow a call to either 0 or other random (unassigned address) - in the FC variant I test for 0 - but don't in the _2 variant.
The functions (macros) can also return a value if needed...
Martin
Re: Macro calling
Posted: Sat Aug 22, 2026 8:34 pm
by alanwms
I got the system running with the switch. The code is bulky with quite a few decisions.. I figured matching the command with a table list would make the code easier and more efficient.
Re: Macro calling
Posted: Sat Aug 22, 2026 8:45 pm
by mnfisher
Yes - if there are multiple options then there can be a significant reduction in code size compared to switch / if statements. As mentioned above it is also quicker
One possible drawback is that all functions need to have the same 'signature' (there are probably ways around this but...) It also opens up self modifying code - and a world of pain lies that way...
I've used the technique several times (and it's a pity that FC doesn't allow pointers - although they do mean that the user can cause lots of issues / have more fun (delete as appropriate) - perhaps there should be a 'I accept liability' tick box option to allow such features?
Martin
Re: Macro calling
Posted: Sat Aug 22, 2026 10:49 pm
by alanwms
Thanks. I'm now clarified...
Re: Macro calling
Posted: Sun Aug 23, 2026 4:09 pm
by alanwms
I can call you the macro but I have to add FCM_ at the beginning. My macro names are in the array table so I want to collect that macro name and call it. I'm not sure how to concatenate the FCM_ to the called name.
Thoughts?
Re: Macro calling
Posted: Sun Aug 23, 2026 4:18 pm
by mnfisher
I'm not sure i follow - the array doesn't hold the macro names, but the addresses of the macros? Calling macros by name of more an interpreter language type of thing...
Can you post an example of what you are trying to do?
Martin