ILI9488 TFT LCD Arduino Shield - Can't use Touch and SDcard reading in the same program

1.2k Views Asked by At

I have a problem with my ILI9488 TFT Touch LCD module (Arduino Uno Shield). (320x480)

I can show .bmp pictures on the screen, read out of a SD-card. In another testprogram, I can Serial.print() a char when I touch the display. (That's all it needs to do)

But when I merge the two programs together, it doesnt't work anymore. I think the libraries are interferring with each other. Is there any way to fix this, or can anyone recommend two compatible libraries to fix this?

Thanks in advance!

    //----------------------------------------------------------------------------------------
//                                  Declaration MCUFRIEND 
//----------------------------------------------------------------------------------------

#include "MCUFRIEND_kbv.h"

MCUFRIEND_kbv tft;
#define LOWFLASH (defined(__AVR_ATmega328P__) && defined(MCUFRIEND_KBV_H_))

//----------------------------------------------------------------------------------------
//                            Declaration SDCard & Dependencies 
//----------------------------------------------------------------------------------------

#include <SPI.h> 
#include <SD.h>
#include <Adafruit_GFX.h>

#include <stdint.h>
#include "TouchScreen.h"

#define YP A2  
#define XM A3  
#define YM 8  
#define XP 9  

#define MINPRESSURE 1000
#define MAXPRESSURE 10000
//---------------------------------------------------------------------------------------

///Variables & Constants 

#define BLACK       0x0000      /*   0,   0,   0 */
#define NAVY        0x000F      /*   0,   0, 128 */
#define DARKGREEN   0x03E0      /*   0, 128,   0 */
#define DARKCYAN    0x03EF      /*   0, 128, 128 */
#define MAROON      0x7800      /* 128,   0,   0 */
#define PURPLE      0x780F      /* 128,   0, 128 */
#define OLIVE       0x7BE0      /* 128, 128,   0 */
#define LIGHTGREY   0xC618      /* 192, 192, 192 */
#define DARKGREY    0x7BEF      /* 128, 128, 128 */
#define BLUE        0x001F      /*   0,   0, 255 */
#define GREEN       0x07E0      /*   0, 255,   0 */
#define CYAN        0x07FF      /*   0, 255, 255 */
#define RED         0xF800      /* 255,   0,   0 */
#define MAGENTA     0xF81F      /* 255,   0, 255 */
#define YELLOW      0xFFE0      /* 255, 255,   0 */
#define WHITE       0xFFFF      /* 255, 255, 255 */
#define ORANGE      0xFDA0      /* 255, 180,   0 */
#define GREENYELLOW 0xB7E0      /* 180, 255,   0 */
#define PINK        0xFC9F

#define SD_CS 10                // Chip Select from SPI Interface
#define LOGO "default" 
#define AUTHUSER "authuser"  
#define ADDUSER "adduser"
     
#define PALETTEDEPTH   4        

File root;
char ReadMode = '0'; // Sleep = 0, Default = 1, Finger ID = 2, Finger Enroll = 3 
char namebuf[32] = "/";         // BMP=Files in the root directory

int x, y, pathlen, count;

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
bool pressed = false;

void setup()
{
  uint16_t ID = tft.readID();
  
  Serial.begin(9600);
  tft.begin(ID);
  
  //Please select the mode you want to use
  //Sleep --> 0
  //Default --> 1
  //FingerID --> 2
  //FingerEnroll --> 3

  //Getting SDCard Ready
  bool good = SD.begin(SD_CS);
  root = SD.open(namebuf);
  pathlen = strlen(namebuf);

  x = 0;
  y = 0;
  
}
void loop(void)
{
  if (Serial.available()) {
    ReadMode = Serial.read();    
  }
  
  if (ReadMode == '0') {
     tft.fillScreen(NULL);
  }
  if(ReadMode == '1') // The Default HomeScreen displays, Read from the SDCard
  {
    for(int i = 0; i < 5; i++)
    {
      char *nm = namebuf + pathlen;
      File f = root.openNextFile();
      uint8_t ret;
      uint32_t start;
      if (f != NULL)
      {
          #ifdef USE_SDFAT
          f.getName(nm, 32 - pathlen);
          #else
          strcpy(nm, (char *)f.name());
          #endif
          f.close();
          strlwr(nm);
          if (strstr(nm, ".bmp") != NULL && strstr(nm, LOGO) != NULL)
          {
              ret = showBMP(namebuf, x, y);
          }
      }
      else root.rewindDirectory();
    }
  }
  if (ReadMode == '2') // The AuthUser displays, Read from the SDCard
    {
    for(int i = 0; i < 5; i++)
    {
      char *nm = namebuf + pathlen;
      File f = root.openNextFile();
      uint8_t ret;
      uint32_t start;
      if (f != NULL)
      {
          #ifdef USE_SDFAT
          f.getName(nm, 32 - pathlen);
          #else
          strcpy(nm, (char *)f.name());
          #endif
          f.close();
          strlwr(nm);
          if (strstr(nm, ".bmp") != NULL && strstr(nm, AUTHUSER) != NULL)
          {
              ret = showBMP(namebuf, x, y);
          }
      }
      else root.rewindDirectory();
    } 
  }
    TSPoint p = ts.getPoint();
    if (p.z > MINPRESSURE && p.z < MAXPRESSURE) { // TOUCH
    Serial.println('X');
    }
}


//----------------------------------------------------------------------------------------
//                        Methods for reading from SDCard 
//----------------------------------------------------------------------------------------

#define BMPIMAGEOFFSET 54

#define BUFFPIXEL      20

uint16_t read16(File& f) {
    uint16_t result;         // read little-endian
    f.read((uint8_t*)&result, sizeof(result));
    return result;
}

uint32_t read32(File& f) {
    uint32_t result;
    f.read((uint8_t*)&result, sizeof(result));
    return result;
}

uint8_t showBMP(char *nm, int x, int y)
{
    File bmpFile;
    int bmpWidth, bmpHeight;    // W+H in pixels
    uint8_t bmpDepth;           // Bit depth (currently must be 24, 16, 8, 4, 1)
    uint32_t bmpImageoffset;    // Start of image data in file
    uint32_t rowSize;           // Not always = bmpWidth; may have padding
    uint8_t sdbuffer[3 * BUFFPIXEL];    // pixel in buffer (R+G+B per pixel)
    uint16_t lcdbuffer[(1 << PALETTEDEPTH) + BUFFPIXEL], *palette = NULL;
    uint8_t bitmask, bitshift;
    boolean flip = true;        // BMP is stored bottom-to-top
    int w, h, row, col, lcdbufsiz = (1 << PALETTEDEPTH) + BUFFPIXEL, buffidx;
    uint32_t pos;               // seek position
    boolean is565 = false;      //

    uint16_t bmpID;
    uint16_t n;                 // blocks read
    uint8_t ret;

    if ((x >= tft.width()) || (y >= tft.height()))
        return 1;               // off screen

    bmpFile = SD.open(nm);      // Parse BMP header
    bmpID = read16(bmpFile);    // BMP signature"
    (void) read32(bmpFile);     // Read & ignore file size
    (void) read32(bmpFile);     // Read & ignore creator bytes
    bmpImageoffset = read32(bmpFile);       // Start of image data
    (void) read32(bmpFile);     // Read & ignore DIB header size
    bmpWidth = read32(bmpFile);
    bmpHeight = read32(bmpFile);
    n = read16(bmpFile);        // # planes -- must be '1'
    bmpDepth = read16(bmpFile); // bits per pixel
    pos = read32(bmpFile);      // format
    if (bmpID != 0x4D42) ret = 2; // bad ID
    else if (n != 1) ret = 3;   // too many planes
    else if (pos != 0 && pos != 3) ret = 4; // format: 0 = uncompressed, 3 = 565
    else if (bmpDepth < 16 && bmpDepth > PALETTEDEPTH) ret = 5; // palette 
    else {
        bool first = true;
        is565 = (pos == 3);               // ?already in 16-bit format
        // BMP rows are padded (if needed) to 4-byte boundary
        rowSize = (bmpWidth * bmpDepth / 8 + 3) & ~3;
        if (bmpHeight < 0) {              // If negative, image is in top-down order.
            bmpHeight = -bmpHeight;
            flip = false;
        }

        w = bmpWidth;
        h = bmpHeight;
        if ((x + w) >= tft.width())       // Crop area to be loaded
            w = tft.width() - x;
        if ((y + h) >= tft.height())      //
            h = tft.height() - y;

        if (bmpDepth <= PALETTEDEPTH) {   // these modes have separate palette
            bmpFile.seek(bmpImageoffset - (4<<bmpDepth)); //54 for regular, diff for colorsimportant
            bitmask = 0xFF;
            if (bmpDepth < 8)
                bitmask >>= bmpDepth;
            bitshift = 8 - bmpDepth;
            n = 1 << bmpDepth;
            lcdbufsiz -= n;
            palette = lcdbuffer + lcdbufsiz;
            for (col = 0; col < n; col++) {
                pos = read32(bmpFile);    //map palette to 5-6-5
                palette[col] = ((pos & 0x0000F8) >> 3) | ((pos & 0x00FC00) >> 5) | ((pos & 0xF80000) >> 8);
            }
        }

        // Set TFT address window to clipped image bounds
        tft.setAddrWindow(x, y, x + w - 1, y + h - 1);
        for (row = 0; row < h; row++) { // For each scanline...
            uint8_t r, g, b, *sdptr;
            int lcdidx, lcdleft;
            if (flip)   // Bitmap is stored bottom-to-top order (normal BMP)
                pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
            else        // Bitmap is stored top-to-bottom
                pos = bmpImageoffset + row * rowSize;
            if (bmpFile.position() != pos) { // Need seek?
                bmpFile.seek(pos);
                buffidx = sizeof(sdbuffer); // Force buffer reload
            }

            for (col = 0; col < w; ) {  //pixels in row
                lcdleft = w - col;
                if (lcdleft > lcdbufsiz) lcdleft = lcdbufsiz;
                for (lcdidx = 0; lcdidx < lcdleft; lcdidx++) { // buffer at a time
                    uint16_t color;
                    // Time to read more pixel data?
                    if (buffidx >= sizeof(sdbuffer)) { // Indeed
                        bmpFile.read(sdbuffer, sizeof(sdbuffer));
                        buffidx = 0; // Set index to beginning
                        r = 0;
                    }
                    switch (bmpDepth) {          // Convert pixel from BMP to TFT format
                        case 24:
                            b = sdbuffer[buffidx++];
                            g = sdbuffer[buffidx++];
                            r = sdbuffer[buffidx++];
                            color = tft.color565(r, g, b);
                            break;
                        case 16:
                            b = sdbuffer[buffidx++];
                            r = sdbuffer[buffidx++];
                            if (is565)
                                color = (r << 8) | (b);
                            else
                                color = (r << 9) | ((b & 0xE0) << 1) | (b & 0x1F);
                            break;
                        case 1:
                        case 4:
                        case 8:
                            if (r == 0)
                                b = sdbuffer[buffidx++], r = 8;
                            color = palette[(b >> bitshift) & bitmask];
                            r -= bmpDepth;
                            b <<= bmpDepth;
                            break;
                    }
                    lcdbuffer[lcdidx] = color;

                }
                tft.pushColors(lcdbuffer, lcdidx, first);
                first = false;
                col += lcdidx;
            }           // end cols
        }               // end rows
        tft.setAddrWindow(0, 0, tft.width() - 1, tft.height() - 1); //restore full screen
        ret = 0;        // good render
    }
    bmpFile.close();
    return (ret);
}
0

There are 0 best solutions below