Shop OBEX P1 Docs P2 Docs Learn Events
Help with strings — Parallax Forums

Help with strings

MacGeek117MacGeek117 Posts: 747
edited 2007-12-14 15:19 in Propeller 1
I'm building a project with ComFile Tech's IntelliLCD. The commands are supposed to be given in string form (i.e. "Line 1,1,100,100;"). The Prop Tool spits out a error message when I replace the numbers with variables.
lcd.str(string("line ",x1,y1,x2,y2,";"))

Help!!!!
RoboGeek

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"I reject your reality and subsitute my own!"

Adam Savage, Mythbusters
www.parallax.com
www.goldmine-elec.com
www.expresspcb.com
www.startrek.com
·

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-12-11 21:20
    string is a compile time constant, so using variables is not permitted. You need to decompose it into constant and non-constant portions:

    lcd.str(string("line "))
    lcd.dec(x1)
    lcd.str(String(", "))
    lcd.dec(y1)
    lcd.str(String(", "))
    
    lcd.dec(x2)
    lcd.str(String(", "))
    
    lcd.dec(y2)
    lcd.out(";")
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • deSilvadeSilva Posts: 2,967
    edited 2007-12-11 21:20
    Well, you do substitute your own reality smile.gif
    STRING() ist not PRINTF()
    There are some attempts to prepare a PRINTF, but the hard thing are missing function pointers...

    You best do it the "standard way":
    lcd.str(string("line "))
    lcd.dec(X1)
    lcd.out(",")

    etc, etc...
  • MacGeek117MacGeek117 Posts: 747
    edited 2007-12-11 23:42
    Thanks, Paul!
    RoboGeek

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "I reject your reality and subsitute my own!"

    Adam Savage, Mythbusters
    www.parallax.com
    www.goldmine-elec.com
    www.expresspcb.com
    www.startrek.com
    ·
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-12-11 23:56
    Here is one of those printf-like attempts deSilva may be referring to. If you're uncomfortable with the callbacks, you can always modify the Format object not to use them and just to use your Lcd output exclusively. To do this, change the putc routine at the bottom of Format.spin to call lcd.out, being sure, of course to declare lcd as an object.

    Someday Spin will have function pointers and — who knows? — maybe even variable-length argument lists. (I'm sure the interpreter will support the former; I'm not so sure about the latter.) Until then, Format is a little awkward, but it works.

    -Phil
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-12-12 02:11
    Phil,

    I looked at your format object and I find the mechanism rather complex.
    The task can be accomplished much simpler I think. For the javelin I wrote
    a Format class some years ago. There are 3 basic methods: printf, sprintf and bprintf.
    The bprintf is the core routine, it takes a buffer, an index value,·a control string and a value parameter.
    It returns an updated index value. This makes it easy to assemble an output string
    in memory for a sprintf with multiple arguments.

    For a target output
    · sprintf("this is module %s the second argument value is %d",value,number)
    it becomes
    · char buf[noparse][[/noparse]128];
    · int k;
    · k = bprintf(buf,0,"this is module %s",value);
    · k = bprintf(buf,k," the second argument value is %d",number);
    · buf[noparse][[/noparse]k] = 0;
    Now simply send buf to whatever output driver

    The type of the value parameter is known by the control character in the control string,
    for %s it is a string, for %d it is an integer value etc. If there is no control character,
    the value parameter is just a dummy. (eg. sprintf(buf,"text without formatters",0))

    The sprintf simply calls bprintf with index value 0 and automatically adds a closing null.
    The printf outputs to the debug serial out, which would be pin SOUT on the spin stamp.

    Converting my javelin Format class to Spin is next on my list for spin code.

    regards peter
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-12-12 17:24
    Here is my Format object. It is converted from my javelin class.
    I have not tested it and floating point is not supported, but it
    works on the javelin so it should work on the propeller.
    See the bprintf and bscanf descriptions for info how to use it.

    regards peter


    Post Edited (Peter Verkaik) : 12/12/2007 5:38:16 PM GMT
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-12-13 05:54
    A small error was introduced while I converted my Format class.
    For bscanf, no hex characters were scanned.
    This has been fixed in the attachement.

    regards peter
  • MacGeek117MacGeek117 Posts: 747
    edited 2007-12-14 02:22
    Thanks, Peter!
    RoboGeek

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "I reject your reality and subsitute my own!"

    Adam Savage, Mythbusters
    www.parallax.com
    www.goldmine-elec.com
    www.expresspcb.com
    www.startrek.com
    ·
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-12-14 02:39
    An updated version of Format.spin plus test program is here:
    http://forums.parallax.com/showthread.php?p=694892

    This updated version uses byte[noparse][[/noparse]str] to access the bytes in a string.
    str[noparse][[/noparse]h] when str is a passed value does not seem to work.

    I am having trouble when converting an integer value, though
    I am convinced the logic is ok (works on the javelin). I you care to take·a look
    that would be great.

    regards peter
  • deSilvadeSilva Posts: 2,967
    edited 2007-12-14 03:09
    Peter,
    (a)
    > str[noparse][[/noparse]h] when str is a passed value does not seem to work.
    I am not sure what you expect... No it will not work, as it is assumed to be a LONG

    (b) You are using >= and <= rather than => and =< smile.gif
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-12-14 03:41
    Thanks for pointing out the =< and <= difference. The latter is an assignment!
    Unfortunately, changing all the occurences did not resolve the issue·I am having.

    regards peter
  • deSilvadeSilva Posts: 2,967
    edited 2007-12-14 15:07
    Well, what exactly is the issue? Which routine? Example?
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-12-14 15:19
    See this thread:
    http://forums.parallax.com/showthread.php?p=694892

    But it is solved now. Adapting to a new language takes time.

    regards peter
Sign In or Register to comment.