
// This is the command sequence that initialises the ST7735 driver
//
// This setup information is in a format accepted by the commandList() function
// which reduces FLASH space, but on an ESP8266 there is plenty available!
//
// See ILI9341_Setup.h file for an alternative simpler format

{
    // Initialization commands for ST7735 screens
    static const uint8_t PROGMEM Bcmd[] =
        {     // Initialization commands for 7735B screens
         18,  // 18 commands in list:
         ST7735_SWRESET,
         TFT_INIT_DELAY,  //  1: Software reset, no args, w/delay
         50,              //     50 ms delay
         ST7735_SLPOUT,
         TFT_INIT_DELAY,  //  2: Out of sleep mode, no args, w/delay
         255,             //     255 = 500 ms delay
         ST7735_COLMOD,
         1 + TFT_INIT_DELAY,  //  3: Set color mode, 1 arg + delay:
         0x05,                //     16-bit color
         10,                  //     10 ms delay
         ST7735_FRMCTR1,
         3 + TFT_INIT_DELAY,  //  4: Frame rate control, 3 args + delay:
         0x00,                //     fastest refresh
         0x06,                //     6 lines front porch
         0x03,                //     3 lines back porch
         10,                  //     10 ms delay
         ST7735_MADCTL, 1,    //  5: Memory access ctrl (directions), 1 arg:
         0x40,                //     Row addr/col addr, bottom to top refresh
         ST7735_DISSET5, 2,   //  6: Display settings #5, 2 args, no delay:
         0x15,                //     1 clk cycle nonoverlap, 2 cycle gate
                              //     rise, 3 cycle osc equalize
         0x02,                //     Fix on VTL
         ST7735_INVCTR, 1,    //  7: Display inversion control, 1 arg:
         0x0,                 //     Line inversion
         ST7735_PWCTR1,
         2 + TFT_INIT_DELAY,  //  8: Power control, 2 args + delay:
         0x02,                //     GVDD = 4.7V
         0x70,                //     1.0uA
         10,                  //     10 ms delay
         ST7735_PWCTR2, 1,    //  9: Power control, 1 arg, no delay:
         0x05,                //     VGH = 14.7V, VGL = -7.35V
         ST7735_PWCTR3, 2,    // 10: Power control, 2 args, no delay:
         0x01,                //     Opamp current small
         0x02,                //     Boost frequency
         ST7735_VMCTR1,
         2 + TFT_INIT_DELAY,  // 11: Power control, 2 args + delay:
         0x3C,                //     VCOMH = 4V
         0x38,                //     VCOML = -1.1V
         10,                  //     10 ms delay
         ST7735_PWCTR6, 2,    // 12: Power control, 2 args, no delay:
         0x11, 0x15, ST7735_GMCTRP1,
         16,  // 13: Magical unicorn dust, 16 args, no delay:
         0x09, 0x16, 0x09, 0x20,  //     (seriously though, not sure what
         0x21, 0x1B, 0x13, 0x19,  //      these config values represent)
         0x17, 0x15, 0x1E, 0x2B, 0x04, 0x05, 0x02, 0x0E, ST7735_GMCTRN1,
         16 + TFT_INIT_DELAY,     // 14: Sparkles and rainbows, 16 args + delay:
         0x0B, 0x14, 0x08, 0x1E,  //     (ditto)
         0x22, 0x1D, 0x18, 0x1E, 0x1B, 0x1A, 0x24, 0x2B, 0x06, 0x06, 0x02, 0x0F,
         10,               //     10 ms delay
         ST7735_CASET, 4,  // 15: Column addr set, 4 args, no delay:
         0x00, 0x02,       //     XSTART = 2
         0x00, 0x81,       //     XEND = 129
         ST7735_RASET, 4,  // 16: Row addr set, 4 args, no delay:
         0x00, 0x02,       //     XSTART = 1
         0x00, 0x81,       //     XEND = 160
         ST7735_NORON,
         TFT_INIT_DELAY,  // 17: Normal display on, no args, w/delay
         10,              //     10 ms delay
         ST7735_DISPON,
         TFT_INIT_DELAY,  // 18: Main screen turn on, no args, w/delay
         255},            //     255 = 500 ms delay

        Rcmd1[] =
            {     // Init for 7735R, part 1 (red or green tab)
             18,  // 15 commands in list:
             ST7735_SWRESET,
             TFT_INIT_DELAY,  //  1: Software reset, 0 args, w/delay
             150,             //     150 ms delay
             ST7735_SLPOUT,
             TFT_INIT_DELAY,  //  2: Out of sleep mode, 0 args, w/delay
             120,             //     500 ms delay
             ST7735_FRMCTR1,
             3,  //  3: Frame rate ctrl - normal mode, 3 args:
             0x05,
             0x3C,
             0x3C,  //     Rate = fosc/(1x2+40) * (LINE+2C+2D)
             ST7735_FRMCTR2,
             3,  //  4: Frame rate control - idle mode, 3 args:
             0x05,
             0x3C,
             0x3C,  //     Rate = fosc/(1x2+40) * (LINE+2C+2D)
             ST7735_FRMCTR3,
             6,  //  5: Frame rate ctrl - partial mode, 6 args:
             0x05,
             0x3C,
             0x3C,  //     Dot inversion mode
             0x05,
             0x3C,
             0x3C,  //     Line inversion mode
             ST7735_INVCTR,
             2,  //  6: Display inversion ctrl, 1 arg, no delay:
             0x03,
             0x02,  //     No inversion
             ST7735_PWCTR1,
             3,  //  7: Power control, 3 args, no delay:
             0xA4,
             0x04,  //     -4.6V
             0x84,  //     AUTO mode
             ST7735_PWCTR2,
             1,     //  8: Power control, 1 arg, no delay:
             0xC5,  //     VGH25 = 2.4C VGSEL = -10 VGH = 3 * AVDD
             ST7735_PWCTR3,
             2,     //  9: Power control, 2 args, no delay:
             0x0D,  //     Opamp current small
             0x00,  //     Boost frequency
             ST7735_PWCTR4,
             2,     // 10: Power control, 2 args, no delay:
             0x8D,  //     BCLK/2, Opamp current small & Medium low
             0x6A,
             ST7735_PWCTR5,
             2,  // 11: Power control, 2 args, no delay:
             0x8D,
             0xEE,
             ST7735_VMCTR1,
             1,  // 12: Power control, 1 arg, no delay:
             0x21,
             ST7735_PWCTR6,
             1,
             0x80,
             0xf0,
             1,
             0x11,
             0xD6,
             1,
             0xCB,
             ST7735_INVOFF,
             0,  // 13: Don't invert display, no args, no delay
             ST7735_MADCTL,
             1,     // 14: Memory access control (directions), 1 arg:
             0xC8,  //     row addr/col addr, bottom to top refresh
             ST7735_COLMOD,
             1,      // 15: set color mode, 1 arg, no delay:
             0x05},  //     16-bit color

        Rcmd2green[] =
            {    // Init for 7735R, part 2 (green tab only)
             2,  //  2 commands in list:
             ST7735_CASET,
             4,  //  1: Column addr set, 4 args, no delay:
             0x00,
             0x02,  //     XSTART = 0
             0x00,
             0x7F + 0x02,  //     XEND = 127
             ST7735_RASET,
             4,  //  2: Row addr set, 4 args, no delay:
             0x00,
             0x01,  //     XSTART = 0
             0x00,
             0x9F + 0x01},  //     XEND = 159

        Rcmd2red[] =
            {    // Init for 7735R, part 2 (red tab only)
             2,  //  2 commands in list:
             ST7735_CASET,
             4,  //  1: Column addr set, 4 args, no delay:
             0x00,
             0x00,  //     XSTART = 0
             0x00,
             0x7F,  //     XEND = 127
             ST7735_RASET,
             4,  //  2: Row addr set, 4 args, no delay:
             0x00,
             0x00,  //     XSTART = 0
             0x00,
             0x9F},  //     XEND = 159

        Rcmd3[] = {    // Init for 7735R, part 3 (red or green tab)
                   4,  //  4 commands in list:
                   ST7735_GMCTRP1,
                   16,  //  1: 16 args, no delay:
                   0x0D,
                   0x0C,
                   0x0C,
                   0x0E,
                   0x0E,
                   0x00,
                   0x00,
                   0x00,
                   0x00,
                   0x09,
                   0x23,
                   0x31,
                   0x00,
                   0x0C,
                   0x03,
                   0x1A,
                   ST7735_GMCTRN1,
                   16,  //  2: 16 args, no delay:
                   0x0A,
                   0x05,
                   0x06,
                   0x07,
                   0x08,
                   0x01,
                   0x00,
                   0x00,
                   0x00,
                   0x05,
                   0x20,
                   0x2E,
                   0x00,
                   0x0A,
                   0x01,
                   0x1A,
                   ST7735_NORON,
                   TFT_INIT_DELAY,  //  3: Normal display on, no args, w/delay
                   10,              //     10 ms delay
                   ST7735_DISPON,
                   TFT_INIT_DELAY,  //  4: Main screen turn on, no args w/delay
                   100};            //     100 ms delay

    if (tabcolor == INITB) {
        commandList(Bcmd);
    } else {
        commandList(Rcmd1);
        if (tabcolor == INITR_GREENTAB) {
            commandList(Rcmd2green);
            colstart = 2;
            rowstart = 1;
        } else if (tabcolor == INITR_GREENTAB2) {
            commandList(Rcmd2green);
            writecommand(ST7735_MADCTL);
            writedata(0xC0);
            colstart = 2;
            rowstart = 1;
        } else if (tabcolor == INITR_GREENTAB3) {
            commandList(Rcmd2green);
            colstart = 2;
            rowstart = 3;
        } else if (tabcolor == INITR_GREENTAB128) {
            commandList(Rcmd2green);
            colstart = 0;
            rowstart = 32;
        } else if (tabcolor == INITR_GREENTAB160x80) {
            commandList(Rcmd2green);
            writecommand(TFT_INVON);
            colstart = 26;
            rowstart = 1;
        } else if (tabcolor == INITR_REDTAB160x80) {
            commandList(Rcmd2green);
            colstart = 24;
            rowstart = 0;
        } else if (tabcolor == INITR_REDTAB) {
            commandList(Rcmd2red);
        } else if (tabcolor == INITR_BLACKTAB) {
            writecommand(ST7735_MADCTL);
            writedata(0xC0);
        }
        commandList(Rcmd3);
    }
}
