Shop OBEX P1 Docs P2 Docs Learn Events
Are you comfortable with Spin? — Parallax Forums

Are you comfortable with Spin?

infoinfo Posts: 31
edited 2014-05-07 14:06 in Propeller 1
What do you do when some things are hard to get done in Spin? Do you beat the dead horse until you get around limitation, or you resort to another programming language?

I'm trying to find a way to repeat block of code - retry on failure. In assembly language I would use conditional jump to label. Spin doesn't have labels or jumps.

I want to check for device busy or device present in the first place (hardware dependent) and restart i2c code if I don't get ACK from i2c device, but I don't want to get stuck in never-ending loop.

-set loop counter to 2

Start here (label)
-send start bit
-send device code byte
-IF counter = 0 then log error code, give up and return to main code
-IF ACK not received, wait a little, decrement counter and start all over again at label

-reset counter
-send register address byte
-IF counter = 0 then log error code, give up and return to main code
-IF ACK not received, wait a little, decrement counter and start all over again at label

-reset counter
-send data byte
-IF counter = 0 then log error code, give up and return to main code
-IF ACK not received, wait a little, decrement counter and start all over again at label

....continue here to do another i2c command or more data bytes


It is easy to do it in assembly, basic, etc.
Is there a way to do that in Spin?
There probably is, I just can't find it.

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-05-03 12:03
    There are many ways to do this in Spin.

    You can end a loop with "quit", or you can use "while" to continue a loop while a condition persists or you can use "until" to end the loop when a condition occurs. You can also skip to the next loop in the repeat with "next".

    You can also end the method with "return" and you can end a method and return several methods up with an "abort" and an abort trap "\".

    I don't see how this is any harder in Spin than in C or many other languages.

    And yes, I am very comfortable with Spin. Thanks for asking.
  • T ChapT Chap Posts: 4,223
    edited 2014-05-03 12:05
    There are a number of ways to not get stuck. Here is just one simple example:
    PUB Main
        Repeat 3
           If i2cdevicepresent == 1   'or whatever the method is to test for present
               quit
           gotonextcode
    

    This code above will check for the device to respond 3 times, but if it gets a reply it will get out of the loop and go to whatever next in the code.


    PUB Main    | counter
           counter~   'clear counter to 0
           Repeat
              If i2cdevicepresent == Fail   'or whatever the method is to test for present
                 counter++   'increment the count
              If  counter == 3
                 Someothercode  
    PUB Someothercode
    
  • kwinnkwinn Posts: 8,697
    edited 2014-05-03 12:07
    You make the sending of a byte a "pri" or "pub" routine that returns the status or error code to the calling program and have that program decide what to do next based on the returned status/error code.
  • kwinnkwinn Posts: 8,697
    edited 2014-05-03 12:18
    This is a perfect example of why I find this forum and it's members amazing. Post a question and 21 minutes later there are 3 answers. In the time it took me to compose a response Duane Degn and T Chap had already posted theirs.
  • infoinfo Posts: 31
    edited 2014-05-03 14:25
    Thanks guys. Yeah, it is amazing how many knowledgable people participate.

    I was beating the dead horse. The "quit" did the job in one loop without calls from main program. Maybe my code is little crude, but it tested ok.


    x := 1 'simulate ACK=0 NAK<>0 to test the code
    repeat i from 2 to 0 'loop counter
    uarts.str(0,string(" Loop x="))
    uarts.dec(0,x)
    uarts.str(0,string(" i="))
    uarts.dec(0,i)
    'i2c.Start(SCLk) 'send start bit
    'x := i2c.Write(SCLk, %0011_1110) 'device code, device address
    if x == 0 'ACK ????
    quit 'if ACK=0 skip the rest of code and do next i2c byte
    waitcnt(clkfreq/2 + cnt) 'wait if device busy
    uarts.str(0,string(" Wait x="))
    uarts.dec(0,x)
    uarts.str(0,string(" i="))
    uarts.dec(0,i)
    uarts.tx(0,13)
    if i == 0
    errcode01 := %1
    quit
    uarts.str(0,string(" ErrorCode01="))
    uarts.dec(0,errcode01)
    uarts.tx(0,13)
    'do next i2c byte


    Now I need to learn how to attach code here. The snippet above looks terrible.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-05-03 14:38
    info wrote: »
    Now I need to learn how to attach code here. The snippet above looks terrible.

    attachment.php?attachmentid=78421&d=1297987572
  • infoinfo Posts: 31
    edited 2014-05-03 18:45
    The instructions warn about wysiwyg editor. Says to use basic editor. I just tried to include code and it crashed, so I may be in the wrong editor, but I don't see selections here. No basic editor.

    I struggle even with things everybody takes for granted. I'm already used to that. The leprechon says that even bad luck is a luck, so I'm the luckiest person.
  • T ChapT Chap Posts: 4,223
    edited 2014-05-03 19:03
    If you want to show the code inside the post, use [noparse]
    at the top of your code and
    
    [/noparse] at the bottom. This will show the code as written with indention.

    [noparse]
    [/noparse]
    PUB  main
        test indention
    
    [noparse]
    
    [/noparse]
  • infoinfo Posts: 31
    edited 2014-05-03 22:36
    I did that. When I clicked preview some lines were trunkated. I tried to go back to look at the original and edit the trunkated lines, but I received only a message saying I need to add code because the code was gone. Only the opening "[code]" was left.

    I will try next time.
  • kwinnkwinn Posts: 8,697
    edited 2014-05-03 23:02
    I use Open Office for windows and never have a problem posting code.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-05-03 23:30
    Note to those trying to assist with forum formatting: If you enclose your hints between [noparse][noparse] and [/noparse][/noparse] tags, you do not need to resort to weird spacing that could mislead the recipient of your help.

    -Phil
  • Clive WakehamClive Wakeham Posts: 152
    edited 2014-05-04 01:59
    info wrote: »
    What do you do when some things are hard to get done in Spin? Do you beat the dead horse until you get around limitation, or you resort to another programming language?

    My limitation is that I am a real amateur in regard to programming languages. I know Basic (including PBasic), Spin, and that's it folks.

    I am the one that tries all variations of code before checking out other's coding examples to include into mine, and even then I usually modify it quite a bit.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-05-04 09:51
    @info, If you use the "Replay With Quote" option on a post with properly formatted code, you can see the code tags. I was curious if Phil had to use [noparse][noparse][noparse] and [/noparse][/noparse][/noparse] in order to display [noparse][noparse] and [/noparse][/noparse]. He did.

    You can snoop on code formatting the same way.

    You can also edit earlier posts to fix the code formatting.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2014-05-04 12:12
    Q: Are you comfortable with Spin?
    A: Yes. I don't know everything. The Stack ambiguity bugs me, but I've never done SO much that that was a critical consideration. I just don't know why it's not part of the IDE to work that out.
  • Heater.Heater. Posts: 21,230
    edited 2014-05-05 06:36
    PJ Allen,
    The Stack ambiguity bugs me, but I've never done SO much that that was a critical consideration. I just don't know why it's not part of the IDE to work that out.
    I think it's because it's actually very hard to have a program analyse your source code and calculate how much stack it will use. Probably impossible in the general case.

    Clearly such an analyser could see how many parameters a method has and how many local variables. From that it knows the stack required to call the thing. Then the analyser would have to recursively inspect all the methods that are called from the top method. One of those "branches" of analysis will get you the maximum stack usage.

    Except it falls down some times:

    1) Perhaps it's impossible to tell from the source code which paths are taken at run time and hence which calls are made.

    2) When you have recursive methods.

    Still perhaps a best effort analysis would be better than none.
  • T ChapT Chap Posts: 4,223
    edited 2014-05-05 07:13
    But it is easy enough to put a test long immediately after each stack and have code test for overflow. Adjust the stack size until you hit the test long with some data.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-05-05 13:03
    T Chap wrote: »
    But it is easy enough to put a test long immediately after each stack and have code test for overflow. Adjust the stack size until you hit the test long with some data.

    Or you can use the code in post #13 and watch the stack get used as the program runs (assuming the buffer dump doesn't get in the way of displaying the other data in your program). I personally thought it was pretty cool to watch the stack space change as the various options in a method were exercised.

    I was trying to think of an example of a method which couldn't have it's stack size calculated at compile time and the only example was some sort of recursive method (as Heater suggested). All the other examples I could think of seemed like they should have predictable stack sizes. If you look through Kye's code, he lists the stack size of each method. Unless Kye has some sort of psychic ability which allows him to do this, I'd think there should be a way for the rest of us, or even the compiler, to accurately calculate the stack size without the need to actually run the code.

    There's some discussion of what types of things add to the stack in the above linked to thread.
  • prof_brainoprof_braino Posts: 4,313
    edited 2014-05-06 21:50
    No. Spin looks too much like C, and C looks too much like work, and work is no fun because of all the lay offs.
    info wrote: »
    What do you do when some things are hard to get done in Spin?

    I use forth. Writing drivers is a breeze, and writing apps is fun. Stuff either works, or it don't. rarely are there things that LOOK like they work, only to later have bugs, hidden by the compiler etc.

    But I'm very lazy, and only do this for fun.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-05-07 01:25
    Spin . . . is no fun.

    Spin and spinning is a lot of fun!

    (Jump ahead to thirty seconds, to see robot moving Spinning.)

    I have lots of fun programming in Spin.

    I'm trying to remember if I've ever had fun programming the Propeller in Forth. . . Nope, I haven't. (Hey, he started it.)
  • prof_brainoprof_braino Posts: 4,313
    edited 2014-05-07 11:58
    Duane Degn wrote: »
    Spin and spinning is a lot of fun! I'm trying to remember if I've ever had fun programming the Propeller in Forth. . . Nope, I haven't. (Hey, he started it.)

    Not to disparage spin, or your opinions, or your great works, but one size might not fit all.

    The original poster DID ask, and clearly is having frustrations and is looking for alternatives.

    We might acknowledge that while some of us love spin, others love C, still others no longer love C for what ever reason, and other excellent alternatives exist, in case they are needed.

    If one does not not find benefit from a given tool set or envirnoment, it may be suitable to investigate other options. I happen to do well with one tool, this does not denigrate the others.
  • ratronicratronic Posts: 1,451
    edited 2014-05-07 14:06
    I am very comfortable with Spin! Chip made it easy to learn and use. For myself even if it is slower than C it is just easier to use and implement.
Sign In or Register to comment.