global constant ?
Chris Micro
Posts: 160
Hi there,
is it possible in SPIN to create global constants which are available in every object in a project?
In "C" there are the header files: you can define gobal constants there and include the header file in every code file.
Is the same thing possible in SPIN?
thanx,
chris
is it possible in SPIN to create global constants which are available in every object in a project?
In "C" there are the header files: you can define gobal constants there and include the header file in every code file.
Is the same thing possible in SPIN?
thanx,
chris
Comments
You can do something similar by declaring constants in one SPIN file, then referring to them in another file using the "#" syntax. For example:
In file "MY_CONSTANTS.SPIN":
CON
CONST_1 = 1
CONST_2 = 2
PUB Dummy
Then, to use these constants in another file:
OBJ
MY_CONSTANTS : "MY_CONSTANTS.SPIN"
CON
X = MY_CONSTANTS#CONST_1
PUB
...
This approach has a few drawbacks - for instance, each constant file you have actually consumes some memory. But it may be useful to you.
Ross.
What I want to have is a file with the hardware ports definitions e.g.
VGA_PINS = 16
...
Of course the hardware connections are for every object the same and therefore it should be possible to have a global pin definition file.
There have been various attempts to come up with a solution to this exact problem. I don't think any of them are ideal. The issue (as you have discovered) is that basic support for this needs to be built into the language in the first place. Various people have tried "bolt on" solutions to overcome the shortcomings of SPIN - but (again) I'm not aware of any having achieved resounding success. They tend to be either non-standard extensions to the SPIN language, or require the use of a SPIN pre-processor.
Try searching the forum for various language tools - you could start with bstc, Homespun and SCUM.
Or you could just use C - see http://forums.parallax.com/showthread.php?p=795326
Ross.
You should look at the compiled code, I do not think you consume that much memory, the problem is you need at least one PUB per spin file, but that one can be just one abort, break or repeat command, one byte (2 really)and few pointers. It will use 10 bytes. With BST you can remove unused methods , and save juicy 2 bytes !!!
test2.spin
consts.spin
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Visit the home of pPropQL, pPropQL020 and OMU for the pPropQL/020 at omnibus.uni-freiburg.de/~rp92
I know, assembler is best But as you know, I like to learn also SPIN, even it's sometime a little bit odd ....
Hi RossH,
thanx for your help Very pitty that it is not possible with spin, so I have to copy the global constant block in every object .-(
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Visit the home of pPropQL, pPropQL020 and OMU for the pPropQL/020 at omnibus.uni-freiburg.de/~rp92
Don't Homespun and BSTC both have a preprocessor?
No preprocessor is of course a glaring missed opportunity in the Propeller tool [noparse]:)[/noparse]
There is not much difference in '#include <const.h>' to get #defines/enum and 'OBJ con : "const.spin" '
except of course that you add con# to each symbol ... which has many advantages over #defines, etc....
Enjoy!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve
Propalyzer: Propeller PC Logic Analyzer
http://forums.parallax.com/showthread.php?p=788230
Not that can include files.. only conditional compilation.
You just can't be all things to all people.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"VOOM"?!? Mate, this bird wouldn't "voom" if you put four million volts through it! 'E's bleedin' demised!