<|start_filename|>examples/linux/test_sdl2.c<|end_filename|>
#include <SDL2/SDL.h>


//
// GUIslice sample SDL test for SDL2.0
// - <NAME>
// - https://www.impulseadventure.com/elec/guislice-gui.html
// - https://github.com/ImpulseAdventure/GUIslice
//
// This program is a simple test to ensure that the SDL
// library is installed correctly. If everything is
// working then you should see the external screen
// (such as PiTFT) display a red-to-blue gradient and
// a green box.
//


// Main entrypoint
SDL_Window*     pWind = NULL;
SDL_Renderer*   pRender = NULL;

int main(int argc,char* args[]) {

  // --------------------------------------
  // Initialization
  // --------------------------------------

  // Setup SDL
  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    fprintf(stderr,"ERROR: SDL_Init(): %s\n",SDL_GetError());
    return 0;
  }

  // Now that we have successfully initialized SDL
  // we need to register an exit handler so that SDL_Quit()
  // gets called at program termination. If we don't do this
  // then some types of errors/aborts will result in
  // the SDL environment being left in a bad state that
  // affects the next SDL program execution.
  atexit(SDL_Quit);

  // Default to using OpenGL rendering engine and full-screen
  // When using SDL_WINDOW_FULLSCREEN, the width & height dimensions are ignored
  pWind = SDL_CreateWindow("GUIslice",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,
          0,0,SDL_WINDOW_FULLSCREEN | SDL_WINDOW_OPENGL);
  if (!pWind) {
    fprintf(stderr,"ERROR: SDL_CreateWindow(): %s\n",SDL_GetError());
    return 0;
  }

  // Save a copy of the display dimensions from the created fullscreen window
  int nResX,nResY;
  SDL_GetWindowSize(pWind,&nResX,&nResY);

  // Create renderer
  pRender = SDL_CreateRenderer(pWind,-1,SDL_RENDERER_ACCELERATED);
  if (!pRender) {
    fprintf(stderr,"ERROR: SDL_CreateRenderer(): %s\n",SDL_GetError());
    return 0;
  }

  // --------------------------------------
  // Perform some simple drawing primitives
  // --------------------------------------

  // Clear the drawing before any new drawing occurs
  SDL_SetRenderDrawColor(pRender,0,0,0,255);
  SDL_RenderClear(pRender);

  // Draw a gradient from red to blue
  SDL_Rect  rectTmp;
  Uint16    nPosX;
  for (nPosX = 0; nPosX < nResX; nPosX++) {
    rectTmp = (SDL_Rect){nPosX,nResY/2,1,nResY/2};
    SDL_SetRenderDrawColor(pRender,nPosX % 256, 0, 255 - (nPosX % 256), 255);
    SDL_RenderFillRect(pRender,&rectTmp);
  }

  // Draw a green box
  SDL_Rect rectBox = {0,0,(Uint16)(nResX),(Uint16)(nResY/2)};
  SDL_SetRenderDrawColor(pRender,0,255,0,255);
  SDL_RenderFillRect(pRender,&rectBox);

  // Now that we've completed drawing, update the main display
  SDL_RenderPresent(pRender);

  // Wait for a short delay
  SDL_Delay(3000);

  // Close down SDL
  SDL_Quit();

  return 1;
}


<|start_filename|>examples/linux/ex42_lnx_ring.c<|end_filename|>
//
// GUIslice Library Examples
// - <NAME>
// - https://www.impulseadventure.com/elec/guislice-gui.html
// - https://github.com/ImpulseAdventure/GUIslice
// - Example 42 (LINUX):
//   - Demonstrate ring gauge, controlled by slider
//
#include "GUIslice.h"
#include "GUIslice_drv.h"

// Include any extended elements
#include "elem/XSlider.h"
#include "elem/XRingGauge.h"


// Defines for resources
#define FONT_FNAME_BTN "/usr/share/fonts/truetype/noto/NotoMono-Regular.ttf"
#define FONT_FNAME_DIAL "/usr/share/fonts/truetype/noto/NotoMono-Regular.ttf"

// Enumerations for pages, elements, fonts, images
enum { E_PG_MAIN };
enum { E_ELEM_BOX, E_ELEM_BTN_QUIT, E_ELEM_XRING, E_ELEM_SLIDER };
enum { E_FONT_BTN, E_FONT_DIAL, MAX_FONT };

bool    m_bQuit = false;

// Instantiate the GUI
#define MAX_PAGE            1
#define MAX_ELEM_PG_MAIN    4

gslc_tsGui                  m_gui;
gslc_tsDriver               m_drv;
gslc_tsFont                 m_asFont[MAX_FONT];
gslc_tsPage                 m_asPage[MAX_PAGE];
gslc_tsElem                 m_asPageElem[MAX_ELEM_PG_MAIN];
gslc_tsElemRef              m_asPageElemRef[MAX_ELEM_PG_MAIN];

gslc_tsXSlider              m_sXSlider;
gslc_tsXRingGauge           m_sXRingGauge;

// Save some element references for quick access
gslc_tsElemRef*  m_pElemSlider = NULL;
gslc_tsElemRef*  m_pElemXRingGauge = NULL;

// Program global variables
int16_t m_nSliderPos = 0;


// Configure environment variables suitable for display
// - These may need modification to match your system
//   environment and display type
// - Defaults for GSLC_DEV_FB and GSLC_DEV_TOUCH are in GUIslice_config.h
// - Note that the environment variable settings can
//   also be set directly within the shell via export
//   (or init script).
//   - eg. export TSLIB_FBDEVICE=/dev/fb1
void UserInitEnv()
{
#if defined(DRV_DISP_SDL1) || defined(DRV_DISP_SDL2)
  setenv((char*)"FRAMEBUFFER",GSLC_DEV_FB,1);
  setenv((char*)"SDL_FBDEV",GSLC_DEV_FB,1);
  setenv((char*)"SDL_VIDEODRIVER",GSLC_DEV_VID_DRV,1);
#endif

#if defined(DRV_TOUCH_TSLIB)
  setenv((char*)"TSLIB_FBDEVICE",GSLC_DEV_FB,1);
  setenv((char*)"TSLIB_TSDEVICE",GSLC_DEV_TOUCH,1);
  setenv((char*)"TSLIB_CALIBFILE",(char*)"/etc/pointercal",1);
  setenv((char*)"TSLIB_CONFFILE",(char*)"/etc/ts.conf",1);
  setenv((char*)"TSLIB_PLUGINDIR",(char*)"/usr/local/lib/ts",1);
#endif

}

// Define debug message function
static int16_t DebugOut(char ch) { fputc(ch,stderr); return 0; }


// Button callbacks
bool CbBtnQuit(void* pvGui, void *pvElemRef, gslc_teTouch eTouch, int16_t nX, int16_t nY)
{
  if (eTouch == GSLC_TOUCH_UP_IN) {
    m_bQuit = true;
  }
  return true;
}


// Callback function for when a slider's position has been updated
bool CbSlidePos(void* pvGui, void* pvElemRef, int16_t nPos)
{
  gslc_tsGui*     pGui     = (gslc_tsGui*)(pvGui);
  gslc_tsElemRef* pElemRef = (gslc_tsElemRef*)(pvElemRef);
  gslc_tsElem*    pElem    = gslc_GetElemFromRef(pGui,pElemRef);
  //gslc_tsXSlider* pSlider = (gslc_tsXSlider*)(pElem->pXData);

  switch (pElem->nId) {
  case E_ELEM_SLIDER:
    m_nSliderPos = gslc_ElemXSliderGetPos(pGui, pElemRef);
    break;
  default:
    break;
  }
  return true;
}

// Create page elements
bool InitOverlays()
{
  gslc_tsElemRef* pElemRef = NULL;
  
  // -----------------------------------
  // Create page elements
  gslc_PageAdd(&m_gui, E_PG_MAIN, m_asPageElem, MAX_ELEM_PG_MAIN, m_asPageElemRef, MAX_ELEM_PG_MAIN);

  // Background flat color
  gslc_SetBkgndColor(&m_gui, GSLC_COL_GRAY_DK2);

  // Create background box
  pElemRef = gslc_ElemCreateBox(&m_gui, E_ELEM_BOX, E_PG_MAIN, (gslc_tsRect) { 10, 50, 300, 150 });
  gslc_ElemSetCol(&m_gui, pElemRef, GSLC_COL_WHITE, GSLC_COL_BLACK, GSLC_COL_BLACK);

  // Create Quit button with text label
  pElemRef = gslc_ElemCreateBtnTxt(&m_gui, E_ELEM_BTN_QUIT, E_PG_MAIN,
    (gslc_tsRect) { 235, 5, 80, 30 }, (char*)"Quit", 0, E_FONT_BTN, &CbBtnQuit);

  // Create a RingGauge
  static char m_str10[10] = "";
  pElemRef = gslc_ElemXRingGaugeCreate(&m_gui, E_ELEM_XRING, E_PG_MAIN, &m_sXRingGauge,
    (gslc_tsRect) { 80, 80, 100, 100 }, m_str10, 10, E_FONT_DIAL);
  gslc_ElemXRingGaugeSetValRange(&m_gui, pElemRef, 0, 100);
  gslc_ElemXRingGaugeSetVal(&m_gui, pElemRef, 60); // Set initial value
  // The following are some additional config options available
  //gslc_ElemXRingGaugeSetAngleRange(&m_gui, pElemRef, 0, 360, true);    // Full circle 
  //gslc_ElemXRingGaugeSetAngleRange(&m_gui, pElemRef, -90, 180, true);  // Upper Semi-circle
  //gslc_ElemXRingGaugeSetAngleRange(&m_gui, pElemRef, 0, 90, true);     // Top-Right Quarter-circle
  //gslc_ElemXRingGaugeSetAngleRange(&m_gui, pElemRef, -135, 270, true); // Three-Quarter circle
  //gslc_ElemXRingGaugeSetThickness(&m_gui,pElemRef, 8);
  //gslc_ElemXRingGaugeSetQuality(&m_gui,pElemRef, 72);
  //gslc_ElemXRingGaugeSetColorInactive(&m_gui,pElemRef, GSLC_COL_GRAY_DK3);
  //gslc_ElemXRingGaugeSetColorActiveFlat(&m_gui,pElemRef, GSLC_COL_ORANGE);
  //gslc_ElemXRingGaugeSetColorActiveGradient(&m_gui, pElemRef, GSLC_COL_BLUE_LT4, GSLC_COL_RED);
  m_pElemXRingGauge = pElemRef; // Save for quick access

   // Create slider
  pElemRef = gslc_ElemXSliderCreate(&m_gui, E_ELEM_SLIDER, E_PG_MAIN, &m_sXSlider,
    (gslc_tsRect) { 200, 80, 100, 20 }, 0, 100, 60, 5, false);
  gslc_ElemXSliderSetStyle(&m_gui, pElemRef, true, (gslc_tsColor) { 0, 0, 128 }, 10,
    5, (gslc_tsColor) { 64, 64, 64 });
  gslc_ElemXSliderSetPosFunc(&m_gui, pElemRef, &CbSlidePos);
  m_pElemSlider = pElemRef; // Save for quick access
  
  return true;
}


int main( int argc, char* args[] )
{
  char   acStr[10];  

  // -----------------------------------
  // Initialize
  gslc_InitDebug(&DebugOut);
  UserInitEnv();
  if (!gslc_Init(&m_gui,&m_drv,m_asPage,MAX_PAGE,m_asFont,MAX_FONT)) { exit(1); }

  // Load Fonts
  // - Normally we would select a number of different fonts 
  if (!gslc_FontSet(&m_gui, E_FONT_BTN, GSLC_FONTREF_FNAME, FONT_FNAME_BTN, 10)) { return 0; }
  if (!gslc_FontSet(&m_gui, E_FONT_DIAL, GSLC_FONTREF_FNAME, FONT_FNAME_DIAL, 18)) { return 0; }

  // -----------------------------------
  // Start display
  InitOverlays();

  // Start up display on main page
  gslc_SetPageCur(&m_gui,E_PG_MAIN);

  m_bQuit = false;

  // Initialize startup state
  m_nSliderPos = gslc_ElemXSliderGetPos(&m_gui, m_pElemSlider);

  // -----------------------------------
  // Main event loop
  while (!m_bQuit) {
  
    // Update the XRingGauge position with the slider position
    gslc_ElemXRingGaugeSetVal(&m_gui, m_pElemXRingGauge, m_nSliderPos);

    // Update the XRingGauge text string with a percentage
    snprintf(acStr, 10, "%d%%", m_nSliderPos);
    gslc_ElemSetTxtStr(&m_gui, m_pElemXRingGauge, acStr);  
  
    gslc_Update(&m_gui);
  }

  // -----------------------------------
  // Close down display
  gslc_Quit(&m_gui);

  return 0;
}


<|start_filename|>examples/linux/ex02_lnx_btn_txt.c<|end_filename|>
//
// GUIslice Library Examples
// - <NAME>
// - https://www.impulseadventure.com/elec/guislice-gui.html
// - https://github.com/ImpulseAdventure/GUIslice
// - Example 02: Accept touch input, text button
//

#include "GUIslice.h"
#include "GUIslice_drv.h"


// Defines for resources
#define FONT1 "/usr/share/fonts/truetype/noto/NotoMono-Regular.ttf"

// Enumerations for pages, elements, fonts, images
enum {E_PG_MAIN};
enum {E_ELEM_BOX,E_ELEM_BTN_QUIT};
enum {E_FONT_BTN,MAX_FONT};

bool    m_bQuit = false;

// Instantiate the GUI
#define MAX_PAGE            1
#define MAX_ELEM_PG_MAIN    2

gslc_tsGui                  m_gui;
gslc_tsDriver               m_drv;
gslc_tsFont                 m_asFont[MAX_FONT];
gslc_tsPage                 m_asPage[MAX_PAGE];
gslc_tsElem                 m_asPageElem[MAX_ELEM_PG_MAIN];
gslc_tsElemRef              m_asPageElemRef[MAX_ELEM_PG_MAIN];


// Configure environment variables suitable for display
// - These may need modification to match your system
//   environment and display type
// - Defaults for GSLC_DEV_FB and GSLC_DEV_TOUCH are in GUIslice_config.h
// - Note that the environment variable settings can
//   also be set directly within the shell via export
//   (or init script).
//   - eg. export TSLIB_FBDEVICE=/dev/fb1
void UserInitEnv()
{
#if defined(DRV_DISP_SDL1) || defined(DRV_DISP_SDL2)
  setenv((char*)"FRAMEBUFFER",GSLC_DEV_FB,1);
  setenv((char*)"SDL_FBDEV",GSLC_DEV_FB,1);
  setenv((char*)"SDL_VIDEODRIVER",GSLC_DEV_VID_DRV,1);
#endif

#if defined(DRV_TOUCH_TSLIB)
  setenv((char*)"TSLIB_FBDEVICE",GSLC_DEV_FB,1);
  setenv((char*)"TSLIB_TSDEVICE",GSLC_DEV_TOUCH,1);
  setenv((char*)"TSLIB_CALIBFILE",(char*)"/etc/pointercal",1);
  setenv((char*)"TSLIB_CONFFILE",(char*)"/etc/ts.conf",1);
  setenv((char*)"TSLIB_PLUGINDIR",(char*)"/usr/local/lib/ts",1);
#endif
}

// Define debug message function
static int16_t DebugOut(char ch) { fputc(ch,stderr); return 0; }

// Button callbacks
bool CbBtnQuit(void* pvGui,void *pvElemRef,gslc_teTouch eTouch,int16_t nX,int16_t nY)
{
  if (eTouch == GSLC_TOUCH_UP_IN) {
    m_bQuit = true;
  }
  return true;
}

int main( int argc, char* args[] )
{
  bool              bOk = true;
  gslc_tsElemRef*   pElemRef = NULL;

  // -----------------------------------
  // Initialize
  gslc_InitDebug(&DebugOut);
  UserInitEnv();
  if (!gslc_Init(&m_gui,&m_drv,m_asPage,MAX_PAGE,m_asFont,MAX_FONT)) { exit(1); }

  // Load Fonts
  bOk = gslc_FontSet(&m_gui,E_FONT_BTN,GSLC_FONTREF_FNAME,FONT1,12);
  if (!bOk) { printf("ERROR: gslc_FontSet() failed\n"); exit(1); }


  // -----------------------------------
  // Create page elements
  gslc_PageAdd(&m_gui,E_PG_MAIN,m_asPageElem,MAX_ELEM_PG_MAIN,m_asPageElemRef,MAX_ELEM_PG_MAIN);

  // Background flat color
  gslc_SetBkgndColor(&m_gui,GSLC_COL_GRAY_DK2);

  // Create background box
  pElemRef = gslc_ElemCreateBox(&m_gui,E_ELEM_BOX,E_PG_MAIN,(gslc_tsRect){10,50,300,150});
  gslc_ElemSetCol(&m_gui,pElemRef,GSLC_COL_WHITE,GSLC_COL_BLACK,GSLC_COL_BLACK);

  // Create Quit button with text label
  pElemRef = gslc_ElemCreateBtnTxt(&m_gui,E_ELEM_BTN_QUIT,E_PG_MAIN,
    (gslc_tsRect){120,100,80,40},"Quit",0,E_FONT_BTN,&CbBtnQuit);

  // -----------------------------------
  // Start display

  // Start up display on main page
  gslc_SetPageCur(&m_gui,E_PG_MAIN);

  // -----------------------------------
  // Main event loop

  m_bQuit = false;
  while (!m_bQuit) {

    // Periodically call GUIslice update function
    gslc_Update(&m_gui);

  } // bQuit


  // -----------------------------------
  // Close down display

  gslc_Quit(&m_gui);

  return 0;
}



<|start_filename|>examples/linux/ex43_lnx_glowball.c<|end_filename|>
//
// GUIslice Library Examples
// - <NAME>
// - https://www.impulseadventure.com/elec/guislice-gui.html
// - https://github.com/ImpulseAdventure/GUIslice
// - Example 43 (LINUX):
//   - Demonstrate XGlowball gauge, controlled by slider
//
#include "GUIslice.h"
#include "GUIslice_drv.h"

// Include any extended elements
#include "elem/XSlider.h"
#include "elem/XGlowball.h"


// Defines for resources
#define FONT_FNAME_BTN "/usr/share/fonts/truetype/noto/NotoMono-Regular.ttf"

// Enumerations for pages, elements, fonts, images
enum { E_PG_MAIN };
enum { E_ELEM_BOX, E_ELEM_BTN_QUIT, E_ELEM_XGLOW, E_ELEM_SLIDER };
enum { E_FONT_BTN, MAX_FONT };

bool    m_bQuit = false;

// Instantiate the GUI
#define MAX_PAGE            1
#define MAX_ELEM_PG_MAIN    4

gslc_tsGui                  m_gui;
gslc_tsDriver               m_drv;
gslc_tsFont                 m_asFont[MAX_FONT];
gslc_tsPage                 m_asPage[MAX_PAGE];
gslc_tsElem                 m_asPageElem[MAX_ELEM_PG_MAIN];
gslc_tsElemRef              m_asPageElemRef[MAX_ELEM_PG_MAIN];

gslc_tsXSlider              m_sXSlider;
gslc_tsXGlowball            m_sXGlowball;

// Save some element references for quick access
gslc_tsElemRef* m_pElemSlider = NULL;
gslc_tsElemRef* m_pElemXGlowball = NULL;


// Program global variables
int16_t m_nSliderPos = 0;



// Define the XGlowball appearance
// The tsXGlowballRing array defines each ring in the Glowball:
// - Inner radius
// - Outer radius
// - Color of ring
// Note that rings should be defined from inner to outer.
// It is valid to leave gaps between the rings if desired.
// If the maximum radius is large, then the quality setting
// may need to be increased to ensure the ring has a smooth appearance.
#define NUM_RINGS 9
gslc_tsXGlowballRing asRings[NUM_RINGS] = {
  {0,12,(gslc_tsColor) { 138, 0, 255 }},
  {12,18,(gslc_tsColor) { 0, 12, 255 }},
  {18,24,(gslc_tsColor) { 0, 96, 255 }},
  {24,30,(gslc_tsColor) { 0, 198, 255 }},
  {30,36,(gslc_tsColor) { 0, 255, 150 }},
  {36,42,(gslc_tsColor) { 33, 217, 0 }},
  {42,48,(gslc_tsColor) { 255, 234, 0 }},
  {48,54,(gslc_tsColor) { 255, 152, 0 }},
  {54,60,(gslc_tsColor) { 255, 0, 0 }}
};

// Configure environment variables suitable for display
// - These may need modification to match your system
//   environment and display type
// - Defaults for GSLC_DEV_FB and GSLC_DEV_TOUCH are in GUIslice_config.h
// - Note that the environment variable settings can
//   also be set directly within the shell via export
//   (or init script).
//   - eg. export TSLIB_FBDEVICE=/dev/fb1
void UserInitEnv()
{
#if defined(DRV_DISP_SDL1) || defined(DRV_DISP_SDL2)
  setenv((char*)"FRAMEBUFFER",GSLC_DEV_FB,1);
  setenv((char*)"SDL_FBDEV",GSLC_DEV_FB,1);
  setenv((char*)"SDL_VIDEODRIVER",GSLC_DEV_VID_DRV,1);
#endif

#if defined(DRV_TOUCH_TSLIB)
  setenv((char*)"TSLIB_FBDEVICE",GSLC_DEV_FB,1);
  setenv((char*)"TSLIB_TSDEVICE",GSLC_DEV_TOUCH,1);
  setenv((char*)"TSLIB_CALIBFILE",(char*)"/etc/pointercal",1);
  setenv((char*)"TSLIB_CONFFILE",(char*)"/etc/ts.conf",1);
  setenv((char*)"TSLIB_PLUGINDIR",(char*)"/usr/local/lib/ts",1);
#endif

}

// Define debug message function
static int16_t DebugOut(char ch) { fputc(ch,stderr); return 0; }


// Button callbacks
bool CbBtnQuit(void* pvGui, void *pvElemRef, gslc_teTouch eTouch, int16_t nX, int16_t nY)
{
  if (eTouch == GSLC_TOUCH_UP_IN) {
    m_bQuit = true;
  }
  return true;
}


// Callback function for when a slider's position has been updated
bool CbSlidePos(void* pvGui, void* pvElemRef, int16_t nPos)
{
  gslc_tsGui*     pGui     = (gslc_tsGui*)(pvGui);
  gslc_tsElemRef* pElemRef = (gslc_tsElemRef*)(pvElemRef);
  gslc_tsElem*    pElem    = gslc_GetElemFromRef(pGui,pElemRef);
  //gslc_tsXSlider* pSlider = (gslc_tsXSlider*)(pElem->pXData);

  switch (pElem->nId) {
  case E_ELEM_SLIDER:
    m_nSliderPos = gslc_ElemXSliderGetPos(pGui, pElemRef);
    break;
  default:
    break;
  }
  return true;
}

// Create page elements
bool InitOverlays()
{
  gslc_tsElemRef* pElemRef = NULL;
  
  // -----------------------------------
  // Create page elements
  gslc_PageAdd(&m_gui, E_PG_MAIN, m_asPageElem, MAX_ELEM_PG_MAIN, m_asPageElemRef, MAX_ELEM_PG_MAIN);

  // Background flat color
  gslc_SetBkgndColor(&m_gui, GSLC_COL_GRAY_DK2);

  // Create background box
  pElemRef = gslc_ElemCreateBox(&m_gui, E_ELEM_BOX, E_PG_MAIN, (gslc_tsRect) { 10, 50, 300, 150 });
  gslc_ElemSetCol(&m_gui, pElemRef, GSLC_COL_WHITE, GSLC_COL_BLACK, GSLC_COL_BLACK);

  // Create Quit button with text label
  pElemRef = gslc_ElemCreateBtnTxt(&m_gui, E_ELEM_BTN_QUIT, E_PG_MAIN,
    (gslc_tsRect) { 235, 5, 80, 30 }, (char*)"Quit", 0, E_FONT_BTN, &CbBtnQuit);

  // Create a Glowball
  pElemRef = gslc_ElemXGlowballCreate(&m_gui, E_ELEM_XGLOW, E_PG_MAIN, &m_sXGlowball,
    120, 120, asRings, NUM_RINGS);
  gslc_ElemXGlowballSetColorBack(&m_gui, pElemRef, GSLC_COL_BLACK);
  gslc_ElemXGlowballSetAngles(&m_gui, pElemRef, 0, 360); // Full-circle (default)
  //gslc_ElemXGlowballSetAngles(&m_gui, pElemRef, 270, 360 + 90); // Semi-circle
  //gslc_ElemXGlowballSetAngles(&m_gui, pElemRef, 0, 90); // Quarter-circle
  m_pElemXGlowball = pElemRef; // Save for quick access


   // Create slider
  pElemRef = gslc_ElemXSliderCreate(&m_gui, E_ELEM_SLIDER, E_PG_MAIN, &m_sXSlider,
    (gslc_tsRect) { 200, 80, 100, 20 }, 0, 9, 0, 5, false);
  gslc_ElemXSliderSetStyle(&m_gui, pElemRef, true, (gslc_tsColor) { 0, 0, 128 }, 10,
    5, (gslc_tsColor) { 64, 64, 64 });
  gslc_ElemXSliderSetPosFunc(&m_gui, pElemRef, &CbSlidePos);
  m_pElemSlider = pElemRef; // Save for quick access
  
  return true;
}


int main( int argc, char* args[] )
{

  // -----------------------------------
  // Initialize
  gslc_InitDebug(&DebugOut);
  UserInitEnv();
  if (!gslc_Init(&m_gui,&m_drv,m_asPage,MAX_PAGE,m_asFont,MAX_FONT)) { exit(1); }

  // Load Fonts
  // - Normally we would select a number of different fonts 
  if (!gslc_FontSet(&m_gui, E_FONT_BTN, GSLC_FONTREF_FNAME, FONT_FNAME_BTN, 10)) { return 0; }

  // -----------------------------------
  // Start display
  InitOverlays();

  // Start up display on main page
  gslc_SetPageCur(&m_gui,E_PG_MAIN);

  m_bQuit = false;

  // Initialize startup state
  m_nSliderPos = gslc_ElemXSliderGetPos(&m_gui, m_pElemSlider);

  // -----------------------------------
  // Main event loop
  while (!m_bQuit) {
  
    // Update the XGlowball value with the slider position
    gslc_ElemXGlowballSetVal(&m_gui, m_pElemXGlowball, m_nSliderPos);

    gslc_Update(&m_gui);
  }

  // -----------------------------------
  // Close down display
  gslc_Quit(&m_gui);

  return 0;
}


<|start_filename|>src/GUIslice_th.h<|end_filename|>
// Abstract touch handler (TH) class
// This is the abstract base class for creating specific touch handlers 
// The touch handler performs the adaption in between the GUIslice framework and any touch driver
// The touch handler used is specified in the main program by calling gslc_InitTouchHandler(&touchHandler);

/// \file GUIslice_th.h

#ifndef _GUISLICE_TH_H_
#define _GUISLICE_TH_H_

#include <Arduino.h>

///////////////////////////////////////////////////
// Point x,y,z Class for usage in the touch handler

class THPoint {
 public:
  THPoint(void);
  THPoint(uint16_t x, uint16_t y, uint16_t z);
  
  bool operator==(THPoint);
  bool operator!=(THPoint);

  uint16_t x, y, z;
};


///////////////////////////////////////////////////
// Abstract TouchHandler - you have to inherit this 

class TouchHandler {
 public:
  //in order to create a specific touch handler you have to write your own constructor
  TouchHandler() {};

  void setSize(uint16_t _disp_xSize, uint16_t _disp_ySize);
  void setCalibration(uint16_t ts_xMin, uint16_t ts_xMax, uint16_t ts_yMin, uint16_t ts_yMax);
  //order of operations: map, swap, constraint, flip 
  void setSwapFlip(bool _swapXY,bool _flipX,bool _flipY);

  THPoint scale(THPoint pIn);
  
  //in order to create a specific touch handler you have to overwrite this methods
  virtual void begin(void);
  virtual THPoint getPoint(void);

private:
    //landscape perspective: x: width, y: heigth
    uint16_t disp_xSize = 320;
    uint16_t disp_ySize = 240;

    uint16_t ts_xMin = 0;
    uint16_t ts_xMax = 4095;
    uint16_t ts_yMin = 0;
    uint16_t ts_yMax = 4095;
    
    bool swapXY = false;    
    bool flipX = false;    
    bool flipY = false;    
};



/////////////////////////////////
// init and set the touch handler 

void gslc_InitTouchHandler(TouchHandler *pTHO);     
TouchHandler* gslc_getTouchHandler(void);     


#endif


<|start_filename|>examples/linux/test_sdl1.c<|end_filename|>
#include "SDL/SDL.h"
#include "SDL/SDL_getenv.h"

//
// GUIslice sample SDL test for SDL1.2
// - <NAME>
// - https://www.impulseadventure.com/elec/guislice-gui.html
// - https://github.com/ImpulseAdventure/GUIslice
//
// This program is a simple test to ensure that the SDL
// library is installed correctly. If everything is
// working then you should see the external screen
// (such as PiTFT) display a red-to-blue gradient and
// a green box.
//

// Define the primary surface for display
SDL_Surface* scrMain = NULL;

// Main entrypoint

int main(int argc, char* args[]) {
  // --------------------------------------
  // Initialization
  // --------------------------------------

  // Update the environment variables for SDL to
  // work correctly with the external display on
  // LINUX frame buffer 1 (fb1).
  putenv((char*) "FRAMEBUFFER=/dev/fb1");
  putenv((char*) "SDL_FBDEV=/dev/fb1");

  // Initialize SDL
  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    fprintf(stderr, "ERROR in SDL_Init(): %s\n", SDL_GetError());
    return 0;
  }
  atexit(SDL_Quit);

  // Fetch the best video mode
  // - Note that the Raspberry Pi generally defaults
  //   to a 16bits/pixel framebuffer
  const SDL_VideoInfo* vInfo = SDL_GetVideoInfo();
  if (!vInfo) {
    fprintf(stderr, "ERROR in SDL_GetVideoInfo(): %s\n", SDL_GetError());
    return 0;
  }
  int nResX = vInfo->current_w;
  int nResY = vInfo->current_h;
  int nDepth = vInfo->vfmt->BitsPerPixel;

  // Configure the video mode
  // - SDL_SWSURFACE appears to be most robust mode
  int nFlags = SDL_SWSURFACE | SDL_FULLSCREEN;
  scrMain = SDL_SetVideoMode(nResX, nResY, nDepth, nFlags);
  if (scrMain == 0) {
    fprintf(stderr, "ERROR in SDL_SetVideoMode(): %s\n", SDL_GetError());
    return 0;
  }


  // --------------------------------------
  // Perform some simple drawing primitives
  // --------------------------------------

  // Draw a gradient from red to blue
  SDL_Rect  rectTmp;
  Uint32    nColTmp;
  Uint16    nPosX;
  for (nPosX = 0; nPosX < nResX; nPosX++) {
    rectTmp.x = nPosX;
    rectTmp.y = nResY / 2;
    rectTmp.w = 1;
    rectTmp.h = nResY / 2;
    nColTmp = SDL_MapRGB(scrMain->format, nPosX % 256, 0, 255 - (nPosX % 256));
    SDL_FillRect(scrMain, &rectTmp, nColTmp);
  }

  // Draw a green box
  Uint32 nColGreen = SDL_MapRGB(scrMain->format, 0, 255, 0);
  SDL_Rect rectBox = {0, 0, (Uint16) (nResX), (Uint16) (nResY / 2)};
  SDL_FillRect(scrMain, &rectBox, nColGreen);


  // Now that we've completed drawing, update the main display
  SDL_Flip(scrMain);

  // Wait for a short delay
  SDL_Delay(3000);

  // Close down SDL
  SDL_Quit();

  return 0;
}


<|start_filename|>examples/linux/ex01_lnx_basic.c<|end_filename|>
//
// GUIslice Library Examples
// - <NAME>
// - https://www.impulseadventure.com/elec/guislice-gui.html
// - https://github.com/ImpulseAdventure/GUIslice
// - Example 01: Display a box
//   No touchscreen enabled
//

#include "GUIslice.h"
#include "GUIslice_drv.h"

#include <unistd.h>      // For sleep()


// Enumerations for pages, elements, fonts, images
enum {E_PG_MAIN};
enum {E_ELEM_BOX};

// Instantiate the GUI
#define MAX_PAGE            1
#define MAX_ELEM_PG_MAIN    5

gslc_tsGui                  m_gui;
gslc_tsDriver               m_drv;
gslc_tsPage                 m_asPage[MAX_PAGE];
gslc_tsElem                 m_asPageElem[MAX_ELEM_PG_MAIN];
gslc_tsElemRef              m_asPageElemRef[MAX_ELEM_PG_MAIN];


// Configure environment variables suitable for display
// - These may need modification to match your system
//   environment and display type
// - Defaults for GSLC_DEV_FB and GSLC_DEV_TOUCH are in GUIslice_config.h
// - Note that the environment variable settings can
//   also be set directly within the shell via export
//   (or init script).
//   - eg. export TSLIB_FBDEVICE=/dev/fb1
void UserInitEnv()
{
#if defined(DRV_DISP_SDL1) || defined(DRV_DISP_SDL2)
  setenv((char*)"FRAMEBUFFER",GSLC_DEV_FB,1);
  setenv((char*)"SDL_FBDEV",GSLC_DEV_FB,1);
  setenv((char*)"SDL_VIDEODRIVER",GSLC_DEV_VID_DRV,1);
#endif

#if defined(DRV_TOUCH_TSLIB)
  setenv((char*)"TSLIB_FBDEVICE",GSLC_DEV_FB,1);
  setenv((char*)"TSLIB_TSDEVICE",GSLC_DEV_TOUCH,1);
  setenv((char*)"TSLIB_CALIBFILE",(char*)"/etc/pointercal",1);
  setenv((char*)"TSLIB_CONFFILE",(char*)"/etc/ts.conf",1);
  setenv((char*)"TSLIB_PLUGINDIR",(char*)"/usr/local/lib/ts",1);
#endif
}

// Define debug message function
static int16_t DebugOut(char ch) { fputc(ch,stderr); return 0; }

int main( int argc, char* args[] )
{
  gslc_tsElemRef*  pElemRef = NULL;

  // Initialize
  gslc_InitDebug(&DebugOut);
  UserInitEnv();
  if (!gslc_Init(&m_gui,&m_drv,m_asPage,MAX_PAGE,NULL,0)) { exit(1); }

  gslc_PageAdd(&m_gui,E_PG_MAIN,m_asPageElem,MAX_ELEM_PG_MAIN,m_asPageElemRef,MAX_ELEM_PG_MAIN);

  // Background flat color
  gslc_SetBkgndColor(&m_gui,GSLC_COL_GRAY_DK2);

  // Create page elements
  pElemRef = gslc_ElemCreateBox(&m_gui,E_ELEM_BOX,E_PG_MAIN,(gslc_tsRect){10,50,300,150});
  gslc_ElemSetCol(&m_gui,pElemRef,GSLC_COL_WHITE,GSLC_COL_BLACK,GSLC_COL_BLACK);

  // Start up display on main page
  gslc_SetPageCur(&m_gui,E_PG_MAIN);

  // Periodically call GUIslice update function
  gslc_Update(&m_gui);

  // Main code here...
  // In a real program, we would detect button presses and take action.
  // For this simple demo, we will terminate after 2 seconds.
  sleep(2);

  // Close down display
  gslc_Quit(&m_gui);

  return 0;
}



<|start_filename|>src/GUIslice_th_XPT2046.h<|end_filename|>
// touch handler (TH) for XPT2046 using the arduino built in driver <XPT2046_touch.h>

// The touch handler performs the adaption in between the GUIslice framework and any touch driver
// The touch handler used is specified in the main program by calling gslc_InitTouchHandler(&touchHandler);

/// \file GUIslice_th_XPT2046.h

#ifndef _GUISLICE_TH_XPT2046_H_
#define _GUISLICE_TH_XPT2046_H_

#include <Arduino.h>
#include <GUIslice_th.h>
#include <XPT2046_touch.h>

class TouchHandler_XPT2046: public TouchHandler {
    public:
        // parameters:
        //   spi object to be used
        //   chip select pin for spi
        TouchHandler_XPT2046(SPIClass &spi, uint8_t spi_cs_pin) : spi(spi), touchDriver(XPT2046_touch(spi_cs_pin, spi)) {
            //empirical calibration values, can be updated by calling setCalibration in the user program
            setCalibration(398,3877,280,3805);
            //swapping and flipping to adopt to default GUIslice orientation
            setSwapFlip(true,false,true);
        }            
   
        //begin is called by gslc_InitTouchHandler
        void begin(void) {
            //init the touch driver
            touchDriver.begin();
        }
                   
        //this method returns the scaled point provided by the touch driver 
        THPoint getPoint(void) {
            //get the coordinates from the touch driver
            TS_Point pt = touchDriver.getPoint();
            //Serial.print("pt= ");Serial.print(pt.x);Serial.print(",");Serial.print(pt.y);Serial.print(",");Serial.println(pt.z);
    
            //perform scaling (this includes swapping and flipping)
            THPoint ps = scale( THPoint(pt.x,pt.y,pt.z) );
            //Serial.print("ps= ");Serial.print(ps.x);Serial.print(",");Serial.print(ps.y);Serial.print(",");Serial.println(ps.z);
            
            return ps;
        };

      SPIClass spi; 
      XPT2046_touch touchDriver;
            
};


#endif
