Library for Mini-DK board

Dependencies:   SPI_TFT_ILI9320

Dependents:   LPC1768_Mini-DK_EasyWeb_DM9161 LPC1768_Mini-DK LPC1768_Mini-DK

Fork of Mini-DK by Frank Vannieuwkerke

Mini-DK board overview (Micro SD connector is at the bottom side)

One serial interface , uses CP2102 (USB to RS232 interface, support ISP download )

RJ45-10/100M Ethernet network interface (Ethernet PHY: DM9161)

2.8 inch TFT color LCD interface (SPI interface or 16Bit parallel interface)

Touch panel controller XPT2046 (ADS7843 compatible)

USB 2.0 interface, USB host and USB Device interface.

TF SD / MMC card (SPI) interface.

Two user buttons, One Reset button and ISP button , One INT0 button, two user-programmable LED lights

Serial ISP download, Standard 20-pin JTAG download simulation debugging interface.

Selection between external 5V power supply or USB 5V supply.

Board size: 95mm * 78mm

All IO available on extension connectors

/media/uploads/frankvnk/mini-dk_top.jpg

04/01/13

Erik Olieman (http://mbed.org/users/Sissors/) joined the code development for the Mini-DK board.

Thanks to his input, we were able to obtain a tremendous speed gain, remove warnings, ...

An overview of all modifications is stored in modifs.h

The old page (http://mbed.org/users/frankvnk/code/LPC1768_Mini-DK/) contains the demo code.

IMPORTANT : Due to a change in the mbed libraries (Stream()), we cannot use the printf instruction - we need to use <SPI_TFT>.printf (example - see main.cpp in http://mbed.org/users/frankvnk/code/LPC1768_Mini-DK/)

WARNING: filetoflash (SD to CPU flash)

The SPI_TFT library contains an option to copy an image from the SD card to the CPU flash memory. This allows you to use an image as background without speed loss when writing other text and graphics.

By default, this option is enabled.

It can be disabled by uncommenting the #define mentioned below in Mini_DK.h:

#define NO_FLASH_BUFFER

Since the flash memory has limited write endurance, DO NOT use this feature when you intend to read multiple images from the SD card (eg: when used as a photo frame).

14/01/13

A newer version of the Mini-DK has been released by the manufacturer: Mini-DK2. They replaced the DM9161 PHY with a LAN8720A PHY and better buttons are fitted on the board. All other hardware remains the same. Code for this PHY is available from the NXP MCU SW application team. This allows us to use the mbed 'EthernetInterface' library with little modifications. Further info - see http://mbed.org/forum/mbed/topic/3684/?page=1#comment-18473.

Notes:

The code in 'lpc_phy_lan8720.c' uses 'msDelay' - needs to be replaced with 'osDelay'.

A custom MAC address can be defined using following code:

extern "C" void mbed_mac_address(char * mac) {
 
// define your own MAC Address
  mac[0] = 0x00;  
  mac[1] = 0x01;  
  mac[2] = 0x02;  
  mac[3] = 0x03;  
  mac[4] = 0x04;  
  mac[5] = 0x05;           
  
};

SPI_TFT/SPI_TFT.cpp

Committer:
Sissors
Date:
2013-01-06
Revision:
10:d43a2f5e661c
Parent:
9:742d15898b2b

File content as of revision 10:d43a2f5e661c:

/**************************************************************************************************
 *****                                                                                        *****
 *****  Name: SPI_TFT.cpp                                                                     *****
 *****  Ver.: 1.0                                                                             *****
 *****  Date: 04/01/2013                                                                      *****
 *****  Auth: Frank Vannieuwkerke                                                             *****
 *****        Erik Olieman                                                                    *****
 *****  Func: library for 240*320 pixel TFT with ILI9320 LCD Controller                       *****
 *****                                                                                        *****
 *****  Rewrite from Peter Drescher code - http://mbed.org/cookbook/SPI-driven-QVGA-TFT       *****
 *****                                                                                        *****
 **************************************************************************************************/

// TODO : BMP routine



#include "SPI_TFT.h"
#include "mbed.h"


#define BPP         16                  // Bits per pixel


SPI_TFT::SPI_TFT(PinName mosi, PinName miso, PinName sclk, PinName cs, const char *name)
    : GraphicsDisplay(name), _spi(mosi, miso, sclk), _cs(cs)
{
    char_x = 0;
    tft_reset();
    set_orientation(0);
}

int SPI_TFT::width()
{
    if (orientation == 0 || orientation == 2) return 240;
    else return 320;
}

int SPI_TFT::height()
{
    if (orientation == 0 || orientation == 2) return 320;
    else return 240;
}

void SPI_TFT::set_orientation(unsigned int o)
{
    orientation = o;
    WindowMax();
}

void SPI_TFT::mod_orientation(void)
{
    switch (orientation)
    {
        case 0:
            wr_reg(0x03, 0x10b0);        // ID1 = 1, ID0 = 1, AM = 0 - Portrait
            break;
        case 1:
            wr_reg(0x03, 0x10a8);        // ID1 = 1, ID0 = 0, AM = 0 - Landscape
            break;
        case 2:
            wr_reg(0x03, 0x1080);        // ID1 = 0, ID0 = 0, AM = 1 - Portrait upside down
            break;
        case 3:
            wr_reg(0x03, 0x1098);        // ID1 = 0, ID0 = 1, AM = 1 - Landscape upside down
            break;
    }
}

void SPI_TFT::wr_cmd(unsigned char cmd)
{
    _cs = 0;
    _spi.write(0x70);
    _spi.write(0x00);
    _spi.write(cmd);
    _cs = 1;
}

void SPI_TFT::wr_dat(unsigned short dat)
{
    unsigned char u,l;
    u = (dat >> 0x08);
    l = (dat & 0xff);
    _cs = 0;
    _spi.write(0x72);
    _spi.write(u);
    _spi.write(l);
    _cs = 1;
}

void SPI_TFT::wr_dat_start(void)
{
    _spi.write(0x72);
}

unsigned short SPI_TFT::rd_dat(void)              // SPI frequency needs to be lowered on read
{
    unsigned short val = 0;
    _cs = 0;
    _spi.frequency(SPI_F_LO);
    _spi.write(0x73);
    _spi.write(0x00);
    val = _spi.write(0);                          // Dummy read
    val = _spi.write(0);                          // Read D8..D15
    val <<= 8;
    val |= _spi.write(0);                         // Read D0..D7
    _cs = 1;
    _spi.frequency(SPI_F_HI);
    return (val);
}

void SPI_TFT::wr_reg(unsigned char reg, unsigned short val)
{
    wr_cmd(reg);
    wr_dat(val);
}

unsigned short SPI_TFT::rd_reg(unsigned char reg)
{
    wr_cmd(reg);
    return(rd_dat());
}

unsigned short SPI_TFT::Read_ID(void)             // IMPORTANT : SPI frequency needs to be lowered when reading
{
    unsigned short val = 0;
    _cs = 0;
    _spi.write(0x70);
    _spi.write(0x00);
    _spi.write(0X00);
    _cs = 1;
    _spi.frequency(SPI_F_LO);
    _cs = 0;
    _spi.write(0x73);
    val = _spi.write(0x00);                       // Dummy read
    val = _spi.write(0x00);                       // Read D8..D15
    val <<= 8;
    val |= _spi.write(0x00);                      // Read D0..D7
    _cs = 1;
    _spi.frequency(SPI_F_HI);
    return (val);
}

void SPI_TFT::SetCursor( unsigned short Xpos, unsigned short Ypos )
{
    wr_reg(0x20, Xpos );
    wr_reg(0x21, Ypos );
}

void SPI_TFT::tft_reset()
{
    _spi.format(8,3);                    // 8 bit spi mode 3
    _spi.frequency(SPI_F_HI);            // 48 Mhz SPI clock

    wr_reg(0x00,0x0000);
    wr_reg(0x01,0x0100); // Driver Output Control
    wr_reg(0x02,0x0700); // LCD Driver Waveform Control
    wr_reg(0x03,0x1030); // Set the scan mode
    wr_reg(0x04,0x0000); // Scaling Control
    wr_reg(0x08,0x0202); // Display Control 2
    wr_reg(0x09,0x0000); // Display Control 3
    wr_reg(0x0a,0x0000); // Frame Cycle Contal
    wr_reg(0x0c,(1<<0)); // Extern Display Interface Control 1
    wr_reg(0x0d,0x0000); // Frame Maker Position
    wr_reg(0x0f,0x0000); // Extern Display Interface Control 2

    wait_ms(50);

    wr_reg(0x07,0x0101); // Display Control

    wait_ms(50);

    wr_reg(0x10,(1<<12)|(0<<8)|(1<<7)|(1<<6)|(0<<4)); // Power Control 1
    wr_reg(0x11,0x0007);                              // Power Control 2
    wr_reg(0x12,(1<<8)|(1<<4)|(0<<0));                // Power Control 3
    wr_reg(0x13,0x0b00);                              // Power Control 4
    wr_reg(0x29,0x0000);                              // Power Control 7
    wr_reg(0x2b,(1<<14)|(1<<4));

    wr_reg(0x50,0);      // Set X Start
    wr_reg(0x51,239);    // Set X End
    wr_reg(0x52,0);      // Set Y Start
    wr_reg(0x53,319);    // Set Y End

    wait_ms(50);

    wr_reg(0x60,0x2700); // Driver Output Control
    wr_reg(0x61,0x0001); // Driver Output Control
    wr_reg(0x6a,0x0000); // Vertical Srcoll Control

    wr_reg(0x80,0x0000); // Display Position Partial Display 1
    wr_reg(0x81,0x0000); // RAM Address Start Partial Display 1
    wr_reg(0x82,0x0000); // RAM Address End-Partial Display 1
    wr_reg(0x83,0x0000); // Displsy Position Partial Display 2
    wr_reg(0x84,0x0000); // RAM Address Start Partial Display 2
    wr_reg(0x85,0x0000); // RAM Address End Partial Display 2

    wr_reg(0x90,(0<<7)|(16<<0)); // Frame Cycle Control
    wr_reg(0x92,0x0000);         // Panel Interface Control 2
    wr_reg(0x93,0x0001);         // Panel Interface Control 3
    wr_reg(0x95,0x0110);         // Frame Cycle Control
    wr_reg(0x97,(0<<8));
    wr_reg(0x98,0x0000);         // Frame Cycle Control
    wr_reg(0x07,0x0133);

    wait_ms(100);
    WindowMax();
}


void SPI_TFT::pixel(int x, int y, int color)
{
    switch (orientation)
    {
        case 0:
            wr_reg(0x20, x);
            wr_reg(0x21, y);
            break;
        case 1:
            wr_reg(0x20, 239-y);
            wr_reg(0x21, x);
            break;
        case 2:
            wr_reg(0x20, 239-x);
            wr_reg(0x21, 319-y);
            break;
        case 3:
            wr_reg(0x20, y);
            wr_reg(0x21, 319-x);
            break;
    }
    wr_cmd(0x22);
    wr_dat(color);
}


void SPI_TFT::window(int x, int y, int w, int h)
{
    unsigned int xw1, yh1;
    xw1 = x + w - 1;
    yh1 = y + h - 1;
    wr_reg(0x20, x);
    wr_reg(0x21, y);
    switch (orientation)
    {
        case 0:
            wr_reg(0x50, x);
            wr_reg(0x51, xw1);
            wr_reg(0x52, y);
            wr_reg(0x53, yh1);
            break;
        case 1:
            wr_reg(0x50, 239 - yh1);
            wr_reg(0x51, 239 - y);
            wr_reg(0x52, x);
            wr_reg(0x53, xw1);
            break;
        case 2:
            wr_reg(0x50, 239 - xw1);
            wr_reg(0x51, 239 - x);
            wr_reg(0x52, 319 - yh1);
            wr_reg(0x53, 319 - y);
            break;
        case 3:
            wr_reg(0x50, y);
            wr_reg(0x51, yh1);
            wr_reg(0x52, 319 - xw1);
            wr_reg(0x53, 319 - x);
            break;
    }
}


void SPI_TFT::WindowMax(void)
{
    window(0, 0, width(),  height());
}


void SPI_TFT::cls (void)
{
    unsigned long int index=0;
//    int color = _background;
    wr_reg(0x03, 0x1030);
    WindowMax();
    SetCursor(0,0);
    wr_cmd(0x22);
     _cs = 0;
     wr_dat_start();
     _spi.format(16,3);
     int num = width()*height();
     
     for( index = 0; index<num; index++ )
     {
        _spi.fastWrite(_background);
     }
     _spi.clearRX();
     
     _spi.format(8,3);
    _cs = 1;
}

void SPI_TFT::hline(int x0, int x1, int y, int color)
{
    unsigned int index=0;
    int w;
    w = x1 - x0 + 1;
    mod_orientation();
    window(x0,y,w,1);
    wr_cmd(0x22);
    _cs = 0;
    wr_dat_start();
    
    _spi.format(16,3);
    int num = x1-x0;
    for( index = 0; index<num; index++ )
     {
        _spi.fastWrite(color);
     }
     _spi.clearRX();
     
    _spi.format(8,3);
    _cs = 1;
    return;
}

void SPI_TFT::vline(int x, int y0, int y1, int color)
{
    unsigned int index=0;
    int h;
    h = y1 - y0 + 1;
    mod_orientation();
    window(x,y0,1,h);
    wr_cmd(0x22);
     _cs = 0;
     wr_dat_start();
    _spi.format(16,3);
    int num = y1-y0;
    for( index = 0; index<num; index++ )
     {
        _spi.fastWrite(color);
     }
     _spi.clearRX();
    _spi.format(8,3);
    _cs = 1;
    return;
}

void SPI_TFT::line(int x0, int y0, int x1, int y1, int color)
{
    wr_reg(0x03, 0x1030);
    WindowMax();
    int   dx = 0, dy = 0;
    int   dx_sym = 0, dy_sym = 0;
    int   dx_x2 = 0, dy_x2 = 0;
    int   di = 0;

    dx = x1-x0;
    dy = y1-y0;

    if (dx == 0) {        /* vertical line */
        if (y1 > y0) vline(x0,y0,y1,color);
        else vline(x0,y1,y0,color);
        return;
    }

    if (dx > 0) {
        dx_sym = 1;
    } else {
        dx_sym = -1;
    }
    if (dy == 0) {        /* horizontal line */
        if (x1 > x0) hline(x0,x1,y0,color);
        else  hline(x1,x0,y0,color);
        return;
    }

    if (dy > 0) {
        dy_sym = 1;
    } else {
        dy_sym = -1;
    }

    dx = dx_sym*dx;
    dy = dy_sym*dy;

    dx_x2 = dx*2;
    dy_x2 = dy*2;

    if (dx >= dy) {
        di = dy_x2 - dx;
        while (x0 != x1) {

            pixel(x0, y0, color);
            x0 += dx_sym;
            if (di<0) {
                di += dy_x2;
            } else {
                di += dy_x2 - dx_x2;
                y0 += dy_sym;
            }
        }
        pixel(x0, y0, color);
    } else {
        di = dx_x2 - dy;
        while (y0 != y1) {
            pixel(x0, y0, color);
            y0 += dy_sym;
            if (di < 0) {
                di += dx_x2;
            } else {
                di += dx_x2 - dy_x2;
                x0 += dx_sym;
            }
        }
        pixel(x0, y0, color);
    }
    return;
}


void SPI_TFT::rect(int x0, int y0, int w, int h, int color)
{
    hline(x0,x0+w,y0,color);
    vline(x0,y0,y0+h,color);
    hline(x0,x0+w,y0+h,color);
    vline(x0+w,y0,y0+h,color);

    return;
}

void SPI_TFT::fillrect(int x0, int y0, int w, int h, int color)
{
    unsigned long int index=0;
    if (w < 0)
    {
        x0 = x0 + w;
        w = -w;
    }
    if (h < 0)
    {
        y0 = y0 + h;
        h = -h;
    }
    mod_orientation();
    window(x0,y0,w,h);
    wr_cmd(0x22);
    _cs = 0;
    wr_dat_start();
    _spi.format(16,3);
    int num = h*w;
    for( index = 0; index<num; index++ )
     {
        _spi.fastWrite(color);
     }
     _spi.clearRX();
    _spi.format(8,3);
    _cs = 1;
    return;
}

void SPI_TFT::draw_ellipse(int xc, int yc, int a, int b, unsigned int color)
{           /* e(x,y) = b^2*x^2 + a^2*y^2 - a^2*b^2 */
    wr_reg(0x03, 0x1030);
    WindowMax();
    int x = 0, y = b;
    long a2 = (long)a*a, b2 = (long)b*b;
    long crit1 = -(a2/4 + a%2 + b2);
    long crit2 = -(b2/4 + b%2 + a2);
    long crit3 = -(b2/4 + b%2);
    long t = -a2*y; /* e(x+1/2,y-1/2) - (a^2+b^2)/4 */
    long dxt = 2*b2*x, dyt = -2*a2*y;
    long d2xt = 2*b2, d2yt = 2*a2;

    while (y>=0 && x<=a)
    {
        pixel(xc+x, yc+y, color);
        if (x!=0 || y!=0)
            pixel(xc-x, yc-y, color);
        if (x!=0 && y!=0)
        {
            pixel(xc+x, yc-y, color);
            pixel(xc-x, yc+y, color);
        }
        if (t + b2*x <= crit1 ||   /* e(x+1,y-1/2) <= 0 */
            t + a2*y <= crit3)     /* e(x+1/2,y) <= 0 */
            incx();
        else if (t - a2*y > crit2) /* e(x+1/2,y-1) > 0 */
            incy();
        else
        {
            incx();
            incy();
        }
    }
}

void SPI_TFT::fill_ellipse(int xc, int yc, int a, int b, unsigned int color)
{           /* e(x,y) = b^2*x^2 + a^2*y^2 - a^2*b^2 */
    int x = 0, y = b;
    int rx = x, ry = y;
    unsigned int width = 1;
    unsigned int height = 1;
    long a2 = (long)a*a, b2 = (long)b*b;
    long crit1 = -(a2/4 + a%2 + b2);
    long crit2 = -(b2/4 + b%2 + a2);
    long crit3 = -(b2/4 + b%2);
    long t = -a2*y; /* e(x+1/2,y-1/2) - (a^2+b^2)/4 */
    long dxt = 2*b2*x, dyt = -2*a2*y;
    long d2xt = 2*b2, d2yt = 2*a2;

    if (b == 0)
    {
        fillrect(xc-a, yc, 2*a+1, 1, color);
        return;
    }

    while (y>=0 && x<=a)
    {
        if (t + b2*x <= crit1 ||    /* e(x+1,y-1/2) <= 0 */
            t + a2*y <= crit3)      /* e(x+1/2,y) <= 0 */
        {
            if (height == 1)
                ; /* draw nothing */
            else if (ry*2+1 > (height-1)*2)
            {
                fillrect(xc-rx, yc-ry, width, height-1, color);
                fillrect(xc-rx, yc+ry+1, width, 1-height, color);
                ry -= height-1;
                height = 1;
            }
            else
            {
                fillrect(xc-rx, yc-ry, width, ry*2+1, color);
                ry -= ry;
                height = 1;
            }
            incx();
            rx++;
            width += 2;
        }
        else if (t - a2*y > crit2)      /* e(x+1/2,y-1) > 0 */
        {
            incy();
            height++;
        }
        else
        {
            if (ry*2+1 > height*2)
            {
                fillrect(xc-rx, yc-ry, width, height, color);
                fillrect(xc-rx, yc+ry+1, width, -height, color);
            }
            else
            {
                fillrect(xc-rx, yc-ry, width, ry*2+1, color);
            }
            incx();
            incy();
            rx++;
            width += 2;
            ry -= height;
            height = 1;
        }
    }

    if (ry > height)
    {
        fillrect(xc-rx, yc-ry, width, height, color);
        fillrect(xc-rx, yc+ry+1, width, -height, color);
    }
    else
    {
        fillrect(xc-rx, yc-ry, width, ry*2+1, color);
    }
}


void SPI_TFT::locate(int x, int y)
{
    char_x = x;
    char_y = y;
}

int SPI_TFT::columns()
{
    return width() / font[1];
}

int SPI_TFT::rows()
{
    return height() / font[2];
}

int SPI_TFT::_putc(int value)
{
    if (value == '\n')    // new line
    {
        char_x = 0;
        char_y = char_y + font[2];
        if (char_y >= height() - font[2])
        {
            char_y = 0;
        }
    }
    else
    {
        character(char_x, char_y, value);
    }
    return value;
}

void SPI_TFT::character(int x, int y, int c)
{
    unsigned int hor,vert,offset,bpl,j,i,b;
    unsigned char* bitmap_char;
    unsigned char z,w;
 
    if ((c < 31) || (c > 127)) return;   // test char range
 
    // read font parameter from start of array
    offset = font[0];                    // bytes / char
    hor = font[1];                       // get hor size of font
    vert = font[2];                      // get vert size of font
    bpl = font[3];                       // bytes per line
 
    if (char_x + hor > width())
    {
        char_x = 0;
        char_y = char_y + vert;
       if (char_y >= height() - font[2])
       {
            char_y = 0;
        }
    }
    mod_orientation();
    
    bitmap_char = &font[((c -32) * offset) + 4]; // start of char bitmap
    w = bitmap_char[0];                          // width of actual char
    window(char_x, char_y,w,vert); // char box
    wr_cmd(0x22);
    _cs = 0;
    wr_dat_start();
    _spi.format(16,3);
    for (j=0; j<vert; j++)                         //  vert line
    {
        for (i=0; i<w; i++)                    //  horz line
        {
            z =  bitmap_char[bpl * i + ((j & 0xF8) >> 3)+1];
            b = 1 << (j & 0x07);
            if (( z & b ) == 0x00)
            {
                _spi.fastWrite(_background);
            }
            else
            {
                _spi.fastWrite(_foreground);
            }
        }
    }
    _spi.clearRX();
    _spi.format(8,3);
    _cs = 1;
    if ((w + 2) < hor)                   // x offset to next char
    {
        char_x += w + 2;
    }
    else char_x += hor;
}

void SPI_TFT::set_font(unsigned char* f)
{
    font = f;
}


void SPI_TFT::Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap)
{
    unsigned int    i,j;
    unsigned short *bitmap_ptr = (unsigned short *)bitmap;
    mod_orientation();
    window(x, y, w, h);
    wr_cmd(0x22);
    _cs = 0;
    wr_dat_start();
    _spi.format(16,3);
    bitmap_ptr += ((h - 1)*w);
    for (j = 0; j < h; j++)             //Lines
    {
        for (i = 0; i < w; i++)         // copy pixel data to TFT
        {
            _spi.fastWrite(*bitmap_ptr);    // one line
            bitmap_ptr++;
        }
        bitmap_ptr -= 2*w;
    }
    _spi.clearRX();
    _spi.format(8,3);
    _cs = 1;
}

int SPI_TFT::Bitmap(unsigned int x, unsigned int y, const char *Name_BMP)
{
 // Current code unusable : Rewrite without DMA is needed
#define OffsetPixelWidth    18
#define OffsetPixelHeigh    22
#define OffsetFileSize      34
#define OffsetPixData       10
#define OffsetBPP           28
#define RGB565CONVERT(red, green, blue) (uint16_t)( (( red   >> 3 ) << 11 ) | (( green >> 2 ) << 5  ) | ( blue  >> 3 ))

    unsigned char BMP_Header[54];
    unsigned short BPP_t;
    unsigned int PixelWidth,PixelHeigh,start_data;
    unsigned int    i,off;
    int padd,j;
    unsigned char *line;
    int bits;

    FILE *Image = fopen(Name_BMP, "rb");  // open the bmp file
    if (!Image) {
        return(0);      // error file not found !
    }

    fread(&BMP_Header[0],1,54,Image);      // get the BMP Header

    if (BMP_Header[0] != 0x42 || BMP_Header[1] != 0x4D) {  // check magic byte
        fclose(Image);
        return(-1);     // error no BMP file
    }

    BPP_t = BMP_Header[OffsetBPP] + (BMP_Header[OffsetBPP + 1] << 8);
    if (BPP_t == 0x0010)
        bits = 16;
    else if (BPP_t == 0x0018)
        bits = 24;
    else {
        fclose(Image);
        return(-2);     // error no 16/24 bit BMP
    }

    PixelHeigh = BMP_Header[OffsetPixelHeigh] + (BMP_Header[OffsetPixelHeigh + 1] << 8) + (BMP_Header[OffsetPixelHeigh + 2] << 16) + (BMP_Header[OffsetPixelHeigh + 3] << 24);
    PixelWidth = BMP_Header[OffsetPixelWidth] + (BMP_Header[OffsetPixelWidth + 1] << 8) + (BMP_Header[OffsetPixelWidth + 2] << 16) + (BMP_Header[OffsetPixelWidth + 3] << 24);
    if (PixelHeigh > height() + y || PixelWidth > width() + x) {
        fclose(Image);
        return(-3);      // too big
    }

    start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24);

    line = (unsigned char *) malloc (bits/8 * PixelWidth); // we need a buffer for a line
    unsigned short *line_short = (unsigned short*) (line); // Same one, addressed as short
    if (line == NULL) {
        return(-4);         // error no memory
    }

    // the bmp lines are padded to multiple of 4 bytes
    padd = -1;
    do {
        padd ++;
    } while ((PixelWidth * bits/8 + padd)%4 != 0);


//fseek(Image, 70 ,SEEK_SET);
    
    
    for (j = PixelHeigh - 1; j >= 0; j--) {               //Lines bottom up
        off = j * (PixelWidth  * bits/8 + padd) + start_data;   // start of line
        fseek(Image, off ,SEEK_SET);
        fread(line,1,PixelWidth * bits/8,Image);       // read a line - slow !
        mod_orientation();
        window(x, y+PixelHeigh - 1 - j,PixelWidth ,1);
        wr_cmd(0x22);
        _cs = 0;
        wr_dat_start();
        _spi.format(16, 3);
        _spi.setFormat();
        
        //If 24 bit format, do some processing
        if (bits == 24) {
            for (i = 0; i<PixelWidth; i++) {
                line_short[i] = RGB565CONVERT(line[3*i+2], line[3*i+1], line[3*i]);
            }
        }
        
        for (i = 0; i < PixelWidth; i++)         // copy pixel data to TFT
        {
            _spi.fastWrite(line_short[i]);    // one line
        }
        
        _spi.clearRX();
        _spi.format(8,3);
        _cs = 1;

    }
    
    
    free (line);
    fclose(Image);
    WindowMax();

    return(1);
}