Working with Named Constants

cpedw

Senior Member
There are 2 features of named (symbol) constants that I find annoying. First, it's not possible to define them using more than one arithmetic operation. So I have to use several extra to achieve a fairly straightforward objective. In the snippet blow, numc1, numc2 and numc3 are parameters that only exist so I can calculate other constants.

More of a problem, if I hover over a name in the program listing (after syntax checking or downloading) then the simple constant HOWMANYLEDS (and the all-numeric numc1) shows its value but all those derived using other named constants show just the name of the first part of their calculation.

Can these 2 limitations be removed?
Code:
Symbol HOWMANYLEDS = 48

SYMBOL numc1 = 128 / 6  
SYMBOL numcol= numc1-2
SYMBOL stp = 255/numcol
SYMBOL maxcol= numcol*stp
SYMBOL numc2 = numc1-1
SYMBOL numc3 = numc2*6
SYMBOL maxptr= numc3-2
 

PieM

Senior Member
Use define directive:
#define numcol 128/6 - 2
#define stp 255/numcol
#define maxptr 128/6 - 1 * 6 - 2

But if stp = 255/numcol and maxcol= numcol*stp then maxcol = 255 !?
 
Last edited:

cpedw

Senior Member
Thanks for that. It seem #define can help me.

But if stp = 255/numcol and maxcol= numcol*stp then maxcol = 255 !?
Well not often. the stp calculation is usually truncated so maxcol is the largest mutiple of stp that is <= 255. In this case, numcol=19, stp=13 and maxcol=247.
 

hippy

Technical Support
Staff member
I will pass on the comments but in the meantime, one way to see what the values are is to print them out when simulated -
Code:
Symbol HOWMANYLEDS = 48

SYMBOL numc1 = 128 / 6  
SYMBOL numcol= numc1-2
SYMBOL stp   = 255/numcol
SYMBOL maxcol= numcol*stp
SYMBOL numc2 = numc1-1
SYMBOL numc3 = numc2*6
SYMBOL maxptr= numc3-2

#IfDef SIMULATING
  SerTxd( "numcol = ", #numcol, CR, LF )
  SerTxd( "stp    = ", #stp,    CR, LF )
  SerTxd( "maxcol = ", #maxcol, CR, LF )
  SerTxd( "maxptr = ", #maxptr, CR, LF )
#EndIf
Given that one is mostly making sure the numbers are right, that the implementation reflects the calculations determined, that can be acceptable.

Another possibility is to use variables rather than named constants -
Code:
Symbol numcol = b2 : numcol = 128 / 6 - 2
Symbol stp    = b3 : stp    = 255 / numcol
Symbol maxcol = b4 : maxcol = numcol * stp
Symbol maxptr = b5 : maxptr = 128 / 6 - 1 * 6 -2

#IfDef SIMULATING
  SerTxd( "numcol = ", #numcol, CR, LF )
  SerTxd( "stp    = ", #stp,    CR, LF )
  SerTxd( "maxcol = ", #maxcol, CR, LF )
  SerTxd( "maxptr = ", #maxptr, CR, LF )
#EndIf
Doing that, but calculating the values in a FOR-NEXT loop, or as a subroutine call for specific values, can be useful for quickly checking the maths work for all values. That's quicker than multiply changing a value, syntax checking, and looking to see what the outcome was.

I will generally do that, get the maths working using variables, tweak the calculations until I have it right, check all values are right in every case, then move the calculations to SYMBOL or #DEFINE, with an initial SERTXD type report just to check things are as expected.
 
Top