Shop OBEX P1 Docs P2 Docs Learn Events
VGA_TEXT_DEMO mods to display decimal digits — Parallax Forums

VGA_TEXT_DEMO mods to display decimal digits

Richard S.Richard S. Posts: 70
edited 2007-01-19 02:58 in Propeller 1
I am reposting this message since I forgot the 'subject' previously.· Also I made a few corrections.

I have been twiddling with VGA_TEXT_DEMO.SPIN and VGA_TEXT.SPIN files.· My goal is to be able to display 3.4 digit number (3 digits to the left of the decimal preceded by spaces and·+/- sign·where appropriate,·and 4 digits to the right of the decimal point padded with zeros where appropriate· i.e. "·97.0035").· What a job!· I like the screen colors, character size, and small size of the program.··My·number to print would be a·LONG (32 bit) eventually coming·from the ENCODER.SPIN program.· I have not yet made the leap·of transfering ENCODER output to the VGA_TEXT display.· I am trying to get numbers to display properly first.

I have·included my edited RFS_VGA_TEXT_DEMO.SPIN and RFS_VGA_TEXT.SPIN files.· Note that if j = 0 the VGA_TEXT·adds an extra 0 on the display...I have not been able to solve this problem.· If j =·1, the problem resolves.· The extra zero is not present if j = -j.· I also have not figured out how to pad 0's between the decimal point and numbers to the·right of the decimal point.

Is there any easier way to do this?



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Richard in Michigan

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-01-14 22:35
    Try this. I modified a routine that I've been using for some time. It converts a number from binary to decimal characters. I basically split the conversion into two segments. First, do a zero suppressed portion, then (if there's a fixed point portion) put out a decimal point and a non-zero suppressed portion. If the specified width is greater than the actual width of the information, pad out on the left with spaces.
    PUB decFixed(value,width,numPlaces) | i, j, p, f, a, b, c, d
    '' Print a decimal number of fixed width with decimal places
      numPlaces #>= 0
      numPlaces <#= 10
      p := @a
      f~
      if value < 0
        -value
        byte[noparse][[/noparse]p++] := "-"
      i := 1_000_000_000
      repeat j from 1 to 10 - numPlaces
        if value => i
          byte[noparse][[/noparse]p++] := value / i + "0"
          value //= i
          f~~
        elseif f or j == (10 - numPlaces)
          byte[noparse][[/noparse]p++] := "0"
        i /= 10
      if numPlaces
        byte[noparse][[/noparse]p++] := "."
        repeat numPlaces
          if value => i
            byte[noparse][[/noparse]p++] := value / i + "0"
            value //= i
          else
            byte[noparse][[/noparse]p++] := "0"
          i /= 10
      byte[noparse][[/noparse]p]~
      i := strsize(@a)
      if i < width
        repeat width - i
          out(" ")
      str(@a)
    
    
  • Richard S.Richard S. Posts: 70
    edited 2007-01-15 00:02
    Thanx Mike...

    I will grind on this for awhile!· smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Richard in Michigan
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-01-15 21:02
    Richard, I deleted your other post for you, in the future you can change the title to your ost by clicking on the pencil icon.

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

    Parallax, Inc.
  • Richard S.Richard S. Posts: 70
    edited 2007-01-16 04:18
    Thanx Mike...I did not know that.

    In the

    'PUB decFixed(value,width,numPlaces) | i, j, p, f, a, b, c, d'

    line there are several unused variables...are they left over from some other project or am I missing something?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Richard in Michigan
  • Mike GreenMike Green Posts: 23,101
    edited 2007-01-16 04:50
    Richard,
    The variables "a" through "d" are used as the string buffer for the result. There have to be 4 variables with "a" as the first one (to make 16 bytes).
    Mike
  • Stan671Stan671 Posts: 103
    edited 2007-01-16 19:28
    I use this method for formatting an Integer into a String.· It does not support decimal places, though:

    '' Format a Number into a Right Justified String with Optional Space Padded and Commas
    '' Method gets the value to convert, the width of the desired string and a flag to enable commas.
    '' Method returns a pointer to the resulting string.
    '' Result string is null(0) terminated.
     
    CON
      MAX_WIDTH = 63  'Maximum width of the result string, including left side padding with spaces
      
    VAR
      long  idx  'pointer into string
      byte  ResultString[noparse][[/noparse] MAX_WIDTH + 1 ]  'String containing results, plus the 0 terminator
      byte  IsNegative, DigitNum
    
    PUB Integer2String( value, Width, Commas ) : StringPointer
    '' Convert signed integer to a string that is,
    '' truncated or space padded to a specific width (when width > 0),
    '' or not padded at all (when width = 0),
    '' with commas inserted (when commas = true) or not (when commas = false) 
     
      if value < 0  'Check for negative value
        IsNegative~~  'true
        ||value  'Use absolute (positive) value only
      else
        IsNegative~  'False
     
      bytefill( @ResultString, " ", MAX_WIDTH )  'Fill string with spaces first
      idx := MAX_WIDTH  'Start placing digits at end of string
      ResultString[noparse][[/noparse] idx-- ] := 0  'Place 0 terminator at end of string
      DigitNum~  'Zero
     
      repeat until value == 0  'Keep slicing digits off of the ones column
        ResultString[noparse][[/noparse] idx-- ] := ( value // 10 ) + "0"  'put in front of previous digits
        value /= 10  'truncate rightmost digit
        if Commas and ( ++DigitNum // 3 == 0 ) and ( value > 0 ) 'For every three digits placed
          ResultString[noparse][[/noparse] idx-- ] := ","  'put a comma in the string, if requested
                      
      if IsNegative  'for a negative number
        ResultString[noparse][[/noparse] idx-- ] := "-"  'place the minus sign in front
     
      if Width == 0 'Check if fixed width is requested
        StringPointer:= @ResultString + idx + 1  'No, so just point to first real character in string
      else  'Yes, padding or truncating may be needed
        Width := 1 #> Width <# MAX_WIDTH  'Make sure requested width is within bounds
        StringPointer := @ResultString + ( MAX_WIDTH - Width )  'point to padded or truncated start of string
     
      return    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Stan Dobrowski
  • Richard S.Richard S. Posts: 70
    edited 2007-01-17 02:00
    Hello Mike...

    RE: variables a, b, c, and d. I assume that the 'method' variables (longs - 4 bytes each) are allocated sequentially in memory as 16 bytes of buffer space (not zero terminated as in zstring). Then one references the first variable, @a, then index into the space??


    Hello Stan...

    Thanx for your integer to string contribution. I am studying it to understand more of how all this is done. It looks like one could fiddle with the comma routine to get number of decimals plus addition of decimal point. Padding out the decimal places with zeros will take some work, however. I may work on that a bit.


    I have had fun changing the color palatte at the end of the vga_text.spin file. All kinds of color schemes can be created. I like the dark blue background the best with a yellow, green, or white foreground. I also made the general screen black with red foreground characters (see first entry in the table).

    ' fore back
    ' RGB RGB
    palette byte %%300, %%000 '0 red / black
    byte %%330, %%001 '1 yellow / dark blue
    byte %%333, %%001 '2 white / dark blue
    byte %%000, %%333 '3 black / white
    byte %%033, %%011 '4 cyan / dark cyan
    byte %%030, %%001 '5 green / dark blue
    byte %%033, %%001 '6 cyan / dark blue
    byte %%330, %%001 '7 yellow / dark blue

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Richard in Michigan
  • Mike GreenMike Green Posts: 23,101
    edited 2007-01-17 02:14
    Richard,
    Yes, the local variables are allocated sequentially. If you look at the code, I assign the address of "a" to a pointer variable "p", then use that from then on.
    Mike
  • Richard S.Richard S. Posts: 70
    edited 2007-01-17 02:40
    Thank-you Mike...

    I do understand. Your patience is much appreciated!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Richard in Michigan
  • Stan671Stan671 Posts: 103
    edited 2007-01-17 07:03
    I thought I had a version of the formatter that handled implied decimal places, but I cannot seem to find it.· But since my routine extracts the digits from the right side of the number, it should be easy to know when to pop the decimal point into the string.· Also, there is no difficulty with zero padding on the right with the implied decimal place because the digits are already there in the integer number.· I'll see if I can work on adding this improvement to the routine, but my Propeller is offline right now.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Stan Dobrowski
  • Richard S.Richard S. Posts: 70
    edited 2007-01-18 03:10
    Stan...

    Thanx again for your comments.· I find your routine very instructive.· If you come across the decimal formatter program I would very much like to see it.· Mike's routine and several others from the PROP page such as Simple_numbers.spin, have been very helpful!· Now to get some of this to work collectively.

    I do have a question.· In a programs such as VGA_TEXT_DEMO.spin that calls (through OBJ) VGA_TEXT.spin which in turn calls (through OBJ) VGA.spin....does each of these programs start in there own cog? or are they lumped together into one large program.· It is not clear to me since none of the three seem specifically call a cog function.

    Mike's routine:···I have added documentation and include it for review and critiquing.··

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Richard in Michigan
  • Mike GreenMike Green Posts: 23,101
    edited 2007-01-18 03:35
    There is no automatic starting of separate cogs. Objects are intended to encapsulate some function and don't necessarily need their own cog to do it. In the case of video generation, it has to be done in assembly for speed and timing accuracy, so requires a cog for the video driver. The start routine in VGA.spin does this for you. TV.spin does the same kind of thing for the same reasons.

    Thanks for adding the extra comments. For the record, I'm not a Parallax employee.

    If you're curious, I could have figured out the number of spaces needed when I found the first non-zero digit, but it would make the logic a lot more complicated. It's simpler to just produce the variable length string and later add any needed leading blanks.
  • Richard S.Richard S. Posts: 70
    edited 2007-01-19 02:58
    Hello Mike and Stan...

    I have succeeded in printing fixed decimal place numbers with sign and appropriate space padding, even retaining zeros in the decimal places!!· Your help has been invaluable.· If one runs the rfs_vga_text_demo.spin you can see different things happening including a negative number counting up slowly through 0.0 and beyond.

    Mike, I took your program, made some adjustments using Stans approach and have come up a working model.

    I use VGA_TEXT_DEMO.spin as the integer input, VGA_TEXT.spin as the integer to ASCII converter plus ASCII string printer, and VGA.spin to run the video.· I chopped out unnecessary routines in the VGA_TEXT.spin program.· The compiled version only takes 446 longs.

    Next is to connect the ENCODER.spin

    I have posted my working models for those interested.

    This will be my last post to this thread.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Richard in Michigan
Sign In or Register to comment.