Compiler directives in Arrays
vining
Posts: 4,368
I've never tried using the compiler directives in this manner before and since it worked I figured I'd post it.
This is a query array that I use to send a rolling query where I sequentially index through the array when on the device's page and comms are idle. Depending on equipment configuration in this case pool equipment some queries aren't necassary since they may or may not have a spa, heat or solar heating. This led me to try the compiler directives inside the query array to include only what would be necassary.
This is a query array that I use to send a rolling query where I sequentially index through the array when on the device's page and comms are idle. Depending on equipment configuration in this case pool equipment some queries aren't necassary since they may or may not have a spa, heat or solar heating. This led me to try the compiler directives inside the query array to include only what would be necassary.
#DEFINE POOL_INCL_HEAT #DEFINE POOL_INCL_SPA //#DEFINE POOL_INCL_SOLAR_HEAT DEFINE_VARIABLE //TX_POLLING_QUERY CMD_ARRY CHAR nPOOL_LEN_QUERY_ARRY = 18 ; CHAR cPOOL_QUERY_ARRY[][10]= { (*01*) 'POOLTMP' // #IF_DEFINED POOL_INCL_SPA (*02*) ,'SPATMP' // #END_IF // (*03*) ,'AIRTMP' // (*04*) ,'PUMPLO' // (*05*) ,'PUMPHI' // (*06*) ,'PUMP' // #IF_DEFINED POOL_INCL_SPA (*07*) ,'SPA' // #END_IF // #IF_DEFINED POOL_INCL_HEAT (*08*) ,'POOLSP' // (*09*) ,'POOLSP2' // #IF_DEFINED POOL_INCL_SPA (*10*) ,'SPASP' // #END_IF // (*11*) ,'POOLHT' // (*12*) ,'POOLHT2' // #IF_DEFINED POOL_INCL_SPA (*13*) ,'SPAHT' // #IF_DEFINED POOL_INCL_SOLAR_HEAT (*14*) ,'SOLHT' // #IF_DEFINED POOL_INCL_SPA (*15*) ,'SPASOLHT' // #END_IF // (*16*) ,'SOLPREF' // #IF_DEFINED POOL_INCL_SPA (*17*) ,'SPASOLPREF' // #END_IF // #END_IF // #END_IF // (*18*) ,'OPMODE' // } DEFINE_START //SET IP if PROVIDED && initial values { nPOOL_LEN_QUERY_ARRY = LENGTH_ARRAY(cPOOL_QUERY_ARRY);Now my array only contains queries that are needed specifically for this installation and I can minimze traffic. I was thinking it wasn't going to work but tried anyway and was pleasantly suprised.
0
Comments
|
|
|
V
On a side note, are you aware you can use more than just #DEFINE as compiler directives? Devices, Constants, Variables, Structure types, and DEFINE_CALLs all work as compiler directives. I often use this to test code in an include file that is dependent on a device being defined externally to the include:
I use compiler directives as little as I can get away with and that's almost never. Plus it just looks ugly and I hate having to read ugly looking code. If it were me I would break it up into different arrays like basic commands and extended commands or something similar. With Denon receivers for instance, they all have the same basic commands, but some have extras so I write one module that accommodates all of them without directives. If the command doesn't exist then it never gets sent and the feedback is parsed in a generic way. The module is a superset of all Denon commands, and the subsets use what they need and ignore the rest. Not sure if you wanted my opinion but there it is
Paul
I think having this at the begining of my code: Is neat and makes it real easy to configure and then these can be used to modify my array as posted or as I would typically use like this:
In my Init buttons function that determines which buttons show based on config. Or in my Update UI function (for new on page UIs) I can limit feedback to just what they need based on the buttons/config used.
I don't have to regulate my parsing feedback since there are no buttons that can be pushed which don't coincide with the config and I'm not polling for any status that isn't part of the config.
Yeah it's messy but I think the benefits out weigh the ugliness factor. The stuff above I really don't find as ugly as the array code that I originally posted.
If you look at the Kaleidescape module they use directives to ignore the SATP code that uses far more memory, so it can be compiled without that code and that is a good reason to use them in that case. You are using them more as logic statements to build arrays, which to me is far less readable. If going this route wouldn't header files be better and then use directives to include the correct one depending on the project?
Paul
I get it now
Paul