Shop OBEX P1 Docs P2 Docs Learn Events
How Do I Shorten These Several Statements Into One Statement? — Parallax Forums

How Do I Shorten These Several Statements Into One Statement?

Hello again everyone,

Well moving into learning Spin,
I'm trying to put together a program to write colour values directly to the video buffer,

Colors are made up of 6 bits, 2bits per colour (rgb)
They are stored in a byte array for now.

screen width is 200
screen height is 112

The data for 4 pixels is stored in 3 byte blocks
Bytes RRGGBBRR | GGBBRRGG | BBRRGGBB
Pixels RRGGBB | RRGGBB | RRGGBB | RRGGBB



My main query this time is how do I shorten these several statements into one formula?

1. Get Pixel-Id
  PixelId:= (pPixelY * vwScrnWidth) + pPixelX   
  'X = (Py * ScrnWidth) + Px


2. Get Pixel-Bits-Offset
  PxlOffsetBits:= (PixelId * COLORBITS)
  'OffsetBits = PixelId (from above) * 6


3. Get Internal-Pixel-Id Inside 3-Byte-Block
  Modulus:= PixelId // 4


4. Use Modulus with above to get byte address for pixel in video buffer
  Case Modulus
    0:
       CanvasByteAddr:= PxlOffsetBits / 8
       DataByte:= Byte[CanvasByteAddr]
       If GetBit(BITINBYTE, pPixelColor, 5) == 1
           SetBit(BITINBYTE, DataByte, 7)
       If GetBit(BITINBYTE, pPixelColor, 4) == 1
           SetBit(BITINBYTE, DataByte, 6)
       If GetBit(BITINBYTE, pPixelColor, 3) == 1
           SetBit(BITINBYTE, DataByte, 5)
       If GetBit(BITINBYTE, pPixelColor, 2) == 1
           SetBit(BITINBYTE, DataByte, 4)
       If GetBit(BITINBYTE, pPixelColor, 1) == 1
           SetBit(BITINBYTE, DataByte, 3)
       If GetBit(BITINBYTE, pPixelColor, 0) == 1
           SetBit(BITINBYTE, DataByte, 2)
       Byte[CanvasByteAddr]:= DataByte

    1:
       CanvasByteAddr:= (PxlOffsetBits-6) / 8
    2:
       CanvasByteAddr:= (PxlOffsetBits-4) / 8
    3:
       CanvasByteAddr:= (PxlOffsetBits-2) / 8



The furthest I could get was:
DirectAddr:= ((pPixelY * vwScrnWidth) + pPixelX) * COLORBITS



I could not figure out how to incorporate the modulus part into my formula nor how to BitShift?

I'm sure the getbit setbit statements could be shortened too,
but I'm struggling to figure out how

It probably has a few bugs/errors, and maybe someone can help me fix them along the way too?.



Thank You

JD


Comments

  • JaanDoh wrote: »
    My main query this time is how do I shorten these several statements into one formula?

    Can you tell us what the code is supposed to accomplish?

    It may be easier for some of us to figure out an algorithm based on a description of what you are trying to do rather than trying to shorten your code.

    What video driver are you using?

  • Try this:
      Case Modulus
        0:
           CanvasByteAddr:= PxlOffsetBits / 8
           Byte[CanvasByteAddr] <<= 2
        1:
           CanvasByteAddr:= (PxlOffsetBits-6) / 8
        2:
           CanvasByteAddr:= (PxlOffsetBits-4) / 8
        3:
           CanvasByteAddr:= (PxlOffsetBits-2) / 8
    

    What you programmed so painstakingly was just a left-shift by 2.

    -Phil
  • Duane Degn wrote: »
    JaanDoh wrote: »
    My main query this time is how do I shorten these several statements into one formula?

    Can you tell us what the code is supposed to accomplish?

    It may be easier for some of us to figure out an algorithm based on a description of what you are trying to do rather than trying to shorten your code.

    What video driver are you using?

    Hello Duane,

    I'm trying to set/unset (program) a PIXEL, as opposed to using characters like in the default Parallax VGA driver.
    So instead of using characters to draw a border or frame, I want to be able to use pixel level addressing instead of characters on an X,Y grid


    I had planned on using 6 bits to represent the color of a pixel.
    The full source of my dabblings can be found below.
    It may give some of you a laugh hahaha
    :D

    Thank you


  • Try this:
      Case Modulus
        0:
           CanvasByteAddr:= PxlOffsetBits / 8
           Byte[CanvasByteAddr] <<= 2
        1:
           CanvasByteAddr:= (PxlOffsetBits-6) / 8
        2:
           CanvasByteAddr:= (PxlOffsetBits-4) / 8
        3:
           CanvasByteAddr:= (PxlOffsetBits-2) / 8
    

    What you programmed so painstakingly was just a left-shift by 2.

    -Phil

    Hello Phil,

    Thank You for your input,
    So If I understand correctly, my direct pixel address formula should be....
    DirectAddr:= (((pPixelY * vwScrnWidth) + pPixelX) * COLORBITS) << 2
    

    Is that right?
    I'm looking forward to trying this stuff out when I get the voltage regulator arrives in the post via ebay,
    to down the voltage from 5V to 3.3v for the propeller chip.
    Until then its simply just dabbling and exercises in trying to get used to the SPIN language unfortunately.

    Thank You

    JD


Sign In or Register to comment.