Loop optimization help

I’m trying to dump a bitmap to a 4bit grayscale LCD, and for some reason this loop takes a 2-3 seconds. I’d love any ideas on how to speed it up.

        public static void DrawBitmap(ref Bitmap bmp)
            byte[] bmpData = bmp.GetBitmap(); // 64 * 128 * 4 channels * 8 bits = 32768 bytes RGBA
            byte[] retval = new byte[4096];
            int inOffset = 0;
            int outOffset = 0;
            float tmpF = 0;
            int tmpI = 0;
            byte gray;
            bool highNIB = false;
            byte grayLast = 0;

            // barf for bad bitmaps
            if ((bmp.Width > 128) || (bmp.Height > 64)) return;

            // go top top left corner
            Set_Column_Address_12864(0x00, 0x3F);
            Set_Row_Address_12864(0x00, 0x7F);            

            while (inOffset<32768) {
                // convert to 4 bit grayscale
                //gray = (byte)(((((float)bmpData[inOffset++]) * .30) + (((float)bmpData[inOffset++]) * .59) + (((float)bmpData[inOffset++]) * .11)) / 16.0);
                //gray = (byte)(((float)bmpData[inOffset++] + (float)bmpData[inOffset++] + (float)bmpData[inOffset++]) / 48.0);
                for (tmpF = 0, tmpI = 0; tmpI < 3; tmpI++)
                    tmpF += bmpData[inOffset++];
                gray = (byte)(tmpF / 48.0);
                inOffset++; // skip a
                if (!highNIB)
                    grayLast = (byte)(gray & 0x0F);
                    highNIB = true;
                    retval[outOffset++] = ((byte)(((gray & 0x0F) << 4) | grayLast));
                    highNIB = false;
            bmpData = new byte[] { }; // free mem

RLP is perfect for this.

While I suspect I’ll be happy enough with just moving the grayscale conversion/packing loop into RLP, is there any way to move the actual SPI comms into RLP as well? i’d love to have a native Bitmap deal. I was thinking of wrapping a bitmap class (since it’s a sealed class) and implementing all the methods as passthroughs and then implementing Flush to go through the RLP and have the grayscale conversion and SPI take place there.

Thanks for all of your help.

Yes you can do that but start without. You will be happy with the results.

What device is this running on? I think we have BPP conversion functions already in the premium library :slight_smile:

I would take a look at examples in tutorials and on codeshare. Some more examples and details in the library documentation too.

Solved. Was a combination of when I did have the RLP part right there was an insidious typo in the C portion that caused me to overrun the output array.

Man that’s loads faster. I can draw the whole screen in a jiffy now.

Two new questions:

  1. Is there a nice way of getting debugging info out of the system?
  2. say I did want to move all of the SPI into RLP, has anyone produced an example of this?

Deleting posts is a bad idea. Now my replies no longer make sense since your posts are gone :slight_smile:

Sorry for skewing the context. The posts ended up being a waste of space.

That said, I’m pretty much disgusted by how easy it is to network this thing. In a couple hours time I went from being able to actually update the screen (So glad I got a Cobra instead of a Panda 2, thanks RLP!) to being able to do NTP and display time. The networking ended up being the easiest part which is really just wrong.

I did work with an ARM based ADuC for a very long time and this has been a change. While I think some of the raw power is lost to overhead, the compromise overall comes out way in favor of running .net on these things, at least with the ability to still do some bits native- without I think I’d be an unhappy customer trying to make this display update fast enough. It’s quite amazing. I spent a very long time trying to get the network up on an ADuC and accomplished that in about 30 minutes here. Insane.

It’s tempting to buy the cobra enclosure and TFT someday, but this OLED is perfecto for what I had in mind.

I’m going to be adding some menu options and extra buttons and things.

Anyway, thanks for all your help.

Now this summer I’m going to get a few pandas and start my other project. I’m salivating just thinking about it.

Glad you ate enjoying your fez :slight_smile: