Ok, so I tried to adjust to your guidance. I found a similar topic
https://forum.arduino.cc/index.php?topic=406416.0
and I modified it to HX8347D_kbv.h libraries, I think the problem is with readPixel because I get the black bitmaps. When i hardcoded i.e red color, bitmap is red.
PS:I uncoment the readGRAM implementation before.
What I'm doing wrong ?
#include <SPI.h>
#include <SdFat.h>
#include <Adafruit_GFX.h>
#include <HX8347D_kbv.h>
#include <Stream.h>
#include <stdint.h>
const byte SD_CS = 5;
const int w = 240; // image width in pixels
const int h = 320; // height
char str[] = "kielok_piyk64ny.bmp";
const unsigned int GREY = 0xDF1C;
const unsigned int LTRED = 0xFE79;
const unsigned int DKGREEN = 0x1BE0;
uint16_t g_identifier;
SdFat SD;
File outFile;
HX8347D_kbv tft;
void setup()
{
Serial.begin(9600);
uint32_t when = millis();
// while (!Serial) ; //hangs a Leonardo until you connect a Serial
if (!Serial) delay(1); //allow some time for Leonardo
Serial.println("Serial took " + String((millis() - when)) + "ms to start");
static uint16_t identifier;
// tft.reset(); //we can't read ID on 9341 until begin()
g_identifier = tft.readID(); //
Serial.print("ID = 0x");
Serial.println(g_identifier, HEX);
//if (g_identifier == 0x00D3 || g_identifier == 0xD3D3) g_identifier = 0x9481; // write-only shield
//if (g_identifier == 0xFFFF) g_identifier = 0x9341; // serial
// g_identifier = 0x9329; // force ID
tft.begin(g_identifier);
/* uint16_t ID;
ID = tft.readID();
tft.begin(ID);*/
// print some objects on TFT to be captured in BMP
tft.setRotation(3);
tft.setTextSize(3);
tft.fillRect(150, 150, 30, 30, GREY);
tft.fillRect(30, 150, 30, 30, LTRED);
tft.fillRect(150, 30, 30, 30, DKGREEN);
tft.setCursor(50, 50);
tft.print("hello world");
// end test print to TFT
//Serial.begin(115200);
//Serial.println("starting");
//init SD Card
if (!SD.begin(SD_CS))
{
Serial.println("err strtng SD");
while (1); //If failed, stop here
}
Serial.println("working");
GrabImage(str);
Serial.println("done");
tft.setCursor(100, 100);
tft.print("Done");
}
void GrabImage(char* str)
{
byte VH, VL;
int i, j = 0;
//Create the File
outFile = SD.open(str, FILE_WRITE);
if (! outFile) {
Serial.println("err opng file");
return;
};
unsigned char bmFlHdr[14] = {
'B', 'M', 0, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0
};
// 54 = std total "old" Windows BMP file header size = 14 + 40
unsigned char bmInHdr[40] = {
40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 16, 0
};
// 40 = info header size
// 1 = num of color planes
// 16 = bits per pixel
// all other header info = 0, including RI_RGB (no compr), DPI resolution
unsigned long fileSize = 2ul * h * w + 54; // pix data + 54 byte hdr
bmFlHdr[ 2] = (unsigned char)(fileSize ); // all ints stored little-endian
bmFlHdr[ 3] = (unsigned char)(fileSize >> 8); // i.e., LSB first
bmFlHdr[ 4] = (unsigned char)(fileSize >> 16);
bmFlHdr[ 5] = (unsigned char)(fileSize >> 24);
bmInHdr[ 4] = (unsigned char)( w );
bmInHdr[ 5] = (unsigned char)( w >> 8);
bmInHdr[ 6] = (unsigned char)( w >> 16);
bmInHdr[ 7] = (unsigned char)( w >> 24);
bmInHdr[ 8] = (unsigned char)( h );
bmInHdr[ 9] = (unsigned char)( h >> 8);
bmInHdr[10] = (unsigned char)( h >> 16);
bmInHdr[11] = (unsigned char)( h >> 24);
outFile.write(bmFlHdr, sizeof(bmFlHdr));
outFile.write(bmInHdr, sizeof(bmInHdr));
for (i = h; i > 0; i--) {
for (j = 0; j < w; j++) {
uint16_t rgb = tft.readPixel(j,i); // get pix color in rgb565 format
/*
//Test color, Red in RGB888
// it works
byte r = 255;
byte g = 0;
byte b = 0;
//Convert RGB888 to RGB565
unsigned short rgb = ((((r>>3)<<11) | ((g>>2)<<5) | (b>>3))) ;
*/
VH = (rgb & 0xFF00) >> 8; // High Byte
VL = rgb & 0x00FF; // Low Byte
//RGB565 to RGB555 conversion... 555 is default for uncompressed BMP
//this conversion is from ...topic=177361.0 and has not been verified
VL = (VH << 7) | ((VL & 0xC0) >> 1) | (VL & 0x1f);
VH = VH >> 1;
//Write image data to file, low byte first
outFile.write(VL);
outFile.write(VH);
}
}
//Close the file
outFile.close();
}
void loop()
{
}