Shop OBEX P1 Docs P2 Docs Learn Events
pst printing variable — Parallax Forums

pst printing variable

mikeamikea Posts: 283
edited 2015-02-21 19:53 in Propeller 1
Hi, I was using the "lookup" command and having a problem with the pst commands to print out the lookup index. I can't find a similar example. I have tried "pst.chars", and "pst.dec". what is the proper way to print the sting or value of "list" associated with lookup? Thanks
CON
        _clkmode = xtal1 + pll16x                                               'Standard clock mode * crystal frequency = 80 MHz
        _xinfreq = 5_000_000
obj
 pst:"parallax serial terminal"
pub view|index,list
 pst.start(115_200)
 waitcnt(clkfreq/2+cnt)
 repeat index from 1 to 7
   list:=lookup(index: "hi",1234,"bye",5678,"bon jour",2222,"adios")
   pst.Dec(list)
   waitcnt(clkfreq+cnt)

Comments

  • JonnyMacJonnyMac Posts: 9,105
    edited 2015-02-20 19:39
    First things first: there are no string variable types in Spin. You can store a string in a table and print from there. Give me a minute, I'll show you how.

    Okay, here's a core that works -- not that you numbers are changed to strings as you cannot mix those types. If you need them to be numbers, put them in a separate list.
    con { pst formatting }
    
       #1, HOME, GOTOXY, #8, BKSP, TAB, LF, CLREOL, CLRDN, CR
      #14, GOTOX, GOTOY, CLS
    
    
    obj
    
      term : "jm_fullduplexserial"
    
    
    var
    
    
    dat
    
      MyList0     byte      "hi", 0
      MyList1     byte      "1234", 0
      MyList2     byte      "bye", 0 
      MyList3     byte      "5678", 0
      MyList4     byte      "bon jour", 0 
      MyList5     byte      "2222", 0
      MyList6     byte      "adios", 0
    
    
    pub main  | idx, p_list
    
      setup
    
      repeat
        repeat idx from 0 to 6
          p_list := lookupz(idx : @MyList0, @MyList1, @Mylist2, @MyList3, @MyList4, @MyList5, @MyList6)
          term.str(p_list)
          term.tx(CR)
          pause(1000)
    
    
    pub setup
    
    '' Setup IO and objects for application
    
      term.start(RX1, TX1, %0000, 115_200)                           ' start serial for terminal
    


    BTW... PST is just FullDuplexSerial with training wheels -- take of the training wheels! :)
  • mikeamikea Posts: 283
    edited 2015-02-20 19:57
    Thank you for the example. I'll try full duplex serial....why is it better?
  • JonnyMacJonnyMac Posts: 9,105
    edited 2015-02-20 20:36
    Not better, it's the same code without all the necessary help. If you're happy with PST, keep using it.

    BTW, here's how I would print the list. I always strive to create code that can be used across a variety of applications.
    con { pst formatting }
    
       #1, HOME, GOTOXY, #8, BKSP, TAB, LF, CLREOL, CLRDN, CR
      #14, GOTOX, GOTOY, CLS
    
    
    obj
    
      term : "jm_fullduplexserial"
    
    
    dat
    
      MyList      byte      "hi", 0
                  byte      "1234", 0
                  byte      "bye", 0 
                  byte      "5678", 0
                  byte      "bon jour", 0 
                  byte      "2222", 0
                  byte      "adios", 0
    
    
    pub main  | idx, p_list
    
      setup
    
      repeat
        repeat idx from 0 to 6
          term.str(str_pntr(@MyList, idx))
          term.tx(CR)
          pause(1000)
    
    
    pub str_pntr(p_list, item) | c
    
    '' Returns pointer for string within a list of strings
    
      repeat while (item > 0)                                        ' loop until item found
        c := byte[p_list++]                                          ' get a character
        if (c == 0)                                                  ' if 0 (end of current string)
          --item                                                     ' decrement item count
    
      return p_list
    


    This uses a method called str_pntr() to find the starting address of a specific string within a list of strings.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-02-21 16:02
    Here's my version of a method which does the same thing as Jon's "str_pntr" method.
    PUB FindString(firstStr, stringIndex)      
    '' Finds start address of one string in a list
    '' of strings. "firstStr" is the address of 
    '' string #0 in the list. "stringIndex"
    '' indicates which of the strings in the list
    '' the method is to find.
    
      result := firstStr 
      repeat while stringIndex    
        repeat while byte[result++]  
        stringIndex--
    

    I admit to being pretty pleased with myself after writing it. I complimented myself with the thought "It looks like something JonnyMac would write".

    While our methods aren't exactly the same, I like to think my compliment was justified.
  • JonnyMacJonnyMac Posts: 9,105
    edited 2015-02-21 16:19
    "It looks like something JonnyMac would write".

    High praise, indeed. ;)

    </sarc>
  • ChrisGaddChrisGadd Posts: 310
    edited 2015-02-21 19:53
    You use the str method to display strings. Char sends a byte to the display exactly as entered, and dec converts a byte to decimal ASCII.

    The lookup list works fine for strings, provided they're formatted correctly:
      repeat index from 1 to 7
        pst.str(lookup(index: string("hi"),string("1234"),string("bye"),string("5678"),string("bon jour"),string("2222"),string("adios")))
        pst.char($0D)
    
Sign In or Register to comment.