Shop OBEX P1 Docs P2 Docs Learn Events
External Header file — Parallax Forums

External Header file

PhilldapillPhilldapill Posts: 1,283
edited 2008-01-06 01:42 in Propeller 1
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.

Comments

  • scottascotta Posts: 168
    edited 2008-01-05 16:13
    Try

    OBJ
    SERIAL : "FullDuplexSerial"


    PUB Main | test

    test:=SERIAL#Constant
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-01-05 16:17
    Er? I'm not trying to send/recieve any data... I'm now confused as to what FullDuplex Serial is for if it is used for THIS...
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-05 17:32
    Philldapill:

    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
    PUB getTabAddr()
     return @startOfTableInDAT
    
    ....
    DAT
    startOfTableInDAT LONG
    ...
    


    will do.
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-01-05 17:37
    So if I store my data in the DAT section, it should be "global" in other files? I want to be able to assign names to constants such as something simple like "Key_0, Key_Mute, Key_Power" etc. The problem I see with trying to keep up these constants in different files is having "Key_0" equal 0 in one file, then 26 in another because I didn't update the other file.
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-05 17:42
    Well if that are really constants, than this is absolutely straightforward. Just use the names of the constants. That is what Scotta recommended, but your reaction seemed negative!?
  • hippyhippy Posts: 1,981
    edited 2008-01-05 17:47
    You can put all your constants in a single file ( say, "const.spin" ) defined in a CON block and then you can include them using OBJ const : "const" in any program or object which needs them and use by referencing them as const#Key_0 etc.

    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.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-01-05 18:43
    Have a look at BoeBotBasic from the Object Exchange. It uses an object called BB_Definitions.spin to hold a lot of its global constants.
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-01-05 20:16
    Ohhhhhhh, I get it. I didn't mean to shrug off your input like that scotta. I'm really not the best explainer sometimes and I though you misunderstood me.
    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!
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-01-05 20:38
    I suppose the same goes for variables? I have a list of variables "KeyID[noparse][[/noparse] i ]" that are simply a number, but I'm not sure how to cycle through them in a function like the one below unless it is declared as a variable...

    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
  • Mike GreenMike Green Posts: 23,101
    edited 2008-01-05 21:07
    Spin only has compile-time scalar constants, no array constants. Even if it did, what you want to do still requires variable space since the data has to be available and organized into an array at run-time. If you want to have a array of constant data, you can declare KeyID in a DAT section like this:
    DAT
    KeyID  long  4, 5, 6, 7, 8, 9, 10, 11
               long  12, 13, 14, 15
    


    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.
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-05 23:49
    .... referencing them needs getting their address - see my posting above.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-01-05 23:55
    That's not quite true. You can reference what I've written by using "KeyID[noparse][[/noparse] i ]" and it will treat that memory area as longs.
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-06 00:01
    You can reference it of course as Mike said in the same objects
    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"
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-01-06 00:31
    Hey, thanks guys. I've got it working perfectly! I'm making a remote control learning object that can "learn" nearly any button on any remote. The program will ask you to press a certain key, and if it can reads it, it will set the cooresponding KeyCode[noparse][[/noparse] i ] to that code the code that the remote sent. eg.
    (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
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-06 01:21
    Philldapill said...
    Hey, thanks guys. I've got it working perfectly!
    Congrat!
    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:
    PUB LearnKey(key, code): success
      if key < MaxKeys
        KeyCode[noparse][[/noparse]key] := code
        success := true
    
    
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-01-06 01:27
    Wow, that's clean code. I like it. Thanks desilva!
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-01-06 01:34
    It would be nice if declarations in the OBJ section could have IMPORT and IMPORT_ALL modifiers. This would eliminate having to prepend the object name each time you want to use a constant or public function in another object (when there are no naming conflicts). I know others have suggested the more localized WITH structure, which is also a good idea; but a global import would round out the repertoire for external access.

    -Phil
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-06 01:42
    As there is no "shadowing" in SPIN this will break programs when an object is updated including a name you use youself (or will even hamper you from the beginning). Standard languages use "shadowing" so it's no issue with them...
Sign In or Register to comment.