Hi!
Has anyone got the TFT BMP demo working with Seeedstudio’s 2.8’’ TFT Touch Shield screen? I have version 1.0 of the screen.
Demo code can be found in the 2.8" TFT Touch Shield’s wiki site. http://seeedstudio.com/wiki/2.8%27%27_TFT_Touch_Shield#Demo_Code_shown
Here is direct link to the example: http://www.seeedstudio.com/wiki/images/1/11/Tftbmp_demo.zip
I’m using Seeeduino Stalker v2.1 with 2Gb microSD-card and arduino v1.0 IDE. I got all other example codes working except this one.
You need to slightly modify the TFT library to get it compiling in the v1.0 arduino IDE.
You need to replace this code in the library:
#include "WProgram.h"
[/code]With this:
[code]#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif[/code]To get it compiling without errors.
The BMP demo pictures is showing on the screen all sorts of scrambled pixels and colored vertical lines. Basically there is no picture.
[img]http://img254.imageshack.us/img254/786/07022012528.jpg[/img]
Although picture data printed in the serial port is showing all the file details with no problems so I guess the file is oppened correctly.
I assume that the problem has something to do with the way the example code converts pixel colors from 8r-8g-8b-mode to 5r-6g-5b-mode.
Can someone help me with this problem? Is anyone else experiencing this same problem?
Here is the demo code that I have used:
[code]// This sketch is adapted to Seeed's TFT library from
// https://github.com/adafruit/TFTLCD-Library/blob/master/examples/tftbmp/tftbmp.pde
// by ladyada/adafruit under MIT license
#include <SD.h>
#include <TFT.h>
// In the SD card, place 24 bit color BMP files (be sure they are 24-bit!)
// the file itself
File bmpFile;
// information we extract about the bitmap file
int bmpWidth, bmpHeight;
uint8_t bmpDepth, bmpImageoffset;
void setup()
{
Serial.begin(57600);
Tft.init();
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT); //10 is used as chip select pin
if (!SD.begin(10)) { //10 is used as chip select pin
Serial.println("failed!");
return;
}
Serial.println("SD OK!");
}
void loop()
{
Tft.setOrientation(0);
//Image 1
bmpFile = SD.open("flower.bmp");
if (! bmpFile) {
Serial.println("didnt find image");
while (1);
}
if (! bmpReadHeader(bmpFile)) {
Serial.println("bad bmp");
return;
}
Serial.print("image size ");
Serial.print(bmpWidth, DEC);
Serial.print(", ");
Serial.println(bmpHeight, DEC);
bmpdraw(bmpFile, 0, 0);
delay(3000);
bmpFile.close();
//Image 2
bmpFile = SD.open("hibiscus.bmp");
if (! bmpFile) {
Serial.println("didnt find image");
while (1);
}
if (! bmpReadHeader(bmpFile)) {
Serial.println("bad bmp");
return;
}
Serial.print("image size ");
Serial.print(bmpWidth, DEC);
Serial.print(", ");
Serial.println(bmpHeight, DEC);
bmpdraw(bmpFile, 0, 0);
delay(3000);
}
/*********************************************/
// This procedure reads a bitmap and draws it to the screen
// its sped up by reading many pixels worth of data at a time
// instead of just one pixel at a time. increading the buffer takes
// more RAM but makes the drawing a little faster. 20 pixels' worth
// is probably a good place
#define BUFFPIXEL 20
void bmpdraw(File f, int x, int y) {
bmpFile.seek(bmpImageoffset);
uint32_t time = millis();
uint16_t p;
uint8_t g, b;
int i, j;
uint8_t sdbuffer[3 * BUFFPIXEL]; // 3 * pixels to buffer
uint8_t buffidx = 3*BUFFPIXEL;
for (i=0; i< bmpHeight; i++) {
Tft.setXY(x, y+bmpHeight-i);
for (j=0; j<bmpWidth; j++) {
// read more pixels
if (buffidx >= 3*BUFFPIXEL) {
bmpFile.read(sdbuffer, 3*BUFFPIXEL);
buffidx = 0;
}
// convert pixel from 888 to 565
b = sdbuffer[buffidx++]; // blue
g = sdbuffer[buffidx++]; // green
p = sdbuffer[buffidx++]; // red
p >>= 3;
p <<= 6;
g >>= 2;
p |= g;
p <<= 5;
b >>= 3;
p |= b;
// write out the 16 bits of color
Tft.sendData(p);
}
}
Serial.print(millis() - time, DEC);
Serial.println(" ms");
}
boolean bmpReadHeader(File f) {
// read header
uint32_t tmp;
if (read16(f) != 0x4D42) {
// magic bytes missing
return false;
}
// read file size
tmp = read32(f);
Serial.print("size 0x"); Serial.println(tmp, HEX);
// read and ignore creator bytes
read32(f);
bmpImageoffset = read32(f);
Serial.print("offset "); Serial.println(bmpImageoffset, DEC);
// read DIB header
tmp = read32(f);
Serial.print("header size "); Serial.println(tmp, DEC);
bmpWidth = read32(f);
bmpHeight = read32(f);
if (read16(f) != 1)
return false;
bmpDepth = read16(f);
Serial.print("bitdepth "); Serial.println(bmpDepth, DEC);
if (read32(f) != 0) {
// compression not supported!
return false;
}
Serial.print("compression "); Serial.println(tmp, DEC);
return true;
}
/*********************************************/
// These read data from the SD card file and convert them to big endian
// (the data is stored in little endian format!)
// LITTLE ENDIAN!
uint16_t read16(File f) {
uint16_t d;
uint8_t b;
b = f.read();
d = f.read();
d <<= 8;
d |= b;
return d;
}
// LITTLE ENDIAN!
uint32_t read32(File f) {
uint32_t d;
uint16_t b;
b = read16(f);
d = read16(f);
d <<= 16;
d |= b;
return d;
}