Shop OBEX P1 Docs P2 Docs Learn Events
Why is this code not working? — Parallax Forums

Why is this code not working?

FranklinFranklin Posts: 4,747
edited 2011-07-04 20:23 in Propeller 1
I don't know why this does not work. I'm not very good with spin yet and am trying to convert a degree reading (0 - 360) to compass points. The error is "Expected end of line between the two N's on the NNE line. Any help appreciated.
PUB convertHeading (degrees)   | Heading
    case  degrees
      0..11   : Heading := "N"
      12..34  : Heading := "NNE"
      35..56  : Heading := "NE"
      57..79  : Heading := "ENE"
      80..101 : Heading := "E"
      102..124: Heading := "ESE"
      125..146: Heading := "SE"
      147..169: Heading := "SSE"
      170..191: Heading := "S"
      192..214: Heading := "SSW"
      215..236: Heading := "SW"
      237..259: Heading := "WSW"
      260..281: Heading := "W"
      282..304: Heading := "WNW"
      305..326: Heading := "NW"
      327..348: Heading := "NNW"
      348..360: Heading := "N"

Comments

  • ericballericball Posts: 774
    edited 2011-07-04 17:38
    I don't program in SPIN much, but I know that all parameters and return values in SPIN are LONGs. You attempt to be returning a string.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-07-04 17:49
    It seems like it ought to be possible to specify word values with strings with two characters or long values with strings with three or four characters, but it doesn't work that way. You can write "W" << 8 | "S" if you want the first character in bits 15-8. For three characters, you might use ("W" << 16) | ("N" << 8) | "N". If you want the characters in some other order, you can specify that any way you want.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-07-04 18:17
    I've attached a possible solution. It uses a DAT table of strings and modifies your routine to point into it.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2011-07-04 18:28
    Similar to JohnnyMac's , pointers to strings
    case  degrees
          0..11   : Heading :=@N     
          12..34  : Heading :=@NNE 
          35..56  : Heading :=@NE
          
    debug.str(Heading) 
     
    dat
     N   byte "N",0
     NNE byte "NNE",0    
     NE  byte "NE",0
    
    

    Jeff T.
  • FranklinFranklin Posts: 4,747
    edited 2011-07-04 18:52
    Thanks guys, I'll look those over as I probably should be using dat statements anyway. Here is what I have now and it works.
    PUB convertHeading (degrees) 
        case  degrees
          0..11   : Heading := "N"
          12..34  : Heading := ("E"<<16)|("N"<<8)|"N"
          35..56  : Heading := ("N"<<8)|"E"
          57..79  : Heading := ("E"<<16)|("N"<<8)|"E"
          80..101 : Heading := "E"
          102..124: Heading := ("E"<<16)|("S"<<8)|"E"
          125..146: Heading := ("S"<<8)|"E"
          147..169: Heading := ("E"<<16)|("S"<<8)|"S"
          170..191: Heading := "S"
          192..214: Heading := ("W"<<16)|("S"<<8)|"S"
          215..236: Heading := ("S"<<8)|"W"
          237..259: Heading := ("W"<<16)|("S"<<8)|"W"
          260..281: Heading := "W"
          282..304: Heading := ("W"<<16)|("N"<<8)|"W"
          305..326: Heading := ("N"<<8)|"W"
          327..348: Heading := ("W"<<16)|("N"<<8)|"N"
          348..360: Heading := "N"
    
    and it is called like this.
    convertHeading(value)
      term.str(@Heading)
    
  • FranklinFranklin Posts: 4,747
    edited 2011-07-04 19:03
    Jon, your code always fascinates me. I have to read it several times to figure out what it all does (rjdec) that's just elegant (or at least freaky)
  • Cluso99Cluso99 Posts: 18,069
    edited 2011-07-04 19:23
    Franklin: You code posted in #6 is a little cumbersome. However, if you define heading in the same way as you did in the case statement in #1, but instead of passing the result returned in Heading, you use Heading as a global varaible, that will work. Essentially that is what you have done in the end but it just is cleaner and is terminated with the required null.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-07-04 20:23
    Jon, your code always fascinates me.

    While I like to write my own code, when I find something really elegant and useful I will snatch it up and put it to use -- such is the case with the rjdec() methd. That code (if under a different method name), I believe, was written by Dave Hein.
Sign In or Register to comment.