Serialize BMP (Sample)

From SEGGER Wiki
Jump to: navigation, search
GUI_SerializeBMP.c
GUI SerializeBMP.png
File(s) required
  • GUI_SerializeBMP.c
Runs in simulation Yes
Runs on target Yes
Download GUI_SerializeBMP.c

This sample shows how screenshots can be created by serializing the LCD contents into a .bmp file. It supports both the usage of Windows file system and emFile.

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_SerializeBMP.c
Purpose     : Sample that demonstrates how to serialize the LCD
              contents into a .BMP file, which means you can
              essentially create screenshots.
              This sample supports both usage in the Windows Simulation as well
              as usage on a target device using emFile.
Requirements: WindowManager - ( )
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/

#include "GUI.h"
#ifdef WIN32
#include <Windows.h>
#else
#include "FS.h"
#endif

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

#ifdef WIN32
  #define FILE_PATH "C:\\Work\\emWin\\Intern\\TutorialV6\\Core\\Bitmaps\\BMPSerialize.bmp"
#else
  #define FILE_PATH "BMPSerialize.bmp"
#endif

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
/*********************************************************************
*
*       _WriteByteToFile
*/
static void _WriteByteToFile(U8 Data, void * p) {
#ifdef WIN32
  U32 BytesWritten;

  WriteFile(*((HANDLE *)p), &Data, 1, &BytesWritten, NULL);
#else
  FS_FWrite(&Data, sizeof(Data), 1, ((FS_FILE *)p));
#endif
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
#ifdef WIN32
  HANDLE    hFile;
#else
  FS_FILE * pFile;
  char      acVolumeName[10];
#endif

  //
  // Initialize GUI
  //
  GUI_Init();
  //
  // Draw something
  //
  GUI_SetBkColor(GUI_BLUE);
  GUI_Clear();
  GUI_SetFont(&GUI_Font16_1);
  GUI_DispString("Serialize BMP Sample");
  GUI_DrawLine(15, 15, 100, 100);
#ifdef WIN32
  //
  // Create file handle
  //
  hFile = CreateFile(FILE_PATH, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  //
  // Serialize BMP by calling the function and passing the _WriteByteToFile callback
  //
  GUI_BMP_Serialize(_WriteByteToFile, &hFile);
  //
  // Close file handle
  //
  CloseHandle(hFile);
#else
  //
  // Start emFile
  //
  FS_Init();
  //
  // Enable long file name support
  //
  FS_FAT_SupportLFN();
  //
  // Mount volume
  //
  FS_GetVolumeName(0, acVolumeName, sizeof(acVolumeName));
  if (FS_Mount(acVolumeName) > 0) {
    //
    // Open file
    //
    pFile = FS_FOpen(FILE_PATH, "wb");
  }
  //
  // Serialize BMP by calling the function and passing the _WriteByteToFile callback
  //
  GUI_BMP_Serialize(_WriteByteToFile, pFile);
  //
  // Close file
  //
  FS_FClose(pFile);
#endif

  while (1) {
    GUI_Delay(100);
  }
}

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