EFM32 Gecko Software Documentation  efm32g-doc-5.1.2
glib.c
Go to the documentation of this file.
1  /*************************************************************************/
16 /* Standard C header files */
17 #include <stdint.h>
18 
19 /* EM types and devices */
20 #include "em_types.h"
21 #include "em_device.h"
22 
23 /* GLIB files */
24 #include "glib.h"
25 
30 #ifndef GLIB_NO_DEFAULT_FONT
31 #define GLIB_DEFAULT_FONT ((GLIB_Font_t *)&GLIB_FontNormal8x8)
32 #endif
33 
38 static __INLINE void GLIB_colorTranslate24bppInl(uint32_t color, uint8_t *red, uint8_t *green, uint8_t *blue)
39 {
40  *red = (color >> RedShift) & 0xFF;
41  *green = (color >> GreenShift) & 0xFF;
42  *blue = (color >> BlueShift) & 0xFF;
43 }
46 /**************************************************************************/
59 EMSTATUS GLIB_contextInit(GLIB_Context_t *pContext)
60 {
61  EMSTATUS status;
62  DMD_DisplayGeometry *pTmpDisplayGeometry;
63 
64  /* Check arguments */
65  if (pContext == NULL) return GLIB_ERROR_INVALID_ARGUMENT;
66 
67  /* Sets the default background and foreground color */
68  pContext->backgroundColor = Black;
69  pContext->foregroundColor = White;
70 
71  /* Sets a pointer to the display geometry struct */
72  status = DMD_getDisplayGeometry(&pTmpDisplayGeometry);
73  if (status != DMD_OK) return status;
74 
75  pContext->pDisplayGeometry = pTmpDisplayGeometry;
76 
77  /* Sets the clipping region to the whole display */
78  GLIB_Rectangle_t tmpRect = {0, 0, pTmpDisplayGeometry->xSize - 1, pTmpDisplayGeometry->ySize - 1};
79  status = GLIB_setClippingRegion(pContext, &tmpRect);
80  if (status != GLIB_OK) return status;
81 
82  /* Configure font. Default to NORMAL 8x8 if included in project. */
83 #ifndef GLIB_NO_DEFAULT_FONT
85 #endif
86 
87  return status;
88 }
89 
90 /**************************************************************************/
98 {
99  /* Use display driver's wake up function */
100  return DMD_wakeUp();
101 }
102 
103 /**************************************************************************/
111 {
112  /* Use Display Driver sleep function */
113  return DMD_sleep();
114 }
115 
116 /**************************************************************************/
131 EMSTATUS GLIB_setClippingRegion(GLIB_Context_t *pContext, const GLIB_Rectangle_t *pRect)
132 {
133  /* Check arguments */
134  if ((pContext == NULL) || (pRect == NULL)) return GLIB_ERROR_INVALID_ARGUMENT;
135 
136  /* Check coordinates against the display region */
137  if ((pRect->xMin >= pRect->xMax) ||
138  (pRect->yMin >= pRect->yMax))
140 
141  if ((pRect->xMin < 0) ||
142  (pRect->yMin < 0) ||
143  (pRect->xMax > pContext->pDisplayGeometry->xSize - 1) ||
144  (pRect->yMax > pContext->pDisplayGeometry->ySize - 1))
145  return GLIB_OUT_OF_BOUNDS;
146 
147  GLIB_Rectangle_t tmpRect = {pRect->xMin, pRect->yMin, pRect->xMax, pRect->yMax};
148  pContext->clippingRegion = tmpRect;
149  return GLIB_applyClippingRegion(pContext);
150 }
151 
152 
153 /**************************************************************************/
163 EMSTATUS GLIB_clear(GLIB_Context_t *pContext)
164 {
165  EMSTATUS status;
166  uint8_t red;
167  uint8_t green;
168  uint8_t blue;
169  uint32_t width;
170  uint32_t height;
171 
172  /* Check arguments */
173  if (pContext == NULL) return GLIB_ERROR_INVALID_ARGUMENT;
174 
175  /* Divide the 24-color into it's components */
176  GLIB_colorTranslate24bpp(pContext->backgroundColor, &red, &green, &blue);
177 
178  /* Reset display driver clipping area */
179  status = GLIB_resetDisplayClippingArea(pContext);
180  if (status != GLIB_OK) return status;
181 
182  /* Fill the display with the background color of the GLIB_Context_t */
183  width = pContext->pDisplayGeometry->clipWidth;
184  height = pContext->pDisplayGeometry->clipHeight;
185  return DMD_writeColor(0, 0, red, green, blue, width * height);
186 }
187 
188 /**************************************************************************/
199 EMSTATUS GLIB_clearRegion(const GLIB_Context_t *pContext)
200 {
201  EMSTATUS status;
202  uint8_t red;
203  uint8_t green;
204  uint8_t blue;
205  uint32_t width;
206  uint32_t height;
207 
208  /* Check arguments */
209  if (pContext == NULL) return GLIB_ERROR_INVALID_ARGUMENT;
210 
211  /* Divide the 24-color into it's components */
212  GLIB_colorTranslate24bpp(pContext->backgroundColor, &red, &green, &blue);
213 
214  status = GLIB_applyClippingRegion(pContext);
215  if (status != DMD_OK) return status;
216 
217  /* Fill the region with the background color of the GLIB_Context_t */
218  width = pContext->clippingRegion.xMax - pContext->clippingRegion.xMin + 1;
219  height = pContext->clippingRegion.yMax - pContext->clippingRegion.yMin + 1;
220  status = DMD_writeColor(0, 0, red, green, blue, width * height);
221  if (status != DMD_OK) return status;
222 
223  return status;
224 }
225 
226 /**************************************************************************/
237 {
238  /* Check arguments */
239  if (pContext == NULL) return GLIB_ERROR_INVALID_ARGUMENT;
240 
241  return DMD_setClippingArea(0, 0, pContext->pDisplayGeometry->xSize,
242  pContext->pDisplayGeometry->ySize);
243 }
244 
245 /**************************************************************************/
256 {
257  /* Check arguments */
258  if (pContext == NULL) return GLIB_ERROR_INVALID_ARGUMENT;
259 
260  pContext->clippingRegion.xMin = 0;
261  pContext->clippingRegion.yMin = 0;
262  pContext->clippingRegion.xMax = pContext->pDisplayGeometry->xSize - 1;
263  pContext->clippingRegion.yMax = pContext->pDisplayGeometry->ySize - 1;
264 
265  return GLIB_OK;
266 }
267 
268 /**************************************************************************/
278 EMSTATUS GLIB_applyClippingRegion(const GLIB_Context_t *pContext)
279 {
280  /* Check arguments */
281  if (pContext == NULL) return GLIB_ERROR_INVALID_ARGUMENT;
282 
283  /* Reset driver clipping area to GLIB clipping region */
284  return DMD_setClippingArea(pContext->clippingRegion.xMin,
285  pContext->clippingRegion.yMin,
286  pContext->clippingRegion.xMax - pContext->clippingRegion.xMin + 1,
287  pContext->clippingRegion.yMax - pContext->clippingRegion.yMin + 1);
288 }
289 
290 /**************************************************************************/
309 void GLIB_colorTranslate24bpp(uint32_t color, uint8_t *red, uint8_t *green, uint8_t *blue)
310 {
311  GLIB_colorTranslate24bppInl(color, red, green, blue);
312 }
313 
314 /**************************************************************************/
331 uint32_t GLIB_rgbColor(uint8_t red, uint8_t green, uint8_t blue)
332 {
333  return (red << RedShift) | (green << GreenShift) | (blue << BlueShift);
334 }
335 
336 /**************************************************************************/
350 EMSTATUS GLIB_drawPixel(GLIB_Context_t *pContext, int32_t x, int32_t y)
351 {
352  uint8_t red;
353  uint8_t green;
354  uint8_t blue;
355 
356  /* Check arguments */
357  if (pContext == NULL) return GLIB_ERROR_INVALID_ARGUMENT;
359 
360  /* Translate color and draw pixel */
361  GLIB_colorTranslate24bppInl(pContext->foregroundColor, &red, &green, &blue);
362  return DMD_writeColor(x, y, red, green, blue, 1);
363 }
364 
365 /**************************************************************************/
381 EMSTATUS GLIB_drawPixelColor(GLIB_Context_t *pContext, int32_t x, int32_t y,
382  uint32_t color)
383 {
384  uint8_t red;
385  uint8_t green;
386  uint8_t blue;
387 
388  /* Check arguments */
389  if (pContext == NULL) return GLIB_ERROR_INVALID_ARGUMENT;
391 
392  /* Translate color and draw pixel */
393  GLIB_colorTranslate24bppInl(color, &red, &green, &blue);
394  return DMD_writeColor(x, y, red, green, blue, 1);
395 }
396 
397 /**************************************************************************/
421 EMSTATUS GLIB_drawPixelRGB(GLIB_Context_t *pContext, int32_t x, int32_t y,
422  uint8_t red, uint8_t green, uint8_t blue)
423 {
424  /* Check arguments */
425  if (pContext == NULL) return GLIB_ERROR_INVALID_ARGUMENT;
427 
428  /* Call Display driver function */
429  return DMD_writeColor(x, y, red, green, blue, 1);
430 }
int32_t yMax
Definition: glib.h:266
int32_t yMin
Definition: glib.h:262
#define GLIB_DEFAULT_FONT
Definition: glib.c:31
#define GLIB_ERROR_NOTHING_TO_DRAW
Definition: glib.h:192
#define Black
Definition: glib_color.h:31
#define RedShift
Definition: glib_color.h:168
const DMD_DisplayGeometry * pDisplayGeometry
Definition: glib.h:276
EMSTATUS GLIB_resetDisplayClippingArea(GLIB_Context_t *pContext)
Reset the display driver clipping area to the whole display.
Definition: glib.c:236
Rectangle structure.
Definition: glib.h:257
CMSIS Cortex-M Peripheral Access Layer for Silicon Laboratories microcontroller devices.
EMSTATUS GLIB_drawPixelRGB(GLIB_Context_t *pContext, int32_t x, int32_t y, uint8_t red, uint8_t green, uint8_t blue)
Draws a pixel at x, y with color defined by red, green and blue 1 byte per channel.
Definition: glib.c:421
#define GLIB_ERROR_INVALID_ARGUMENT
Definition: glib.h:200
EMSTATUS GLIB_displayWakeUp()
Returns the display from sleep mode.
Definition: glib.c:97
#define GreenShift
Definition: glib_color.h:170
uint32_t foregroundColor
Definition: glib.h:282
Silicon Labs Graphics Library.
EMSTATUS GLIB_clear(GLIB_Context_t *pContext)
Clears the display with the background color of the GLIB_Context_t.
Definition: glib.c:163
uint32_t GLIB_rgbColor(uint8_t red, uint8_t green, uint8_t blue)
Convert 3 uint8_t color components into a 24-bit color.
Definition: glib.c:331
void GLIB_colorTranslate24bpp(uint32_t color, uint8_t *red, uint8_t *green, uint8_t *blue)
Extracts the color components from the 32-bit color passed and puts them in the passed in 8-bits ints...
Definition: glib.c:309
#define GLIB_OUT_OF_BOUNDS
Definition: glib.h:196
#define GLIB_OK
Definition: glib.h:190
EMSTATUS GLIB_contextInit(GLIB_Context_t *pContext)
Initialize the GLIB_Context_t.
Definition: glib.c:59
EMSTATUS GLIB_applyClippingRegion(const GLIB_Context_t *pContext)
Apply the clipping region from the GLIB_Context_t in the DMD driver.
Definition: glib.c:278
bool GLIB_rectContainsPoint(const GLIB_Rectangle_t *pRect, int32_t xCenter, int32_t yCenter)
Checks if the point passed in is in the interior of the rectangle passed in.
EMSTATUS GLIB_drawPixelColor(GLIB_Context_t *pContext, int32_t x, int32_t y, uint32_t color)
Draws a pixel at x, y using the color parameter.
Definition: glib.c:381
EMSTATUS GLIB_resetClippingRegion(GLIB_Context_t *pContext)
Reset the GLIB_Context_t clipping region to the whole display.
Definition: glib.c:255
EMSTATUS GLIB_drawPixel(GLIB_Context_t *pContext, int32_t x, int32_t y)
Draws a pixel at x, y using foregroundColor defined in the GLIB_Context_t.
Definition: glib.c:350
#define White
Definition: glib_color.h:161
EMSTATUS GLIB_setFont(GLIB_Context_t *pContext, GLIB_Font_t *pFont)
Set new font for the library. Note that GLIB defines a default font in glib.c. Redefine GLIB_DEFAULT_...
Definition: glib_string.c:223
int32_t xMax
Definition: glib.h:264
EMSTATUS GLIB_clearRegion(const GLIB_Context_t *pContext)
Clears the clipping region by filling it with the background color of the GLIB_Context_t.
Definition: glib.c:199
#define BlueShift
Definition: glib_color.h:172
EMSTATUS GLIB_setClippingRegion(GLIB_Context_t *pContext, const GLIB_Rectangle_t *pRect)
Sets the clippingRegion of the passed in GLIB_Context_t.
Definition: glib.c:131
EMSTATUS GLIB_displaySleep()
Sets the display in sleep mode.
Definition: glib.c:110
#define GLIB_ERROR_INVALID_CLIPPINGREGION
Definition: glib.h:198
GLIB_Rectangle_t clippingRegion
Definition: glib.h:285
int32_t xMin
Definition: glib.h:260
uint32_t backgroundColor
Definition: glib.h:279
GLIB Drawing Context (Multiple instances of GLIB_Context_t can exist)
Definition: glib.h:273