programme pour l'ecran TFT-ST7735

Dependencies:   mbed DmTftLibrary

main.cpp

Committer:
displaymodule
Date:
2014-07-07
Revision:
2:14024c30df8d
Parent:
1:4a281af0d8e6
Child:
4:d58fe9f48040

File content as of revision 2:14024c30df8d:

/**********************************************************************************************
 Copyright (c) 2014 DisplayModule. All rights reserved.

 Redistribution and use of this source code, part of this source code or any compiled binary
 based on this source code is permitted as long as the above copyright notice and following
 disclaimer is retained.

 DISCLAIMER:
 THIS SOFTWARE IS SUPPLIED "AS IS" WITHOUT ANY WARRANTIES AND SUPPORT. DISPLAYMODULE ASSUMES
 NO RESPONSIBILITY OR LIABILITY FOR THE USE OF THE SOFTWARE.
 ********************************************************************************************/

/******************************************************************************
 * Includes
 *****************************************************************************/

#include "mbed.h"

#include "DmTftHX8353C.h"
#include "DmTftS6D0164.h"
#include "DmTftIli9325.h"
#include "DmTftIli9341.h"
#include "DmTftSsd2119.h"

/******************************************************************************
 * Typedefs and defines
 *****************************************************************************/

/* Note that there are restrictions on which platforms that can use printf
   in combinations with the DmTftLibrary. Some platforms (e.g. LPC1549 LPCXpresso)
   use the same pins for USBRX/USBTX and display control. Printing will
   cause the display to not work. Read more about this on the display's notebook
   page. */
//#define log(...) printf(__VA_ARGS__)
#define log(...)

/******************************************************************************
 * Local variables
 *****************************************************************************/

DmTftHX8353C tft;  /* DM_TFT18_101 */
//DmTftS6D0164 tft;  /* DM_TFT22_102 */
//DmTftIli9325 tft;  /* DM_TFT28_103 and DM_TFT24_104 */
//DmTftIli9341 tft;  /* DM_TFT28_105 */
//DmTftSsd2119 tft;   /* DM_TFT35_107 */

int bmpWidth, bmpHeight;
uint8_t bmpImageoffset;

/******************************************************************************
 * Global variables
 *****************************************************************************/

extern uint8_t dmlogo[];

/******************************************************************************
 * Local functions
 *****************************************************************************/

// LITTLE ENDIAN!
uint16_t read16(uint8_t *src)
{
  uint16_t d;
  uint8_t b;
  b = *src;
  d = *(src+1);
  d <<= 8;
  d |= b;
  return d;
}

// LITTLE ENDIAN!
uint32_t read32(uint8_t *src)
{
  uint32_t d;
  uint16_t b;

  b = read16(src);
  d = read16(src+2);
  d <<= 16;
  d |= b;
  return d;
}

void drawBmpFromFlash(int x, int y)
{
  uint16_t pos = bmpImageoffset;

  uint16_t p;  // pixel
  uint8_t g, b;
  int i, j; // line, column

  for(i=bmpHeight; i>0; i--) {
    for(j=0; j<bmpWidth; j++) {
      b = *(dmlogo+pos++);
      g = *(dmlogo+pos++);
      p = *(dmlogo+pos++);

      p >>= 3;
      p <<= 6;

      g >>= 2;
      p |= g;
      p <<= 5;

      b >>= 3;
      p |= b;

      // write out the 16 bits of color
      tft.setPixel(j, i+y, p);
    }
  }
}


int bmpReadHeader() {
  uint32_t fileSize;
  uint32_t headerSize;
  uint16_t bmpDepth;
  uint16_t pos = 0;
  log("reading bmp header\r\n");
  log("Magic byte is: %d \r\n", read16(dmlogo));

  if (read16(dmlogo) !=0x4D42){ // read magic byte
    log("Magic byte not found\r\n");
    return false;
  }
  pos += 2;

  // read file size
  fileSize = read32(dmlogo+pos);
  log("filesize is: %d \r\n", fileSize);
  log("");
  pos += 4;

  pos += 4; // Skip creator bytes

  bmpImageoffset = read32(dmlogo+pos);
  pos += 4;

  // read DIB header
  headerSize = read32(dmlogo+pos);
  pos +=4;
  bmpWidth = read32(dmlogo+pos);
  pos += 4;
  bmpHeight = read32(dmlogo+pos);
  pos += 4;

  log("Image size:        %d\r\n", fileSize);
  log("Image offset:      %d\r\n", bmpImageoffset);
  log("Header size:       %d\r\n", headerSize);
  log("Image width:       %d\r\n", bmpWidth );
  log("Image height:      %d\r\n", bmpHeight );

  if (read16(dmlogo+pos) != 1){
    // number of color planes must be 1
    return false;
  }
  pos += 2;

  bmpDepth = read16(dmlogo+pos);
  pos +=2;
  log("Bitdepth:          %d\r\n", bmpDepth);

  if (read16(dmlogo+pos) != 0) {
    // compression not supported!
    return false;
  }
  pos += 2; // Should really be 2??

  return true;
}

/******************************************************************************
 * Main
 *****************************************************************************/

int main() {
  log("init tft \r\n");
  tft.init();

  tft.drawString(0,32,"www.");
  tft.drawString(12,48,"displaymodule");
  tft.drawString(88,64,".com");

  tft.drawRectangle(20,85,40,105,GREEN);
  tft.drawCircle(60,95,10,BLUE);
  tft.drawTriangle(90,85, 80,105, 100,105, RED);

  if (! bmpReadHeader()) {
    log("bad bmp\r\n");
    return -1;
  }

  drawBmpFromFlash(0, 0);
  drawBmpFromFlash(0, 130);

  while(1) {
  }
}