External image - BMP (Sample)

From SEGGER Wiki
Jump to: navigation, search
GUI_DrawExternalImage_BMP.c
GUI CreateBitmapFromStream.png
File(s) required
  • GUI_DrawExternalImage_BMP.c
  • segger-logo.bmp
Runs in simulation Yes
Runs on target Yes
Download GUI_DrawExternalImage_BMP.zip

This sample shows how to draw BMP images in emWin from external memory. The sample 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_DrawExternalImage_BMP.c
Purpose     : Sample that shows how to draw external BMP images
              with emWin.
              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\\segger-logo.bmp"
#else
  #define FILE_PATH "segger-logo.bmp"
#endif

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       APP_GetData
*/
int APP_GetData(void * p, const U8 ** ppData, unsigned NumBytes, U32 Off) {
  static U8   acBuffer[0x200];
#ifdef WIN32
  HANDLE    * phFile;
  DWORD       NumBytesRead;
#else
  FS_FILE   * pFile;
  int         NumBytesRead;
#endif

#ifdef WIN32
  phFile = (HANDLE *)p;
  //
  // Check buffer size
  //
  if (NumBytes > sizeof(acBuffer)) {
    NumBytes = sizeof(acBuffer);
  }
  //
  // Set file pointer to the required position
  //
  SetFilePointer(*phFile, Off, 0, FILE_BEGIN);
  //
  // Read data into buffer
  //
  ReadFile(*phFile, acBuffer, NumBytes, &NumBytesRead, NULL);
  //
  // Set data pointer to the beginning of the buffer
  //
  *ppData = (const U8 *)acBuffer;
  //
  // Return number of available bytes
  //
  return NumBytesRead;
#else
  pFile = (FS_FILE *)p;
  //
  // Check buffer size
  //
  if (NumBytes > sizeof(acBuffer)) {
    NumBytes = sizeof(acBuffer);
  }
  //
  // Set file pointer to the required position
  //
  FS_SetFilePos(pFile, Off, FS_FILE_BEGIN);
  //
  // Read data into buffer
  //
  NumBytesRead = FS_FRead(acBuffer, 1, NumBytes, pFile);
  //
  // Set data pointer to the beginning of the buffer
  //
  *ppData = (const U8 *)acBuffer;
  //
  // Return number of available bytes
  //
  return NumBytesRead;
#endif
}

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

  //
  // Init GUI.
  //
  GUI_Init();
#ifdef WIN32
  //
  // Create file handle
  //
  hFile = CreateFile(FILE_PATH, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  //
  // Draw the bmp image by passing a pointer to the file handle and the GetData function
  //
  GUI_BMP_DrawEx(APP_GetData, &hFile, 10, 10);
  //
  // 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, "rb");
  }
  //
  // Draw the bmp image by passing a pointer to the file handle and the GetData function
  //
  GUI_BMP_DrawEx(APP_GetData, pFile, 10, 10);
  //
  // Close file
  //
  FS_FClose(pFile);
#endif

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

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