Shop OBEX P1 Docs P2 Docs Learn Events
Use characters as longs — Parallax Forums

Use characters as longs

stevenmess2004stevenmess2004 Posts: 1,102
edited 2008-01-08 17:22 in Propeller 1
Is it possible to use a series of byte as a long? I have tried it but with no luck so far.

If I have a prototype
PUB callObject(objectName,methodName,arg)

is there some way of doing this
return callObject("test","run",0)

I can just use numbers but if I can use words it will mean less mistakes.

Does anyone have any ideas?

Steven

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-01-08 07:13
    You can, but it's awkward:

    a := constant("t" | "e" << 8 | "s" << 16 | "t" << 24)
    
    
    


    There really isn't a simple, natural-looking constructor in Spin for concatenating bytes into a long. As an alternative you could use string, but that creates an additional level of indirection that you may not want.

    -Phil
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-01-08 07:17
    In that case it would be simpler to just use numbers. Could use hex though. It would be better than nothing.

    Steven
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-01-08 07:21
    More expressive would be to use defined constants, viz:

    CON
      test = 0
      run  = 1
    
    ...
    
    return callObject(test, run, 0)
    
    
    


    -Phil
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-01-08 07:31
    True. I was thinking about using it for DOL. Because the objects will be compiled separately they will have to be declared in every object that wants to call other objects. But that's just a copy and paste so its not that hard.

    Thanks Phil

    Steven
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-01-08 07:40
    You can access another object's defined constants using the # accessor: e.g. myObject#test. It's a little wordy if you use long names in your OBJ section, but it keeps objects and the programs that use them in sync, in case of changes.

    -Phil
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-01-08 07:43
    Yes, but only if that other object is declared in an 'OBJ' section. DOL will allow you to load objects at runtime. When you compile the objects you can call other objects that have dynamically loaded at runtime and so are not in the 'OBJ' section.

    Steven
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-01-08 07:47
    You can set up an enumeration:

    CON
    #0, test, run, thing1, thing2

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-01-08 08:52
    Steven,

    With the DOL, you could still create otherwise-null objects that just have constants in them and declare those objects in both caller and callee programs. Referring to a single definition file is a Good Thing that eliminates all kinds of maintenance headaches. Granted, the objectname#const notation is a bit wordy.

    -Phil
  • CardboardGuruCardboardGuru Posts: 443
    edited 2008-01-08 08:59
    Phil Pilgrim (PhiPi) said...
    Granted, the objectname#const notation is a bit wordy.
    -Phil

    Less so if you set such constant files to be referred to by a single letter such as "c" or "g" in the OBJ statement.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Help to build the Propeller wiki - propeller.wikispaces.com
    Play Defender - Propeller version of the classic game
    Prop Room Robotics - my web store for Roomba spare parts in the UK
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-08 09:15
    I should definitely use constants from a referenced Object.
    Whatever DOL does, constants are resolved at compile time, there is no overhead.

    When you use "ABC" as LONGs or WORDs you are in the bad habbit of some languages of the "everything is a string" kind, which is acceptable for GB memories but not for the Propeller.

    Note that that the LONG containing "ABC" has to be alloctated somewhere; the storrage situation will be equivalent to:

    DAT
      cABC LONG BYTE "ABC"
    
    
  • Nick MuellerNick Mueller Posts: 815
    edited 2008-01-08 10:13
    I have the impression that what Steven wants, are something multi-byte char (constants).
    In C, they look like this int32 = "ABcd". They only occupy 4 (2) bytes (no terminating zero). They are handy for signatures.

    That doesn't work in Spin.

    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-01-08 10:49
    Yes nick, that is what I want.

    @Phil and CardboardGuru - I thought you had to have a method in a *.spin file? I'll check. If you don't then that's what I'll do.
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-01-08 11:10
    Steven,
    this works:
    CON
    · name = "t"<<24 + "e"<<16 + "s"<<8 + "t"


    and you then use it like
    callObject(objectConstants#name,...)


    regards peter
  • CardboardGuruCardboardGuru Posts: 443
    edited 2008-01-08 14:56
    stevenmess2004 said...
    Yes nick, that is what I want.

    @Phil and CardboardGuru - I thought you had to have a method in a *.spin file? I'll check. If you don't then that's what I'll do.

    You do. So just end the file with:
    PUB dummy

    You don't have to call it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Help to build the Propeller wiki - propeller.wikispaces.com
    Play Defender - Propeller version of the classic game
    Prop Room Robotics - my web store for Roomba spare parts in the UK
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-08 17:22
    I shall try to explain again.

    It is very clear to most persons here (to some earlier, to some later) what stevenmess intends to do: He wants to have a "speaking" parametrization. This is quite common in "everything is a string"- languages, as TK, Perl, PHP,...

    In SPIN the construct "ABC" does not denote a string (as there is no datastructor or "strings" in SPIN), rather it can be used in CONSTANT context as a specific preset.

    string("ABC")
    


    statically allocates "A", "B", "C", 0 to the memory and yields a pointer to it.

    DAT
    LONG BYTE "ABC"
    


    also allocates memory (three bytes).

    The main objective of stevenmess - to have "speaking parameters" - can not be obtained in this way, as "ABC" is just not a valid element as a parameter.

    If it were, it would take also take the same amount of additional storage as in my second example.

    Languages that allow strings and word constants sometimes distinguish this be using ".." for strings and '...' for alphanumeric contents.

    So please use constants as recomended many times here!
Sign In or Register to comment.