/*    PacoMouseCYD throttle -- F. Cañada 2025-2026 --  https://usuaris.tinet.cat/fmco/
      Very basic Graphical User Interface (GUI) for PacoMouseCYD
      All data in absolute coordinates
*/

#include <TFT_eSPI.h>                                       // Graphics and font library for ILI9341 driver chip  v2.5.43
#include "FreeSans7pt7b.h"
#include "FreeSansBold6pt7b.h"


#define MAX_OBJ_STACK       100
#define MAX_LABEL_LNG       150
#define TMR_RESOLUTION      100

#define NOT_USED            0xFF
#define OBJ_NOT_FOUND       0xFFFF


////////////////////////////////////////////////////////////
// ***** FONTS *****
////////////////////////////////////////////////////////////

#ifndef LOAD_GFXFF
ERROR_Please_enable_LOAD_GFXFF_in_User_Setup!
#endif

#define GFXFF               1
#define FSS7                &FreeSans7pt7b
#define FSS9                &FreeSans9pt7b
#define FSSB6               &FreeSansBold6pt7b
#define FSSB9               &FreeSansBold9pt7b
#define FSSB12              &FreeSansBold12pt7b


////////////////////////////////////////////////////////////
// ***** COLORS *****
////////////////////////////////////////////////////////////

// Colour definitions for 64K colour mode (RGB565)
// Bits 0..4 ->   Blue 0..4
// Bits 5..10 ->  Green 0..5
// Bits 11..15 -> Red 0..4
// Assign human-readable names to some common 16-bit color values: http://rinkydinkelectronics.com/calc_rgb565.php
// Examples: https://github.com/newdigate/rgb565_colors



const uint16_t colorDraw[] = {COLOR_BLACK, COLOR_BLUE, COLOR_RED, COLOR_MAGENTA, COLOR_GREEN, COLOR_CYAN, COLOR_YELLOW, COLOR_WHITE,
                              COLOR_BACKGROUND, COLOR_TRANSPARENT, COLOR_GHOST_WHITE, COLOR_AQUA, COLOR_CREAM, COLOR_SMOKYBLACK, COLOR_SKYBLUE,
                              COLOR_GOLD,
                             };


////////////////////////////////////////////////////////////
// ***** OBJECT *****
////////////////////////////////////////////////////////////

enum objTypeGUI {OBJ_UNDEF, OBJ_TIMER, OBJ_WIN, OBJ_BUTTON, OBJ_LABEL, OBJ_TXT, OBJ_BAR, OBJ_DRAWSTR, OBJ_ICON, OBJ_KEYBOARD, OBJ_SWITCH,
                 OBJ_GAUGE, OBJ_LPIC, OBJ_FNC, OBJ_SLIDER, OBJ_RADIO, OBJ_CHAR,
                };

struct wObj {                                           // Graphic objects
  uint16_t  objType;
  uint16_t  objID;
};

struct wObj objStack[MAX_OBJ_STACK];                    // Object stack
uint16_t endObjStack;
uint16_t lastWinStack;


////////////////////////////////////////////////////////////
// ***** EVENT *****
////////////////////////////////////////////////////////////

enum EventGUI {EVNT_CLICK, EVNT_DRAW, EVNT_WOPEN, EVNT_WCLOSE, EVNT_TIMER, EVNT_BOOT};

typedef struct {                                        // Events
  uint16_t  objType;
  uint16_t  objID;
  uint16_t  eventID;
} wEvent;

wEvent eventStack[32];                                  // stack for events (size 32, hardcoded in functions. Don't change!)
uint16_t  eventIn;
uint16_t  eventOut;
uint16_t  eventsPending;
uint16_t  lastClickX;
uint16_t  lastClickY;


////////////////////////////////////////////////////////////
// ***** TIMER *****
////////////////////////////////////////////////////////////

enum timers {TMR_BLIGHT, TMR_END_LOGO, TMR_POWER, TMR_SPEEDO, TMR_INFO, TMR_WAIT, TMR_STEAM, TMR_ACCESSORY, TMR_SCAN, TMR_FNC_ECOS,
             TMR_STA_RUN,
             MAX_SYS_TIMER
            };

enum timerType {TMR_STOP, TMR_ONESHOT, TMR_PERIODIC};

typedef struct {
  uint16_t  tmrDelay;
  uint16_t  tmrCount;
  uint16_t  type;
} wTimer;

wTimer wTimerStack[MAX_SYS_TIMER];
uint32_t  timerSys;

#if (TFT_WIDTH == 240)
#include "gui240x320.h"
#endif
#if (TFT_WIDTH == 320)
#include "gui320x480.h"
#endif
