-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.c
129 lines (108 loc) · 4.5 KB
/
graphics.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/***********************************************************************************************//**
* \file graphics.c
* \brief Draws the graphics on the display
***************************************************************************************************
* <b> (C) Copyright 2015 Silicon Labs, http://www.silabs.com</b>
***************************************************************************************************
* This file is licensed under the Silabs License Agreement. See the file
* "Silabs_License_Agreement.txt" for details. Before using this software for
* any purpose, you must agree to the terms of that agreement.
**************************************************************************************************/
/* standard headers */
#include <string.h>
#include <stdio.h>
#include "bg_types.h"
#include "em_types.h"
#include "glib.h"
#include "dmd.h"
#include "display.h"
/* Own header */
#include "graphics.h"
/***************************************************************************************************
Local Variables
**************************************************************************************************/
/* Global glib context */
static GLIB_Context_t glibContext;
/* Current line number stored for printing text */
static uint8_t graphLineNum = 0;
/* Device name string */
static const char *deviceHeader = NULL;
/***************************************************************************************************
Static Function Declarations
**************************************************************************************************/
static void graphPrintCenter(GLIB_Context_t *pContext, const char *pString);
/***************************************************************************************************
Function Definitions
**************************************************************************************************/
void graphInit(const char *header)
{
EMSTATUS status;
/* Initialize the display module. */
status = DISPLAY_Init();
if (DISPLAY_EMSTATUS_OK != status) {
while (1)
;
}
/* Initialize the DMD module for the DISPLAY device driver. */
status = DMD_init(0);
if (DMD_OK != status) {
while (1)
;
}
status = GLIB_contextInit(&glibContext);
if (GLIB_OK != status) {
while (1)
;
}
glibContext.backgroundColor = White;
glibContext.foregroundColor = Black;
/* Use Narrow font */
GLIB_setFont(&glibContext, (GLIB_Font_t *)&GLIB_FontNarrow6x8);
deviceHeader = header;
}
void graphWriteString(const char *string)
{
GLIB_clear(&glibContext);
/* Reset line number, print header and device name */
graphLineNum = 0;
graphPrintCenter(&glibContext, deviceHeader);
/* Print the string below the header center aligned */
graphPrintCenter(&glibContext, string);
DMD_updateDisplay();
}
/***************************************************************************************************
Static Function Definitions
**************************************************************************************************/
/***********************************************************************************************//**
* \brief Print the given string center aligned
* \note The string may contain several lines separated by new line
* characters ('\n'). Each line will be printed center aligned.
* \param[in] pContext Context
* \param[in] pString String to be displayed
**************************************************************************************************/
static void graphPrintCenter(GLIB_Context_t *pContext, const char *pString)
{
do {
const char* nextToken;
uint8_t len;
/* Search for the next important token (new line or terminating NULL) */
for (nextToken = pString; ((*nextToken != '\n') && (*nextToken != '\0')); nextToken++) {
;
}
len = nextToken - pString;
/* Print the line if it is not null length */
if (len) {
uint8_t strWidth = len * pContext->font.fontWidth;
uint8_t posX = (pContext->pDisplayGeometry->xSize - strWidth) >> 1;
uint8_t posY = ((pContext->font.lineSpacing + pContext->font.fontHeight) * graphLineNum)
+ pContext->font.lineSpacing;
GLIB_drawString(pContext, pString, len, posX, posY, 0);
}
pString = nextToken;
/* If the token at the end of the line is new line character, then increase line number */
if (*nextToken == '\n') {
graphLineNum++;
pString++;
}
} while (*pString); /* while terminating NULL is not reached */
}