Open nosmaster89 opened 2 days ago
#include <TFT_eSPI.h>
#include <SPIFFS.h>
TFT_eSPI tft = TFT_eSPI();
const int WIDTH = 320;
const int HEIGHT = 170;
uint16_t read16(fs::File &f)
{
uint16_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read(); // MSB
return result;
}
uint32_t read32(fs::File &f)
{
uint32_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read();
((uint8_t *)&result)[2] = f.read();
((uint8_t *)&result)[3] = f.read(); // MSB
return result;
}
void drawBmp(const char *filename, int16_t x, int16_t y)
{
Serial.println("Loading image '" + String(filename) + "'");
if ((x >= tft.width()) || (y >= tft.height()))
{
Serial.println("Image is off screen");
return;
}
fs::File bmpFS;
// Open requested file on SD card
bmpFS = SPIFFS.open(filename, "r");
if (!bmpFS)
{
Serial.println("File not found");
return;
}
uint32_t seekOffset;
uint16_t w, h, row, col;
uint8_t r, g, b;
uint32_t startTime = millis();
Serial.println("trying to load bmp file");
if (read16(bmpFS) == 0x4D42)
{
Serial.println("found BMP file!");
read32(bmpFS);
read32(bmpFS);
seekOffset = read32(bmpFS);
read32(bmpFS);
w = read32(bmpFS);
h = read32(bmpFS);
if ((read16(bmpFS) == 1) && (read16(bmpFS) == 24) && (read32(bmpFS) == 0))
{
y += h - 1;
bool oldSwapBytes = tft.getSwapBytes();
tft.setSwapBytes(true);
bmpFS.seek(seekOffset);
uint16_t padding = (4 - ((w * 3) & 3)) & 3;
uint8_t lineBuffer[w * 3 + padding];
for (row = 0; row < h; row++)
{
bmpFS.read(lineBuffer, sizeof(lineBuffer));
uint8_t *bptr = lineBuffer;
uint16_t *tptr = (uint16_t *)lineBuffer;
// Convert 24 to 16-bit colours
for (uint16_t col = 0; col < w; col++)
{
b = *bptr++;
g = *bptr++;
r = *bptr++;
*tptr++ = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}
// Push the pixel row to screen, pushImage will crop the line if needed
// y is decremented as the BMP image is drawn bottom up
tft.pushImage(x, y--, w, 1, (uint16_t *)lineBuffer);
}
tft.setSwapBytes(oldSwapBytes);
Serial.print("Loaded in ");
Serial.print(millis() - startTime);
Serial.println(" ms");
}
else
Serial.println("BMP format not recognized.");
}
Serial.println("File not loaded");
bmpFS.close();
}
uint16_t *bmp_file = (uint16_t *)malloc(WIDTH * HEIGHT);
void convertBMP(String name)
{
fs::File bmpFS;
bmpFS = SPIFFS.open(name, "r");
if (!bmpFS)
{
Serial.println("File not found");
return;
}
uint32_t seekOffset;
uint16_t w, h, row, col;
uint8_t r, g, b;
if (read16(bmpFS) == 0x4D42)
{
read32(bmpFS);
read32(bmpFS);
seekOffset = read32(bmpFS);
read32(bmpFS);
w = read32(bmpFS);
h = read32(bmpFS);
if ((read16(bmpFS) == 1) && (read16(bmpFS) == 24) && (read32(bmpFS) == 0))
{
bool oldSwapBytes = tft.getSwapBytes();
tft.setSwapBytes(true);
bmpFS.seek(seekOffset);
uint16_t padding = (4 - ((w * 3) & 3)) & 3;
uint8_t lineBuffer[w * 3 + padding];
for (row = 0; row < h; row++)
{
bmpFS.read(lineBuffer, sizeof(lineBuffer));
uint8_t *bptr = lineBuffer;
uint16_t *tptr = (uint16_t *)lineBuffer;
// Convert 24 to 16-bit colours
for (uint16_t col = 0; col < w; col++)
{
b = *bptr++;
g = *bptr++;
r = *bptr++;
*tptr++ = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}
}
tft.setSwapBytes(oldSwapBytes);
// store it to bmp_file
for (int i = 0; i < WIDTH / 2 * HEIGHT; i++)
{
bmp_file[i] = lineBuffer[i];
}
}
else
Serial.println("BMP format not recognized.");
}
bmpFS.close();
}
void setup()
{
// init spiffs
if (!SPIFFS.begin(true))
{
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
Serial.begin(115200);
Serial.println("TFT_eSPI test");
tft.init();
tft.setRotation(1);
tft.setSwapBytes(true);
Serial.println("TFT_eSPI test pattern");
// tft.fillScreen(TFT_BLUE);
tft.fillScreen(TFT_BLACK);
Serial.println("TFT_eSPI pushImage");
// get the test image from the spiffs img.png
drawBmp("/img.bmp", 0, 0);
Serial.println("TFT_eSPI readRect");
convertBMP("/img.bmp");
// read back the test pattern
uint16_t *color_readback;
color_readback = (uint16_t *)malloc(WIDTH * HEIGHT);
// read in every pixel
for (int i = 0; i < WIDTH / 2 * HEIGHT; i++)
{
color_readback[i] = tft.readPixel(i % (WIDTH / 2), i / (WIDTH / 2));
}
unsigned long bmp_file_length = sizeof(bmp_file) / sizeof(bmp_file[0]);
int lines = bmp_file_length / (WIDTH / 2);
Serial.println("lines " + String(lines));
tft.setSwapBytes(false);
for (int i = 0; i < WIDTH / 2 * HEIGHT; i++)
{
tft.drawPixel(i % (WIDTH / 2) + 170, i / (WIDTH / 2), color_readback[i]);
}
}
void loop()
{
Serial.println("loop");
delay(1000);
}
here is the code i have used for this test
hi i have recently aquired a t-display-s3 non-touch, im trying to read back areas of the screen , i have compiled a simple text that puts a bmp on half the screen, read back those pixels and then replay them to the other half of the screen
readRect seems to give me garbage in the buffer ,
readPixel gives me the correct data but its all color shifted
i have followed your guidace here setting spi and spi read to many different rates in user_setup_206 but this doesn't have any effect on the read pixels https://github.com/Bodmer/TFT_eSPI/discussions/951 TIA nos