Shop OBEX P1 Docs P2 Docs Learn Events
Towards a one line spin printf — Parallax Forums

Towards a one line spin printf

jazzedjazzed Posts: 11,803
edited 2008-07-08 00:08 in Propeller 1
Lack of a C like printf feature in spin is very annoying. Here is one way to achieve a printf on one line.
Thanks to Peter Verkaik for sharing his format code;·the underlying source is a variant of his work.

CON ' test clock settings
   _clkmode  = xtal1 + pll16x
   _xinfreq  = 5_000_000
OBJ
  tv : "TV_Text"
  s : "snprintf"
 
PUB main | name
  tv.start(20)
  name := string("life") ' replace with your favorite noun
  printf(string("Hello World\n"),0)
  printf(string("I love my %s.\n"),s.p(name,0))
  [s]printf(string("P1 $%x, P2 $%x\nP3 $%x, P4 $%x"),s.p($1,s.p($2,s.p($3,s.p($C,0)))))[/s]
  printf(string("P1 $%x, P2 $%x\nP3 $%x, P4 $%x"), p($1)+p($2)+p($3)+p($C))

PUB printf(fmt, arg) | pb[noparse][[/noparse]256]
  result := s.snprintf(@pb,256,fmt,arg)
  tv.str(@pb)
 
PUB p(v)
  return s.p(v)



Two required methods·are·snprintf and p. Method snprintf fills the buffer up to a specified length
with a string specified by fmt and arg. The fmt string follows the C printf convention. The arg
parameter is a call to the "P" method. Letter·P stands for parameter; using a name like "parm"
would cause a long line. The parameter passed to·P is the content to be substituted in the
format specifier (i.e. %d) in left to right order as appears in fmt. Always use a sum of p() calls
like p(v)+p(v)+p(v) to get a good return code from printf. If the number of format specifiers
does not match the number of p() calls, printf will return 0.

If there are better ways to do this or some good cosmetic suggestions, please let me know.
There may be more room for improvement, but looks·great to me now with Phil's suggestions.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


Post Edited (jazzed) : 7/8/2008 1:13:37 AM GMT

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-07-07 18:01
    I think it would look a little neater if you changed p to take one argument and return a 1. That way you could add the arguments together and not have all those trailing parentheses. As a bonus, printf would know how many arguments were on the stack, in case it didn't agree with the number of fields in the format string:

    
      printf(string("Hello World\n"),0)
      printf(string("I love my %s.\n"),s.p(name))
      printf(string("P1 $%x, P2 $%x\nP3 $%x, P4 $%x"), s.p($1) + s.p($2) + s.p($3) + s.p($C))
    
    
    


    Doing it this way will require the "stack" to be treated as a queue to get the parameters off in the proper order.

    Going one step further, you could define another, local p that called s.p to avoid all those extra periods:

    
      printf(string("Hello World\n"),0)
      printf(string("I love my %s.\n"),s.p(name))
      printf(string("P1 $%x, P2 $%x\nP3 $%x, P4 $%x"), p($1) + p($2) + p($3) + p($C))
    
      ...
    
    PRI p(arg)
    
      s.p(arg)
    
    
    



    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Still some PropSTICK Kit bare PCBs left!
  • jazzedjazzed Posts: 11,803
    edited 2008-07-07 18:20
    Very nice Phil !! Will do.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • jazzedjazzed Posts: 11,803
    edited 2008-07-08 00:08
    I've updated the first post / package with Phil's idea.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sign In or Register to comment.