#ifndef TFT_H
#define TFT_H

/*
 * Adapted from an example under the MIT license:
 * Copyright © 2022 atanisoft (github.com/atanisoft)
 * 
 * MIT LICENSE:
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <driver/gpio.h>
#include <driver/ledc.h>
#include <driver/spi_master.h>
#include <esp_err.h>
#include <esp_freertos_hooks.h>
#include <esp_log.h>
#include <esp_lcd_panel_io.h>
#include <esp_lcd_panel_vendor.h>
#include <esp_lcd_panel_ops.h>
#include "esp_lcd_ili9488/esp_lcd_ili9488.h"
#include <esp_timer.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <lvgl.h>
#include <stdio.h>
#include "sdkconfig.h"

// Uncomment the following line to enable using double buffering of LVGL color
// data.
// #define USE_DOUBLE_BUFFERING 1

// rotation swaps the horizontal and vertical pixel counts
#define DISPLAY_HORIZONTAL_PIXELS 480
#define DISPLAY_VERTICAL_PIXELS 320
#define DISPLAY_COMMAND_BITS 8
#define DISPLAY_PARAMETER_BITS 8
#define DISPLAY_REFRESH_HZ 40000000
#define DISPLAY_SPI_QUEUE_LEN 10
#define SPI_MAX_TRANSFER_SIZE 32768

#define TFT_PIN_MOSI GPIO_NUM_17
#define TFT_PIN_MISO GPIO_NUM_18
#define TFT_PIN_CLK GPIO_NUM_16
#define TFT_PIN_CS GPIO_NUM_NC
#define TFT_PIN_DC GPIO_NUM_15
#define TFT_PIN_RESET GPIO_NUM_8

#define TFT_INVERT_COLOR false

// Default to 50 lines of color data
#define LV_BUFFER_SIZE DISPLAY_HORIZONTAL_PIXELS * 50
#define LVGL_UPDATE_PERIOD_MS 5

#define BACKLIGHT_LEDC_MODE LEDC_LOW_SPEED_MODE
#define BACKLIGHT_LEDC_CHANNEL LEDC_CHANNEL_0
#define BACKLIGHT_LEDC_TIMER LEDC_TIMER_1
#define BACKLIGHT_LEDC_TIMER_RESOLUTION LEDC_TIMER_10_BIT
#define BACKLIGHT_LEDC_FRQUENCY 5000

extern lv_obj_t* screen;

extern SemaphoreHandle_t xGuiSemaphore;

void create_demo_ui();
void init_tft();

#endif /* TFT_H */