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

« Back to documentation index

Show/hide line numbers ANIMATION_Basics.c Source File

ANIMATION_Basics.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        : ANIMATION_Basics.c
00041 Purpose     : Sample showing the basics of using the animation object.
00042 Requirements: WindowManager - ( )
00043               MemoryDevices - ( )
00044               AntiAliasing  - ( )
00045               VNC-Server    - ( )
00046               PNG-Library   - ( )
00047               TrueTypeFonts - ( )
00048 ----------------------------------------------------------------------
00049 */
00050 
00051 #include <stddef.h>
00052 
00053 #include "GUI.h"
00054 
00055 /*********************************************************************
00056 *
00057 *       Types
00058 *
00059 **********************************************************************
00060 */
00061 typedef struct {
00062   int xSize, ySize;
00063   int xPart, yPart;
00064   int xPos, yPos;
00065   int Dir;
00066   int Size;
00067   int ObjectSize;
00068 } ANIM_DATA;
00069 
00070 /*********************************************************************
00071 *
00072 *       Static code
00073 *
00074 **********************************************************************
00075 */
00076 /*********************************************************************
00077 *
00078 *       _CalcPosition
00079 *
00080 *  Purpose:
00081 *    Optional application defined position calculation in dependence
00082 *    of the point of time within the animations timeline.
00083 */
00084 static I32 _CalcPosition(GUI_TIMER_TIME ts, GUI_TIMER_TIME te, GUI_TIMER_TIME tNow) {
00085   I32 Result;
00086   int Diff, Pos, Modulation;
00087 
00088   Diff = te - ts;
00089   //
00090   // Linear calculation
00091   //
00092   if (Diff) {
00093     Result = (tNow * GUI_ANIM_RANGE) / (te - ts);
00094   } else {
00095     Result = GUI_ANIM_RANGE;
00096   }
00097   //
00098   // Modulation
00099   //
00100   Modulation = GUI_ANIM_RANGE >> 3;
00101   Pos = Result / Modulation;
00102   Result -= Modulation * Pos;
00103   if (Pos & 1) {
00104     Result  = Modulation - Result;
00105     Result  = (Result * Result) / Modulation;
00106     Result  = Modulation - Result;
00107   } else {
00108     Result  = (Result * Result) / Modulation;
00109   }
00110   Result += Modulation * Pos;
00111   return Result;
00112 }
00113 
00114 /*********************************************************************
00115 *
00116 *       _SliceInfo
00117 *
00118 *  Purpose:
00119 *    Called before the first and after the last animation item of one
00120 *    'slice' is drawn. A 'slice' means one or more animation item
00121 *    which need to be drawn at a determined point in time.
00122 *
00123 *    That mechanism is used here to avoid flickering.
00124 */
00125 static void _SliceInfo(int State, void * pVoid) {
00126   GUI_USE_PARA(pVoid);
00127   switch (State) {
00128   case GUI_ANIM_START:
00129     GUI_MULTIBUF_Begin();
00130     break;
00131   case GUI_ANIM_END:
00132     GUI_MULTIBUF_End();
00133     break;
00134   }
00135 }
00136 
00137 /*********************************************************************
00138 *
00139 *       _PrepareDrawing
00140 *
00141 *  Parameters:
00142 *    pInfo   - Animation information passed by emWin to the application
00143 *    pVoid   - Application defined void pointer
00144 *    ppData  - Data (pointer) pointer to application data
00145 *    xPosOld - Position to be used for clearing the display
00146 *    Index   - Index of animation
00147 */
00148 static void _PrepareDrawing(GUI_ANIM_INFO * pInfo, void * pVoid, ANIM_DATA ** ppData, int xPosOld, int Index) {
00149   ANIM_DATA * pData;
00150 
00151   //
00152   // Use custom void pointer for pointing to application defined data structure
00153   //
00154   pData = *ppData = (ANIM_DATA *)pVoid;
00155   //
00156   // Calculate x-position in dependence of current animation value
00157   //
00158   switch (pData->Dir) {
00159   case +1:
00160     pData->xPos = pData->xPart + ((pData->xSize - pData->xPart * 2) * pInfo->Pos) / GUI_ANIM_RANGE;
00161     break;
00162   case -1:
00163     pData->xPos = pData->xSize - pData->xPart - 1 - ((pData->xSize - pData->xPart * 2) * pInfo->Pos) / GUI_ANIM_RANGE;
00164     break;
00165   }
00166   //
00167   // Calculate object size in dependence of position
00168   //
00169   pData->ObjectSize = (pData->Size * pInfo->Pos) / GUI_ANIM_RANGE;
00170   //
00171   // Calculate y-position in dependence of animation index
00172   //
00173   pData->yPos = pData->yPart * (2 * Index + 1);
00174   //
00175   // Clears the area of the previous drawing
00176   //
00177   if (xPosOld) {
00178     GUI_ClearRect(xPosOld - pData->yPart, pData->yPos - pData->yPart, xPosOld + pData->yPart, pData->yPos + pData->yPart - 1);
00179   }
00180   //
00181   // Set item color in dependence of animation state
00182   //
00183   switch (pInfo->State) {
00184   case GUI_ANIM_END:
00185     pData->Dir = ((((pData->Dir + 1) / 2) ^ 1) * 2) - 1;
00186     //lint -fallthrough
00187   case GUI_ANIM_START:
00188     GUI_SetColor(GUI_RED);
00189     break;
00190   case GUI_ANIM_RUNNING:
00191     GUI_SetColor(GUI_GREEN);
00192     break;
00193   }
00194 }
00195 
00196 /*********************************************************************
00197 *
00198 *       _AnimDrawCircle
00199 */
00200 static void _AnimDrawCircle(GUI_ANIM_INFO * pInfo, void * pVoid) {
00201   ANIM_DATA * pData;
00202   static int xPosOld;
00203 
00204   _PrepareDrawing(pInfo, pVoid, &pData, xPosOld, 0);
00205   GUI_DrawCircle(pData->xPos, pData->yPos, pData->ObjectSize);
00206   xPosOld = pData->xPos;
00207 }
00208 
00209 /*********************************************************************
00210 *
00211 *       _AnimDrawRect
00212 */
00213 static void _AnimDrawRect(GUI_ANIM_INFO * pInfo, void * pVoid) {
00214   ANIM_DATA * pData;
00215   static int xPosOld;
00216 
00217   _PrepareDrawing(pInfo, pVoid, &pData, xPosOld, 2);
00218   GUI_DrawRect(pData->xPos - pData->ObjectSize, pData->yPos - pData->ObjectSize, pData->xPos + pData->ObjectSize, pData->yPos + pData->ObjectSize);
00219   xPosOld = pData->xPos;
00220 }
00221 
00222 /*********************************************************************
00223 *
00224 *       _AnimFillCircle
00225 */
00226 static void _AnimFillCircle(GUI_ANIM_INFO * pInfo, void * pVoid) {
00227   ANIM_DATA * pData;
00228   static int xPosOld;
00229 
00230   _PrepareDrawing(pInfo, pVoid, &pData, xPosOld, 1);
00231   GUI_FillCircle(pData->xPos, pData->yPos, pData->ObjectSize);
00232   xPosOld = pData->xPos;
00233 }
00234 
00235 /*********************************************************************
00236 *
00237 *       _AnimFillRect
00238 */
00239 static void _AnimFillRect(GUI_ANIM_INFO * pInfo, void * pVoid) {
00240   ANIM_DATA * pData;
00241   static int xPosOld;
00242 
00243   _PrepareDrawing(pInfo, pVoid, &pData, xPosOld, 3);
00244   GUI_FillRect(pData->xPos - pData->ObjectSize, pData->yPos - pData->ObjectSize, pData->xPos + pData->ObjectSize, pData->yPos + pData->ObjectSize);
00245   xPosOld = pData->xPos;
00246 }
00247 
00248 /*********************************************************************
00249 *
00250 *       _AnimDrawCross
00251 */
00252 static void _AnimDrawCross(GUI_ANIM_INFO * pInfo, void * pVoid) {
00253   ANIM_DATA * pData;
00254   static int xPosOld;
00255 
00256   _PrepareDrawing(pInfo, pVoid, &pData, xPosOld, 4);
00257   GUI_DrawHLine(pData->yPos, pData->xPos - pData->ObjectSize, pData->xPos + pData->ObjectSize);
00258   GUI_DrawVLine(pData->xPos, pData->yPos - pData->ObjectSize, pData->yPos + pData->ObjectSize);
00259   xPosOld = pData->xPos;
00260 }
00261 
00262 /*********************************************************************
00263 *
00264 *       _SetupAnimationData
00265 */
00266 static void _SetupAnimationData(ANIM_DATA * pData) {
00267   //
00268   // Get display size
00269   //
00270   pData->xSize = LCD_GetXSize();
00271   pData->ySize = LCD_GetYSize();
00272   pData->xPart = pData->xSize / 10;
00273   pData->yPart = pData->ySize / 10;
00274   pData->Size  = (pData->yPart * 4) / 5;
00275   pData->Dir   = 1;
00276 }
00277 
00278 /*********************************************************************
00279 *
00280 *       _AnimCreate
00281 */
00282 static void _AnimCreate(ANIM_DATA * pData) {
00283   GUI_ANIM_HANDLE hAnim;
00284 
00285   //
00286   // Create animation object
00287   //
00288   //   Remark: The min time/frame here is 100 to be able to notice
00289   //           the colors. In a real application this value should
00290   //           be significantly smaller to ensure a smooth motion.
00291   //
00292   //   Slice callback routine --------------+
00293   //   Custom *void pointer --------+       |
00294   //   Minimum time per frame --+   |       |
00295   //   Duration ----------+     |   |       |
00296   //                      |     |   |       |
00297   hAnim = GUI_ANIM_Create(4000, 50, pData, _SliceInfo);
00298   //
00299   // Add animation items
00300   //
00301   //   Animation routine to be called ----------------------------+
00302   //   Custom *void pointer ---------------------------+          |
00303   //   Method of position calculation +                |          |
00304   //   End on timeline ---------+     |                |          |
00305   //   Start on timeline -+     |     |                |          |
00306   //                      |     |     |                |          |
00307   GUI_ANIM_AddItem(hAnim,    0, 2000, ANIM_ACCEL,      pData + 0, _AnimDrawCircle);
00308   GUI_ANIM_AddItem(hAnim,  500, 2500, ANIM_DECEL,      pData + 1, _AnimDrawRect);
00309   GUI_ANIM_AddItem(hAnim, 1000, 3000, ANIM_ACCELDECEL, pData + 2, _AnimFillCircle);
00310   GUI_ANIM_AddItem(hAnim, 1500, 3500, ANIM_LINEAR,     pData + 3, _AnimFillRect);
00311   GUI_ANIM_AddItem(hAnim, 2000, 4000, _CalcPosition,   pData + 4, _AnimDrawCross);  // Item with custom defined position calculation
00312   //
00313   // Start animation
00314   //
00315   // Function to be called on animation delete --+
00316   //                           +-----------------+
00317   // Num loops ------------+   |
00318   // Animation ------+     |   |
00319   //                 |     |   |
00320   GUI_ANIM_StartEx(hAnim, -1, NULL);  // -1 means endless loop
00321 }
00322 
00323 /*********************************************************************
00324 *
00325 *       Public code
00326 *
00327 **********************************************************************
00328 */
00329 /*********************************************************************
00330 *
00331 *       MainTask
00332 */
00333 void MainTask(void) {
00334   ANIM_DATA aData[5];
00335   int i;
00336 
00337   GUI_Init();
00338   //
00339   // Initialize some data used for drawing operations
00340   //
00341   for (i = 0; i < (int)GUI_COUNTOF(aData); i++) {
00342     _SetupAnimationData(&aData[i]);
00343   }
00344   //
00345   // Create animation and pass pointer to user data and animation context
00346   //
00347   _AnimCreate(aData);
00348   //
00349   // Keep alive...
00350   //
00351   while (1) {
00352     GUI_Delay(100);
00353   }
00354 }
00355 
00356 /*************************** End of file ****************************/