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
Macro calling
-
alanwms
- Posts: 159
- http://meble-kuchenne.info.pl
- Joined: Fri Dec 04, 2020 2:29 pm
- Has thanked: 26 times
- Been thanked: 8 times
-
mnfisher
- Valued Contributor
- Posts: 2139
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 172 times
- Been thanked: 989 times
Re: Macro calling
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
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
-
mnfisher
- Valued Contributor
- Posts: 2139
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 172 times
- Been thanked: 989 times
Re: Macro calling
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
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
- Attachments
-
- macro_table_2.fcfx
- (12.26 KiB) Downloaded 34 times
-
- macro_table.fcfx
- (12.09 KiB) Downloaded 32 times
Re: Macro calling
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.
-
mnfisher
- Valued Contributor
- Posts: 2139
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 172 times
- Been thanked: 989 times
Re: Macro calling
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
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
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?
Thoughts?
-
mnfisher
- Valued Contributor
- Posts: 2139
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 172 times
- Been thanked: 989 times
Re: Macro calling
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
Can you post an example of what you are trying to do?
Martin