This library is for Arduino TFT LCD Screen module.(for new LCD driver IC)

Dependents:   ArduinoTFTLCDScreenSample_SPI18TFT

Fork of ST7735_TFT by Jonne Valola

いきさつ

Arduino TFT LCD ScreenというLCD moduleをmbedで使用しました。(module詳細 switch science web)

/media/uploads/suupen/2014-09-21_10.39.18_-240x320-.jpg

このLCD moduleのLCD driver ICはST7735 ですが、複数のバージョンがあり、libraryとICのバージョンが合わないと正常に表示しません。

mbedにはこのICに対応したlibrary(ST7735_TFT)がありますが、私が入手したLCD moduleではICの種類が違うためか正常に動作しませんでした。幸い、switch scienceのwebにarduino用の対応libraryがあったので、これを元にmbedのlibrary(ST7735_TFT)に移植しました。

libraryの移植

移植元のArduino library

移植元はswitch science webにある置き換え用TFTライブラリ(Arduino IDE1.0.5用)を使用します。このファイルを解凍してできる、"Adfruit_ST7735.cpp"の230行目以降のGcmd[]配列が該当するdriver ICの設定データになります。

Adfruit_ST7735.cpp

・
・
・
  Gcmd[] = {                  // Initialization commands for 7735B screens
    19,                       // 18 commands in list:
    ST7735_SWRESET,   DELAY,  //  1: Software reset, no args, w/delay
      50,                     //     50 ms delay
    ST7735_SLPOUT ,   DELAY,  //  2: Out of sleep mode, no args, w/delay
      100,                    //     255 = 500 ms delay
    0x26 , 1,  			// 3: Set default gamma
      0x04,                     //     16-bit color
    0xb1, 2,              	// 4: Frame Rate
      0x0b,
      0x14,
    0xc0, 2,                    // 5: VRH1[4:0] & VC[2:0]
      0x08,
      0x00,
    0xc1, 1,                    // 6: BT[2:0]
      0x05,
    0xc5, 2,                    // 7: VMH[6:0] & VML[6:0]
      0x41,
      0x30,
    0xc7, 1,                    // 8: LCD Driving control
      0xc1,
    0xEC, 1,                    // 9: Set pumping color freq
      0x1b,
    0x3a , 1 + DELAY,  	        // 10: Set color format
      0x55,                     //     16-bit color
      100,
    0x2a, 4,                    // 11: Set Column Address
      0x00,
      0x00,
      0x00,
      0x7f,
    0x2b, 4,                    // 12: Set Page Address
      0x00,
      0x00,
      0x00,
      0x9f,
    0x36, 1,                    // 12+1: Set Scanning Direction
      0xc8,
    0xb7, 1,			// 14: Set Source Output Direciton
      0x00,
    0xf2, 1,			// 15: Enable Gamma bit
      0x00,
    0xe0, 15 + DELAY,		// 16: magic
      0x28, 0x24, 0x22, 0x31,
      0x2b, 0x0e, 0x53, 0xa5,
      0x42, 0x16, 0x18, 0x12,
      0x1a, 0x14, 0x03,
      50,
    0xe1, 15 + DELAY,		// 17: more magic
      0x17, 0x1b, 0x1d, 0x0e,
      0x14, 0x11, 0x2c, 0xa5,
      0x3d, 0x09, 0x27, 0x2d,
      0x25, 0x2b, 0x3c, 
      50, 
    ST7735_NORON  ,   DELAY,  // 17: Normal display on, no args, w/delay
      10,                     //     10 ms delay
    ST7735_DISPON ,   DELAY,  // 18: Main screen turn on, no args, w/delay
      255 };                  //     255 = 500 ms delay
・
・
・

移植先のmbed library

移植先となるmbed libraryはST7735_TFT libraryとなります。この中のST7735_TFT.cppのtft_reset() にIC設定データを追加します。追加したコードを区別するため"ST7735B"というdefine定義で条件コンパイルするようにしました。

追加したコードの最後にある"switch(orientation)"の節はarduinoのlibraryにはなく、mbedのlibraryから修正したものです。

ST7735_TFT.cpp

・
・
・
void ST7735_TFT::tft_reset() {
    static unsigned short driverCode;
    
    // init SPI
    _spi.format(8,3);                 // 8 bit spi mode 3
    _spi.frequency(16000000);         // 16Mhz SPI clock ... 15Mhz is maximum for display, but it seems to work
    
    // reset exactly like in Arduino version
    _cs = 0;
    _reset = 1;                       // reset
    wait_ms(500);
    _reset = 0;                       // reset
    wait_ms(500);
    _reset = 1;                       // reset
    wait_ms(500);
 
 #ifdef ST7735B //@ss   
    /* Start Initial Sequence ----------------------------------------------------*/
    wr_cmd(ST7735_SWRESET);                         /* SW Reset                       */
    wait_ms(150);
    wr_cmd(ST7735_SLPOUT);                         /* Out of sleepmode               */
    wait_ms(500);
    
    wr_cmd(0x26);   // 3: Set default gamma
    wr_dat(0x04);   //      16-bit color
    
    wr_cmd(ST7735_FRMCTR1);                         /*4 Frame rate in normal mode            */
    wr_dat(0x0b);                              
    wr_dat(0x14);

    


    wr_cmd(0xc0);   // 5 POWER CONTROL 1   
    wr_dat(0x08); 
    wr_dat(0x00);
    
    wr_cmd(0xc1);   // 6 POWER CONTROL 2   
    wr_dat(0x05);

    wr_cmd(0xC5);   // POWER CONTROL 6   
    wr_dat(0x41);            // 
    wr_dat(0x30);

    wr_cmd(0xc7);   // 8:LCD Driving control
    wr_dat(0xc1);
    
    wr_cmd(0xec);   // 9:Set color format
    wr_dat(0x1b);   //      16-bit color
     
    wr_cmd(0x3A);   // COLOR MODE   
    wr_dat(0x55);            //      
    wait_ms(100);
    
    wr_cmd(0x2a);   // 11 COLUMN ADDR SET   
    wr_dat(0x00);   //
    wr_dat(0x00);   // xstart = 0
    wr_dat(0x00);   //
    wr_dat(0x7F);   // xend = 127
    
    wr_cmd(0x2b);   // ROW ADDR SET   
    wr_dat(0x00);   //
    wr_dat(0x00);   // ystart = 0
    wr_dat(0x00);   //
    wr_dat(0x9F);   // yend = 159            
    
    wr_cmd(0x36);   // 13 Set Scanning Direction
    wr_dat(0xc8);
    
    wr_cmd(0xb7);   // 14 Set Source Output Direction
    wr_dat(0x00);
    /* Gamma settings  -----------------------------------------------------------*/

  wr_cmd(0xE0); // GMCTRP1
  wr_dat(0x28);
  wr_dat(0x24);
  wr_dat(0x22);
  wr_dat(0x31);
  
  wr_dat(0x2b);
  wr_dat(0x0e);
  wr_dat(0x53);
  wr_dat(0xa5);
  
  wr_dat(0x42);
  wr_dat(0x16);
  wr_dat(0x18);
  wr_dat(0x12);
  
  wr_dat(0x1a);
  wr_dat(0x14);
  wr_dat(0x03);

    wait_ms(50);

  wr_cmd(0xE1); // GMCTRN1
  wr_dat(0x17); 
  wr_dat(0x1b); 
  wr_dat(0x1d); 
  wr_dat(0x0e);
   
  wr_dat(0x14); 
  wr_dat(0x11); 
  wr_dat(0x2c); 
  wr_dat(0xa5); 
  
  wr_dat(0x3d); 
  wr_dat(0x09); 
  wr_dat(0x27); 
  wr_dat(0x2d); 
  
  wr_dat(0x25); 
  wr_dat(0x2b); 
  wr_dat(0x3c); 

    wait_ms(50);
  
  wr_cmd(0x13); // 18 Normal display on no args.
  wait_ms(10);

  wr_cmd(0x29);  // 19 Main screen turn on, no args.normal display on
  wait_ms(500);
  
  switch (orientation) {
        case 0:
            wr_reg(0x36, 0x0008);   //originalでの0xc8は間違いで、ST7735_MADCTL(0x36)ではないか
            break;
        case 1:
            wr_reg(0x36, 0x0068);
            break;
        case 2:
            wr_reg(0x36, 0x00C8);
            break;
        case 3:
            wr_reg(0x36, 0x00A8);
            break;
    }

#else //@ss ~ST7735B (original)
   /* Start Initial Sequence ----------------------------------------------------*/
    wr_cmd(ST7735_SWRESET);                         /* SW Reset                       */
    wait_ms(150);
    wr_cmd(ST7735_SLPOUT);                         /* Out of sleepmode               */
    wait_ms(500);
・
・
・

mbed library自体の修正

上記のdriver IC設定値の修正だけでは、LCDの表示領域がずれるなどの不具合がありました。この修正も行います。この不具合の原因が、driver ICの種類の違いよるものなのか、コードそのものの不具合なのかはわかりません。
修正コードは"@ss"のコメントをつけた行になります。

1.driver ICへの画像データバッファアドレス指示の修正
pixel()関数で使用しているwindow()関数の引数が間違っています。元のコードは、終点のx,yが絶対座標形式のようになっていますが、視点からの相対座標を設定しないといけません。

ST7735_TFT.cpp

・
・
・
void ST7735_TFT::pixel(int x, int y, int color) {
  if ((x >= width()) || (y >= height())) return;

    window(x,y,1,1);    //@ss
//@ss  window(x,y,x+1,y+1);
  
  // setup for data
・
・
・


2.window()関数の座標原点のずれ
driver ICの仕様かもしれませんが、driver ICの画像データバッファの原点が、x=2, y=1となっています。このlibraryで使用される原点はx=0, y=0なので、offsetを追加します。

ST7735_TFT.cpp

・
・
・
void ST7735_TFT::window (unsigned int x, unsigned int y, unsigned int w, unsigned int h) {
    //@ss なぜか CASET(X軸)で2, RASET(Y軸)で1 が最小値になっており、これがLCDDisplayのx,y原点になっていない(LCDの原点を設定するためには(-2,-1)を指示する必要がある
    //@ss プログラム上はLCD原点を0,0にするために、この関数でoffsetをかける。
    x-=2;   //@ss
    y-=1;   //@ss
  wr_cmd(ST7735_CASET);  // column addr set
  wr_dat(0x00);
・
・
・


3.端子定義処理での"orientation"変数の初期値設定タイミングの変更

"ST7735_TFT()"関数内の"orientation"変数の初期値設定がtft_reset()の後になっていますが、tft_reset()内でこの変数を使用しているので、tft_reset()実行前に、変数値を設定する必要があります。

ST7735_TFT.cpp

・
・
・
ST7735_TFT::ST7735_TFT(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName rs, PinName reset, const char *name)
        : _spi(mosi, miso, sclk), _cs(cs), _rs(rs), _reset(reset),GraphicsDisplay(name) {
    orientation = 1;    //@ss
    tft_reset();
//@ss    orientation = 2;
    char_x = 0;
・
・
・

補足:LCD 表示原点の設定

今回使用したmoduleのLCDは160*128ピクセルのものです。画像データの送信によって、x,y軸を入れ替える(横長、縦長)ことができます。これを設定するのが、上記"3"のorientation変数になります。この変数は"set_orientation()"関数の引数として与えることで変更することができます。引数は0,1,2,3となります。

<縦横選択>
0,2 : 縦長(x=0-127, y=0-159)
1,3 : 横長(x=0-159, y=0-127)

<縦(y軸)座標値増加方向>
0,1を基準にした場合、2,3は上下が逆転する

<表示例>
orientation = 0
/media/uploads/suupen/orientation_0--_1_-320x240-.jpg

orientation = 1
/media/uploads/suupen/orientation_1--_2_-320x240-.jpg

orientation =2
/media/uploads/suupen/orientation_2-_3_-320x240-.jpg

orientation=3
/media/uploads/suupen/orientation_3--_4_-320x240-.jpg

以上

Committer:
suupen
Date:
Sat Sep 20 12:44:49 2014 +0000
Revision:
2:9a62e159e30c
Parent:
0:246f2fb5be59
This library is for Arduino TFT LCD Screen Module.(for new LCD driver)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
suupen 2:9a62e159e30c 1 /*
suupen 2:9a62e159e30c 2 この Libraryは Arduino TFT LCD Screenを使うために"ST7735_TFT"Libraryを改造したものです。
suupen 2:9a62e159e30c 3 original library からの変更箇所はcomment"//@ss"の行になります。
suupen 2:9a62e159e30c 4
suupen 2:9a62e159e30c 5 */
suupen 2:9a62e159e30c 6
smultron1977 0:246f2fb5be59 7 /* mbed library for 128*160 pixel display TFT based on ST7735 LCD Controller
smultron1977 0:246f2fb5be59 8 * ST7735 specific routines (initialization, window addressing, pixel output)
smultron1977 0:246f2fb5be59 9 * Copyright (c) 2011 Jonne Valola
smultron1977 0:246f2fb5be59 10 *
smultron1977 0:246f2fb5be59 11 * WARNING !! WORK IN PROGRESS !!!
smultron1977 0:246f2fb5be59 12 *
smultron1977 0:246f2fb5be59 13 * Graphics routines and SPI routines derived work used with permission from:
smultron1977 0:246f2fb5be59 14 * mbed library for 240*320 pixel display TFT based on HX8347D LCD Controller
smultron1977 0:246f2fb5be59 15 * Copyright (c) 2011 Peter Drescher - DC2PD
smultron1977 0:246f2fb5be59 16 *
smultron1977 0:246f2fb5be59 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
smultron1977 0:246f2fb5be59 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
smultron1977 0:246f2fb5be59 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
smultron1977 0:246f2fb5be59 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
smultron1977 0:246f2fb5be59 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
smultron1977 0:246f2fb5be59 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
smultron1977 0:246f2fb5be59 23 * THE SOFTWARE.
smultron1977 0:246f2fb5be59 24 */
smultron1977 0:246f2fb5be59 25
smultron1977 0:246f2fb5be59 26 #ifndef MBED_ST7735_TFT_H
smultron1977 0:246f2fb5be59 27 #define MBED_ST7735_TFT_H
smultron1977 0:246f2fb5be59 28
smultron1977 0:246f2fb5be59 29 #include "mbed.h"
smultron1977 0:246f2fb5be59 30 #include "GraphicsDisplay.h"
smultron1977 0:246f2fb5be59 31
suupen 2:9a62e159e30c 32
suupen 2:9a62e159e30c 33
smultron1977 0:246f2fb5be59 34 #define RGB(r,g,b) (((r&0xF8)<<8)|((g&0xFC)<<3)|((b&0xF8)>>3)) //5 red | 6 green | 5 blue
smultron1977 0:246f2fb5be59 35
suupen 2:9a62e159e30c 36 #define ST7735B //@SS
suupen 2:9a62e159e30c 37
smultron1977 0:246f2fb5be59 38 /*define ST7735 Commands */
smultron1977 0:246f2fb5be59 39
smultron1977 0:246f2fb5be59 40 #define ST7735_NOP 0x0
smultron1977 0:246f2fb5be59 41 #define ST7735_SWRESET 0x01
smultron1977 0:246f2fb5be59 42 #define ST7735_RDDID 0x04
smultron1977 0:246f2fb5be59 43 #define ST7735_RDDST 0x09
smultron1977 0:246f2fb5be59 44
smultron1977 0:246f2fb5be59 45 #define ST7735_SLPIN 0x10
smultron1977 0:246f2fb5be59 46 #define ST7735_SLPOUT 0x11
smultron1977 0:246f2fb5be59 47 #define ST7735_PTLON 0x12
smultron1977 0:246f2fb5be59 48 #define ST7735_NORON 0x13
smultron1977 0:246f2fb5be59 49
smultron1977 0:246f2fb5be59 50 #define ST7735_INVOFF 0x20
smultron1977 0:246f2fb5be59 51 #define ST7735_INVON 0x21
smultron1977 0:246f2fb5be59 52 #define ST7735_DISPOFF 0x28
smultron1977 0:246f2fb5be59 53 #define ST7735_DISPON 0x29
smultron1977 0:246f2fb5be59 54 #define ST7735_CASET 0x2A
smultron1977 0:246f2fb5be59 55 #define ST7735_RASET 0x2B
smultron1977 0:246f2fb5be59 56 #define ST7735_RAMWR 0x2C
smultron1977 0:246f2fb5be59 57 #define ST7735_RAMRD 0x2E
smultron1977 0:246f2fb5be59 58
smultron1977 0:246f2fb5be59 59 #define ST7735_COLMOD 0x3A
smultron1977 0:246f2fb5be59 60 #define ST7735_MADCTL 0x36
smultron1977 0:246f2fb5be59 61
smultron1977 0:246f2fb5be59 62
smultron1977 0:246f2fb5be59 63 #define ST7735_FRMCTR1 0xB1
smultron1977 0:246f2fb5be59 64 #define ST7735_FRMCTR2 0xB2
smultron1977 0:246f2fb5be59 65 #define ST7735_FRMCTR3 0xB3
smultron1977 0:246f2fb5be59 66 #define ST7735_INVCTR 0xB4
smultron1977 0:246f2fb5be59 67 #define ST7735_DISSET5 0xB6
smultron1977 0:246f2fb5be59 68
smultron1977 0:246f2fb5be59 69 #define ST7735_PWCTR1 0xC0
smultron1977 0:246f2fb5be59 70 #define ST7735_PWCTR2 0xC1
smultron1977 0:246f2fb5be59 71 #define ST7735_PWCTR3 0xC2
smultron1977 0:246f2fb5be59 72 #define ST7735_PWCTR4 0xC3
smultron1977 0:246f2fb5be59 73 #define ST7735_PWCTR5 0xC4
smultron1977 0:246f2fb5be59 74 #define ST7735_VMCTR1 0xC5
smultron1977 0:246f2fb5be59 75
smultron1977 0:246f2fb5be59 76 #define ST7735_RDID1 0xDA
smultron1977 0:246f2fb5be59 77 #define ST7735_RDID2 0xDB
smultron1977 0:246f2fb5be59 78 #define ST7735_RDID3 0xDC
smultron1977 0:246f2fb5be59 79 #define ST7735_RDID4 0xDD
smultron1977 0:246f2fb5be59 80
smultron1977 0:246f2fb5be59 81 #define ST7735_PWCTR6 0xFC
smultron1977 0:246f2fb5be59 82
smultron1977 0:246f2fb5be59 83 #define ST7735_GMCTRP1 0xE0
smultron1977 0:246f2fb5be59 84 #define ST7735_GMCTRN1 0xE1
smultron1977 0:246f2fb5be59 85
smultron1977 0:246f2fb5be59 86 /* some RGB color definitions */
smultron1977 0:246f2fb5be59 87 #define Black 0x0000 /* 0, 0, 0 */
smultron1977 0:246f2fb5be59 88 #define Navy 0x000F /* 0, 0, 128 */
smultron1977 0:246f2fb5be59 89 #define DarkGreen 0x03E0 /* 0, 128, 0 */
smultron1977 0:246f2fb5be59 90 #define DarkCyan 0x03EF /* 0, 128, 128 */
smultron1977 0:246f2fb5be59 91 #define Maroon 0x7800 /* 128, 0, 0 */
smultron1977 0:246f2fb5be59 92 #define Purple 0x780F /* 128, 0, 128 */
smultron1977 0:246f2fb5be59 93 #define Olive 0x7BE0 /* 128, 128, 0 */
smultron1977 0:246f2fb5be59 94 #define LightGrey 0xC618 /* 192, 192, 192 */
smultron1977 0:246f2fb5be59 95 #define DarkGrey 0x7BEF /* 128, 128, 128 */
smultron1977 0:246f2fb5be59 96 #define Blue 0x001F /* 0, 0, 255 */
smultron1977 0:246f2fb5be59 97 #define Green 0x07E0 /* 0, 255, 0 */
smultron1977 0:246f2fb5be59 98 #define Cyan 0x07FF /* 0, 255, 255 */
smultron1977 0:246f2fb5be59 99 #define Red 0xF800 /* 255, 0, 0 */
smultron1977 0:246f2fb5be59 100 #define Magenta 0xF81F /* 255, 0, 255 */
smultron1977 0:246f2fb5be59 101 #define Yellow 0xFFE0 /* 255, 255, 0 */
smultron1977 0:246f2fb5be59 102 #define White 0xFFFF /* 255, 255, 255 */
smultron1977 0:246f2fb5be59 103 #define Orange 0xFD20 /* 255, 165, 0 */
smultron1977 0:246f2fb5be59 104 #define GreenYellow 0xAFE5 /* 173, 255, 47 */
smultron1977 0:246f2fb5be59 105
smultron1977 0:246f2fb5be59 106 class ST7735_TFT : public GraphicsDisplay {
smultron1977 0:246f2fb5be59 107 public:
smultron1977 0:246f2fb5be59 108
smultron1977 0:246f2fb5be59 109 /** Create a ST7735_TFT object connected to SPI and three pins. ST7735 requires rs pin to toggle between data/command
smultron1977 0:246f2fb5be59 110 *
smultron1977 0:246f2fb5be59 111 * @param mosi,miso,sclk SPI
smultron1977 0:246f2fb5be59 112 * @param cs pin connected to CS of display (called SS for 'Slave Select' in ST7735 datasheet)
smultron1977 0:246f2fb5be59 113 * @param rs pin connected to RS of display (called D/CX in ST7735 datasheet)
smultron1977 0:246f2fb5be59 114 * @param reset pin connected to RESET of display
smultron1977 0:246f2fb5be59 115 *
smultron1977 0:246f2fb5be59 116 */
smultron1977 0:246f2fb5be59 117 ST7735_TFT(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName rs, PinName reset,const char* name ="TFT");
smultron1977 0:246f2fb5be59 118
smultron1977 0:246f2fb5be59 119 /** Get the width of the screen in pixel
smultron1977 0:246f2fb5be59 120 *
smultron1977 0:246f2fb5be59 121 * @param
smultron1977 0:246f2fb5be59 122 * @returns width of screen in pixel
smultron1977 0:246f2fb5be59 123 *
smultron1977 0:246f2fb5be59 124 */
smultron1977 0:246f2fb5be59 125 virtual int width();
smultron1977 0:246f2fb5be59 126
smultron1977 0:246f2fb5be59 127 /** Get the height of the screen in pixel
smultron1977 0:246f2fb5be59 128 *
smultron1977 0:246f2fb5be59 129 * @returns height of screen in pixel
smultron1977 0:246f2fb5be59 130 *
smultron1977 0:246f2fb5be59 131 */
smultron1977 0:246f2fb5be59 132 virtual int height();
smultron1977 0:246f2fb5be59 133
smultron1977 0:246f2fb5be59 134 /** Draw a pixel at x,y with color
smultron1977 0:246f2fb5be59 135 *
smultron1977 0:246f2fb5be59 136 * @param x horizontal position
smultron1977 0:246f2fb5be59 137 * @param y vertical position
smultron1977 0:246f2fb5be59 138 * @param color 16 bit pixel color
smultron1977 0:246f2fb5be59 139 */
smultron1977 0:246f2fb5be59 140 virtual void pixel(int x, int y, int colour);
smultron1977 0:246f2fb5be59 141
smultron1977 0:246f2fb5be59 142 /** Get colour of pixel at x,y
smultron1977 0:246f2fb5be59 143 *
smultron1977 0:246f2fb5be59 144 * @param x horizontal position
smultron1977 0:246f2fb5be59 145 * @param y vertical position
smultron1977 0:246f2fb5be59 146 */
smultron1977 0:246f2fb5be59 147
smultron1977 0:246f2fb5be59 148 int getpixel(unsigned int x, unsigned int y);
smultron1977 0:246f2fb5be59 149
smultron1977 0:246f2fb5be59 150 /** draw a circle
smultron1977 0:246f2fb5be59 151 *
smultron1977 0:246f2fb5be59 152 * @param x0,y0 center
smultron1977 0:246f2fb5be59 153 * @param r radius
smultron1977 0:246f2fb5be59 154 * @param color 16 bit color *
smultron1977 0:246f2fb5be59 155 *
smultron1977 0:246f2fb5be59 156 */
smultron1977 0:246f2fb5be59 157
smultron1977 0:246f2fb5be59 158 void circle(int x, int y, int r, int colour);
smultron1977 0:246f2fb5be59 159
smultron1977 0:246f2fb5be59 160 /** draw a filled circle
smultron1977 0:246f2fb5be59 161 *
smultron1977 0:246f2fb5be59 162 * @param x0,y0 center
smultron1977 0:246f2fb5be59 163 * @param r radius
smultron1977 0:246f2fb5be59 164 * @param color 16 bit color *
smultron1977 0:246f2fb5be59 165 *
smultron1977 0:246f2fb5be59 166 * use circle with different radius,
smultron1977 0:246f2fb5be59 167 * can miss some pixel
smultron1977 0:246f2fb5be59 168 */
smultron1977 0:246f2fb5be59 169 void fillcircle(int x, int y, int r, int colour);
smultron1977 0:246f2fb5be59 170
smultron1977 0:246f2fb5be59 171 /** draw a 1 pixel line
smultron1977 0:246f2fb5be59 172 *
smultron1977 0:246f2fb5be59 173 * @param x0,y0 start point
smultron1977 0:246f2fb5be59 174 * @param x1,y1 stop point
smultron1977 0:246f2fb5be59 175 * @param color 16 bit color
smultron1977 0:246f2fb5be59 176 *
smultron1977 0:246f2fb5be59 177 */
smultron1977 0:246f2fb5be59 178 void line(int x0, int y0, int x1, int y1, int colour);
smultron1977 0:246f2fb5be59 179
smultron1977 0:246f2fb5be59 180 /** draw a rect
smultron1977 0:246f2fb5be59 181 *
smultron1977 0:246f2fb5be59 182 * @param x0,y0 top left corner
smultron1977 0:246f2fb5be59 183 * @param x1,y1 down right corner
smultron1977 0:246f2fb5be59 184 * @param color 16 bit color
smultron1977 0:246f2fb5be59 185 * *
smultron1977 0:246f2fb5be59 186 */
smultron1977 0:246f2fb5be59 187 void rect(int x0, int y0, int x1, int y1, int colour);
smultron1977 0:246f2fb5be59 188
smultron1977 0:246f2fb5be59 189 /** draw a filled rect
smultron1977 0:246f2fb5be59 190 *
smultron1977 0:246f2fb5be59 191 * @param x0,y0 top left corner
smultron1977 0:246f2fb5be59 192 * @param x1,y1 down right corner
smultron1977 0:246f2fb5be59 193 * @param color 16 bit color
smultron1977 0:246f2fb5be59 194 *
smultron1977 0:246f2fb5be59 195 */
smultron1977 0:246f2fb5be59 196 void fillrect(int x0, int y0, int x1, int y1, int colour);
smultron1977 0:246f2fb5be59 197
smultron1977 0:246f2fb5be59 198 /** setup cursor position
smultron1977 0:246f2fb5be59 199 *
smultron1977 0:246f2fb5be59 200 * @param x x-position (top left)
smultron1977 0:246f2fb5be59 201 * @param y y-position
smultron1977 0:246f2fb5be59 202 */
smultron1977 0:246f2fb5be59 203 void locate(int x, int y);
smultron1977 0:246f2fb5be59 204
smultron1977 0:246f2fb5be59 205 /** Fill the screen with _backgroun color
smultron1977 0:246f2fb5be59 206 *
smultron1977 0:246f2fb5be59 207 */
smultron1977 0:246f2fb5be59 208 virtual void cls (void);
smultron1977 0:246f2fb5be59 209
smultron1977 0:246f2fb5be59 210 /** calculate the max number of char in a line
smultron1977 0:246f2fb5be59 211 *
smultron1977 0:246f2fb5be59 212 * @returns max columns
smultron1977 0:246f2fb5be59 213 * depends on actual font size
smultron1977 0:246f2fb5be59 214 *
smultron1977 0:246f2fb5be59 215 */
smultron1977 0:246f2fb5be59 216 int columns(void);
smultron1977 0:246f2fb5be59 217
smultron1977 0:246f2fb5be59 218 /** calculate the max number of columns
smultron1977 0:246f2fb5be59 219 *
smultron1977 0:246f2fb5be59 220 * @returns max column
smultron1977 0:246f2fb5be59 221 * depends on actual font size
smultron1977 0:246f2fb5be59 222 *
smultron1977 0:246f2fb5be59 223 */
smultron1977 0:246f2fb5be59 224 int rows(void);
smultron1977 0:246f2fb5be59 225
smultron1977 0:246f2fb5be59 226 /** put a char on the screen
smultron1977 0:246f2fb5be59 227 *
smultron1977 0:246f2fb5be59 228 * @param value char to print
smultron1977 0:246f2fb5be59 229 * @returns printed char
smultron1977 0:246f2fb5be59 230 *
smultron1977 0:246f2fb5be59 231 */
smultron1977 0:246f2fb5be59 232 int _putc(int value);
smultron1977 0:246f2fb5be59 233
smultron1977 0:246f2fb5be59 234 /** draw a character on given position out of the active font to the TFT
smultron1977 0:246f2fb5be59 235 *
smultron1977 0:246f2fb5be59 236 * @param x x-position of char (top left)
smultron1977 0:246f2fb5be59 237 * @param y y-position
smultron1977 0:246f2fb5be59 238 * @param c char to print
smultron1977 0:246f2fb5be59 239 *
smultron1977 0:246f2fb5be59 240 */
smultron1977 0:246f2fb5be59 241 virtual void character(int x, int y, int c);
smultron1977 0:246f2fb5be59 242
smultron1977 0:246f2fb5be59 243 /** paint a bitmap on the TFT
smultron1977 0:246f2fb5be59 244 *
smultron1977 0:246f2fb5be59 245 * @param x,y : upper left corner
smultron1977 0:246f2fb5be59 246 * @param w width of bitmap
smultron1977 0:246f2fb5be59 247 * @param h high of bitmap
smultron1977 0:246f2fb5be59 248 * @param *bitmap pointer to the bitmap data
smultron1977 0:246f2fb5be59 249 *
smultron1977 0:246f2fb5be59 250 * bitmap format: 16 bit R5 G6 B5
smultron1977 0:246f2fb5be59 251 *
smultron1977 0:246f2fb5be59 252 * use Gimp to create / load , save as BMP, option 16 bit R5 G6 B5
smultron1977 0:246f2fb5be59 253 * use winhex to load this file and mark data stating at offset 0x46 to end
smultron1977 0:246f2fb5be59 254 * use edit -> copy block -> C Source to export C array
smultron1977 0:246f2fb5be59 255 * paste this array into your program
smultron1977 0:246f2fb5be59 256 *
smultron1977 0:246f2fb5be59 257 * define the array as static const unsigned char to put it into flash memory
smultron1977 0:246f2fb5be59 258 * cast the pointer to (unsigned char *) :
smultron1977 0:246f2fb5be59 259 * tft.Bitmap(10,40,309,50,(unsigned char *)scala);
smultron1977 0:246f2fb5be59 260 */
smultron1977 0:246f2fb5be59 261 void Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap);
smultron1977 0:246f2fb5be59 262
smultron1977 0:246f2fb5be59 263
smultron1977 0:246f2fb5be59 264 /** paint a 16 bit BMP from local filesytem on the TFT (slow)
smultron1977 0:246f2fb5be59 265 *
smultron1977 0:246f2fb5be59 266 * @param x,y : upper left corner
smultron1977 0:246f2fb5be59 267 * @param *Name_BMP name of the BMP file
smultron1977 0:246f2fb5be59 268 * @returns 1 if bmp file was found and painted
smultron1977 0:246f2fb5be59 269 * @returns -1 if bmp file was found not found
smultron1977 0:246f2fb5be59 270 * @returns -2 if bmp file is not 16bit
smultron1977 0:246f2fb5be59 271 * @returns -3 if bmp file is to big for screen
smultron1977 0:246f2fb5be59 272 * @returns -4 if buffer malloc go wrong
smultron1977 0:246f2fb5be59 273 *
smultron1977 0:246f2fb5be59 274 * bitmap format: 16 bit R5 G6 B5
smultron1977 0:246f2fb5be59 275 *
smultron1977 0:246f2fb5be59 276 * use Gimp to create / load , save as BMP, option 16 bit R5 G6 B5
smultron1977 0:246f2fb5be59 277 * copy to internal file system
smultron1977 0:246f2fb5be59 278 *
smultron1977 0:246f2fb5be59 279 */
smultron1977 0:246f2fb5be59 280
smultron1977 0:246f2fb5be59 281 int BMP_16(unsigned int x, unsigned int y, const char *Name_BMP);
smultron1977 0:246f2fb5be59 282
smultron1977 0:246f2fb5be59 283 /** Read an area from the LCD RAM to MCU RAM
smultron1977 0:246f2fb5be59 284 *
smultron1977 0:246f2fb5be59 285 * @param x,y : upper left corner
smultron1977 0:246f2fb5be59 286 * @param w width of bitmap
smultron1977 0:246f2fb5be59 287 * @param h high of bitmap
smultron1977 0:246f2fb5be59 288 * @param *buffer pointer to the buffer
smultron1977 0:246f2fb5be59 289 */
smultron1977 0:246f2fb5be59 290
smultron1977 0:246f2fb5be59 291 void read_area(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *buffer);
smultron1977 0:246f2fb5be59 292
smultron1977 0:246f2fb5be59 293 /** select the font to use
smultron1977 0:246f2fb5be59 294 *
smultron1977 0:246f2fb5be59 295 * @param f pointer to font array
smultron1977 0:246f2fb5be59 296 *
smultron1977 0:246f2fb5be59 297 * font array can created with GLCD Font Creator from http://www.mikroe.com
smultron1977 0:246f2fb5be59 298 * you have to add 4 parameter at the beginning of the font array to use:
smultron1977 0:246f2fb5be59 299 * - the number of byte / char
smultron1977 0:246f2fb5be59 300 * - the vertial size in pixel
smultron1977 0:246f2fb5be59 301 * - the horizontal size in pixel
smultron1977 0:246f2fb5be59 302 * - the number of byte per vertical line
smultron1977 0:246f2fb5be59 303 * you also have to change the array to char[]
smultron1977 0:246f2fb5be59 304 *
smultron1977 0:246f2fb5be59 305 */
smultron1977 0:246f2fb5be59 306 void set_font(unsigned char* f);
smultron1977 0:246f2fb5be59 307
smultron1977 0:246f2fb5be59 308 /** Set the orientation of the screen
smultron1977 0:246f2fb5be59 309 * x,y: 0,0 is always top left
smultron1977 0:246f2fb5be59 310 *
smultron1977 0:246f2fb5be59 311 * @param o direction to use the screen (0-3) 90&#65533; Steps
smultron1977 0:246f2fb5be59 312 *
smultron1977 0:246f2fb5be59 313 */
smultron1977 0:246f2fb5be59 314 void set_orientation(unsigned int o);
smultron1977 0:246f2fb5be59 315
smultron1977 0:246f2fb5be59 316 SPI _spi;
smultron1977 0:246f2fb5be59 317 DigitalOut _cs;
smultron1977 0:246f2fb5be59 318 DigitalOut _rs;
smultron1977 0:246f2fb5be59 319 DigitalOut _reset;
smultron1977 0:246f2fb5be59 320 unsigned char* font;
smultron1977 0:246f2fb5be59 321
smultron1977 0:246f2fb5be59 322 protected:
smultron1977 0:246f2fb5be59 323
smultron1977 0:246f2fb5be59 324 /** draw a horizontal line
smultron1977 0:246f2fb5be59 325 *
smultron1977 0:246f2fb5be59 326 * @param x0 horizontal start
smultron1977 0:246f2fb5be59 327 * @param x1 horizontal stop
smultron1977 0:246f2fb5be59 328 * @param y vertical position
smultron1977 0:246f2fb5be59 329 * @param color 16 bit color
smultron1977 0:246f2fb5be59 330 *
smultron1977 0:246f2fb5be59 331 */
smultron1977 0:246f2fb5be59 332 void hline(int x0, int x1, int y, int colour);
smultron1977 0:246f2fb5be59 333
smultron1977 0:246f2fb5be59 334 /** draw a vertical line
smultron1977 0:246f2fb5be59 335 *
smultron1977 0:246f2fb5be59 336 * @param x horizontal position
smultron1977 0:246f2fb5be59 337 * @param y0 vertical start
smultron1977 0:246f2fb5be59 338 * @param y1 vertical stop
smultron1977 0:246f2fb5be59 339 * @param color 16 bit color
smultron1977 0:246f2fb5be59 340 */
smultron1977 0:246f2fb5be59 341 void vline(int y0, int y1, int x, int colour);
smultron1977 0:246f2fb5be59 342
smultron1977 0:246f2fb5be59 343 /** Set draw window region
smultron1977 0:246f2fb5be59 344 *
smultron1977 0:246f2fb5be59 345 * @param x horizontal position
smultron1977 0:246f2fb5be59 346 * @param y vertical position
smultron1977 0:246f2fb5be59 347 * @param w window width in pixel
smultron1977 0:246f2fb5be59 348 * @param h window height in pixels
smultron1977 0:246f2fb5be59 349 */
smultron1977 0:246f2fb5be59 350 void window (unsigned int x, unsigned int y, unsigned int w, unsigned int h);
smultron1977 0:246f2fb5be59 351
smultron1977 0:246f2fb5be59 352 /** Set draw window region to whole screen
smultron1977 0:246f2fb5be59 353 *
smultron1977 0:246f2fb5be59 354 */
smultron1977 0:246f2fb5be59 355 void WindowMax (void);
smultron1977 0:246f2fb5be59 356
smultron1977 0:246f2fb5be59 357 /** Init the ST7735 controller
smultron1977 0:246f2fb5be59 358 *
smultron1977 0:246f2fb5be59 359 */
smultron1977 0:246f2fb5be59 360 void tft_reset();
smultron1977 0:246f2fb5be59 361
smultron1977 0:246f2fb5be59 362 /** Write data to the LCD controller
smultron1977 0:246f2fb5be59 363 *
smultron1977 0:246f2fb5be59 364 * @param dat data written to LCD controller
smultron1977 0:246f2fb5be59 365 *
smultron1977 0:246f2fb5be59 366 */
smultron1977 0:246f2fb5be59 367 void wr_dat(int value);
smultron1977 0:246f2fb5be59 368
smultron1977 0:246f2fb5be59 369 /** Write a command the LCD controller
smultron1977 0:246f2fb5be59 370 *
smultron1977 0:246f2fb5be59 371 * @param cmd: command to be written
smultron1977 0:246f2fb5be59 372 *
smultron1977 0:246f2fb5be59 373 */
smultron1977 0:246f2fb5be59 374 void wr_cmd(int value);
smultron1977 0:246f2fb5be59 375
smultron1977 0:246f2fb5be59 376 /** Start data sequence to the LCD controller
smultron1977 0:246f2fb5be59 377 *
smultron1977 0:246f2fb5be59 378 */
smultron1977 0:246f2fb5be59 379 void wr_dat_start();
smultron1977 0:246f2fb5be59 380
smultron1977 0:246f2fb5be59 381 /** Stop of data writing to the LCD controller
smultron1977 0:246f2fb5be59 382 *
smultron1977 0:246f2fb5be59 383 */
smultron1977 0:246f2fb5be59 384 void wr_dat_stop();
smultron1977 0:246f2fb5be59 385
smultron1977 0:246f2fb5be59 386 /** write data to the LCD controller
smultron1977 0:246f2fb5be59 387 *
smultron1977 0:246f2fb5be59 388 * @param data to be written
smultron1977 0:246f2fb5be59 389 * *
smultron1977 0:246f2fb5be59 390 */
smultron1977 0:246f2fb5be59 391 void wr_dat_only(unsigned short dat);
smultron1977 0:246f2fb5be59 392
smultron1977 0:246f2fb5be59 393 /** Read data from the LCD controller
smultron1977 0:246f2fb5be59 394 *
smultron1977 0:246f2fb5be59 395 * @returns data from LCD controller
smultron1977 0:246f2fb5be59 396 *
smultron1977 0:246f2fb5be59 397 */
smultron1977 0:246f2fb5be59 398 unsigned short rd_dat(void);
smultron1977 0:246f2fb5be59 399
smultron1977 0:246f2fb5be59 400 /** Write a value to the to a LCD register
smultron1977 0:246f2fb5be59 401 *
smultron1977 0:246f2fb5be59 402 * @param reg register to be written
smultron1977 0:246f2fb5be59 403 * @param val data to be written
smultron1977 0:246f2fb5be59 404 */
smultron1977 0:246f2fb5be59 405 void wr_reg (unsigned char reg, unsigned short val);
smultron1977 0:246f2fb5be59 406
smultron1977 0:246f2fb5be59 407 /** Read a LCD register
smultron1977 0:246f2fb5be59 408 *
smultron1977 0:246f2fb5be59 409 * @param reg register to be read
smultron1977 0:246f2fb5be59 410 * @returns value of the register
smultron1977 0:246f2fb5be59 411 */
smultron1977 0:246f2fb5be59 412 unsigned short rd_reg (unsigned char reg);
smultron1977 0:246f2fb5be59 413
smultron1977 0:246f2fb5be59 414 unsigned int orientation;
smultron1977 0:246f2fb5be59 415 unsigned int char_x;
smultron1977 0:246f2fb5be59 416 unsigned int char_y;
smultron1977 0:246f2fb5be59 417
smultron1977 0:246f2fb5be59 418
smultron1977 0:246f2fb5be59 419 };
smultron1977 0:246f2fb5be59 420
smultron1977 0:246f2fb5be59 421 #endif