Quick hack to display bitmaps on 2.8" TFT LCD

Breakout boards, sensors, Drawdio, Game of Life, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

Quick hack to display bitmaps on 2.8" TFT LCD

Postby Kabong » Tue Mar 08, 2011 10:59 pm

Hello all,

I thought I'd share a quick hack I put together to get bitmaps to display on the 2.8" TFT LCD. It's not that elegant of a solution, involves a few steps, but it works. Here's what I do:
1) Open bitmap in your favorite image editor, flip the canvas horizontally, save bitmap
2) Run the saved bitmap through this mismash of quickly thrown together MATLAB code (or port to your favorite language). You'll notice one way I'm saving space on the Arduino is not to bother writing out pixels which are the color of the background. Just do a tft.fillScreen with your color first, then display the bitmap (getting to this soon).
Code: Select all
function bmpToPixels
   
    img = imread('c:\userinput.bmp');
    fid = fopen('C:\userinput.txt','w');
    fprintf(fid,'FLASH_ARRAY(uint8_t,XVals,');
    counter=0;
    for x=1:(size(img,1))
        for y=1:(size(img,2))
            R = img(x,y,1);
            G = img(x,y,2);
            B = img(x,y,3);           
            if(R ~= 255 & G ~= 255 & B ~= 255)
                counter=counter+1;
                fprintf(fid,'%d,',x);
            end
        end
    end
    fprintf(fid,');\n\n// Counter: %d',counter);
    fprintf(fid,'\n\nFLASH_ARRAY(uint8_t,YVals,');
    for x=1:(size(img,1))
        for y=1:(size(img,2))
            R = img(x,y,1);
            G = img(x,y,2);
            B = img(x,y,3);             
            if(R ~= 255 & G ~= 255 & B ~= 255)               
                fprintf(fid,'%d,',y);
            end
        end
    end   
   
    fprintf(fid,');\n\nFLASH_ARRAY(uint8_t,RVals,');
    for x=1:(size(img,1))
       for y=1:(size(img,2))
            R = img(x,y,1);
            G = img(x,y,2);
            B = img(x,y,3);         
            if(R ~= 255 & G ~= 255 & B ~= 255)             
                fprintf(fid,'%d,',R);
            end
        end
    end
   
    fprintf(fid,');\n\nFLASH_ARRAY(uint8_t,GVals,');
    for x=1:(size(img,1))
        for y=1:(size(img,2))
            R = img(x,y,1);
            G = img(x,y,2);
            B = img(x,y,3);   
           
            if(R ~= 255 & G ~= 255 & B ~= 255)
               
                fprintf(fid,'%d,',G);
            end
        end
    end
   
    fprintf(fid,');\n\nFLASH_ARRAY(uint8_t,BVals,');
    for x=1:(size(img,1))
        for y=1:(size(img,2))
            R = img(x,y,1);
            G = img(x,y,2);
            B = img(x,y,3);   
            if(R ~= 255 & G ~= 255 & B ~= 255)                 
                fprintf(fid,'%d,',B);
            end
        end
    end   
   
    fclose(fid);
end

3) Open the resulting file and copy/paste the output into your Arduino code.
4) Add these two functions to your Arduino code (note: I found these online somewhere in a Google search, I don't claim to have written them but I've lost the URL where I found them) :
Code: Select all
uint16_t RGB24toRGB565(uint8_t r, uint8_t g, uint8_t b) {
return ((r / 8) << 11) | ((g / 4) << 5) | (b / 8);
}
void bmpPixelDraw(uint16_t x, uint16_t y,uint8_t r, uint8_t g, uint8_t b){
  uint16_t color = RGB24toRGB565(r,g,b);
  tft.drawPixel(x,y,color);
}


5) You now have the bitmap data stored in five easily accessible 1-dimensional arrays (one for X values, one for Y values, one for red value, one for green value, one for blue value , all that's left is to loop through it and turn your pixels on as such:
Code: Select all
  for(uint16_t z=1;z < VALUE_FROM_COUNTER_LINE_IN_TEXT_FILE; z++ ){
    PP(XVals[z],YVals[z],RVals[z],GVals[z],BVals[z]);
  }


Hooray! Bitmaps on the 2.8" TFT LCD. Now only if this darn Duemilanov had more than 32k of memory.......
http://www.jiggywatts.com/
@Kabong on Twitter.
Kabong
 
Posts: 3
Joined: Mon Feb 28, 2011 4:56 pm

Re: Quick hack to display bitmaps on 2.8" TFT LCD

Postby Kabong » Tue Mar 08, 2011 11:04 pm

Almost forgot, this requires the Flash library, which you can download here:
http://arduiniana.org/libraries/flash/

And also forgot to mention that this is my first whack at doing anything on the Arduino, so I'm completely open to suggestions of how to do this better!
http://www.jiggywatts.com/
@Kabong on Twitter.
Kabong
 
Posts: 3
Joined: Mon Feb 28, 2011 4:56 pm

Re: Quick hack to display bitmaps on 2.8" TFT LCD

Postby Kabong » Tue Mar 08, 2011 11:12 pm

Ack, one more addendum since I don't see a way to edit my posts here. The last bit of code should read:

Code: Select all
for(uint16_t z=1;z < VALUE_FROM_COUNTER_LINE_IN_TEXT_FILE; z++ ){
    bmpPixelDraw(XVals[z],YVals[z],RVals[z],GVals[z],BVals[z]); // THIS LINE CHANGED!
  }
http://www.jiggywatts.com/
@Kabong on Twitter.
Kabong
 
Posts: 3
Joined: Mon Feb 28, 2011 4:56 pm

Re: Quick hack to display bitmaps on 2.8" TFT LCD

Postby adafruit_support_bill » Wed Mar 09, 2011 7:03 am

Thanks for posting that. I'll have to give it a try.

1) Open bitmap in your favorite image editor, flip the canvas horizontally, save bitmap

I think this manual step could be omitted if you just iterated the x-axis in reverse in your code.
User avatar
adafruit_support_bill
 
Posts: 16083
Joined: Sat Feb 07, 2009 9:11 am


Return to Other Adafruit products

Who is online

Users browsing this forum: No registered users and 5 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [108]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
Android[6]
 
BeagleBone[24]
 
XBee[10]
More Dev Boards[31]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[34]
LCDs & Displays[48]
Components & Parts[70]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[111]
 
Wireless[14]
Cables[62]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]