Rafa Juárez
Published © GPL3+

TFT screen 2.8 " design adding jpg pictures

Idea is to create an stand alone distance measurement device, with a guidance to the user.

Beginner2 hours441
TFT screen 2.8 " design adding jpg pictures

Story

Read more

Code

Arduino code for first test with the screen design

Arduino
#include <gfxfont.h>

// HACER QUE PINTE UNA VEZ Y HASTA QUE NO DETECTE LA TOUCH NO CAMBIE, O EL PUERTO SERIE
// LIMPIAR EL CDIGO NO USADO

#include "TouchScreen.h"
#include <Adafruit_GFX.h> // Hardware-specific library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <SPI.h>
#include <SD.h>
#define SD_CS  10  // CHIP SELECT CARTA SD DEL TFT

#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin

// Pins for the LCD Shield
#define YP A3 // must be analog
#define XM A2 // must be analog
#define YM 9  // digital or analog pin
#define XP 8  // digital or analog pin

#define MINPRESSURE 1
#define MAXPRESSURE 1000

// Calibration mins and max for raw data when touching edges of screen
#define TS_MINX 210
#define TS_MINY 210
#define TS_MAXX 915
#define TS_MAXY 910

// Assign human-readable names to some common 16-bit color values:
#define BLACK   0x0000
#define BLUE    0x001F
#define BLUE2    0x00FF
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

int sensorPin = A0;  
int sensorValue = 0; 
float sensorValor=0.00;

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
int touchCnt = 0;
int buttonState = 0;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
uint16_t x = 50, y = 100;
String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

void setup()
{
    // put your setup code here, to run once:
      uint16_t tmp;     
    Serial.begin(9600);
    // reserve 200 bytes for the inputString:
    inputString.reserve(200);
    tft.reset();
    delay(100);
    tft.begin(9600);
    uint16_t identifier = tft.readID();
    Serial.print("El ID de la placa tft es = 0x");
    Serial.println(identifier, HEX);
    tft.begin(identifier);
    tft.fillScreen(BLACK);
  
    Serial.print(F("Initializing SD card..."));
    tft.setRotation(1);
    tft.setTextSize(2);
    tft.println("ID de la placa tft = 0x");
    tft.println(identifier, HEX);
    delay(1000);
    tft.println("");
    tft.println(F("Inicializando carta SD..."));
    delay(1500);
if (!SD.begin(SD_CS))
{
tft.println("Error:No puedo acceder a la SD");
return;
}
  Serial.println(F("OK!"));
  tft.print(F("...."));
  delay(500);
  tft.println(F(".............."));
   tft.println(F(""));
  tft.println(F("SD lista!"));
  delay(1000);
  bmpDraw("4.bmp", 0, 0);
  bmpDraw("1.bmp", 270, 5);
  pantallaPU();
}



void loop()
{
     inputString = ""; 
  
}

void pantallaPU()
{
      tft.setCursor(0, 0);
      tft.setTextSize(1);
      tft.println("TFT_PU_BMP.INO");
      tft.println("               ");
      tft.println("rjuarez7@gmail.com");
      delay(500);
        
      //tft.fillScreen(BLACK);
      //tft.setRotation(1);
      tft.setCursor(80, 40);
      tft.drawRect(70, 30, 175,30,BLUE);
      tft.setTextSize(2);
      tft.setTextColor(RED);
      tft.println("PU2GLASS_EDGE"); 
      //WS CD391
      // CUADROS LADO SUPERIOR
      tft.fillRect(30, 65, 50,20,CYAN);
      tft.fillRect(100, 65, 50,20,CYAN);
      tft.fillRect(170, 65, 50,20,CYAN);
      tft.fillRect(240, 65, 50,20,CYAN);
      //CUADROS LADO IZQUIERDO
      tft.fillRect(10, 100, 50,20,CYAN);
      tft.fillRect(10, 140, 50,20,CYAN);
      tft.fillRect(10, 180, 50,20,CYAN);
      tft.fillRect(10, 220, 50,20,CYAN);
      //CUADROS LADO DERECHO
      tft.fillRect(260, 100, 50,20,CYAN);
      tft.fillRect(260, 140, 50,20,CYAN);
      tft.fillRect(260, 180, 50,20,CYAN);
      tft.fillRect(260, 220, 50,20,CYAN);
      //TTULO WS CD 391
      tft.setCursor(120, 120);
      tft.setTextSize(2);
      tft.println("WS-CD391");
      // FIN PANTALLA WS CD391
      
      //VENTANA SENSOR
      tft.setCursor(110, 165);
      tft.setTextSize(2);
      tft.setTextColor(YELLOW);
      tft.println("..SENSOR..");
      tft.fillRect(125,190,80,40,WHITE);
      tft.setCursor(130, 200);
      tft.setTextSize(1);
      tft.setTextColor(BLUE);
      
      //FIN SENSOR
      
      //while(p.z < MINPRESSURE)
//delay(1000);
}

void pantalla2()
{
    tft.setCursor(0, 0);
    tft.setTextSize(1);
    tft.println("TFT_PU_BMP.INO");
    delay(500);
      
    //tft.fillScreen(BLACK);
    //tft.setRotation(1);
    tft.setCursor(40, 30);
    tft.drawRect(30, 20, 260,30,BLUE);
    tft.setTextSize(2);
    tft.setTextColor(RED);
    tft.println("PU / GLASS EDGE mm"); 
    //WS CD391
    // CUADROS LADO SUPERIOR
    tft.fillRect(30, 60, 50,20,CYAN);
    tft.fillRect(100, 60, 50,20,CYAN);
    tft.fillRect(170, 60, 50,20,CYAN);
    tft.fillRect(240, 60, 50,20,CYAN);
    //CUADROS LADO IZQUIERDO
    tft.fillRect(10, 100, 50,20,CYAN);
    tft.fillRect(10, 140, 50,20,CYAN);
    tft.fillRect(10, 180, 50,20,CYAN);
    tft.fillRect(10, 220, 50,20,CYAN);
    //CUADROS LADO DERECHO
    tft.fillRect(260, 100, 50,20,CYAN);
    tft.fillRect(260, 140, 50,20,CYAN);
    tft.fillRect(260, 180, 50,20,CYAN);
    tft.fillRect(260, 220, 50,20,CYAN);
    //TTULO WS CD 391
    tft.setCursor(120, 120);
    tft.setTextSize(2);
    tft.println("WS-CD391");
    // FIN PANTALLA WS CD391
    
    //VENTANA SENSOR
    tft.setCursor(110, 165);
    tft.setTextSize(2);
    tft.setTextColor(YELLOW);
    tft.println("..SENSOR..");
    tft.fillRect(125,190,80,40,WHITE);
    tft.setCursor(130, 200);
    tft.setTextSize(1);
    tft.setTextColor(BLUE);
    
    //FIN SENSOR
    
    //while(p.z < MINPRESSURE)
//delay(1000);
}

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() {
    while (Serial.available()) {
        // get the new byte:
        char inChar = (char)Serial.read();
        // add it to the inputString:
        inputString += inChar;
        //Borra cuadro input serie
        tft.fillRect(125,190,80,40,WHITE);
        tft.setCursor(130, 200);
        tft.setTextSize(2);
        tft.setTextColor(BLUE);
        //Escribe lo recibido por el puerto
        tft.setCursor(140, 200);
        tft.setTextSize(2);
        tft.setTextColor(BLUE);
        tft.setCursor(140, 200);
        tft.println(inputString);
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
              if (inChar == '\n') {
                stringComplete = true;
              }
      }//fin while principal
}// fin funcin puerto serie

/////////////////////////////////////////////////////
// This function opens a Windows Bitmap (BMP) file and
// displays it at the given coordinates.  It's sped up
// by reading many pixels worth of data at a time
// (rather than pixel by pixel).  Increasing the buffer
// size takes more of the Arduino's precious RAM but
// makes loading a little faster.  20 pixels seems a
// good balance.

#define BUFFPIXEL 20

void bmpDraw(char *filename, int x, int y) {

  File     bmpFile;
  int      bmpWidth, bmpHeight;   // W+H in pixels
  uint8_t  bmpDepth;              // Bit depth (currently must be 24)
  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[BUFFPIXEL];  // pixel out buffer (16-bit per pixel)
  uint8_t  buffidx = sizeof(sdbuffer); // Current position in sdbuffer
  boolean  goodBmp = false;       // Set to true on valid header parse
  boolean  flip    = true;        // BMP is stored bottom-to-top
  int      w, h, row, col;
  uint8_t  r, g, b;
  uint32_t pos = 0, startTime = millis();
  uint8_t  lcdidx = 0;
  boolean  first = true;

  if((x >= tft.width()) || (y >= tft.height())) return;

  Serial.println();
  Serial.print(F("Loading image '"));
  Serial.print(filename);
  Serial.println('\'');
  // Open requested file on SD card
  if ((bmpFile = SD.open(filename)) == NULL) {
    Serial.println(F("File not found"));
    return;
  }

  // Parse BMP header
  if(read16(bmpFile) == 0x4D42) { // BMP signature
    Serial.println(F("File size: ")); Serial.println(read32(bmpFile));
    (void)read32(bmpFile); // Read & ignore creator bytes
    bmpImageoffset = read32(bmpFile); // Start of image data
    Serial.print(F("Image Offset: ")); Serial.println(bmpImageoffset, DEC);
    // Read DIB header
    Serial.print(F("Header size: ")); Serial.println(read32(bmpFile));
    bmpWidth  = read32(bmpFile);
    bmpHeight = read32(bmpFile);
    if(read16(bmpFile) == 1) { // # planes -- must be '1'
      bmpDepth = read16(bmpFile); // bits per pixel
      Serial.print(F("Bit Depth: ")); Serial.println(bmpDepth);
      if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed

        goodBmp = true; // Supported BMP format -- proceed!
        Serial.print(F("Image size: "));
        Serial.print(bmpWidth);
        Serial.print('x');
        Serial.println(bmpHeight);

        // BMP rows are padded (if needed) to 4-byte boundary
        rowSize = (bmpWidth * 3 + 3) & ~3;

        // If bmpHeight is negative, image is in top-down order.
        // This is not canon but has been observed in the wild.
        if(bmpHeight < 0) {
          bmpHeight = -bmpHeight;
          flip      = false;
        }

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

        // 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...
          // Seek to start of scan line.  It might seem labor-
          // intensive to be doing this on every line, but this
          // method covers a lot of gritty details like cropping
          // and scanline padding.  Also, the seek only takes
          // place if the file position actually needs to change
          // (avoids a lot of cluster math in SD library).
          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; col++) { // For each column...
            // Time to read more pixel data?
            if (buffidx >= sizeof(sdbuffer)) { // Indeed
              // Push LCD buffer to the display first
              if(lcdidx > 0) {
                tft.pushColors(lcdbuffer, lcdidx, first);
                lcdidx = 0;
                first  = false;
              }
              bmpFile.read(sdbuffer, sizeof(sdbuffer));
              buffidx = 0; // Set index to beginning
            }

            // Convert pixel from BMP to TFT format
            b = sdbuffer[buffidx++];
            g = sdbuffer[buffidx++];
            r = sdbuffer[buffidx++];
            lcdbuffer[lcdidx++] = tft.color565(r,g,b);
          } // end pixel
        } // end scanline
        // Write any remaining data to LCD
        if(lcdidx > 0) {
          tft.pushColors(lcdbuffer, lcdidx, first);
        } 
        Serial.print(F("Loaded in "));
        Serial.print(millis() - startTime);
        Serial.println(" ms");
      } // end goodBmp
    }
  }

  bmpFile.close();
  if(!goodBmp) Serial.println(F("BMP format not recognized."));
}

// These read 16- and 32-bit types from the SD card file.
// BMP data is stored little-endian, Arduino is little-endian too.
// May need to reverse subscript order if porting elsewhere.

uint16_t read16(File f) {
  uint16_t result;
  ((uint8_t *)&result)[0] = f.read(); // LSB
  ((uint8_t *)&result)[1] = f.read(); // MSB
  return result;
}

uint32_t read32(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;
}

Credits

Rafa Juárez

Rafa Juárez

18 projects • 39 followers
Very interested in prototyping of new ideas. 30 years experience in electronics.

Comments