Shop OBEX P1 Docs P2 Docs Learn Events
A pfth puzzle -- passing a variable from one cog to another — Parallax Forums

A pfth puzzle -- passing a variable from one cog to another

LoopyBytelooseLoopyByteloose Posts: 12,537
edited 2014-01-26 22:27 in General Discussion
Hi.
I am looking for help with this as I just can't seem to make it work.

In pfth, I am attempting to start an infinite loop that generates a change in a variable created in the hubram dictionary. And then I want a second cog to monitor the change in order to take action according to its status.

Comments

  • mindrobotsmindrobots Posts: 6,506
    edited 2014-01-26 06:22
    George,

    This line gives me pause:
    : Startcogloop begin 100 ms SetTestvar 1f 100 unsetTestvar again ;
    

    you delay...ok
    you set your variable...ok

    at this point there isn't anything on the stack - what is 1f - puts hex 1F on the stack?
    then you put 100 on the stack
    then you unset your variable

    and loop to do it all again.

    Unless I'm missing something or need more coffee, I just see this overflowing the stack after a few loops.
  • Dave HeinDave Hein Posts: 6,347
    edited 2014-01-26 07:26
    Loopy, this program has lots of problems. When I try to load it in pfth I get the following errors:
    include test.fth
    crset?
    cogAconfig?
    1f?
            cogAstack?
    Stack Underflow
            cogAstack?
    Stack Underflow
            cogAreturn?
    Stack Underflow
            cogAreturn?
    Stack Underflow
     ok
    
    The hex number 1f is not defined if you are running in decimal mode. If you are running in hex mode then "100 ms" will give you 256 msecs of delay in decimal. What does crset do? I'll create a dummy crset to see if I can get this to work.
    Some of the code needs to be re-ordered so that words are defined before they are used.
  • Dave HeinDave Hein Posts: 6,347
    edited 2014-01-26 07:47
    I fixed a few things, and this code works for me. I added a word ReadTestvar1 that loops 20 times. I use that instead of ReadTestvar, which runs in an infinite loop. I also removed the TAB characters that are used in setting up cogAconfig. They cause a problem with recognizing the variable names. pfth doesn't treat TABs as whitespace, and will include them with the variable name.

    I also removed the "1f". I'm not sure what that was for. I added a " ms" after the second 100, which I think is what you intended.
    \ Revised to operate properly -- Following toggle.fth as a template
    \ Delays using ms to avoid shifting input to hex from decimal
    
    : crset ;
    
    Variable Testvar
    2 Testvar !
    
    : SetTestvar 1 Testvar ! ;
    
    : UnsetTestvar 0 Testvar ! ;
    
    : Startcogloop begin 100 ms SetTestvar 100 ms unsetTestvar again ;
    
    create cogAstack 80 allot \ allots an 80 byte or 20 32bit cell stack space
    create cogAreturn 80 allot \ allots an 80 byte or 20 32bit cell stack space
    
    create cogAconfig
       ' Startcogloop >body ,
       cogAstack ,
       cogAstack ,
       cogAreturn ,
       cogAreturn ,
    
    : ReadTestvar Begin  25 ms Testvar @ . cr crset again ;
    
    : ReadTestvar1 20 0 do 25 ms Testvar @ . cr crset loop ;
    
    : StartcogA forth @ cogAconfig cognew ; \ used to start an empty Cog
    
    : StartAll StartcogA ReadTestvar ;  \ use to start cogA and to get Terminal to report via Cog1
    
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2014-01-26 08:57
    Hi all,
    Well, I seemed to have loaded an incorrect file. The 1F was the delay interval in one version where I tried using Hex and then reverted to decimal. But I intended removing it and providing an all decimal solution herein.



    It seems the the crux of the problem was Tabs included in the file. The rest appears to be an editting error on my part.

    I have had trouble with Tabs before as pfth doesn't recognize them as whitespace.

    Thanks, I will try the revision.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2014-01-26 09:07
    I am a bit curious about CRSET. What is that? Why is it required? I have been loading this via adding it to the list in INFILE.

    Thanks for the tested code.
  • mindrobotsmindrobots Posts: 6,506
    edited 2014-01-26 09:14
    I am a bit curious about CRSET. What is that? Why is it required? I have been loading this via adding it to the list in INFILE.

    Thanks for the tested code.

    You had it your original word definition:
    : ReadTestvar Begin  25 ms Testvar @ . cr [B]crset[/B] again ;
    

    Dave said he just created a dummy crset just because he couldn't find it and was testing.

    "You started it!!!" :smile:
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2014-01-26 09:45
    Hmmm... CRSET seems to be another typo. All very muddle-minded of me. So sorry.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2014-01-26 22:27
    Well, the problems are now resolved for good.

    TAB inserted in .fth code causes a failure to compile correctly. And I seem to hit the TAB key regardless on knowing that.

    SO, in my preferred test editor - Gedit; I have located a feature that provides SPACES whenever I use the TAB key.

    And, I have found an add-on feature for Gedit that shows me my white space info with spaces as grey dots and tabs as grey arrows.

    Now that the white space items are visible, I suspect that the issue will not arrise again.

    GEDIT is available in Windows and LInux, and much better for creating Forth code as it is a traditional text editor for programmers, rather than a fancy GUI document creator. I just needed to learn how to tweak it to be optimal for pfth.

    ++++
    The test for passing a variable between two cogs is working just fine.

    And a larger project that depends on this has had all the hidden TABs finally located and removed. It has been rather frustrating to get a good work method, but it seems all resolved.
Sign In or Register to comment.