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:
1:967235e6fd48
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
smultron1977 0:246f2fb5be59 27 #include "ST7735_TFT.h"
smultron1977 0:246f2fb5be59 28 #include "mbed.h"
smultron1977 0:246f2fb5be59 29
smultron1977 0:246f2fb5be59 30 #define BPP 16 // Bits per pixel
smultron1977 0:246f2fb5be59 31
smultron1977 0:246f2fb5be59 32 ST7735_TFT::ST7735_TFT(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName rs, PinName reset, const char *name)
smultron1977 0:246f2fb5be59 33 : _spi(mosi, miso, sclk), _cs(cs), _rs(rs), _reset(reset),GraphicsDisplay(name) {
suupen 2:9a62e159e30c 34 orientation = 1; //@ss
smultron1977 0:246f2fb5be59 35 tft_reset();
suupen 2:9a62e159e30c 36 //@ss orientation = 2;
smultron1977 0:246f2fb5be59 37 char_x = 0;
smultron1977 0:246f2fb5be59 38 }
smultron1977 0:246f2fb5be59 39
smultron1977 0:246f2fb5be59 40
smultron1977 0:246f2fb5be59 41 int ST7735_TFT::width() {
smultron1977 0:246f2fb5be59 42 if (orientation == 0 || orientation == 2) return 128;
smultron1977 0:246f2fb5be59 43 else return 160;
smultron1977 0:246f2fb5be59 44 }
smultron1977 0:246f2fb5be59 45
smultron1977 0:246f2fb5be59 46
smultron1977 0:246f2fb5be59 47 int ST7735_TFT::height() {
smultron1977 0:246f2fb5be59 48 if (orientation == 0 || orientation == 2) return 160;
smultron1977 0:246f2fb5be59 49 else return 128;
smultron1977 0:246f2fb5be59 50 }
smultron1977 0:246f2fb5be59 51
smultron1977 0:246f2fb5be59 52
smultron1977 0:246f2fb5be59 53 void ST7735_TFT::set_orientation(unsigned int o) {
smultron1977 0:246f2fb5be59 54 orientation = o;
smultron1977 0:246f2fb5be59 55 switch (orientation) {
smultron1977 0:246f2fb5be59 56 case 0:
smultron1977 0:246f2fb5be59 57 wr_reg(ST7735_MADCTL, 0x0008);
smultron1977 0:246f2fb5be59 58 break;
smultron1977 0:246f2fb5be59 59 case 1:
smultron1977 0:246f2fb5be59 60 wr_reg(ST7735_MADCTL, 0x0068);
smultron1977 0:246f2fb5be59 61 break;
smultron1977 0:246f2fb5be59 62 case 2:
smultron1977 0:246f2fb5be59 63 wr_reg(ST7735_MADCTL, 0x00C8);
smultron1977 0:246f2fb5be59 64 break;
smultron1977 0:246f2fb5be59 65 case 3:
smultron1977 0:246f2fb5be59 66 wr_reg(ST7735_MADCTL, 0x00A8);
smultron1977 0:246f2fb5be59 67 break;
smultron1977 0:246f2fb5be59 68 }
smultron1977 0:246f2fb5be59 69 }
smultron1977 0:246f2fb5be59 70
smultron1977 0:246f2fb5be59 71
smultron1977 0:246f2fb5be59 72
smultron1977 0:246f2fb5be59 73 void ST7735_TFT::wr_cmd(int cmd) {
smultron1977 0:246f2fb5be59 74 _rs = 0; // rs low, cs low for transmitting command
smultron1977 0:246f2fb5be59 75 _cs = 0;
smultron1977 0:246f2fb5be59 76 _spi.write(cmd);
smultron1977 0:246f2fb5be59 77 _cs = 1;
smultron1977 0:246f2fb5be59 78 }
smultron1977 0:246f2fb5be59 79
smultron1977 0:246f2fb5be59 80
smultron1977 0:246f2fb5be59 81
smultron1977 0:246f2fb5be59 82 void ST7735_TFT::wr_dat(int dat) {
smultron1977 0:246f2fb5be59 83 _rs = 1; // rs high, cs low for transmitting data
smultron1977 0:246f2fb5be59 84 _cs = 0;
smultron1977 0:246f2fb5be59 85 _spi.write(dat);
smultron1977 0:246f2fb5be59 86 _cs = 1;
smultron1977 0:246f2fb5be59 87 }
smultron1977 0:246f2fb5be59 88
smultron1977 0:246f2fb5be59 89
smultron1977 0:246f2fb5be59 90
smultron1977 0:246f2fb5be59 91 void ST7735_TFT::wr_dat_start(void) {
smultron1977 0:246f2fb5be59 92 _rs = 1; // rs high, cs low for transmitting data
smultron1977 0:246f2fb5be59 93 _cs = 0;
smultron1977 0:246f2fb5be59 94 }
smultron1977 0:246f2fb5be59 95
smultron1977 0:246f2fb5be59 96
smultron1977 0:246f2fb5be59 97
smultron1977 0:246f2fb5be59 98 void ST7735_TFT::wr_dat_stop (void) {
smultron1977 0:246f2fb5be59 99 _cs = 1;
smultron1977 0:246f2fb5be59 100 }
smultron1977 0:246f2fb5be59 101
smultron1977 0:246f2fb5be59 102
smultron1977 0:246f2fb5be59 103
smultron1977 0:246f2fb5be59 104 void ST7735_TFT::wr_dat_only (unsigned short dat) {
smultron1977 0:246f2fb5be59 105 _spi.write(dat);
smultron1977 0:246f2fb5be59 106 }
smultron1977 0:246f2fb5be59 107
smultron1977 0:246f2fb5be59 108
smultron1977 0:246f2fb5be59 109 unsigned short ST7735_TFT::rd_dat (void) {
smultron1977 0:246f2fb5be59 110 unsigned short val = 0;
smultron1977 0:246f2fb5be59 111 _cs = 0;
smultron1977 0:246f2fb5be59 112 _spi.write(0); /* Dummy read 1 */
smultron1977 0:246f2fb5be59 113 val = _spi.write(0); /* Read D8..D15 */
smultron1977 0:246f2fb5be59 114 val <<= 8;
smultron1977 0:246f2fb5be59 115 val |= _spi.write(0); /* Read D0..D7 */
smultron1977 0:246f2fb5be59 116 _cs = 1;
smultron1977 0:246f2fb5be59 117 return (val);
smultron1977 0:246f2fb5be59 118 }
smultron1977 0:246f2fb5be59 119
smultron1977 0:246f2fb5be59 120 void ST7735_TFT::wr_reg (unsigned char reg, unsigned short val) {
smultron1977 0:246f2fb5be59 121
smultron1977 0:246f2fb5be59 122 wr_cmd(reg);
smultron1977 0:246f2fb5be59 123 wr_dat(val);
smultron1977 0:246f2fb5be59 124 }
smultron1977 0:246f2fb5be59 125
smultron1977 0:246f2fb5be59 126
smultron1977 0:246f2fb5be59 127 unsigned short ST7735_TFT::rd_reg (unsigned char reg) {
smultron1977 0:246f2fb5be59 128
smultron1977 0:246f2fb5be59 129 wr_cmd(reg);
smultron1977 0:246f2fb5be59 130 return(rd_dat());
smultron1977 0:246f2fb5be59 131 }
smultron1977 0:246f2fb5be59 132
smultron1977 0:246f2fb5be59 133 void ST7735_TFT::read_area(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *buffer) {
smultron1977 0:246f2fb5be59 134 // BEWARE !
smultron1977 0:246f2fb5be59 135 // DOES NOT WORK CORRECTLY YET !!!
smultron1977 0:246f2fb5be59 136 int val;
smultron1977 0:246f2fb5be59 137 window(x,y,w,h);
smultron1977 0:246f2fb5be59 138 wr_cmd(ST7735_RAMRD); // write to RAM
smultron1977 0:246f2fb5be59 139 _cs = 0;
smultron1977 0:246f2fb5be59 140 _rs = 1;
smultron1977 0:246f2fb5be59 141 _spi.write(0); /* Dummy read 1 */
smultron1977 0:246f2fb5be59 142
smultron1977 0:246f2fb5be59 143 val = _spi.write(0); /* Read D8..D15 */
smultron1977 0:246f2fb5be59 144 val <<= 8;
smultron1977 0:246f2fb5be59 145 val |= _spi.write(0); /* Read D0..D7 */
smultron1977 0:246f2fb5be59 146 _cs = 1;
smultron1977 0:246f2fb5be59 147 }
smultron1977 0:246f2fb5be59 148
smultron1977 0:246f2fb5be59 149 int ST7735_TFT::getpixel(unsigned int x, unsigned int y) {
smultron1977 0:246f2fb5be59 150 // BEWARE !
smultron1977 0:246f2fb5be59 151 // DOES NOT WORK CORRECTLY YET !!!
smultron1977 0:246f2fb5be59 152 int val;
smultron1977 0:246f2fb5be59 153 _spi.format(8,3);
smultron1977 0:246f2fb5be59 154 wr_cmd(ST7735_CASET); // column addr set
smultron1977 0:246f2fb5be59 155 wr_dat(0x00);
smultron1977 0:246f2fb5be59 156 wr_dat(x+2); // XSTART
smultron1977 0:246f2fb5be59 157 wr_dat(0x00);
smultron1977 0:246f2fb5be59 158 wr_dat(x+2+2); // XEND
smultron1977 0:246f2fb5be59 159
smultron1977 0:246f2fb5be59 160 wr_cmd(ST7735_RASET); // row addr set
smultron1977 0:246f2fb5be59 161 wr_dat(0x00);
smultron1977 0:246f2fb5be59 162 wr_dat(y+1); // YSTART
smultron1977 0:246f2fb5be59 163 wr_dat(0x00);
smultron1977 0:246f2fb5be59 164 wr_dat(y+1+1); // YEND
smultron1977 0:246f2fb5be59 165
smultron1977 0:246f2fb5be59 166 _rs = 0; // rs low, cs low for transmitting command
smultron1977 0:246f2fb5be59 167 _cs = 0;
smultron1977 0:246f2fb5be59 168 _spi.write(0x2E);
smultron1977 0:246f2fb5be59 169 _rs = 1;
smultron1977 0:246f2fb5be59 170 _spi.write(0x00); /* Dummy read 1 */
smultron1977 0:246f2fb5be59 171
smultron1977 0:246f2fb5be59 172 val = _spi.write(0x00); /* Read D8..D15 */
smultron1977 0:246f2fb5be59 173 val <<= 8;
smultron1977 0:246f2fb5be59 174 val |= _spi.write(0x00); /* Read D0..D7 */
smultron1977 0:246f2fb5be59 175
smultron1977 0:246f2fb5be59 176 _cs = 1;
smultron1977 0:246f2fb5be59 177 return val;
smultron1977 0:246f2fb5be59 178 }
smultron1977 0:246f2fb5be59 179
smultron1977 0:246f2fb5be59 180
smultron1977 0:246f2fb5be59 181 void ST7735_TFT::tft_reset() {
smultron1977 0:246f2fb5be59 182 static unsigned short driverCode;
smultron1977 0:246f2fb5be59 183
smultron1977 0:246f2fb5be59 184 // init SPI
smultron1977 0:246f2fb5be59 185 _spi.format(8,3); // 8 bit spi mode 3
smultron1977 1:967235e6fd48 186 _spi.frequency(16000000); // 16Mhz SPI clock ... 15Mhz is maximum for display, but it seems to work
smultron1977 0:246f2fb5be59 187
smultron1977 0:246f2fb5be59 188 // reset exactly like in Arduino version
smultron1977 0:246f2fb5be59 189 _cs = 0;
smultron1977 0:246f2fb5be59 190 _reset = 1; // reset
smultron1977 0:246f2fb5be59 191 wait_ms(500);
smultron1977 0:246f2fb5be59 192 _reset = 0; // reset
smultron1977 0:246f2fb5be59 193 wait_ms(500);
smultron1977 0:246f2fb5be59 194 _reset = 1; // reset
smultron1977 0:246f2fb5be59 195 wait_ms(500);
suupen 2:9a62e159e30c 196
suupen 2:9a62e159e30c 197 #ifdef ST7735B //@ss
suupen 2:9a62e159e30c 198 /* Start Initial Sequence ----------------------------------------------------*/
suupen 2:9a62e159e30c 199 wr_cmd(ST7735_SWRESET); /* SW Reset */
suupen 2:9a62e159e30c 200 wait_ms(150);
suupen 2:9a62e159e30c 201 wr_cmd(ST7735_SLPOUT); /* Out of sleepmode */
suupen 2:9a62e159e30c 202 wait_ms(500);
smultron1977 0:246f2fb5be59 203
suupen 2:9a62e159e30c 204 wr_cmd(0x26); // 3: Set default gamma
suupen 2:9a62e159e30c 205 wr_dat(0x04); // 16-bit color
suupen 2:9a62e159e30c 206
suupen 2:9a62e159e30c 207 wr_cmd(ST7735_FRMCTR1); /*4 Frame rate in normal mode */
suupen 2:9a62e159e30c 208 wr_dat(0x0b);
suupen 2:9a62e159e30c 209 wr_dat(0x14);
suupen 2:9a62e159e30c 210
suupen 2:9a62e159e30c 211
suupen 2:9a62e159e30c 212
suupen 2:9a62e159e30c 213
suupen 2:9a62e159e30c 214 wr_cmd(0xc0); // 5 POWER CONTROL 1
suupen 2:9a62e159e30c 215 wr_dat(0x08);
suupen 2:9a62e159e30c 216 wr_dat(0x00);
suupen 2:9a62e159e30c 217
suupen 2:9a62e159e30c 218 wr_cmd(0xc1); // 6 POWER CONTROL 2
suupen 2:9a62e159e30c 219 wr_dat(0x05);
suupen 2:9a62e159e30c 220
suupen 2:9a62e159e30c 221 wr_cmd(0xC5); // POWER CONTROL 6
suupen 2:9a62e159e30c 222 wr_dat(0x41); //
suupen 2:9a62e159e30c 223 wr_dat(0x30);
suupen 2:9a62e159e30c 224
suupen 2:9a62e159e30c 225 wr_cmd(0xc7); // 8:LCD Driving control
suupen 2:9a62e159e30c 226 wr_dat(0xc1);
suupen 2:9a62e159e30c 227
suupen 2:9a62e159e30c 228 wr_cmd(0xec); // 9:Set color format
suupen 2:9a62e159e30c 229 wr_dat(0x1b); // 16-bit color
suupen 2:9a62e159e30c 230
suupen 2:9a62e159e30c 231 wr_cmd(0x3A); // COLOR MODE
suupen 2:9a62e159e30c 232 wr_dat(0x55); //
suupen 2:9a62e159e30c 233 wait_ms(100);
suupen 2:9a62e159e30c 234
suupen 2:9a62e159e30c 235 wr_cmd(0x2a); // 11 COLUMN ADDR SET
suupen 2:9a62e159e30c 236 wr_dat(0x00); //
suupen 2:9a62e159e30c 237 wr_dat(0x00); // xstart = 0
suupen 2:9a62e159e30c 238 wr_dat(0x00); //
suupen 2:9a62e159e30c 239 wr_dat(0x7F); // xend = 127
suupen 2:9a62e159e30c 240
suupen 2:9a62e159e30c 241 wr_cmd(0x2b); // ROW ADDR SET
suupen 2:9a62e159e30c 242 wr_dat(0x00); //
suupen 2:9a62e159e30c 243 wr_dat(0x00); // ystart = 0
suupen 2:9a62e159e30c 244 wr_dat(0x00); //
suupen 2:9a62e159e30c 245 wr_dat(0x9F); // yend = 159
suupen 2:9a62e159e30c 246
suupen 2:9a62e159e30c 247 wr_cmd(0x36); // 13 Set Scanning Direction
suupen 2:9a62e159e30c 248 wr_dat(0xc8);
suupen 2:9a62e159e30c 249
suupen 2:9a62e159e30c 250 wr_cmd(0xb7); // 14 Set Source Output Direction
suupen 2:9a62e159e30c 251 wr_dat(0x00);
suupen 2:9a62e159e30c 252 /* Gamma settings -----------------------------------------------------------*/
suupen 2:9a62e159e30c 253
suupen 2:9a62e159e30c 254 wr_cmd(0xE0); // GMCTRP1
suupen 2:9a62e159e30c 255 wr_dat(0x28);
suupen 2:9a62e159e30c 256 wr_dat(0x24);
suupen 2:9a62e159e30c 257 wr_dat(0x22);
suupen 2:9a62e159e30c 258 wr_dat(0x31);
suupen 2:9a62e159e30c 259
suupen 2:9a62e159e30c 260 wr_dat(0x2b);
suupen 2:9a62e159e30c 261 wr_dat(0x0e);
suupen 2:9a62e159e30c 262 wr_dat(0x53);
suupen 2:9a62e159e30c 263 wr_dat(0xa5);
suupen 2:9a62e159e30c 264
suupen 2:9a62e159e30c 265 wr_dat(0x42);
suupen 2:9a62e159e30c 266 wr_dat(0x16);
suupen 2:9a62e159e30c 267 wr_dat(0x18);
suupen 2:9a62e159e30c 268 wr_dat(0x12);
suupen 2:9a62e159e30c 269
suupen 2:9a62e159e30c 270 wr_dat(0x1a);
suupen 2:9a62e159e30c 271 wr_dat(0x14);
suupen 2:9a62e159e30c 272 wr_dat(0x03);
suupen 2:9a62e159e30c 273
suupen 2:9a62e159e30c 274 wait_ms(50);
suupen 2:9a62e159e30c 275
suupen 2:9a62e159e30c 276 wr_cmd(0xE1); // GMCTRN1
suupen 2:9a62e159e30c 277 wr_dat(0x17);
suupen 2:9a62e159e30c 278 wr_dat(0x1b);
suupen 2:9a62e159e30c 279 wr_dat(0x1d);
suupen 2:9a62e159e30c 280 wr_dat(0x0e);
suupen 2:9a62e159e30c 281
suupen 2:9a62e159e30c 282 wr_dat(0x14);
suupen 2:9a62e159e30c 283 wr_dat(0x11);
suupen 2:9a62e159e30c 284 wr_dat(0x2c);
suupen 2:9a62e159e30c 285 wr_dat(0xa5);
suupen 2:9a62e159e30c 286
suupen 2:9a62e159e30c 287 wr_dat(0x3d);
suupen 2:9a62e159e30c 288 wr_dat(0x09);
suupen 2:9a62e159e30c 289 wr_dat(0x27);
suupen 2:9a62e159e30c 290 wr_dat(0x2d);
suupen 2:9a62e159e30c 291
suupen 2:9a62e159e30c 292 wr_dat(0x25);
suupen 2:9a62e159e30c 293 wr_dat(0x2b);
suupen 2:9a62e159e30c 294 wr_dat(0x3c);
suupen 2:9a62e159e30c 295
suupen 2:9a62e159e30c 296 wait_ms(50);
suupen 2:9a62e159e30c 297
suupen 2:9a62e159e30c 298 wr_cmd(0x13); // 18 Normal display on no args.
suupen 2:9a62e159e30c 299 wait_ms(10);
suupen 2:9a62e159e30c 300
suupen 2:9a62e159e30c 301 wr_cmd(0x29); // 19 Main screen turn on, no args.normal display on
suupen 2:9a62e159e30c 302 wait_ms(500);
suupen 2:9a62e159e30c 303
suupen 2:9a62e159e30c 304 switch (orientation) {
suupen 2:9a62e159e30c 305 case 0:
suupen 2:9a62e159e30c 306 wr_reg(0x36, 0x0008); //originalでの0xc8は間違いで、ST7735_MADCTL(0x36)ではないか
suupen 2:9a62e159e30c 307 break;
suupen 2:9a62e159e30c 308 case 1:
suupen 2:9a62e159e30c 309 wr_reg(0x36, 0x0068);
suupen 2:9a62e159e30c 310 break;
suupen 2:9a62e159e30c 311 case 2:
suupen 2:9a62e159e30c 312 wr_reg(0x36, 0x00C8);
suupen 2:9a62e159e30c 313 break;
suupen 2:9a62e159e30c 314 case 3:
suupen 2:9a62e159e30c 315 wr_reg(0x36, 0x00A8);
suupen 2:9a62e159e30c 316 break;
suupen 2:9a62e159e30c 317 }
suupen 2:9a62e159e30c 318
suupen 2:9a62e159e30c 319 #else //@ss ~ST7735B (original)
suupen 2:9a62e159e30c 320 /* Start Initial Sequence ----------------------------------------------------*/
smultron1977 0:246f2fb5be59 321 wr_cmd(ST7735_SWRESET); /* SW Reset */
smultron1977 0:246f2fb5be59 322 wait_ms(150);
smultron1977 0:246f2fb5be59 323 wr_cmd(ST7735_SLPOUT); /* Out of sleepmode */
smultron1977 0:246f2fb5be59 324 wait_ms(500);
smultron1977 0:246f2fb5be59 325
smultron1977 0:246f2fb5be59 326 wr_cmd(ST7735_FRMCTR1); /* Frame rate in normal mode */
smultron1977 0:246f2fb5be59 327 wr_dat(0x01);
smultron1977 0:246f2fb5be59 328 wr_dat(0x2C);
smultron1977 0:246f2fb5be59 329 wr_dat(0x2D);
smultron1977 0:246f2fb5be59 330
smultron1977 0:246f2fb5be59 331 wr_cmd(ST7735_FRMCTR2); /* Frame rate in idle mode */
smultron1977 0:246f2fb5be59 332 wr_dat(0x01);
smultron1977 0:246f2fb5be59 333 wr_dat(0x2C);
smultron1977 0:246f2fb5be59 334 wr_dat(0x2D);
smultron1977 0:246f2fb5be59 335
smultron1977 0:246f2fb5be59 336 wr_cmd(ST7735_FRMCTR3); /* Frame rate in partial mode */
smultron1977 0:246f2fb5be59 337 wr_dat(0x01);
smultron1977 0:246f2fb5be59 338 wr_dat(0x2C);
smultron1977 0:246f2fb5be59 339 wr_dat(0x2D);
smultron1977 0:246f2fb5be59 340 wr_dat(0x01); // inversion mode settings
smultron1977 0:246f2fb5be59 341 wr_dat(0x2C);
smultron1977 0:246f2fb5be59 342 wr_dat(0x2D);
smultron1977 0:246f2fb5be59 343
smultron1977 0:246f2fb5be59 344 wr_cmd(ST7735_INVCTR); // Inverted mode off
smultron1977 0:246f2fb5be59 345 wr_dat(0x07);
smultron1977 0:246f2fb5be59 346
smultron1977 0:246f2fb5be59 347 wr_cmd(ST7735_PWCTR1); // POWER CONTROL 1
smultron1977 0:246f2fb5be59 348 wr_dat(0xA2);
smultron1977 0:246f2fb5be59 349 wr_dat(0x02); // -4.6V
smultron1977 0:246f2fb5be59 350 wr_dat(0x84); // AUTO mode
smultron1977 0:246f2fb5be59 351
smultron1977 0:246f2fb5be59 352 wr_cmd(ST7735_PWCTR2); // POWER CONTROL 2
smultron1977 0:246f2fb5be59 353 wr_dat(0xC5); // VGH25 = 2.4C VGSEL =-10 VGH = 3*AVDD
smultron1977 0:246f2fb5be59 354
smultron1977 0:246f2fb5be59 355 wr_cmd(ST7735_PWCTR3); // POWER CONTROL 3
smultron1977 0:246f2fb5be59 356 wr_dat(0x0A); // Opamp current small
smultron1977 0:246f2fb5be59 357 wr_dat(0x00); // Boost freq
smultron1977 0:246f2fb5be59 358
smultron1977 0:246f2fb5be59 359 wr_cmd(ST7735_PWCTR4); // POWER CONTROL 4
smultron1977 0:246f2fb5be59 360 wr_dat(0x8A); // BCLK/2, Opamp current small / medium low
smultron1977 0:246f2fb5be59 361 wr_dat(0x2A); //
smultron1977 0:246f2fb5be59 362
smultron1977 0:246f2fb5be59 363 wr_cmd(ST7735_PWCTR5); // POWER CONTROL 5
smultron1977 0:246f2fb5be59 364 wr_dat(0x8A); // BCLK/2, Opamp current small / medium low
smultron1977 0:246f2fb5be59 365 wr_dat(0xEE); //
smultron1977 0:246f2fb5be59 366
smultron1977 0:246f2fb5be59 367 wr_cmd(ST7735_VMCTR1); // POWER CONTROL 6
smultron1977 0:246f2fb5be59 368 wr_dat(0x0E); //
smultron1977 0:246f2fb5be59 369
smultron1977 0:246f2fb5be59 370 wr_cmd(ST7735_INVOFF); // INVOFF
smultron1977 0:246f2fb5be59 371
smultron1977 0:246f2fb5be59 372 wr_cmd(ST7735_MADCTL); // ORIENTATION
smultron1977 0:246f2fb5be59 373 wr_dat(0xC8); //
smultron1977 0:246f2fb5be59 374
smultron1977 0:246f2fb5be59 375 wr_cmd(ST7735_COLMOD); // COLOR MODE
smultron1977 0:246f2fb5be59 376 wr_dat(0x05); //
smultron1977 0:246f2fb5be59 377
smultron1977 0:246f2fb5be59 378 wr_cmd(ST7735_CASET); // COLUMN ADDR SET
smultron1977 0:246f2fb5be59 379 wr_dat(0x00); //
smultron1977 0:246f2fb5be59 380 wr_dat(0x00); // xstart = 0
smultron1977 0:246f2fb5be59 381 wr_dat(0x00); //
smultron1977 0:246f2fb5be59 382 wr_dat(0x7F); // xend = 127
smultron1977 0:246f2fb5be59 383
smultron1977 0:246f2fb5be59 384 wr_cmd(ST7735_RASET); // ROW ADDR SET
smultron1977 0:246f2fb5be59 385 wr_dat(0x00); //
smultron1977 0:246f2fb5be59 386 wr_dat(0x00); // ystart = 0
smultron1977 0:246f2fb5be59 387 wr_dat(0x00); //
smultron1977 0:246f2fb5be59 388 wr_dat(0x9F); // yend = 159
smultron1977 0:246f2fb5be59 389
smultron1977 0:246f2fb5be59 390 /* Gamma settings -----------------------------------------------------------*/
smultron1977 0:246f2fb5be59 391
smultron1977 0:246f2fb5be59 392 wr_cmd(0xE0); // GMCTRP1
smultron1977 0:246f2fb5be59 393 wr_dat(0x02);
smultron1977 0:246f2fb5be59 394 wr_dat(0x1c);
smultron1977 0:246f2fb5be59 395 wr_dat(0x07);
smultron1977 0:246f2fb5be59 396 wr_dat(0x12);
smultron1977 0:246f2fb5be59 397 wr_dat(0x37);
smultron1977 0:246f2fb5be59 398 wr_dat(0x32);
smultron1977 0:246f2fb5be59 399 wr_dat(0x29);
smultron1977 0:246f2fb5be59 400 wr_dat(0x2d);
smultron1977 0:246f2fb5be59 401 wr_dat(0x29);
smultron1977 0:246f2fb5be59 402 wr_dat(0x25);
smultron1977 0:246f2fb5be59 403 wr_dat(0x2B);
smultron1977 0:246f2fb5be59 404 wr_dat(0x39);
smultron1977 0:246f2fb5be59 405 wr_dat(0x00);
smultron1977 0:246f2fb5be59 406 wr_dat(0x01);
smultron1977 0:246f2fb5be59 407 wr_dat(0x03);
smultron1977 0:246f2fb5be59 408 wr_dat(0x10);
smultron1977 0:246f2fb5be59 409 wr_cmd(0xE1); // GMCTRN1
smultron1977 0:246f2fb5be59 410 wr_dat(0x03);
smultron1977 0:246f2fb5be59 411 wr_dat(0x1d);
smultron1977 0:246f2fb5be59 412 wr_dat(0x07);
smultron1977 0:246f2fb5be59 413 wr_dat(0x06);
smultron1977 0:246f2fb5be59 414 wr_dat(0x2E);
smultron1977 0:246f2fb5be59 415 wr_dat(0x2C);
smultron1977 0:246f2fb5be59 416 wr_dat(0x29);
smultron1977 0:246f2fb5be59 417 wr_dat(0x2D);
smultron1977 0:246f2fb5be59 418 wr_dat(0x2E);
smultron1977 0:246f2fb5be59 419 wr_dat(0x2E);
smultron1977 0:246f2fb5be59 420 wr_dat(0x37);
smultron1977 0:246f2fb5be59 421 wr_dat(0x3F);
smultron1977 0:246f2fb5be59 422 wr_dat(0x00);
smultron1977 0:246f2fb5be59 423 wr_dat(0x00);
smultron1977 0:246f2fb5be59 424 wr_dat(0x02);
smultron1977 0:246f2fb5be59 425 wr_dat(0x10);
smultron1977 0:246f2fb5be59 426
smultron1977 0:246f2fb5be59 427 wr_cmd(ST7735_DISPON); // display ON
smultron1977 0:246f2fb5be59 428 wait_ms(100);
smultron1977 0:246f2fb5be59 429
smultron1977 0:246f2fb5be59 430 wr_cmd(ST7735_NORON); // normal display on
smultron1977 0:246f2fb5be59 431 wait_ms(10);
smultron1977 0:246f2fb5be59 432
smultron1977 0:246f2fb5be59 433 switch (orientation) {
smultron1977 0:246f2fb5be59 434 case 0:
smultron1977 0:246f2fb5be59 435 wr_reg(0xC8, 0x0008);
smultron1977 0:246f2fb5be59 436 break;
smultron1977 0:246f2fb5be59 437 case 1:
smultron1977 0:246f2fb5be59 438 wr_reg(0xC8, 0x0068);
smultron1977 0:246f2fb5be59 439 break;
smultron1977 0:246f2fb5be59 440 case 2:
smultron1977 0:246f2fb5be59 441 wr_reg(0xC8, 0x00C8);
smultron1977 0:246f2fb5be59 442 break;
smultron1977 0:246f2fb5be59 443 case 3:
smultron1977 0:246f2fb5be59 444 wr_reg(0xC8, 0x00A8);
smultron1977 0:246f2fb5be59 445 break;
smultron1977 0:246f2fb5be59 446 }
suupen 2:9a62e159e30c 447 #endif // ST7735B
suupen 2:9a62e159e30c 448
smultron1977 0:246f2fb5be59 449 WindowMax ();
smultron1977 0:246f2fb5be59 450 }
smultron1977 0:246f2fb5be59 451
smultron1977 0:246f2fb5be59 452
smultron1977 0:246f2fb5be59 453 void ST7735_TFT::pixel(int x, int y, int color) {
smultron1977 0:246f2fb5be59 454 if ((x >= width()) || (y >= height())) return;
smultron1977 0:246f2fb5be59 455
suupen 2:9a62e159e30c 456 window(x,y,1,1); //@ss
suupen 2:9a62e159e30c 457 //@ss window(x,y,x+1,y+1);
smultron1977 0:246f2fb5be59 458
smultron1977 0:246f2fb5be59 459 // setup for data
smultron1977 0:246f2fb5be59 460 _rs = 1;
smultron1977 0:246f2fb5be59 461 _cs = 0;
smultron1977 0:246f2fb5be59 462 _spi.format(16,3);
smultron1977 0:246f2fb5be59 463 _spi.write(color);
smultron1977 0:246f2fb5be59 464 _cs = 1;
smultron1977 0:246f2fb5be59 465 _spi.format(8,3);
smultron1977 0:246f2fb5be59 466 }
smultron1977 0:246f2fb5be59 467
smultron1977 0:246f2fb5be59 468 void ST7735_TFT::window (unsigned int x, unsigned int y, unsigned int w, unsigned int h) {
suupen 2:9a62e159e30c 469 //@ss なぜか CASET(X軸)で2, RASET(Y軸)で1 が最小値になっており、これがLCDDisplayのx,y原点になっていない(LCDの原点を設定するためには(-2,-1)を指示する必要がある
suupen 2:9a62e159e30c 470 //@ss プログラム上はLCD原点を0,0にするために、この関数でoffsetをかける。
suupen 2:9a62e159e30c 471 x-=2; //@ss
suupen 2:9a62e159e30c 472 y-=1; //@ss
smultron1977 0:246f2fb5be59 473 wr_cmd(ST7735_CASET); // column addr set
smultron1977 0:246f2fb5be59 474 wr_dat(0x00);
suupen 2:9a62e159e30c 475
smultron1977 0:246f2fb5be59 476 wr_dat(x+2); // XSTART
smultron1977 0:246f2fb5be59 477 wr_dat(0x00);
smultron1977 0:246f2fb5be59 478 wr_dat(x+w+1); // XEND
smultron1977 0:246f2fb5be59 479
smultron1977 0:246f2fb5be59 480 wr_cmd(ST7735_RASET); // row addr set
smultron1977 0:246f2fb5be59 481 wr_dat(0x00);
suupen 2:9a62e159e30c 482 wr_dat(y+1); // YSTART
smultron1977 0:246f2fb5be59 483 wr_dat(0x00);
smultron1977 0:246f2fb5be59 484 wr_dat(y+h+1); // YEND
smultron1977 0:246f2fb5be59 485
smultron1977 0:246f2fb5be59 486 wr_cmd(ST7735_RAMWR); // write to RAM
smultron1977 0:246f2fb5be59 487 }
smultron1977 0:246f2fb5be59 488
smultron1977 0:246f2fb5be59 489
smultron1977 0:246f2fb5be59 490 void ST7735_TFT::WindowMax (void) {
smultron1977 0:246f2fb5be59 491 window (0, 0, width(), height());
smultron1977 0:246f2fb5be59 492 }
smultron1977 0:246f2fb5be59 493
smultron1977 0:246f2fb5be59 494
smultron1977 0:246f2fb5be59 495 void ST7735_TFT::cls (void) {
smultron1977 0:246f2fb5be59 496 unsigned int i;
smultron1977 0:246f2fb5be59 497 WindowMax();
smultron1977 0:246f2fb5be59 498 wr_dat_start();
smultron1977 0:246f2fb5be59 499 _spi.format(16,3);
smultron1977 0:246f2fb5be59 500 for (i = 0; i < ( (width()+1) * (height()+3)); i++) {
smultron1977 0:246f2fb5be59 501 _spi.write(_background);
smultron1977 0:246f2fb5be59 502 }
smultron1977 0:246f2fb5be59 503 _spi.format(8,3);
smultron1977 0:246f2fb5be59 504 wr_dat_stop();
smultron1977 0:246f2fb5be59 505 }
smultron1977 0:246f2fb5be59 506
smultron1977 0:246f2fb5be59 507
smultron1977 0:246f2fb5be59 508 void ST7735_TFT::circle(int x0, int y0, int r, int color) {
smultron1977 0:246f2fb5be59 509
smultron1977 0:246f2fb5be59 510 int draw_x0, draw_y0;
smultron1977 0:246f2fb5be59 511 int draw_x1, draw_y1;
smultron1977 0:246f2fb5be59 512 int draw_x2, draw_y2;
smultron1977 0:246f2fb5be59 513 int draw_x3, draw_y3;
smultron1977 0:246f2fb5be59 514 int draw_x4, draw_y4;
smultron1977 0:246f2fb5be59 515 int draw_x5, draw_y5;
smultron1977 0:246f2fb5be59 516 int draw_x6, draw_y6;
smultron1977 0:246f2fb5be59 517 int draw_x7, draw_y7;
smultron1977 0:246f2fb5be59 518 int xx, yy;
smultron1977 0:246f2fb5be59 519 int di;
smultron1977 0:246f2fb5be59 520 WindowMax();
smultron1977 0:246f2fb5be59 521 if (r == 0) { /* no radius */
smultron1977 0:246f2fb5be59 522 return;
smultron1977 0:246f2fb5be59 523 }
smultron1977 0:246f2fb5be59 524
smultron1977 0:246f2fb5be59 525 draw_x0 = draw_x1 = x0;
smultron1977 0:246f2fb5be59 526 draw_y0 = draw_y1 = y0 + r;
smultron1977 0:246f2fb5be59 527 if (draw_y0 < height()) {
smultron1977 0:246f2fb5be59 528 pixel(draw_x0, draw_y0, color); /* 90 degree */
smultron1977 0:246f2fb5be59 529 }
smultron1977 0:246f2fb5be59 530
smultron1977 0:246f2fb5be59 531 draw_x2 = draw_x3 = x0;
smultron1977 0:246f2fb5be59 532 draw_y2 = draw_y3 = y0 - r;
smultron1977 0:246f2fb5be59 533 if (draw_y2 >= 0) {
smultron1977 0:246f2fb5be59 534 pixel(draw_x2, draw_y2, color); /* 270 degree */
smultron1977 0:246f2fb5be59 535 }
smultron1977 0:246f2fb5be59 536
smultron1977 0:246f2fb5be59 537 draw_x4 = draw_x6 = x0 + r;
smultron1977 0:246f2fb5be59 538 draw_y4 = draw_y6 = y0;
smultron1977 0:246f2fb5be59 539 if (draw_x4 < width()) {
smultron1977 0:246f2fb5be59 540 pixel(draw_x4, draw_y4, color); /* 0 degree */
smultron1977 0:246f2fb5be59 541 }
smultron1977 0:246f2fb5be59 542
smultron1977 0:246f2fb5be59 543 draw_x5 = draw_x7 = x0 - r;
smultron1977 0:246f2fb5be59 544 draw_y5 = draw_y7 = y0;
smultron1977 0:246f2fb5be59 545 if (draw_x5>=0) {
smultron1977 0:246f2fb5be59 546 pixel(draw_x5, draw_y5, color); /* 180 degree */
smultron1977 0:246f2fb5be59 547 }
smultron1977 0:246f2fb5be59 548
smultron1977 0:246f2fb5be59 549 if (r == 1) {
smultron1977 0:246f2fb5be59 550 return;
smultron1977 0:246f2fb5be59 551 }
smultron1977 0:246f2fb5be59 552
smultron1977 0:246f2fb5be59 553 di = 3 - 2*r;
smultron1977 0:246f2fb5be59 554 xx = 0;
smultron1977 0:246f2fb5be59 555 yy = r;
smultron1977 0:246f2fb5be59 556 while (xx < yy) {
smultron1977 0:246f2fb5be59 557
smultron1977 0:246f2fb5be59 558 if (di < 0) {
smultron1977 0:246f2fb5be59 559 di += 4*xx + 6;
smultron1977 0:246f2fb5be59 560 } else {
smultron1977 0:246f2fb5be59 561 di += 4*(xx - yy) + 10;
smultron1977 0:246f2fb5be59 562 yy--;
smultron1977 0:246f2fb5be59 563 draw_y0--;
smultron1977 0:246f2fb5be59 564 draw_y1--;
smultron1977 0:246f2fb5be59 565 draw_y2++;
smultron1977 0:246f2fb5be59 566 draw_y3++;
smultron1977 0:246f2fb5be59 567 draw_x4--;
smultron1977 0:246f2fb5be59 568 draw_x5++;
smultron1977 0:246f2fb5be59 569 draw_x6--;
smultron1977 0:246f2fb5be59 570 draw_x7++;
smultron1977 0:246f2fb5be59 571 }
smultron1977 0:246f2fb5be59 572 xx++;
smultron1977 0:246f2fb5be59 573 draw_x0++;
smultron1977 0:246f2fb5be59 574 draw_x1--;
smultron1977 0:246f2fb5be59 575 draw_x2++;
smultron1977 0:246f2fb5be59 576 draw_x3--;
smultron1977 0:246f2fb5be59 577 draw_y4++;
smultron1977 0:246f2fb5be59 578 draw_y5++;
smultron1977 0:246f2fb5be59 579 draw_y6--;
smultron1977 0:246f2fb5be59 580 draw_y7--;
smultron1977 0:246f2fb5be59 581
smultron1977 0:246f2fb5be59 582 if ( (draw_x0 <= width()) && (draw_y0>=0) ) {
smultron1977 0:246f2fb5be59 583 pixel(draw_x0, draw_y0, color);
smultron1977 0:246f2fb5be59 584 }
smultron1977 0:246f2fb5be59 585
smultron1977 0:246f2fb5be59 586 if ( (draw_x1 >= 0) && (draw_y1 >= 0) ) {
smultron1977 0:246f2fb5be59 587 pixel(draw_x1, draw_y1, color);
smultron1977 0:246f2fb5be59 588 }
smultron1977 0:246f2fb5be59 589
smultron1977 0:246f2fb5be59 590 if ( (draw_x2 <= width()) && (draw_y2 <= height()) ) {
smultron1977 0:246f2fb5be59 591 pixel(draw_x2, draw_y2, color);
smultron1977 0:246f2fb5be59 592 }
smultron1977 0:246f2fb5be59 593
smultron1977 0:246f2fb5be59 594 if ( (draw_x3 >=0 ) && (draw_y3 <= height()) ) {
smultron1977 0:246f2fb5be59 595 pixel(draw_x3, draw_y3, color);
smultron1977 0:246f2fb5be59 596 }
smultron1977 0:246f2fb5be59 597
smultron1977 0:246f2fb5be59 598 if ( (draw_x4 <= width()) && (draw_y4 >= 0) ) {
smultron1977 0:246f2fb5be59 599 pixel(draw_x4, draw_y4, color);
smultron1977 0:246f2fb5be59 600 }
smultron1977 0:246f2fb5be59 601
smultron1977 0:246f2fb5be59 602 if ( (draw_x5 >= 0) && (draw_y5 >= 0) ) {
smultron1977 0:246f2fb5be59 603 pixel(draw_x5, draw_y5, color);
smultron1977 0:246f2fb5be59 604 }
smultron1977 0:246f2fb5be59 605 if ( (draw_x6 <=width()) && (draw_y6 <= height()) ) {
smultron1977 0:246f2fb5be59 606 pixel(draw_x6, draw_y6, color);
smultron1977 0:246f2fb5be59 607 }
smultron1977 0:246f2fb5be59 608 if ( (draw_x7 >= 0) && (draw_y7 <= height()) ) {
smultron1977 0:246f2fb5be59 609 pixel(draw_x7, draw_y7, color);
smultron1977 0:246f2fb5be59 610 }
smultron1977 0:246f2fb5be59 611 }
smultron1977 0:246f2fb5be59 612 return;
smultron1977 0:246f2fb5be59 613 }
smultron1977 0:246f2fb5be59 614
smultron1977 0:246f2fb5be59 615 void ST7735_TFT::fillcircle(int x, int y, int r, int color) {
smultron1977 0:246f2fb5be59 616 int i;
smultron1977 0:246f2fb5be59 617 for (i = 0; i <= r; i++)
smultron1977 0:246f2fb5be59 618 circle(x,y,i,color);
smultron1977 0:246f2fb5be59 619 }
smultron1977 0:246f2fb5be59 620
smultron1977 0:246f2fb5be59 621
smultron1977 0:246f2fb5be59 622
smultron1977 0:246f2fb5be59 623 void ST7735_TFT::hline(int x0, int x1, int y, int color) {
smultron1977 0:246f2fb5be59 624 int w;
smultron1977 0:246f2fb5be59 625 w = x1 - x0 + 1;
smultron1977 0:246f2fb5be59 626 window(x0,y,w,1);
smultron1977 0:246f2fb5be59 627 wr_cmd(0x2C);
smultron1977 0:246f2fb5be59 628 wr_dat_start();
smultron1977 0:246f2fb5be59 629 for (int x=0; x<w; x++) {
smultron1977 0:246f2fb5be59 630 _spi.write(color);
smultron1977 0:246f2fb5be59 631 _spi.write(color >> 8);
smultron1977 0:246f2fb5be59 632 }
smultron1977 0:246f2fb5be59 633 wr_dat_stop();
smultron1977 0:246f2fb5be59 634 return;
smultron1977 0:246f2fb5be59 635 }
smultron1977 0:246f2fb5be59 636
smultron1977 0:246f2fb5be59 637
smultron1977 0:246f2fb5be59 638
smultron1977 0:246f2fb5be59 639 void ST7735_TFT::vline(int x, int y0, int y1, int color) {
smultron1977 0:246f2fb5be59 640 int h;
smultron1977 0:246f2fb5be59 641 h = y1 - y0 + 1;
smultron1977 0:246f2fb5be59 642 window(x,y0,1,h);
smultron1977 0:246f2fb5be59 643 wr_cmd(0x2C);
smultron1977 0:246f2fb5be59 644 wr_dat_start();
smultron1977 0:246f2fb5be59 645 for (int y=0; y<h; y++) {
smultron1977 0:246f2fb5be59 646 _spi.write(color);
smultron1977 0:246f2fb5be59 647 _spi.write(color >> 8);
smultron1977 0:246f2fb5be59 648 }
smultron1977 0:246f2fb5be59 649 wr_dat_stop();
smultron1977 0:246f2fb5be59 650 return;
smultron1977 0:246f2fb5be59 651 }
smultron1977 0:246f2fb5be59 652
smultron1977 0:246f2fb5be59 653
smultron1977 0:246f2fb5be59 654
smultron1977 0:246f2fb5be59 655 void ST7735_TFT::line(int x0, int y0, int x1, int y1, int color) {
smultron1977 0:246f2fb5be59 656 WindowMax();
smultron1977 0:246f2fb5be59 657 int dx = 0, dy = 0;
smultron1977 0:246f2fb5be59 658 int dx_sym = 0, dy_sym = 0;
smultron1977 0:246f2fb5be59 659 int dx_x2 = 0, dy_x2 = 0;
smultron1977 0:246f2fb5be59 660 int di = 0;
smultron1977 0:246f2fb5be59 661
smultron1977 0:246f2fb5be59 662 dx = x1-x0;
smultron1977 0:246f2fb5be59 663 dy = y1-y0;
smultron1977 0:246f2fb5be59 664
smultron1977 0:246f2fb5be59 665 if (dx == 0) { /* vertical line */
smultron1977 0:246f2fb5be59 666 if (y1 > y0) vline(x0,y0,y1,color);
smultron1977 0:246f2fb5be59 667 else vline(x0,y1,y0,color);
smultron1977 0:246f2fb5be59 668 return;
smultron1977 0:246f2fb5be59 669 }
smultron1977 0:246f2fb5be59 670
smultron1977 0:246f2fb5be59 671 if (dx > 0) {
smultron1977 0:246f2fb5be59 672 dx_sym = 1;
smultron1977 0:246f2fb5be59 673 } else {
smultron1977 0:246f2fb5be59 674 dx_sym = -1;
smultron1977 0:246f2fb5be59 675 }
smultron1977 0:246f2fb5be59 676 if (dy == 0) { /* horizontal line */
smultron1977 0:246f2fb5be59 677 if (x1 > x0) hline(x0,x1,y0,color);
smultron1977 0:246f2fb5be59 678 else hline(x1,x0,y0,color);
smultron1977 0:246f2fb5be59 679 return;
smultron1977 0:246f2fb5be59 680 }
smultron1977 0:246f2fb5be59 681
smultron1977 0:246f2fb5be59 682 if (dy > 0) {
smultron1977 0:246f2fb5be59 683 dy_sym = 1;
smultron1977 0:246f2fb5be59 684 } else {
smultron1977 0:246f2fb5be59 685 dy_sym = -1;
smultron1977 0:246f2fb5be59 686 }
smultron1977 0:246f2fb5be59 687
smultron1977 0:246f2fb5be59 688 dx = dx_sym*dx;
smultron1977 0:246f2fb5be59 689 dy = dy_sym*dy;
smultron1977 0:246f2fb5be59 690
smultron1977 0:246f2fb5be59 691 dx_x2 = dx*2;
smultron1977 0:246f2fb5be59 692 dy_x2 = dy*2;
smultron1977 0:246f2fb5be59 693
smultron1977 0:246f2fb5be59 694 if (dx >= dy) {
smultron1977 0:246f2fb5be59 695 di = dy_x2 - dx;
smultron1977 0:246f2fb5be59 696 while (x0 != x1) {
smultron1977 0:246f2fb5be59 697
smultron1977 0:246f2fb5be59 698 pixel(x0, y0, color);
smultron1977 0:246f2fb5be59 699 x0 += dx_sym;
smultron1977 0:246f2fb5be59 700 if (di<0) {
smultron1977 0:246f2fb5be59 701 di += dy_x2;
smultron1977 0:246f2fb5be59 702 } else {
smultron1977 0:246f2fb5be59 703 di += dy_x2 - dx_x2;
smultron1977 0:246f2fb5be59 704 y0 += dy_sym;
smultron1977 0:246f2fb5be59 705 }
smultron1977 0:246f2fb5be59 706 }
smultron1977 0:246f2fb5be59 707 pixel(x0, y0, color);
smultron1977 0:246f2fb5be59 708 } else {
smultron1977 0:246f2fb5be59 709 di = dx_x2 - dy;
smultron1977 0:246f2fb5be59 710 while (y0 != y1) {
smultron1977 0:246f2fb5be59 711 pixel(x0, y0, color);
smultron1977 0:246f2fb5be59 712 y0 += dy_sym;
smultron1977 0:246f2fb5be59 713 if (di < 0) {
smultron1977 0:246f2fb5be59 714 di += dx_x2;
smultron1977 0:246f2fb5be59 715 } else {
smultron1977 0:246f2fb5be59 716 di += dx_x2 - dy_x2;
smultron1977 0:246f2fb5be59 717 x0 += dx_sym;
smultron1977 0:246f2fb5be59 718 }
smultron1977 0:246f2fb5be59 719 }
smultron1977 0:246f2fb5be59 720 pixel(x0, y0, color);
smultron1977 0:246f2fb5be59 721 }
smultron1977 0:246f2fb5be59 722 return;
smultron1977 0:246f2fb5be59 723 }
smultron1977 0:246f2fb5be59 724
smultron1977 0:246f2fb5be59 725
smultron1977 0:246f2fb5be59 726
smultron1977 0:246f2fb5be59 727
smultron1977 0:246f2fb5be59 728 void ST7735_TFT::rect(int x0, int y0, int x1, int y1, int color) {
smultron1977 0:246f2fb5be59 729
smultron1977 0:246f2fb5be59 730 if (x1 > x0) hline(x0,x1,y0,color);
smultron1977 0:246f2fb5be59 731 else hline(x1,x0,y0,color);
smultron1977 0:246f2fb5be59 732
smultron1977 0:246f2fb5be59 733 if (y1 > y0) vline(x0,y0,y1,color);
smultron1977 0:246f2fb5be59 734 else vline(x0,y1,y0,color);
smultron1977 0:246f2fb5be59 735
smultron1977 0:246f2fb5be59 736 if (x1 > x0) hline(x0,x1,y1,color);
smultron1977 0:246f2fb5be59 737 else hline(x1,x0,y1,color);
smultron1977 0:246f2fb5be59 738
smultron1977 0:246f2fb5be59 739 if (y1 > y0) vline(x1,y0,y1,color);
smultron1977 0:246f2fb5be59 740 else vline(x1,y1,y0,color);
smultron1977 0:246f2fb5be59 741
smultron1977 0:246f2fb5be59 742 return;
smultron1977 0:246f2fb5be59 743 }
smultron1977 0:246f2fb5be59 744
smultron1977 0:246f2fb5be59 745
smultron1977 0:246f2fb5be59 746
smultron1977 0:246f2fb5be59 747 void ST7735_TFT::fillrect(int x0, int y0, int x1, int y1, int color) {
smultron1977 0:246f2fb5be59 748
smultron1977 0:246f2fb5be59 749 int h = y1 - y0 + 1;
smultron1977 0:246f2fb5be59 750 int w = x1 - x0 + 1;
smultron1977 0:246f2fb5be59 751 int pixel = h * w;
smultron1977 0:246f2fb5be59 752 window(x0,y0,w,h);
smultron1977 0:246f2fb5be59 753 wr_cmd(0x2C);
smultron1977 0:246f2fb5be59 754 wr_dat_start();
smultron1977 0:246f2fb5be59 755 for (int p=0; p<pixel; p++) {
smultron1977 0:246f2fb5be59 756 _spi.write(color);
smultron1977 0:246f2fb5be59 757 _spi.write(color >> 8);
smultron1977 0:246f2fb5be59 758 }
smultron1977 0:246f2fb5be59 759 wr_dat_stop();
smultron1977 0:246f2fb5be59 760 return;
smultron1977 0:246f2fb5be59 761 }
smultron1977 0:246f2fb5be59 762
smultron1977 0:246f2fb5be59 763
smultron1977 0:246f2fb5be59 764
smultron1977 0:246f2fb5be59 765 void ST7735_TFT::locate(int x, int y) {
smultron1977 0:246f2fb5be59 766 char_x = x;
smultron1977 0:246f2fb5be59 767 char_y = y;
smultron1977 0:246f2fb5be59 768 }
smultron1977 0:246f2fb5be59 769
smultron1977 0:246f2fb5be59 770
smultron1977 0:246f2fb5be59 771
smultron1977 0:246f2fb5be59 772 int ST7735_TFT::columns() {
smultron1977 0:246f2fb5be59 773 return width() / font[1];
smultron1977 0:246f2fb5be59 774 }
smultron1977 0:246f2fb5be59 775
smultron1977 0:246f2fb5be59 776
smultron1977 0:246f2fb5be59 777
smultron1977 0:246f2fb5be59 778 int ST7735_TFT::rows() {
smultron1977 0:246f2fb5be59 779 return height() / font[2];
smultron1977 0:246f2fb5be59 780 }
smultron1977 0:246f2fb5be59 781
smultron1977 0:246f2fb5be59 782
smultron1977 0:246f2fb5be59 783
smultron1977 0:246f2fb5be59 784 int ST7735_TFT::_putc(int value) {
smultron1977 0:246f2fb5be59 785 if (value == '\n') { // new line
smultron1977 0:246f2fb5be59 786 char_x = 0;
smultron1977 0:246f2fb5be59 787 char_y = char_y + font[2];
smultron1977 0:246f2fb5be59 788 if (char_y >= height() - font[2]) {
smultron1977 0:246f2fb5be59 789 char_y = 0;
smultron1977 0:246f2fb5be59 790 }
smultron1977 0:246f2fb5be59 791 } else {
smultron1977 0:246f2fb5be59 792 character(char_x, char_y, value);
smultron1977 0:246f2fb5be59 793 }
smultron1977 0:246f2fb5be59 794 return value;
smultron1977 0:246f2fb5be59 795 }
smultron1977 0:246f2fb5be59 796
smultron1977 0:246f2fb5be59 797
smultron1977 0:246f2fb5be59 798
smultron1977 0:246f2fb5be59 799
smultron1977 0:246f2fb5be59 800 void ST7735_TFT::character(int x, int y, int c) {
smultron1977 0:246f2fb5be59 801 unsigned int hor,vert,offset,bpl,j,i,b;
smultron1977 0:246f2fb5be59 802 unsigned char* zeichen;
smultron1977 0:246f2fb5be59 803 unsigned char z,w;
smultron1977 0:246f2fb5be59 804
smultron1977 0:246f2fb5be59 805 if ((c < 31) || (c > 127)) return; // test char range
smultron1977 0:246f2fb5be59 806
smultron1977 0:246f2fb5be59 807 // read font parameter from start of array
smultron1977 0:246f2fb5be59 808 offset = font[0]; // bytes / char
smultron1977 0:246f2fb5be59 809 hor = font[1]; // get hor size of font
smultron1977 0:246f2fb5be59 810 vert = font[2]; // get vert size of font
smultron1977 0:246f2fb5be59 811 bpl = font[3]; // bytes per line
smultron1977 0:246f2fb5be59 812
smultron1977 0:246f2fb5be59 813 if (char_x + hor > width()) {
smultron1977 0:246f2fb5be59 814 char_x = 0;
smultron1977 0:246f2fb5be59 815 char_y = char_y + vert;
smultron1977 0:246f2fb5be59 816 if (char_y >= height() - font[2]) {
smultron1977 0:246f2fb5be59 817 char_y = 0;
smultron1977 0:246f2fb5be59 818 }
smultron1977 0:246f2fb5be59 819 }
smultron1977 0:246f2fb5be59 820
smultron1977 0:246f2fb5be59 821 window(char_x, char_y,hor,vert); // char box
smultron1977 0:246f2fb5be59 822 wr_cmd(0x2C);
smultron1977 0:246f2fb5be59 823 wr_dat_start();
smultron1977 0:246f2fb5be59 824 zeichen = &font[((c -32) * offset) + 4]; // start of char bitmap
smultron1977 0:246f2fb5be59 825 w = zeichen[0]; // width of actual char
smultron1977 0:246f2fb5be59 826 _spi.format(16,3); // pixel are 16 bit
smultron1977 0:246f2fb5be59 827
smultron1977 0:246f2fb5be59 828 for (j=0; j<vert; j++) { // vert line
smultron1977 0:246f2fb5be59 829 for (i=0; i<hor; i++) { // horz line
smultron1977 0:246f2fb5be59 830 z = zeichen[bpl * i + ((j & 0xF8) >> 3)+1];
smultron1977 0:246f2fb5be59 831 b = 1 << (j & 0x07);
smultron1977 0:246f2fb5be59 832 if (( z & b ) == 0x00) {
smultron1977 0:246f2fb5be59 833 _spi.write(_background);
smultron1977 0:246f2fb5be59 834 } else {
smultron1977 0:246f2fb5be59 835 _spi.write(_foreground);
smultron1977 0:246f2fb5be59 836 }
smultron1977 0:246f2fb5be59 837 }
smultron1977 0:246f2fb5be59 838 }
smultron1977 0:246f2fb5be59 839 _spi.format(8,3); // 8 bit
smultron1977 0:246f2fb5be59 840 wr_dat_stop();
smultron1977 0:246f2fb5be59 841 if ((w + 2) < hor) { // x offset to next char
smultron1977 0:246f2fb5be59 842 char_x += w + 2;
smultron1977 0:246f2fb5be59 843 } else char_x += hor;
smultron1977 0:246f2fb5be59 844 }
smultron1977 0:246f2fb5be59 845
smultron1977 0:246f2fb5be59 846
smultron1977 0:246f2fb5be59 847
smultron1977 0:246f2fb5be59 848
smultron1977 0:246f2fb5be59 849
smultron1977 0:246f2fb5be59 850 void ST7735_TFT::set_font(unsigned char* f) {
smultron1977 0:246f2fb5be59 851 font = f;
smultron1977 0:246f2fb5be59 852 }
smultron1977 0:246f2fb5be59 853
smultron1977 0:246f2fb5be59 854
smultron1977 0:246f2fb5be59 855
smultron1977 0:246f2fb5be59 856 void ST7735_TFT::Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap) {
smultron1977 0:246f2fb5be59 857 unsigned int i,j,value;
smultron1977 0:246f2fb5be59 858 unsigned short *bitmap_ptr = (unsigned short *)bitmap;
smultron1977 0:246f2fb5be59 859 window(x, y, w, h);
smultron1977 0:246f2fb5be59 860 wr_cmd(0x2C);
smultron1977 0:246f2fb5be59 861 wr_dat_start();
smultron1977 0:246f2fb5be59 862 for (j = 0; j < h; j++) { //Lines
smultron1977 0:246f2fb5be59 863 for (i = 0; i < w; i++) { // copy pixel data to TFT
smultron1977 0:246f2fb5be59 864 _spi.write(*bitmap_ptr); // one line
smultron1977 0:246f2fb5be59 865 _spi.write(*bitmap_ptr >> 8);
smultron1977 0:246f2fb5be59 866 bitmap_ptr++;
smultron1977 0:246f2fb5be59 867 }
smultron1977 0:246f2fb5be59 868 }
smultron1977 0:246f2fb5be59 869 wr_dat_stop();
smultron1977 0:246f2fb5be59 870 }
smultron1977 0:246f2fb5be59 871
smultron1977 0:246f2fb5be59 872
smultron1977 0:246f2fb5be59 873 int ST7735_TFT::BMP_16(unsigned int x, unsigned int y, const char *Name_BMP) {
smultron1977 0:246f2fb5be59 874 // BEWARE !
smultron1977 0:246f2fb5be59 875 // NOT TESTED
smultron1977 0:246f2fb5be59 876 #define OffsetPixelWidth 18
smultron1977 0:246f2fb5be59 877 #define OffsetPixelHeigh 22
smultron1977 0:246f2fb5be59 878 #define OffsetFileSize 34
smultron1977 0:246f2fb5be59 879 #define OffsetPixData 10
smultron1977 0:246f2fb5be59 880 #define OffsetBPP 28
smultron1977 0:246f2fb5be59 881
smultron1977 0:246f2fb5be59 882 char filename[50];
smultron1977 0:246f2fb5be59 883 unsigned char BMP_Header[54];
smultron1977 0:246f2fb5be59 884 unsigned short BPP_t;
smultron1977 0:246f2fb5be59 885 unsigned int PixelWidth,PixelHeigh,start_data;
smultron1977 0:246f2fb5be59 886 unsigned int i,off;
smultron1977 0:246f2fb5be59 887 int padd,j;
smultron1977 0:246f2fb5be59 888 unsigned short *line;
smultron1977 0:246f2fb5be59 889
smultron1977 0:246f2fb5be59 890 // get the filename
smultron1977 0:246f2fb5be59 891 LocalFileSystem local("local");
smultron1977 0:246f2fb5be59 892 sprintf(&filename[0],"/local/");
smultron1977 0:246f2fb5be59 893 i=7;
smultron1977 0:246f2fb5be59 894 while (*Name_BMP!='\0') {
smultron1977 0:246f2fb5be59 895 filename[i++]=*Name_BMP++;
smultron1977 0:246f2fb5be59 896 }
smultron1977 0:246f2fb5be59 897 FILE *Image = fopen((const char *)&filename[0], "r"); // open the bmp file
smultron1977 0:246f2fb5be59 898 if (!Image) {
smultron1977 0:246f2fb5be59 899 return(0); // error file not found !
smultron1977 0:246f2fb5be59 900 }
smultron1977 0:246f2fb5be59 901
smultron1977 0:246f2fb5be59 902 fread(&BMP_Header[0],1,54,Image); // get the BMP Header
smultron1977 0:246f2fb5be59 903
smultron1977 0:246f2fb5be59 904 if (BMP_Header[0] != 0x42 || BMP_Header[1] != 0x4D) { // check magic byte
smultron1977 0:246f2fb5be59 905 fclose(Image);
smultron1977 0:246f2fb5be59 906 return(-1); // error no BMP file
smultron1977 0:246f2fb5be59 907 }
smultron1977 0:246f2fb5be59 908
smultron1977 0:246f2fb5be59 909 BPP_t = BMP_Header[OffsetBPP] + (BMP_Header[OffsetBPP + 1] << 8);
smultron1977 0:246f2fb5be59 910 if (BPP_t != 0x0010) {
smultron1977 0:246f2fb5be59 911 fclose(Image);
smultron1977 0:246f2fb5be59 912 return(-2); // error no 16 bit BMP
smultron1977 0:246f2fb5be59 913 }
smultron1977 0:246f2fb5be59 914
smultron1977 0:246f2fb5be59 915 PixelHeigh = BMP_Header[OffsetPixelHeigh] + (BMP_Header[OffsetPixelHeigh + 1] << 8) + (BMP_Header[OffsetPixelHeigh + 2] << 16) + (BMP_Header[OffsetPixelHeigh + 3] << 24);
smultron1977 0:246f2fb5be59 916 PixelWidth = BMP_Header[OffsetPixelWidth] + (BMP_Header[OffsetPixelWidth + 1] << 8) + (BMP_Header[OffsetPixelWidth + 2] << 16) + (BMP_Header[OffsetPixelWidth + 3] << 24);
smultron1977 0:246f2fb5be59 917 if (PixelHeigh > height() + y || PixelWidth > width() + x) {
smultron1977 0:246f2fb5be59 918 fclose(Image);
smultron1977 0:246f2fb5be59 919 return(-3); // to big
smultron1977 0:246f2fb5be59 920 }
smultron1977 0:246f2fb5be59 921
smultron1977 0:246f2fb5be59 922 start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24);
smultron1977 0:246f2fb5be59 923
smultron1977 0:246f2fb5be59 924 line = (unsigned short *) malloc (PixelWidth); // we need a buffer for a line
smultron1977 0:246f2fb5be59 925 if (line == NULL) {
smultron1977 0:246f2fb5be59 926 return(-4); // error no memory
smultron1977 0:246f2fb5be59 927 }
smultron1977 0:246f2fb5be59 928
smultron1977 0:246f2fb5be59 929 // the lines are padded to multiple of 4 bytes
smultron1977 0:246f2fb5be59 930 padd = -1;
smultron1977 0:246f2fb5be59 931 do {
smultron1977 0:246f2fb5be59 932 padd ++;
smultron1977 0:246f2fb5be59 933 } while ((PixelWidth * 2 + padd)%4 != 0);
smultron1977 0:246f2fb5be59 934
smultron1977 0:246f2fb5be59 935 window(x, y,PixelWidth,PixelHeigh);
smultron1977 0:246f2fb5be59 936 wr_cmd(0x2C);
smultron1977 0:246f2fb5be59 937 wr_dat_start();
smultron1977 0:246f2fb5be59 938 _spi.format(16,3);
smultron1977 0:246f2fb5be59 939 for (j = PixelHeigh - 1; j >= 0; j--) { //Lines bottom up
smultron1977 0:246f2fb5be59 940 off = j * (PixelWidth * 2 + padd) + start_data; // start of line
smultron1977 0:246f2fb5be59 941 fseek(Image, off ,SEEK_SET);
smultron1977 0:246f2fb5be59 942 fread(line,1,PixelWidth * 2,Image); // read a line - slow !
smultron1977 0:246f2fb5be59 943 for (i = 0; i < PixelWidth; i++) { // copy pixel data to TFT
smultron1977 0:246f2fb5be59 944 _spi.write(line[i]); // one 16 bit pixel
smultron1977 0:246f2fb5be59 945 }
smultron1977 0:246f2fb5be59 946 }
smultron1977 0:246f2fb5be59 947 _spi.format(8,3);
smultron1977 0:246f2fb5be59 948 wr_dat_stop();
smultron1977 0:246f2fb5be59 949 free (line);
smultron1977 0:246f2fb5be59 950 fclose(Image);
smultron1977 0:246f2fb5be59 951 return(1);
smultron1977 0:246f2fb5be59 952 }