Difference between revisions of "YUV device (Sample)"

From SEGGER Wiki
Jump to: navigation, search
Line 20: Line 20:
   
 
This sample demonstrates how YUV devices are created in emWin and how the user can read the data from a YUV device. Note that with the current emWin Version (V6.12), this sample cannot be executed in the simulation, but only on targets.
 
This sample demonstrates how YUV devices are created in emWin and how the user can read the data from a YUV device. Note that with the current emWin Version (V6.12), this sample cannot be executed in the simulation, but only on targets.
  +
  +
== Demo ==
  +
  +
<html><iframe src="https://wiki.segger.com/demo/emwin/GUI_YUV_PixelData/GUI_YUV_PixelData.html" width="480" height="272" style="border: 1px #c0c0c0 solid;" marginwidth="0" marginheight="0" hspace="0" vspace="0" scrolling="no"> </iframe></html>
   
 
== Code ==
 
== Code ==

Revision as of 16:21, 7 March 2022

GUI_YUV_PixelData.c
Sample showing the YUV color data for a selected pixel.
File(s) required
  • GUI_YUV_PixelData.c
Runs in simulation No
Runs on target Yes
Download GUI_YUV_PixelData.c

This sample demonstrates how YUV devices are created in emWin and how the user can read the data from a YUV device. Note that with the current emWin Version (V6.12), this sample cannot be executed in the simulation, but only on targets.

Demo

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        : GUI_YUV_PixelData.c
Purpose     : Sample that demonstrates how pixel colors can be read
              from the buffer of a YUV device in emWin.
Note        : Please note that YUV devices are supported only by
              display drivers with direct interface like GUIDRV_Lin.
Requirements: WindowManager - (x)
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/

#ifdef WIN32
#error "This sample does not currently work in the Windows Simulation."
#endif

#include "DIALOG.h"
#include <stdlib.h>
#include <stdio.h>

/*********************************************************************
*
*       Defines
*
**********************************************************************
*/
#define YUV_AREA_SIZE  150
#define MSG_NEW_COLORS (WM_USER + 1)
#define REFRESH_PERIOD 3000

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/
/*********************************************************************
*
*       _aColor
*/
static GUI_COLOR _aColor[] = {
  GUI_BLUE,
  GUI_GREEN,
  GUI_RED,
  GUI_CYAN,
  GUI_MAGENTA,
  GUI_YELLOW,
  GUI_LIGHTBLUE,
  GUI_LIGHTGREEN,
  GUI_LIGHTRED,
  GUI_LIGHTCYAN,
  GUI_LIGHTMAGENTA,
  GUI_LIGHTYELLOW,
  GUI_DARKBLUE,
  GUI_DARKGREEN,
  GUI_DARKRED,
  GUI_DARKCYAN,
  GUI_DARKMAGENTA,
  GUI_DARKYELLOW,
  GUI_WHITE,
  GUI_LIGHTGRAY,
  GUI_GRAY,
  GUI_DARKGRAY,
  GUI_BLACK,
  GUI_BROWN,
  GUI_ORANGE,
};

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
/*********************************************************************
*
*       _cbYUV
*/
static void _cbYUV(WM_MESSAGE * pMsg) {
  GUI_RECT   Rect;
  static int r[2];

  switch (pMsg->MsgId) {
  case WM_PAINT:
    //
    // Fill window with random pixels
    //
    WM_GetClientRect(&Rect);
    GUI_DrawGradientVEx(&Rect, _aColor[r[0]], _aColor[r[1]]);
    //
    // Mark window area
    //
    GUI_SetColor(GUI_BLACK);
    GUI_DrawRectEx(&Rect);
    break;
#ifdef WIN32
  case WM_MOUSEOVER:
    WM_SendToParent(pMsg->hWin, pMsg);
    break;
#endif
  case MSG_NEW_COLORS:
    srand(GUI_GetTime());
    do {
      r[0] = rand() % GUI_COUNTOF(_aColor);
      r[1] = rand() % GUI_COUNTOF(_aColor);
    } while (r[0] == r[1]);
    WM_InvalidateWindow(WM_HBKWIN);
    WM_InvalidateWindow(pMsg->hWin);
    break;
  default:
    WM_DefaultProc(pMsg);
  }
}

/*********************************************************************
*
*       _cbBk
*/
static void _cbBk(WM_MESSAGE * pMsg) {
  static WM_HWIN  hWin;
  char            acBuffer[64];
  GUI_PID_STATE * pState;
  static int      x, y;
  U32           * pData;
  U32             Size;
  U32             v;
  int             Offset;
  U8              ColorY, ColorU, ColorV;

  switch(pMsg->MsgId) {
  case WM_SET_CALLBACK:
    //
    // Create child window to display random pixels for YUV data.
    //
    hWin = WM_CreateWindowAsChild(10, 10, YUV_AREA_SIZE, YUV_AREA_SIZE, pMsg->hWin, WM_CF_SHOW, _cbYUV, 0);
    break;
  case WM_PAINT:
    GUI_SetBkColor(GUI_WHITE);
    GUI_Clear();
    //
    // Display data.
    //
    GUI_SetFont(&GUI_Font24B_1);
    GUI_SetColor(GUI_BLACK);
    GUI_DispStringAt("emWin YUV device sample", 170, 10);
    GUI_SetFont(&GUI_Font16_1);
    sprintf(acBuffer, "Selected pixel: (%d | %d)", x, y);
    GUI_DispStringAt(acBuffer, 170, 40);
    //
    // Get YUV data and process the data.
    //
    pData = GUI_YUV_GetpData(&Size);
    if (pData) {
      //
      // Calculate position in the array.
      //
      Offset  = y * (LCD_GetXSize() / 2);
      Offset += x / 2;
      //
      // Process value.
      // The value is comprised of the following:
      // 0xVVY2UUY1
      //   | | | |
      //   | | | +-- 1st byte: Y value of 1st pixel.
      //   | | +---- 2nd byte: U value of 1st and 2nd pixel.
      //   | +------ 3rd byte: Y value of 2nd pixel.
      //   +-------- 4th byte: V value of 1st and 2nd pixel.
      //
      v = *(pData + Offset);
      ColorY = (x % 2 == 0) ? v : v >> 16;
      ColorU = v >> 8;
      ColorV = v >> 24;
    }
    GUI_SetFont(&GUI_Font16B_1);
    GUI_DispStringAt("Y:", 170, 60);
    GUI_DispStringAt("U:", 170, 80);
    GUI_DispStringAt("V:", 170, 100);
    sprintf(acBuffer, "%d", ColorY);
    GUI_DispStringAt(acBuffer, 185, 60);
    sprintf(acBuffer, "%d", ColorU);
    GUI_DispStringAt(acBuffer, 185, 80);
    sprintf(acBuffer, "%d", ColorV);
    GUI_DispStringAt(acBuffer, 185, 100);
    sprintf(acBuffer, "(0x%X)", ColorY);
    GUI_DispStringAt(acBuffer, 210, 60);
    sprintf(acBuffer, "(0x%X)", ColorU);
    GUI_DispStringAt(acBuffer, 210, 80);
    sprintf(acBuffer, "(0x%X)", ColorV);
    GUI_DispStringAt(acBuffer, 210, 100);
    break;
#ifndef WIN32
  case WM_TOUCH_CHILD:
    pState = (GUI_PID_STATE *)((WM_MESSAGE *)pMsg->Data.p)->Data.p;
    x = pState->x;
    y = pState->y;
    WM_InvalidateWindow(pMsg->hWin);
    break;
  case WM_TOUCH:
#else
  case WM_MOUSEOVER:
#endif
    pState = (GUI_PID_STATE *)pMsg->Data.p;
    x = pState->x;
    y = pState->y;
    WM_InvalidateWindow(pMsg->hWin);
    break;
  case MSG_NEW_COLORS:
    WM_SendMessageNoPara(hWin, MSG_NEW_COLORS);
    break;
  default:
    WM_DefaultProc(pMsg);
  }
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
  //
  // Init emWin.
  //
  GUI_Init();
  WM_MULTIBUF_Enable(1);
  //
  // Create YUV device.
  //
  GUI_YUV_Create();
  GUI_YUV_SetPeriod(2800);
  //
  // Set callback to background window.
  //
  WM_SetCallback(WM_HBKWIN, _cbBk);
#ifndef WIN32
  GUI_CURSOR_Show();
#endif

  while(1) {
    WM_SendMessageNoPara(WM_HBKWIN, MSG_NEW_COLORS);
    GUI_Delay(3000);
  }
}

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