Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tft.readPixel returning mangled colors #3417

Open
JooJooBee666 opened this issue Jul 25, 2024 · 0 comments
Open

tft.readPixel returning mangled colors #3417

JooJooBee666 opened this issue Jul 25, 2024 · 0 comments

Comments

@JooJooBee666
Copy link

JooJooBee666 commented Jul 25, 2024

Problem:
Using the TFT_ReadWrite_Test is returning some odd results when reading the pixel. I made some slight modifications to simply make wr static and return the bits and RGB equivalent, but I'm not getting the same value I push out. If it is 0 (black) it is always correct. However, anything above that most likely returns an invalid color due to what looks like some incorrect bit shifting.

Setup information:
TFT_eSPI version: 2.5.43

PlatformIO Info: Platform espressif32 @ 6.7.0 (required: espressif32)
framework-arduinoespressif32 @ 3.20016.0 (required: platformio/framework-arduinoespressif32 @ ~3.20016.0)

Processor: ESP32 S3 N16R8V

Screen Info: SPI, IL9431 w/touch. For testing, I unhooked the TDO from MISO (they are shared normally in my setup)

Setup File: [SetupJNL_ILI9341_ESP32.zip](https://github.com/user-attachments/files/16380351/SetupJNL_ILI9341_ESP32.zip

Example 1 (Modified TFT_ReadWrite_Test):
static uint32_t wr = TFT_RED;
results in:

Pixel value written = F800 (255, 0, 0)
  bits: 11111000 00000000 00000000
  RGB32: 248, 0, 0

Pixel value read    = 7C0
  bits: 00000000 11111000 00000000
  RGB32: 0, 248, 0

static uint32_t wr = TFT_PURPLE;
results in:

Pixel value written = 780F (128, 0, 128)
  bits: 01111000 00000000 01111000
  RGB32: 120, 0, 120

Pixel value read    = 3C0
  bits: 00000000 01111000 00000000
  RGB32: 0, 120, 0

As you can see above, the bits are getting mangled pretty good here but there is a clear pattern to it. I injected a Serial.printf in TFT_eSPI.cpp for the readpixel method:

      uint8_t r = tft_Read_8();
      uint8_t g = tft_Read_8();
      uint8_t b = tft_Read_8();
      Serial.printf("[r:%i g:%i b:%i] ", r, g, b);    <--- Added this to line 1237
      color = color565(r, g, b);

The output from this matches what my other code is seeing so this is not getting accurate reads. Output from TFT_PURPLE:
[r:0 g:120 b:0]

Here's some pseudo code I used to read the image, then dump it afterward, along with pictures of the results; which speak for themselves.

Example 2:

#include <TFT_eSPI.h>

uint16_t SCREEN_WIDTH = 320;
uint16_t SCREEN_HEIGHT = 240;

TFT_eSPI tft = TFT_eSPI(SCREEN_WIDTH, SCREEN_HEIGHT);
uint16_t *framebuffer;

void setup() {
  tft.init();
  //tft.begin();                     // Initialize the ILI9341 TFT Screen
  tft.setRotation(0);             // Rotate screen 90 clockwise
  //tft.fillScreen(0);
  tft.fillScreen(TFT_BLACK);
  psramInit();                       //initialize PSRAM
  framebuffer = (uint16_t *)ps_malloc(SCREEN_WIDTH * SCREEN_HEIGHT * 16);   //allocate enough space in the framefuffer
  ....
  // draw some things here :)
  ....
  if (framebuffer != NULL) {
    fillFrameBuffer();
  }
}

void fillFrameBuffer(){
  int bufPos = 0;
  for (uint16_t y=0; y< SCREEN_HEIGHT; y++){
    for (uint16_t x=0; x< SCREEN_WIDTH; x++){
      uint16_t pix = tft.readPixel(x,y);
      framebuffer[bufPos] = pix;
      bufPos++;
    }
  }
}

void drawFrameBuffer(){
  TFT_eSprite fb_sprite(&tft);
  fb_sprite.createSprite(back_width, back_height);
  int bufPos = 0;
  for (uint16_t y=0; y< SCREEN_HEIGHT; y++){
    for (uint16_t x=0; x< SCREEN_WIDTH; x++){
      fb_sprite.drawPixel(x, y, framebuffer[bufPos]);
      bufPos++;
    }
  }
  fb_sprite.pushSprite(0, 0);  
  fb_sprite.flush();
}

void loop{
  delay(3000);      // take picture here!
  drawFrameBuffer();
}

Ignore the date, time, location and reflection with the red in it 😛

Image Before reading to frame buffer:
before-buffer-read

Image after drawing all pixels from the buffer in the sprite and pushing the sprite:
after-buffer-write

Am I missing something in the config or is there a problem with one of the settings? Or, is this a known issue with some of the drivers?

Thanks.

@JooJooBee666 JooJooBee666 changed the title tft.readPixel returning mangled tft.readPixel returning mangled colors Jul 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant