-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.c
135 lines (114 loc) · 4.57 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
130
131
132
133
134
135
/***************************************************************************//**
* @file
* @brief Draws the graphics on the display
*******************************************************************************
* # License
* <b>Copyright 2018 Silicon Laboratories Inc. www.silabs.com</b>
*******************************************************************************
*
* The licensor of this software is Silicon Laboratories Inc. Your use of this
* software is governed by the terms of Silicon Labs Master Software License
* Agreement (MSLA) available at
* www.silabs.com/about-us/legal/master-software-license-agreement. This
* software is distributed to you in Source Code format and is governed by the
* sections of the MSLA applicable to Source Code.
*
******************************************************************************/
/* 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 char *deviceHeader = NULL;
/***************************************************************************************************
Static Function Declarations
**************************************************************************************************/
static void graphPrintCenter(GLIB_Context_t *pContext, char *pString);
/***************************************************************************************************
Function Definitions
**************************************************************************************************/
void graphInit(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(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, char *pString)
{
do {
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 */
}