External Header file
Philldapill
Posts: 1,283
Ok, so I've looked through the manual and can't find what I want to do. I like to have my files neat and modularized like everyone else. I have different .spin files for different classes of functions that are all related, but don't require a new cog. Just quick and easy access. One of these is a keycommand aquisition set of functions that waits for a command from an infrared remote, then returns the cooresponding code from a table. The only thing is this... The table will be used by at least two different files.
Is there anyway to have two seperate .spin files look at the same list of constants in a seperate .spin file? I really don't want to have to deal with having two seperate constant lists that are the same in each of the two files.
Is there anyway to have two seperate .spin files look at the same list of constants in a seperate .spin file? I really don't want to have to deal with having two seperate constant lists that are the same in each of the two files.
Comments
OBJ
SERIAL : "FullDuplexSerial"
PUB Main | test
test:=SERIAL#Constant
When you instantiate an Object, only the VAR section is duplicated. All data contained in DAT are identical.
That means you can call whatever "instance" you like, a getter like
will do.
The only inconvenience is that your 'constant file' has to have a PUB Main to be used that way. That adds a small overhead to the code size every time you use OBJ const : "const" but is an easy solution to the problem.
It's not a perfect solution but does mean you only have to edit one file when you need to change those constants.
I'm still trying to get the hang of spin since most everything I know programming wise is C. That makes a lot of sense. Obj Const: "constfile" is kind of like the "#include" directive and const#myconst is like Const->myconst when referencing an object owned by const. Wow. Really, scotta, sorry about blowing that off. You were dead on. Thanks again guys!
PUB InterpretCode(code) | i
'Find the KeyID that matches the HexCode transmitted by the remote
· i := 0
· repeat MaxKeys
··· if KeyID[noparse][[/noparse] i ] == code
····· return i
··· i++
· 'Return MaxKeys if no match could be found
· return MaxKeys
Like I said, KeyID[noparse][[/noparse] i ] is declared as a long variable, but I want it to only be a constant so that it's not taking up variable space. How would I do this so that I can still cycle through each instance?
Post Edited (Philldapill) : 1/5/2008 8:43:06 PM GMT
You refer to it the same way as if it were declared in a VAR section.
You can add further values as show above on the second line.
However I was under the impression that we were talking aboiut hidingthings a way in a separate object.
What I said is the simple way. (data coupling)
It is of course infinitely better to use true methods, e.g. specific getters in that "Constant Pool"
(This is just part of it)
PUB LearnKey(key, code)
· if key < MaxKeys
··· KeyCode[noparse][[/noparse]key] := code
··· return true
· else
··· return false
Works like a charm! In 20 years or so when I'm finally a prop pro and have my "big box" working and able to be sold, I intend to let the user set the device up to their specs, using nothing but the remote for their TV! I have a menu selection object going right now which should give a nice pretty user interface for setting various parameters and scheduling.
Post Edited (Philldapill) : 1/6/2008 12:43:00 AM GMT
BTW: SPIN has a lot of nice features, one of then is the "named return value with zero preset"; you could also write your snippet as:
-Phil