Shop OBEX P1 Docs P2 Docs Learn Events
Accessing hub vars in objects via spin. — Parallax Forums

Accessing hub vars in objects via spin.

Is it possible to access HUB variables in child objects from the top object? I can access constants via OBJNAME.CONNAME, but can't do the same with VARs. I usually bubble up any VARs that I need with a pointer, but in this case, I just need to read a single var for a conditional and don't want to have to bubble anything up.

Comments

  • You need to create a method to read it. In PNut/PropTool, anyways. Flexspin can read variables like that, but not write them (for no technical reason, just a parser bug that has never been deemed important enough to fix)

  • Brilliant. I didn't think to do that, but have done it without thinking many times. Thanks!

  • If your process is small and you embed it in the parent object, Spin code running in its own cog can access hub variables. For example, in the P1 I often do this

    pri background | t
    
      t := cnt
      repeat
        waitcnt(t += MS_001)
        millis += 1
    

    In this case, millis is a global variable in the parent, and this secondary cog can see it. You do have to be mindful of race conditions in certain circumstances. FWIW, you can to a fair bit of work in 1ms and most of my projects that use this background milliseconds timer serval other things.

    If you have a separate file that wants to read its parent's variables, you could put the variables that will be accessed in a known order and then pass a pointer to the first (best to keep the same size for all variables). Per Ada's suggestion, you can create read methods that use the base pointer and an offset into that group.

Sign In or Register to comment.