Shop OBEX P1 Docs P2 Docs Learn Events
Confusion about Strings — Parallax Forums

Confusion about Strings

LuckyLucky Posts: 98
edited 2010-03-31 06:02 in Propeller 1
As of right now, I have a lookupz table, filled with values such as "Shift", "Up", "F9". The problem is that when I increment the index, the program does not print on the screen everything between the parentheses at once. It goes S, h, i, f, t, U, p, F, 9, etc. I assign the value to a byte and print it out using the debug.tx method from the fullduplexserial object. Is there a way to print out the whole string of characters in the word at once?

······································································································································································· -Thanks





▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"You do not really understand something unless you can explain it to your grandmother."


-Lucky[size=-1][/size]

Comments

  • SRLMSRLM Posts: 5,045
    edited 2010-03-30 20:10
    debug.str

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Powered by enthusiasm
  • StefanL38StefanL38 Posts: 2,292
    edited 2010-03-30 20:31
    hello Lucky,

    here is a democode that shows two ways how to do that

    pay attention to the terminating zero of the DAT-section strings

    best regards

    Stefan

      'some tmeplates for copy & paste
      'Debug.Str(string(""))
      'Debug.Str(string("",13))
      'Debug.Dec()
      'Debug.Rx()
      'Debug.Tx()
    
    CON
      _clkmode      = xtal1 + pll16x                        ' use crystal x 16
      _xinfreq      = 5_000_000
    
      'defining constants makes code selfexplaining
      'see line with Debug.Start(.... 
      DebugTx_Pin = 30
      DebugRx_Pin = 31
      Baudrate    = 115200
      FDX_Mode    = 0  
    
      
    VAR
    
    OBJ
      Debug   : "FullDuplexSerialPlus"
    
      
    PUB Main
      StartSerialDebugging
    
      repeat
        Waitcnt(ClkFreq + cnt)
        Debug.str(string("Hallo?"))
        Debug.Tx(13)
        
        Debug.str(@MyStr1)
        Debug.Tx(13)
        Debug.str(@MyStr2)
        Debug.Tx(13)
        Debug.Tx(13)
      
    
    Pub StartSerialDebugging
      Debug.Start(DebugRx_Pin,DebugTx_Pin,FDX_Mode,Baudrate)
    
      Debug.Str(string("FullDuplexSerialPlus-Driver started with ",13))
      Debug.Str(string("Start(DebugRx_Pin="))
      Debug.Dec(DebugRx_Pin)
      Debug.Str(string(",DebugTx_Pin="))
      Debug.Dec(DebugTx_Pin)
      Debug.Str(string(",FDX_Mode="))
      Debug.Dec(FDX_Mode)
      Debug.Str(string(",Baudrate="))
      Debug.Dec(Baudrate)
      Debug.Str(string(13,13))
    
    DAT
      MyStr1 byte "Hello World",0
      MyStr2 byte "How are you?",0
    
    
  • LuckyLucky Posts: 98
    edited 2010-03-30 20:43
    I think thats what I'm looking for StefanL38, thanks! One more question though, can the MyStr1 be longer, for example: MyStr1 byte "Shift", 0, "Up", 0, "Left", 0

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "You do not really understand something unless you can explain it to your grandmother."


    -Lucky[size=-1][/size]
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-03-30 20:58
    I think the issue is how lookupz handles character strings.· I believe lookupz(index: "Shift", "Up", "F9") is the same as lookupz(index : "S", "h", "i", "f", "t", "U", "p", "F", "9").· They generate the same spin code bytes anyhow.· If you want to index on the strings you need to create a table with pointers to the strings as follows:

    DAT
    · str1 byte "Shift", 0
    · str2 byte "Up", 0
    · str3 byte "F9", 0
    · table long @str1, @str2, @str3

    You could then select one of the strings and print it with the following statement:

    · ser.str(@@table[noparse][[/noparse]index])

    The @@ is required because spin stores relative addresses for str1, str2 and str3 in the table arrary.· @@ is needed to add the correct offset to get the absolute addresses.· At least I think that is right.· Could somebody verify that for me.

    Now where was that thread that said that Spin was a simple language for beginners?confused.gif
  • StefanL38StefanL38 Posts: 2,292
    edited 2010-03-30 21:58
    the method str sends characters until it encounters value zero
    that's why it's called "zero-terminated" strings.

    So if you want to have the string "Shift Up Left"

    you code

    DAT
      MyStr1 byte "Shift Up Left",0
    
    



    you can make the string as long as the 32kB lasts

    DAT
      MyStr1 byte "almostendlessstring almostendlessstring almostendlessstring almostendlessstring almostendlessstring almostendlessstring almostendlessstring ",0
    
    



    if you want three different strings you code

    DAT
      MyStr1 byte "Shift",0
      MyStr2 byte  "Up",0 
      MyStr3 byte "Left",0
      'etc. etc.
    
    



    best regards

    Stefan
  • mparkmpark Posts: 1,305
    edited 2010-03-31 06:02
    You could use string() expressions inside the lookupz, like so:
    _xinfreq = 10_000_000
    _clkmode = xtal1 + pll8x
    
    obj ser : "fullduplexserial"
    
    pub Main | i, x
      ser.start( 31, 30, 0, 115200 )
      waitcnt( clkfreq * 3 + cnt )
      ser.str( string("lookupz test",13) )
      repeat i from 0 to 4
        ser.dec( i )
        ser.tx( " " )
        x := lookupz( i: string("shift"), string("up"), string("F9") )
        if x
          ser.str( x )
        else
          ser.str( string("out of bounds") )
        ser.tx( 13 )
    
    


    Output is
    lookupz test
    0 shift
    1 up
    2 F9
    3 out of bounds
    4 out of bounds
    
    
Sign In or Register to comment.