Andrew Reed / Mbed OS CITY1082-i2c_master_wifi_mqtt
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers 2DGL_DrawBMP.c Source File

2DGL_DrawBMP.c

00001 /*********************************************************************
00002 *                SEGGER Microcontroller GmbH                         *
00003 *        Solutions for real time microcontroller applications        *
00004 **********************************************************************
00005 *                                                                    *
00006 *        (c) 1996 - 2018  SEGGER Microcontroller GmbH                *
00007 *                                                                    *
00008 *        Internet: www.segger.com    Support:  support@segger.com    *
00009 *                                                                    *
00010 **********************************************************************
00011 
00012 ** emWin V5.48 - Graphical user interface for embedded applications **
00013 All  Intellectual Property rights  in the Software belongs to  SEGGER.
00014 emWin is protected by  international copyright laws.  Knowledge of the
00015 source code may not be used to write a similar product.  This file may
00016 only be used in accordance with the following terms:
00017 
00018 The software  has been licensed to  Cypress Semiconductor Corporation,
00019 whose registered  office is situated  at 198 Champion Ct. San Jose, CA 
00020 95134 USA  solely for the  purposes of creating  libraries for Cypress
00021 PSoC3 and  PSoC5 processor-based devices,  sublicensed and distributed
00022 under  the  terms  and  conditions  of  the  Cypress  End User License
00023 Agreement.
00024 Full source code is available at: www.segger.com
00025 
00026 We appreciate your understanding and fairness.
00027 ----------------------------------------------------------------------
00028 Licensing information
00029 Licensor:                 SEGGER Microcontroller Systems LLC
00030 Licensed to:              Cypress Semiconductor Corp, 198 Champion Ct., San Jose, CA 95134, USA
00031 Licensed SEGGER software: emWin
00032 License number:           GUI-00319
00033 License model:            Services and License Agreement, signed June 10th, 2009
00034 Licensed platform:        Any Cypress platform (Initial targets are: PSoC3, PSoC5)
00035 ----------------------------------------------------------------------
00036 Support and Update Agreement (SUA)
00037 SUA period:               2009-06-12 - 2022-07-27
00038 Contact to extend SUA:    sales@segger.com
00039 ----------------------------------------------------------------------
00040 File        : 2DGL_DrawBMP.c
00041 Purpose     : Example for drawing bitmap files
00042 Requirements: WindowManager - ( )
00043               MemoryDevices - ( )
00044               AntiAliasing  - ( )
00045               VNC-Server    - ( )
00046               PNG-Library   - ( )
00047               TrueTypeFonts - ( )
00048 
00049               Can be used in a MS Windows environment only!
00050 ----------------------------------------------------------------------
00051 */
00052 
00053 #ifndef SKIP_TEST
00054 
00055 #include <windows.h>
00056 #include <stdio.h>
00057 #include "GUI.h"
00058 
00059 /*********************************************************************
00060 *
00061 *       Defines
00062 *
00063 **********************************************************************
00064 */
00065 //
00066 // Recommended memory to run the sample with adequate performance
00067 //
00068 #define RECOMMENDED_MEMORY (1024L * 5)
00069 
00070 /*********************************************************************
00071 *
00072 *       Static data
00073 *
00074 **********************************************************************
00075 */
00076 static char _acBuffer[0x1000];
00077 
00078 /*******************************************************************
00079 *
00080 *       Static functions
00081 *
00082 ********************************************************************
00083 */
00084 /*********************************************************************
00085 *
00086 *       _GetData
00087 *
00088 * Function description
00089 *   This routine is called by the functions GUI_BMP_GetXSizeEx(),
00090 *   GUI_BMP_GetYSizeEx() and GUI_BMP_DrawEx(). The routine is responsible
00091 *   for setting the data pointer to a valid data location with at least
00092 *   one valid byte.
00093 *
00094 * Parameters
00095 *   p           - Pointer to application defined data.
00096 *   NumBytesReq - Number of bytes requested.
00097 *   ppData      - Pointer to data pointer. This pointer should be set to
00098 *                 a valid location.
00099 *   StartOfFile - If this flag is 1, the data pointer should be set to the
00100 *                 beginning of the data stream.
00101 *
00102 * Return value
00103 *   Number of data bytes available.
00104 */
00105 static int _GetData(void * p, const U8 ** ppData, unsigned NumBytesReq, U32 Off) {
00106   DWORD    NumBytesRead;
00107   HANDLE * phFile;
00108 
00109   phFile = (HANDLE *)p;
00110   //
00111   // Check buffer size
00112   //
00113   if (NumBytesReq > sizeof(_acBuffer)) {
00114     NumBytesReq = sizeof(_acBuffer);
00115   }
00116   //
00117   // Set file pointer to the required position
00118   //
00119   SetFilePointer(*phFile, Off, 0, FILE_BEGIN);
00120   //
00121   // Read data into buffer
00122   //
00123   ReadFile(*phFile, _acBuffer, NumBytesReq, &NumBytesRead, NULL);
00124   //
00125   // Set data pointer to the beginning of the buffer
00126   //
00127   *ppData = _acBuffer;
00128   //
00129   // Return number of available bytes
00130   //
00131   return NumBytesRead;
00132 }
00133 
00134 /*******************************************************************
00135 *
00136 *       _ShowBMP
00137 *
00138 * Function description
00139 *   Shows the contents of a bitmap file
00140 */
00141 static void _ShowBMP(const char * sFilename) {
00142   HANDLE hFile;
00143   int    XSize;
00144   int    YSize;
00145   int    XPos;
00146   int    YPos;
00147 
00148   hFile = CreateFile(sFilename, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
00149   GUI_ClearRect(0, 60, 319, 239);
00150   XSize = GUI_BMP_GetXSizeEx(_GetData, &hFile);
00151   YSize = GUI_BMP_GetYSizeEx(_GetData, &hFile);
00152   XPos  = (XSize > 320) ?  0 : 160 - (XSize / 2);
00153   YPos  = (YSize > 180) ? 60 : 150 - (YSize / 2);
00154   if (!GUI_BMP_DrawEx(_GetData, &hFile, XPos, YPos)) {
00155     GUI_Delay(2000);
00156   }
00157   CloseHandle(hFile);
00158 }
00159 
00160 /*******************************************************************
00161 *
00162 *       _GetFirstBitmapDirectory
00163 *
00164 * Function description
00165 *   Returns the first directory which contains one or more BMP files
00166 */
00167 static int _GetFirstBitmapDirectory(char * pDir, char * pBuffer) {
00168   WIN32_FIND_DATA Context;
00169   HANDLE          hFind;
00170   char            acMask[_MAX_PATH];
00171   char            acPath[_MAX_PATH];
00172 
00173   sprintf(acMask, "%s\\*.bmp", pDir);
00174   hFind = FindFirstFile(acMask, &Context);
00175   if (hFind != INVALID_HANDLE_VALUE) {
00176     strcpy(pBuffer, pDir);
00177     return 1;
00178   }
00179   sprintf(acMask, "%s\\*.", pDir);
00180   hFind = FindFirstFile(acMask, &Context);
00181   if (hFind != INVALID_HANDLE_VALUE) {
00182     do {
00183       if ((strcmp(Context.cFileName, ".") != 0) && (strcmp(Context.cFileName, "..") != 0)) {
00184         if (Context.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
00185           sprintf(acPath, "%s\\%s", pDir, Context.cFileName);
00186           if (_GetFirstBitmapDirectory(acPath, pBuffer)) {
00187             return 1;
00188           }
00189         }
00190       }
00191     } while (FindNextFile(hFind, &Context));
00192   }
00193   return 0;
00194 }
00195 
00196 /*******************************************************************
00197 *
00198 *       _DrawWindowsDirectoryBitmaps
00199 *
00200 * Function description
00201 *   Iterates over all *.bmp-files of the windows directory
00202 */
00203 static void _DrawWindowsDirectoryBitmaps(void) {
00204   WIN32_FIND_DATA Context;
00205   HANDLE          hFind;
00206   char            acPath[_MAX_PATH];
00207   char            acMask[_MAX_PATH];
00208   char            acFile[_MAX_PATH];
00209   char            acBuffer[_MAX_PATH];
00210 
00211   GUI_SetBkColor(GUI_BLACK);
00212   GUI_Clear();
00213   GUI_SetColor(GUI_WHITE);
00214   GUI_SetFont(&GUI_Font24_ASCII);
00215   GUI_DispStringHCenterAt("DrawBMP - Sample", 160, 5);
00216   GUI_SetFont(&GUI_Font8x16);
00217   GetWindowsDirectory(acBuffer, sizeof(acBuffer));
00218   _GetFirstBitmapDirectory(acBuffer, acPath);
00219   sprintf(acMask, "%s\\*.bmp", acPath);
00220   hFind = FindFirstFile(acMask, &Context);
00221   if (hFind != INVALID_HANDLE_VALUE) {
00222     do {
00223       sprintf(acFile, "%s\\%s", acPath, Context.cFileName);
00224       GUI_DispStringAtCEOL(acFile, 5, 40);
00225       _ShowBMP(acFile);
00226     } while (FindNextFile(hFind, &Context));
00227   }
00228 }
00229 
00230 /*********************************************************************
00231 *
00232 *       Public code
00233 *
00234 **********************************************************************
00235 */
00236 /*********************************************************************
00237 *
00238 *       MainTask
00239 */
00240 void MainTask(void) {
00241   GUI_Init();
00242   //
00243   // Check if recommended memory for the sample is available
00244   //
00245   if (GUI_ALLOC_GetNumFreeBytes() < RECOMMENDED_MEMORY) {
00246     GUI_ErrorOut("Not enough memory available."); 
00247     return;
00248   }
00249   while(1) {
00250     _DrawWindowsDirectoryBitmaps();
00251   }
00252 }
00253 
00254 #endif
00255 
00256 /*************************** End of file ****************************/