GRAPH - Custom (Sample)

From SEGGER Wiki
Jump to: navigation, search
GRAPH_Custom.c
GRAPH Custom.gif
File(s) required
  • GRAPH_Custom.c
Runs in simulation Yes
Runs on target Yes
Download GRAPH_Custom.c

This sample demonstrates custom drawing of a GRAPH widget.

Code

/*********************************************************************
*                    SEGGER Microcontroller GmbH                     *
*        Solutions for real time microcontroller applications        *
**********************************************************************
*                                                                    *
*        (c) 1996 - 2020  SEGGER Microcontroller GmbH                *
*                                                                    *
*        Internet: www.segger.com    Support:  support@segger.com    *
*                                                                    *
**********************************************************************

** emWin V6.10 - Graphical user interface for embedded applications **
emWin is protected by international copyright laws.   Knowledge of the
source code may not be used to write a similar product.  This file may
only  be used  in accordance  with  a license  and should  not be  re-
distributed in any way. We appreciate your understanding and fairness.
----------------------------------------------------------------------
File        : GRAPH_Custom.c
Purpose     : Sample that demonstrates how user draw callback
              functions are used with GRAPH widgets in emWin.
Requirements: WindowManager - (x)
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/

#include "DIALOG.h"

/*********************************************************************
*
*       Defines
*
**********************************************************************
*/

#define LEFT_BORDER    30
#define GRID_RECT_SIZE 10
#define Y_OFFSET       5

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
/*********************************************************************
*
*       _UserDrawGraph
*
*  Function description:
*    This function is periodically called: after the border was drawn,
*    before anything is drawn except the background being filled,
*    and at last when the final drawing operation is done.
*
*  Notes:
*    Therefore you're not able to completely draw the widget yourself,
*    but instead just add drawings.
*
*/
static void _UserDrawGraph(WM_HWIN hWin, int Stage) {
  GUI_RECT Rect;
  int      Dist;
  int      i, j;
  int      xAmount, yAmount;

  switch (Stage) {
  case GRAPH_DRAW_FIRST:
    //
    // Gives the application the possibility to perform 
    // drawing operations at the beginning of the drawing process.
    //
    
    //
    // Draw grid
    //
    GUI_SetColor(GUI_GRAY_7C);
    xAmount = (LCD_GetXSize() - LEFT_BORDER) / GRID_RECT_SIZE;
    yAmount = LCD_GetYSize() / GRID_RECT_SIZE + 1;
    for (i = 0; i < yAmount; i++) {
      for (j = 0; j < xAmount; j++) {
        GUI_DrawRect(LEFT_BORDER + (j * GRID_RECT_SIZE),
                    (i * GRID_RECT_SIZE) - Y_OFFSET,
                    LEFT_BORDER + (j * GRID_RECT_SIZE) + GRID_RECT_SIZE,
                    (i * GRID_RECT_SIZE) + GRID_RECT_SIZE - Y_OFFSET);
      }
    }
    break;
  case GRAPH_DRAW_AFTER_BORDER:
    //
    // Gives the application the possibility to perform 
    // drawing operations after the border was drawn.
    //

    //
    // Draw small extra border around the widget
    //
    WM_GetClientRect(&Rect);
    GUI_SetColor(GUI_GRAY_60);
    GUI_SetPenSize(2);
    Rect.x0 += 1;
    Rect.y0 += 1;
    Rect.x1 -= 1;
    Rect.y1 -= 1;
    GUI_DrawRectEx(&Rect);
    break;
  case GRAPH_DRAW_LAST:
    //
    // Performs final drawing operations.
    // User can draw e.g. texts or user-defined scales
    //

    //
    // Draw a header
    //
    GUI_SetTextMode(GUI_TM_TRANS);
    GUI_SetColor(GUI_RED);
    GUI_SetFont(&GUI_Font16B_1);
    Dist = GUI_GetStringDistX("Graph Widget Sample");
    GUI_DispStringAt("Graph Widget Sample", (LCD_GetXSize() / 2) - (Dist / 2), 30);
    break;
  }
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
  GRAPH_DATA_Handle  hData;
  GRAPH_SCALE_Handle hScale;
  WM_HWIN            hGraph;
  int                Angle;
  int                Value;

  //
  // Init GUI
  //
  GUI_Init();
  //
  // Enable multi-buffering to avoid flickering during the continuous update of the graph.
  //
  WM_MULTIBUF_Enable(1);
  //
  // Create graph widget
  // For more explanation on how to create graphs,
  // see the sample GUI_GRAPH_GraphUsage.c
  //
  hGraph = GRAPH_CreateEx(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_HBKWIN, WM_CF_SHOW, GRAPH_CF_GRID_FIXED_X, GUI_ID_GRAPH0);
  //GRAPH_SetGridVis(hGraph, 1);   
  //GRAPH_SetGridDistX(hGraph, 10);
  //GRAPH_SetGridDistY(hGraph, 10);
  //GRAPH_SetGridOffY(hGraph, -5); 
  GRAPH_SetBorder(hGraph, LEFT_BORDER, 0, 0, 0);
  //
  // Create data object
  //
  hData = GRAPH_DATA_YT_Create(GUI_RED, LCD_GetXSize(), NULL, 0);
  GRAPH_DATA_YT_SetAlign(hData, GRAPH_ALIGN_LEFT);
  GRAPH_DATA_YT_SetOffY(hData, 75);
  GRAPH_AttachData(hGraph, hData);
  //
  // Create scale object
  //
  hScale = GRAPH_SCALE_Create(15, GUI_TA_HCENTER | GUI_TA_VCENTER, GRAPH_SCALE_CF_VERTICAL, 1);
  GRAPH_SCALE_SetNumDecs(hScale, 0);  
  GRAPH_SCALE_SetFactor(hScale, 1);   
  GRAPH_SCALE_SetTickDist(hScale, 20);
  GRAPH_SCALE_SetOff(hScale, 130);
  GRAPH_AttachScale(hGraph, hScale);
  //
  // Set a user draw function to the graph
  //
  GRAPH_SetUserDraw(hGraph, _UserDrawGraph);
  //
  // Continuously feed the graph with data to create a sine curve
  //
  Angle = 0;
  while (1) {
    GUI_Delay(10);
    Value = GUI__SinHQ(((Angle++) % 360) * 1000);
    Value = (50 * Value) >> 16;
    GRAPH_DATA_YT_AddValue(hData, (I16)Value + 60);
  }
}

/*************************** End of file ****************************/