EFR32 Mighty Gecko 13 Software Documentation  efr32mg13-doc-5.1.2
glib_string.c
Go to the documentation of this file.
1  /*************************************************************************/
16 /* Standard C header files */
17 #include <stdint.h>
18 #include <string.h>
19 #include <stdbool.h>
20 /* EM types */
21 #include "em_types.h"
22 
23 /* GLIB header files */
24 #include "glib.h"
25 #include "glib_color.h"
26 
27 /**************************************************************************/
51 EMSTATUS GLIB_drawChar(GLIB_Context_t *pContext, char myChar, int32_t x, int32_t y,
52  bool opaque)
53 {
54  EMSTATUS status;
55  uint16_t fontIdx;
56  uint8_t *pPixMap8;
57  uint16_t *pPixMap16;
58  uint32_t *pPixMap32;
59  uint16_t row;
60  uint16_t currentRow;
61  uint16_t xOffset;
62  uint32_t drawnElements = 0;
63 
64  /* Check arguments */
65  if (pContext == NULL) return GLIB_ERROR_INVALID_ARGUMENT;
66 
67  /* Check input char */
68  if ((myChar < ' ') || (myChar > '~')) return GLIB_ERROR_INVALID_CHAR;
69 
70  /* Sets the index in the font array */
71  if (pContext->font.class == NumbersOnlyFont) {
72  fontIdx = (myChar - '0');
73  if (myChar == ':') {
74  fontIdx = 10;
75  }
76  if (myChar == ' ') {
77  fontIdx = 11;
78  }
79  } else { /* FullFont class */
80  fontIdx = myChar - ' ';
81  }
82 
83  if (fontIdx > (pContext->font.cntOfMapElements - 1)) {
85  }
86 
87  /* Loop through the rows and draw the font */
88  pPixMap8 = (uint8_t *)pContext->font.pFontPixMap;
89  pPixMap16 = (uint16_t *)pContext->font.pFontPixMap;
90  pPixMap32 = (uint32_t *)pContext->font.pFontPixMap;
91 
92  for (row = 0; row < pContext->font.fontHeight; row++) {
93 
94  switch (pContext->font.sizeOfMapElement) {
95  case 1:
96  currentRow = pPixMap8[fontIdx];
97  break;
98 
99  case 2:
100  currentRow = pPixMap16[fontIdx];
101  break;
102 
103  default:
104  currentRow = pPixMap32[fontIdx];
105  }
106 
107  for (xOffset = 0; xOffset < pContext->font.fontWidth; ++xOffset) {
108  /* Bit 1 means draw, Bit 0 means do not draw */
109  if (currentRow & 0x1) {
110  status = GLIB_drawPixel(pContext, x + xOffset, y + row);
111  if (status > GLIB_ERROR_NOTHING_TO_DRAW) return status;
112  if (status == GLIB_OK) drawnElements++;
113  }
114  else if (opaque) {
115  /* Draw background pixel */
116  status = GLIB_drawPixelColor(pContext, x + xOffset, y + row, pContext->backgroundColor);
117  if (status > GLIB_ERROR_NOTHING_TO_DRAW) return status;
118  if (status == GLIB_OK) drawnElements++;
119  }
120  currentRow >>= 1;
121  }
122 
123  /* Handle character spacing */
124  for (; xOffset < pContext->font.fontWidth + pContext->font.charSpacing; ++xOffset) {
125  if (opaque) {
126  /* Draw background pixel */
127  status = GLIB_drawPixelColor(pContext, x + xOffset, y + row, pContext->backgroundColor);
128  if (status > GLIB_ERROR_NOTHING_TO_DRAW) return status;
129  if (status == GLIB_OK) drawnElements++;
130  }
131  }
132 
133  /* fontIdx offset for a new row */
134  fontIdx += pContext->font.fontRowOffset;
135  }
136  return ((drawnElements == 0) ? GLIB_ERROR_NOTHING_TO_DRAW : GLIB_OK);
137 }
138 
139 /**************************************************************************/
166 EMSTATUS GLIB_drawString(GLIB_Context_t *pContext, const char* pString, uint32_t sLength,
167  int32_t x0, int32_t y0, bool opaque)
168 {
169  EMSTATUS status;
170  uint32_t drawnElements = 0;
171  uint32_t stringIndex;
172  int32_t x, y;
173 
174  /* Check arguments */
175  if (pContext == NULL || pString == NULL) {
177  }
178 
179  if (pContext->font.class == InvalidFont) {
181  }
182 
183  x = x0;
184  y = y0;
185 
186  /* Loops through the string and prints char for char */
187  for (stringIndex = 0; stringIndex < sLength; stringIndex++)
188  {
189  /* Newline char */
190  if (pString[stringIndex] == '\n') {
191  x = x0;
192  y = y + pContext->font.fontHeight + pContext->font.lineSpacing;
193  continue;
194  }
195 
196  /* Draw the current char */
197  status = GLIB_drawChar(pContext, pString[stringIndex], x, y, opaque);
198  if (status > GLIB_ERROR_NOTHING_TO_DRAW) return status;
199  if (status == GLIB_OK) drawnElements++;
200 
201  /* Adjust x and y coordinate */
202  x += (pContext->font.fontWidth + pContext->font.charSpacing);
203  }
204  return ((drawnElements == 0) ? GLIB_ERROR_NOTHING_TO_DRAW : GLIB_OK);
205 }
206 
207 
208 /**************************************************************************/
223 EMSTATUS GLIB_setFont(GLIB_Context_t *pContext, GLIB_Font_t *pFont)
224 {
225  /* Check arguments */
226  if (pContext == NULL) {
228  }
229 
230  if (pFont == NULL)
231  {
232  memset(&pContext->font, 0, sizeof(GLIB_Font_t));
234  }
235  else
236  {
237  memcpy(&pContext->font, pFont, sizeof(GLIB_Font_t));
238  return GLIB_OK;
239  }
240 }
#define GLIB_ERROR_NOTHING_TO_DRAW
Definition: glib.h:192
Font definition structure.
Definition: glib.h:223
void * pFontPixMap
Definition: glib.h:226
#define GLIB_ERROR_INVALID_ARGUMENT
Definition: glib.h:200
uint16_t cntOfMapElements
Definition: glib.h:229
EMSTATUS GLIB_drawString(GLIB_Context_t *pContext, const char *pString, uint32_t sLength, int32_t x0, int32_t y0, bool opaque)
Draws a string using the font supplied with the library.
Definition: glib_string.c:166
uint8_t fontHeight
Definition: glib.h:241
Silicon Labs Graphics Library.
uint8_t fontRowOffset
Definition: glib.h:235
GLIB_Font_t font
Definition: glib.h:288
uint8_t fontWidth
Definition: glib.h:238
#define GLIB_ERROR_INVALID_CHAR
Definition: glib.h:194
#define GLIB_OK
Definition: glib.h:190
uint8_t sizeOfMapElement
Definition: glib.h:232
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_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
Silicon Labs Graphics Library: Color Defines.
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
GLIB_Font_Class class
Definition: glib.h:251
uint32_t backgroundColor
Definition: glib.h:279
EMSTATUS GLIB_drawChar(GLIB_Context_t *pContext, char myChar, int32_t x, int32_t y, bool opaque)
Draws a char using the font supplied with the library.
Definition: glib_string.c:51
uint8_t charSpacing
Definition: glib.h:247
uint8_t lineSpacing
Definition: glib.h:244
GLIB Drawing Context (Multiple instances of GLIB_Context_t can exist)
Definition: glib.h:273