#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 600
#End Region
'Ctrl+Click to open the C code folder: ide://run?File=%WINDIR%\System32\explorer.exe&Args=%PROJECT%\Objects\Src
Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
End Sub
Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
End Sub
#if C
// Include the libraries
#define LGFX_USE_V1
#include "Arduino.h"
#include <lvgl.h>
#include "demos/lv_demos.h"
#include <LovyanGFX.hpp>
#include <Ticker.h>
#define off_pin 35
#define buf_size 120
class LGFX : public lgfx::LGFX_Device
{
  lgfx::Panel_GC9A01 _panel_instance;
  lgfx::Bus_SPI _bus_instance;
public:
  LGFX(void)
  {
    {
      // Configuring the SPI bus
      auto cfg = _bus_instance.config();
      cfg.spi_host = SPI2_HOST;          // Select the SPI to use ESP32-S2,C3 : SPI2_HOST or SPI3_HOST / ESP32 : VSPI_HOST or HSPI_HOST
      // *With the ESP-IDF version upgrade, the description of VSPI_HOST and HSPI_HOST will be deprecated, so if an error occurs, please use SPI2_HOST and SPI3_HOST instead.
      cfg.spi_mode = 0;                  // Set SPI communication mode (0 ~ 3)
      cfg.freq_write = 80000000;         // SPI clock during transmission (up to 80MHz, rounded to the value obtained by dividing 80MHz by an integer)
      cfg.freq_read = 20000000;          // SPI clock when receiving
      cfg.spi_3wire = true;              // Set true if receiving is done using MOSI pin
      cfg.use_lock = true;               //Set to true when using transaction lock
      cfg.dma_channel = SPI_DMA_CH_AUTO; // Set the DMA channel to use (0=DMA not used / 1=1ch / 2=ch / SPI_DMA_CH_AUTO=automatic setting)
      // * Due to the ESP-IDF version upgrade, SPI_DMA_CH_AUTO (automatic setting) is recommended for the DMA channel. Specifying 1ch or 2ch is not recommended.
      cfg.pin_sclk = 6;                  // Set SPI SCLK pin number
      cfg.pin_mosi = 7;                  // Set SPI CLK pin number
      cfg.pin_miso = -1;                 // Set SPI MISO pin number (-1 = disable)
      cfg.pin_dc = 2;                    // Set SPI D/C pin number (-1 = disable)
      _bus_instance.config(cfg);              // Reflect the settings on the bus.
      _panel_instance.setBus(&_bus_instance); // Set the bus on the panel.
    }
    {
      // Set display panel control.
      auto cfg = _panel_instance.config(); // Get the structure for display panel settings.
      cfg.pin_cs = 10;   // Pin number to which CS is connected (-1 = disable)
      cfg.pin_rst = -1;  // Pin number to which RST is connected (-1 = disable)
      cfg.pin_busy = -1; // Pin number to which BUSY is connected (-1 = disable)
      // * The following setting values are general initial values set for each panel and the pin number to which BUSY is connected (-1 = disable), so please comment out any unknown items and try again.
      cfg.memory_width = 240;   // Maximum width supported by driver IC
      cfg.memory_height = 240;  // Maximum height supported by driver IC
      cfg.panel_width = 240;    // Actual displayable width
      cfg.panel_height = 240;   // Actual display height
      cfg.offset_x = 0;         // Panel X direction offset amount
      cfg.offset_y = 0;         // Panel Y direction offset amount
      cfg.offset_rotation = 0;  // The offset of the value in the direction of rotation is 0~7 (4~7 is inverted)
      cfg.dummy_read_pixel = 8; // The number of dummy bits to read before reading the pixel
      cfg.dummy_read_bits = 1;  // The number of virtual read bits before reading data other than pixels
      cfg.readable = false;     // Set to true if data can be read
      cfg.invert = true;        // Set to true if the panel's light and dark are inverted
      cfg.rgb_order = false;    // Set to true if the red and blue colors of the panel are swapped
      cfg.dlen_16bit = false;   // Set to true for panels that send data length in 16-bit units
      cfg.bus_shared = false;   // Set to true if the bus is shared with the SD card (use drawJpgFile etc. to perform bus control)
      _panel_instance.config(cfg);
    }
    setPanel(&_panel_instance); //Set the panel to use.
  }
};
// Create an instance of the prepared class.
LGFX tft;
/*Change to your screen resolution*/
static const uint32_t screenWidth = 240;
static const uint32_t screenHeight = 240;
static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[2][screenWidth * buf_size];
/* Display flushing */
void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p)
{
  if (tft.getStartCount() == 0)
  {
    tft.endWrite();
  }
  tft.pushImageDMA(area->x1, area->y1, area->x2 - area->x1 + 1, area->y2 - area->y1 + 1, (lgfx::swap565_t *)&color_p->full);
  lv_disp_flush_ready(disp); /* tell lvgl that flushing is done */
}
Ticker ticker;
void setup()
{
  Serial.begin(115200); /* prepare for possible serial debug */
  Serial.println("I am LVGL_Arduino");
  tft.init();
  tft.initDMA();
  tft.startWrite();
  tft.setColor(0, 0, 0);
  lv_init();
  lv_disp_draw_buf_init(&draw_buf, buf[0], buf[1], screenWidth * buf_size);
  /*Initialize the display*/
  static lv_disp_drv_t disp_drv;
  lv_disp_drv_init(&disp_drv);
  /*Change the following line to your display resolution*/
  disp_drv.hor_res = screenWidth;
  disp_drv.ver_res = screenHeight;
  disp_drv.flush_cb = my_disp_flush;
  disp_drv.draw_buf = &draw_buf;
  lv_disp_drv_register(&disp_drv);
  /* Create simple label */
  lv_obj_t *label = lv_label_create( lv_scr_act() );
  lv_label_set_text( label, "Hello Arduino! (V8.0.X)" );
  lv_obj_align( label, LV_ALIGN_CENTER, 0, 0 );
  //ui_mian(); // watch
  Serial.println("Setup done");
  pinMode(3, OUTPUT);
  digitalWrite(3, HIGH);
  pinMode(0, INPUT);
}
void loop()
{
  lv_timer_handler(); /* let the GUI do its work */
  delay(5);
}
#End if