Page 2 of 2

Re: Switch with more decisions

Posted: Mon Sep 16, 2024 7:48 pm
by mnfisher
A simple (rather contrived) example.

As Steve mentions - simulation is really not happening here.

There are some advantages over switch statements (at least for lots of options) - the code is smaller and the options run in constant time.
It also 'forces' the code to be split into multiple small macros rather than one large 'do-it-all' macro - which makes for more readable code.

I used an Arduino Nano - which has limited memory - so I can use 16 bit 'pointers'. If the MCU you are using has a larger address space JumpTable needs to be 32 bit. On the esp32 in might be necessary to 'cast' the macro address (FCM_Name) to a MX_UINT32 i.e. FCL_JUMPTABLE[0] = (MX_UINT32)FCM_Name;

One thing demonstrated here is the check on a option being defined - if(fn) - you can have a sparse table if needed.
I don't demonstrate passing arguments (or returning a result) - but it is certainly possible.


Martin

Re: Switch with more decisions

Posted: Sun Sep 29, 2024 5:18 pm
by S_VE
Thank You Martin