Difference between revisions of "YUV device (Sample)"
(→Code) |
|||
Line 33: | Line 33: | ||
********************************************************************** |
********************************************************************** |
||
* * |
* * |
||
− | * (c) 1996 - |
+ | * (c) 1996 - 2022 SEGGER Microcontroller GmbH * |
* * |
* * |
||
* Internet: www.segger.com Support: support@segger.com * |
* Internet: www.segger.com Support: support@segger.com * |
||
Line 39: | Line 39: | ||
********************************************************************** |
********************************************************************** |
||
− | ** emWin V6. |
+ | ** emWin V6.24 - Graphical user interface for embedded applications ** |
emWin is protected by international copyright laws. Knowledge of the |
emWin is protected by international copyright laws. Knowledge of the |
||
source code may not be used to write a similar product. This file may |
source code may not be used to write a similar product. This file may |
||
Line 56: | Line 56: | ||
PNG-Library - ( ) |
PNG-Library - ( ) |
||
TrueTypeFonts - ( ) |
TrueTypeFonts - ( ) |
||
+ | Wiki link : https://wiki.segger.com/YUV_device_(Sample) |
||
---------------------------END-OF-HEADER------------------------------ |
---------------------------END-OF-HEADER------------------------------ |
||
*/ |
*/ |
||
Line 79: | Line 80: | ||
/********************************************************************* |
/********************************************************************* |
||
* |
* |
||
− | * Static data |
+ | * Static const data |
* |
* |
||
********************************************************************** |
********************************************************************** |
||
Line 87: | Line 88: | ||
* _aColor |
* _aColor |
||
*/ |
*/ |
||
− | static GUI_COLOR _aColor[] = { |
+ | static const GUI_COLOR _aColor[] = { |
GUI_BLUE, |
GUI_BLUE, |
||
GUI_GREEN, |
GUI_GREEN, |
||
Line 114: | Line 115: | ||
GUI_ORANGE, |
GUI_ORANGE, |
||
}; |
}; |
||
+ | |||
+ | /********************************************************************* |
||
+ | * |
||
+ | * Static data |
||
+ | * |
||
+ | ********************************************************************** |
||
+ | */ |
||
+ | static WM_HWIN hWinYUV; |
||
/********************************************************************* |
/********************************************************************* |
||
Line 121: | Line 130: | ||
********************************************************************** |
********************************************************************** |
||
*/ |
*/ |
||
+ | /********************************************************************* |
||
+ | * |
||
+ | * _IsPointInRect |
||
+ | */ |
||
+ | int _IsPointInRect(GUI_RECT * pRect, int x, int y) { |
||
+ | if ((x >= pRect->x0) && (x <= pRect->x1) && (y >= pRect->y0) && (y <= pRect->y1)) { |
||
+ | return 1; |
||
+ | } else { |
||
+ | return 0; |
||
+ | } |
||
+ | } |
||
+ | |||
+ | /********************************************************************* |
||
+ | * |
||
+ | * _CheckPos |
||
+ | */ |
||
+ | static void _CheckPos(GUI_PID_STATE * pState, int * px, int * py) { |
||
+ | GUI_RECT RectYUV; |
||
+ | |||
+ | // |
||
+ | // Check if point is within the YUV window. |
||
+ | // |
||
+ | WM_GetClientRectEx(hWinYUV, &RectYUV); |
||
+ | if (_IsPointInRect(&RectYUV, pState->x, pState->y)) { |
||
+ | // |
||
+ | // Save position. |
||
+ | // |
||
+ | *px = pState->x; |
||
+ | *py = pState->y; |
||
+ | // |
||
+ | // Invalidate main window. |
||
+ | // |
||
+ | WM_InvalidateWindow(WM_GetParent(hWinYUV)); |
||
+ | } |
||
+ | } |
||
+ | |||
/********************************************************************* |
/********************************************************************* |
||
* |
* |
||
Line 166: | Line 211: | ||
*/ |
*/ |
||
static void _cbBk(WM_MESSAGE * pMsg) { |
static void _cbBk(WM_MESSAGE * pMsg) { |
||
− | static WM_HWIN hWin; |
||
char acBuffer[64]; |
char acBuffer[64]; |
||
GUI_PID_STATE * pState; |
GUI_PID_STATE * pState; |
||
Line 180: | Line 224: | ||
// |
// |
||
// Create child window to display random pixels for YUV data. |
// Create child window to display random pixels for YUV data. |
||
+ | // YUV devices are created for a given layer. This window is only |
||
+ | // created for demonstration purposes in this sample. |
||
// |
// |
||
− | + | hWinYUV = WM_CreateWindowAsChild(10, 10, YUV_AREA_SIZE, YUV_AREA_SIZE, pMsg->hWin, WM_CF_SHOW, _cbYUV, 0); |
|
break; |
break; |
||
case WM_PAINT: |
case WM_PAINT: |
||
Line 240: | Line 286: | ||
case WM_TOUCH_CHILD: |
case WM_TOUCH_CHILD: |
||
pState = (GUI_PID_STATE *)((WM_MESSAGE *)pMsg->Data.p)->Data.p; |
pState = (GUI_PID_STATE *)((WM_MESSAGE *)pMsg->Data.p)->Data.p; |
||
+ | _CheckPos(pState, &x, &y); |
||
− | x = pState->x; |
||
− | y = pState->y; |
||
− | WM_InvalidateWindow(pMsg->hWin); |
||
break; |
break; |
||
case WM_TOUCH: |
case WM_TOUCH: |
||
Line 249: | Line 293: | ||
#endif |
#endif |
||
pState = (GUI_PID_STATE *)pMsg->Data.p; |
pState = (GUI_PID_STATE *)pMsg->Data.p; |
||
+ | _CheckPos(pState, &x, &y); |
||
− | x = pState->x; |
||
− | y = pState->y; |
||
− | WM_InvalidateWindow(pMsg->hWin); |
||
break; |
break; |
||
case MSG_NEW_COLORS: |
case MSG_NEW_COLORS: |
||
− | WM_SendMessageNoPara( |
+ | WM_SendMessageNoPara(hWinYUV, MSG_NEW_COLORS); |
break; |
break; |
||
default: |
default: |
Latest revision as of 11:01, 22 March 2022
GUI_YUV_PixelData.c | |
---|---|
File(s) required |
|
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.24), 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 - 2022 SEGGER Microcontroller GmbH *
* *
* Internet: www.segger.com Support: support@segger.com *
* *
**********************************************************************
** emWin V6.24 - 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 - ( )
Wiki link : https://wiki.segger.com/YUV_device_(Sample)
---------------------------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 const data
*
**********************************************************************
*/
/*********************************************************************
*
* _aColor
*/
static const 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 data
*
**********************************************************************
*/
static WM_HWIN hWinYUV;
/*********************************************************************
*
* Static code
*
**********************************************************************
*/
/*********************************************************************
*
* _IsPointInRect
*/
int _IsPointInRect(GUI_RECT * pRect, int x, int y) {
if ((x >= pRect->x0) && (x <= pRect->x1) && (y >= pRect->y0) && (y <= pRect->y1)) {
return 1;
} else {
return 0;
}
}
/*********************************************************************
*
* _CheckPos
*/
static void _CheckPos(GUI_PID_STATE * pState, int * px, int * py) {
GUI_RECT RectYUV;
//
// Check if point is within the YUV window.
//
WM_GetClientRectEx(hWinYUV, &RectYUV);
if (_IsPointInRect(&RectYUV, pState->x, pState->y)) {
//
// Save position.
//
*px = pState->x;
*py = pState->y;
//
// Invalidate main window.
//
WM_InvalidateWindow(WM_GetParent(hWinYUV));
}
}
/*********************************************************************
*
* _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) {
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.
// YUV devices are created for a given layer. This window is only
// created for demonstration purposes in this sample.
//
hWinYUV = 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;
_CheckPos(pState, &x, &y);
break;
case WM_TOUCH:
#else
case WM_MOUSEOVER:
#endif
pState = (GUI_PID_STATE *)pMsg->Data.p;
_CheckPos(pState, &x, &y);
break;
case MSG_NEW_COLORS:
WM_SendMessageNoPara(hWinYUV, 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 ****************************/