Shop OBEX P1 Docs P2 Docs Learn Events
Array of bytes as a local variable — Parallax Forums

Array of bytes as a local variable

MightorMightor Posts: 338
edited 2007-08-15 04:01 in Propeller 1
Hey there,

I looked around in the Prop manual but I couldn't find any reference to it, so I thought I'd ask here.

Is it possible to declare a local variable that's an array of bytes? I am looking to use it inside a single function only. The idea is that I have a single cog that takes care of the servo of the PING))) sensor and takes readings at the same time. I want to build a map of its surroundings by dividing the full 128 brads into 16 sections, giving me 17 readings in total. This map should only be passed down once a single pass has been made (from left to right or from right to left) but I still want an older copy of the map to be available to the rest of the system while the Radar object works on a new version. I intended to bytemove to copy my local array to the global one. I would have make sure that the underlying system checks a "busy" flag before it consults the map (or before I write a new map to it). The busy flag would be a kind of semaphore. I am aware of the fact that SPIN can do semaphores but I am not familiar enough with those yet to be able to use them yet. Maybe in the next version.

So, is this possible or will I need to just use an Object wide array for this purpose?

Gr,
Mightor

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
| Any sufficiently advanced technology is indistinguishable from magic.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-14 18:13
    All local variables are longs. I've used these for byte arrays (for strings). Basically, you declare a local variable as a long array, sized to hold the number of bytes you want (call the array TEMP). You can use BYTE[noparse][[/noparse]@TEMP][noparse][[/noparse]x] to access the elements as bytes. You can also use TEMP.BYTE[noparse][[/noparse]x].

    Make sure the stack area for the cog is large enough for this array (and other local variables).

    Post Edited (Mike Green) : 8/14/2007 6:19:17 PM GMT
  • MightorMightor Posts: 338
    edited 2007-08-14 18:23
    Thanks for that tip, Mike. Am I right in assuming then that this is a proper way to declare a local array of longs? Am I allowed to access the array as shown?

    PUB foo() : bazbaz[noparse][[/noparse] 5 ]
        ' Is this even legal? - clear all the bytes in the array.
        bazbaz.byte[noparse][[/noparse]0..19]~
    
    



    Gr,
    Mightor

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    | Any sufficiently advanced technology is indistinguishable from magic.
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-14 18:31
    The subscript form z[noparse][[/noparse]x..y] is only allowed for INA, OUTA, DIRA and their "B" equivalents and only for access to a sequence of bits within those registers. The declaration of bazbaz as a local variable would be correct except you've got ":" instead of "|".
  • MightorMightor Posts: 338
    edited 2007-08-14 18:34
    Mike Green said...
    The subscript form z[noparse][[/noparse]x..y] is only allowed for INA, OUTA, DIRA and their "B" equivalents and only for access to a sequence of bits within those registers. The declaration of bazbaz as a local variable would be correct except you've got ":" instead of "|".
    Mike,

    Oh ok, thanks. I am always messing those two operators up, the : and | [noparse]:)[/noparse] Still learning! Shame about the sequence subscripts not being legal, I guess that's what you get when you've been programming Python all day at work [noparse]:)[/noparse]

    Gr,
    Mightor

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    | Any sufficiently advanced technology is indistinguishable from magic.
  • deSilvadeSilva Posts: 2,967
    edited 2007-08-14 18:35
    Not quite smile.gif Why don't you start the SPIN IDE before you ask?

    (a) There is no such thing as "()"
    (b) bazbaz is not a return value, use the "|"
    (c) ".." is only valid with I/O register bit addressing


    BTW: Why do you not want to use an array in the VAR section?
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-14 18:37
    You know, OUTB and DIRB would be great for packing and unpacking funny sequences of bits into 32-bit words on the current Prop since they're not used for anything and are just read/write locations and the bitstring access notation can be used with them.
  • MightorMightor Posts: 338
    edited 2007-08-14 19:00
    deSilva said...
    Not quite smile.gif Why don't you start the SPIN IDE before you ask?

    (a) There is no such thing as "()"
    (b) bazbaz is not a return value, use the "|"
    (c) ".." is only valid with I/O register bit addressing

    BTW: Why do you not want to use an array in the VAR section?
    I was typing up a quick example of how to declare a local array so typos are easy to make then. I *am* using the IDE. The question came up as I was programming my new object. I can't compile a half-finished program to check for syntax errors. As for not wanting a global array, I guess that's just a matter of taste. I've never been a fan of global vars in any of the languages I have programmed and will avoid them when possible. I'll only use them when there's no other way or using them would result in having to resort to very convoluted programming practices. SPIN supports local vars, so I am keen to make use of them.

    Mike,

    Yeah, I thought of that and then realised that it might cause really weird behaviour if I ended up using a Prop2 controller some day with this new object of mine [noparse]:)[/noparse] I prefer to not touch things you're not supposed to, especially if I don't understand all the basic principles yet, maybe when I've become more comfortable with the Prop I'll venture into that territory.

    Gr,
    Mightor

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    | Any sufficiently advanced technology is indistinguishable from magic.
  • deSilvadeSilva Posts: 2,967
    edited 2007-08-14 20:18
    Using "local" variables is not only a matter of "scope" but also of memory allocation. SPIN procedures are re-entrant (have to!) and thus recursively callable. This implies each call generates its own storage. So mind the STACK, as Mike already warned!
  • deSilvadeSilva Posts: 2,967
    edited 2007-08-14 20:24
    BTW: You can use OUTB safely and compatibly when you leave DIRB on zero! All registers are "shadows" in each COG, and only connected to the "real" I/O - if available - masked by DIR !
  • MightorMightor Posts: 338
    edited 2007-08-15 04:01
    deSilva,

    The re-entrant part hadn't occurred to me yet, probably because I would only really use a single cog to deal with my problem but I see what you mean. With two or more cogs you might need to do resource locking in some cases to prevent race conditions or conflicts. As for stack space, I have very little code atm, so I am going to give my cog copious amounts of stack space and narrow it down to what it really needs as my ability to judge that kind of thing gets better.

    Maybe using OUTB isn't such a bad idea then, I'll consider it [noparse]:)[/noparse]

    Gr,
    Mightor

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    | Any sufficiently advanced technology is indistinguishable from magic.
Sign In or Register to comment.