#include "lgfx.h"

LGFX::LGFX(void)
{
    {                                      // Configure bus control settings
        auto cfg = _bus_instance.config(); // Get structure for bus configuration
                                           // 8-bit parallel bus configuration
        // cfg.i2s_port = I2S_NUM_0;     // Select I2S port to use (I2S_NUM_0 or I2S_NUM_1) (ESP32 I2S
        // LCD mode is used)
        cfg.freq_write = 20000000;              // Transmission clock (max 20MHz, rounded to integer division of 80MHz)
        cfg.pin_wr = 47;                        // Pin number connected to WR
        cfg.pin_rd = -1;                        // Pin number connected to RD
        cfg.pin_rs = 0;                         // Pin number connected to RS(D/C)
        cfg.pin_d0 = 9;                         // Pin number connected to D0
        cfg.pin_d1 = 46;                        // Pin number connected to D1
        cfg.pin_d2 = 3;                         // Pin number connected to D2
        cfg.pin_d3 = 8;                         // Pin number connected to D3
        cfg.pin_d4 = 18;                        // Pin number connected to D4
        cfg.pin_d5 = 17;                        // Pin number connected to D5
        cfg.pin_d6 = 16;                        // Pin number connected to D6
        cfg.pin_d7 = 15;                        // Pin number connected to D7
        _bus_instance.config(cfg);              // Apply configuration values to bus
        _panel_instance.setBus(&_bus_instance); // Set bus to panel
    }

    {                                        // Configure display panel control settings
        auto cfg = _panel_instance.config(); // Get structure for display panel configuration

        cfg.pin_cs = -1;   // Pin number connected to CS   (-1 = disable)
        cfg.pin_rst = 4;   // Pin number connected to RST  (-1 = disable)
        cfg.pin_busy = -1; // Pin number connected to BUSY (-1 = disable)

        // Note:
        // The following configuration values have common default values set for each panel, so please comment out
        // unknown items and try them.

        cfg.panel_width = 320;    // Actual displayable width
        cfg.panel_height = 480;   // Actual displayable height
        cfg.offset_x = 0;         // Panel X direction offset amount
        cfg.offset_y = 0;         // Panel Y direction offset amount
        cfg.offset_rotation = 0;  // Rotation direction value offset 0~7 (4~7 are upside down)
        cfg.dummy_read_pixel = 8; // Number of dummy read bits before pixel reading
        cfg.dummy_read_bits = 1;  // Number of dummy read bits before reading data other than pixels
        cfg.readable = true;      // Set to true if data reading is possible
        cfg.invert = true;        // Set to true if panel brightness is inverted
        cfg.rgb_order = false;    // Set to true if panel red and blue are swapped
        cfg.dlen_16bit =
            false; // Set to true for panels that send data length in 16-bit units via 16-bit parallel or SPI
        cfg.bus_shared = true; // Set to true if sharing bus with SD card (bus control is performed by drawJpgFile etc.)

        // The following should only be set if the display is misaligned on drivers with variable pixel counts like
        // ST7735 or ILI9163.
        //    cfg.memory_width     =   240;  // Maximum width supported by driver IC
        //    cfg.memory_height    =   320;  // Maximum height supported by driver IC

        _panel_instance.config(cfg);
    }

    //*
    {                                        // Configure backlight control settings (delete if not needed)
        auto cfg = _light_instance.config(); // Get structure for backlight configuration

        cfg.pin_bl = 45;     // Pin number connected to backlight
        cfg.invert = false;  // Set to true to invert backlight brightness
        cfg.freq = 44100;    // Backlight PWM frequency
        cfg.pwm_channel = 7; // PWM channel number to use

        _light_instance.config(cfg);
        _panel_instance.setLight(&_light_instance); // Set backlight to panel
    }

    { // Configure touch screen control settings (delete if not needed)
        auto cfg = _touch_instance.config();

        cfg.x_min = 0;           // Minimum X value obtained from touch screen (raw value)
        cfg.x_max = 319;         // Maximum X value obtained from touch screen (raw value)
        cfg.y_min = 0;           // Minimum Y value obtained from touch screen (raw value)
        cfg.y_max = 479;         // Maximum Y value obtained from touch screen (raw value)
        cfg.pin_int = 7;         // Pin number connected to INT
        cfg.bus_shared = true;   // Set to true if using common bus with screen
        cfg.offset_rotation = 0; // Adjustment when display and touch orientation don't match, set value 0~7
                                 // For I2C connection
        cfg.i2c_port = 1;        // Select I2C to use (0 or 1)
        cfg.i2c_addr = 0x38;     // I2C device address number
        cfg.pin_sda = 6;         // Pin number connected to SDA
        cfg.pin_scl = 5;         // Pin number connected to SCL
        cfg.freq = 400000;       // Set I2C clock

        _touch_instance.config(cfg);
        _panel_instance.setTouch(&_touch_instance); // Set touch screen to panel
    }
    //*/
    setPanel(&_panel_instance); // Set panel to use
};
