Shop OBEX P1 Docs P2 Docs Learn Events
Passing variables within methods — Parallax Forums

Passing variables within methods

WolfbrotherWolfbrother Posts: 129
edited 2011-02-06 09:10 in Propeller 1
Maybe I am up too late but... I'm doing something very odd here. When using this little switch reading routine that JonnyMac helped me with. I'm at the very last part of my odyssey on the propeller design and I wanted to pass a simple flag back from this counter routine to tell me when my mspntr has reached zero or has been forced to zero by pressing the kill switch. This passed flag will be what I use to disable my RFID reader and put the whole device in a low power mode.

Originally the relaycntl was a PRI instead of a PUB, I think I understand that to mean that there aren't global variables passed and any variables used in a PRI to be only available in that method. So, I changed to a PUB, which I think would pass variables. But using the Serial Debugger to output to the screen implies to me that I am never changing this variable. Also in my repeat loop, something happens where I never get a screen print that rlyflag was zero, I think the first time through I should see a zero since I set the variable right before this instruction. Also I commented out the debug statements after the conditional, if flagpass := 1 because the whole program then seems to hang.

I'm sure there is something ridiculously simple I am missing.

Thanks,

Dave

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2011-02-05 23:33
    You have two cases where you use if variable := value, surely you want comparison (==) not assignment (:=)?

    And for the record, a private method doesn't affect variable access, it's just not visible outside the object.
  • WolfbrotherWolfbrother Posts: 129
    edited 2011-02-05 23:44
    Well, that certainly implies I am up too late, I have repaired those incorrect comparisions. It no longer hangs in the debug statements, but it still seems like I am not changing/passing the rlyflag. Any ideas on how I can pass this simple flag that would be set when the mspntr goes to zero?

    Thanks on the PUB/PRI clarification, I'm still getting confused with objects/methods and cogs and passing things around.

    switch test with passing2.spin
  • kuronekokuroneko Posts: 3,623
    edited 2011-02-05 23:53
    The issue is that you pass rlyflag by value rather than by reference (address). This way only the local variable flagpass is updated but never reported back. As rlyflag is global you might as well access it directly (no need for passing it to the method). Otherwise - if that feels wrong - pass its address (@rlyflag) to the method and modify/access it by using byte[flagpass] := expression.
  • kuronekokuroneko Posts: 3,623
    edited 2011-02-05 23:57
    flagpass := [COLOR="red"](long[mspntr] > 0)[/COLOR]
    
        if flagpass == 1
    
    Note that the red expression will either be -1 (TRUE) or 0 (FALSE). So your comparison shouldn't be against 1 but just using the boolean value, i.e.
    if flagpass
    
    .
  • WolfbrotherWolfbrother Posts: 129
    edited 2011-02-06 00:10
    Hi,

    Thanks. I have tried both of your suggestions and they both work the same. What was interesting to me is that it jumps from 255 ( byte max value) to 0. Then I realized I have an 80Mhz clock and outputting to the screen takes way more than 255 cycles. It does look like this pauses the output for me, so that's what I wanted. I'm not sure which is better form though.

    switch test with passing3.spinswitch test with passing4.spin
  • kuronekokuroneko Posts: 3,623
    edited 2011-02-06 00:16
    What was interesting to me is that it jumps from 255 ( byte max value) to 0.
    You assign either TRUE or FALSE to rlyflag, the former will result in 255 (-1 reduced to byte size, $FFFFFFFF) and the latter 0. There are no values in between and there is no connection to clock frequency. If you want other values you have to manually put them there :)
  • WolfbrotherWolfbrother Posts: 129
    edited 2011-02-06 00:32
    Ok, this has been a good conversation for me to understand a few new things.

    I thought the expression byte[flagpass] := (long[mspntr] > 0) meant if msnpntr is greater than zero, return a 1

    So at that point I was passing back to to rlyflag either a 1 or a 0. But it turns out that it's a -1 or a 0. Which then explains why it would hang up looking for a 1.

    So in this case, I prefer the default condition to be a zero and use that to tell my other things (RFID, Tone Generator) to stop running. What I mean to say is that when the rlyflag is nonzero, I can let everything else run. Once it goes to zero, I disable my RFID reader and the tone generator, until the go button is pressed again and the msnpntr is reset.

    Thanks,

    Dave
  • kuronekokuroneko Posts: 3,623
    edited 2011-02-06 00:44
    Yes, you have to deal with the fact that in SPIN TRUE means -1 (see data sheet, 6.3.1 predefined constants). If you want 1/0 instead you could use
    byte[flagpass] := [COLOR="red"]-[/COLOR](long[mspntr] > 0)
    
    but personally if I know I get a boolean back I try not to tie it to a specific value. Glad you have it working now.
  • WolfbrotherWolfbrother Posts: 129
    edited 2011-02-06 09:10
    Hi, Thanks for all the help, I think I spent too long working on this project yesterday, I made some mistakes I should have known better on, like the conditionals. This morning I got up and wrote a little test to see that I was passing my flag around and it worked just fine. Now I just need to merge this new bit of code into my main program and I should be ok.
    switch test with passing5.spin
Sign In or Register to comment.