-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlcd.cpp
319 lines (277 loc) · 7.84 KB
/
lcd.cpp
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
*
* Copyright (c) 2020 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <string.h>
#include "demo-ui.h"
#include "lcd.h"
#include "dmd.h"
#include "glib.h"
#if (SLI_SI91X_MCU_INTERFACE)
#include "sl_memlcd.h"
#endif
#ifdef QR_CODE_ENABLED
#include "qrcodegen.h"
#endif // QR_CODE_ENABLED
#include "sl_board_control.h"
#define LCD_SIZE 128
#define QR_CODE_VERSION 4
#define QR_CODE_MODULE_SIZE 3
#define QR_CODE_BORDER_SIZE 0
#define SL_BOARD_ENABLE_DISPLAY_PIN 0
#ifdef QR_CODE_ENABLED
static uint8_t qrCode[qrcodegen_BUFFER_LEN_FOR_VERSION(QR_CODE_VERSION)];
static uint8_t workBuffer[qrcodegen_BUFFER_LEN_FOR_VERSION(QR_CODE_VERSION)];
#endif // QR_CODE_ENABLED
CHIP_ERROR SilabsLCD::Init(uint8_t * name, bool initialState)
{
EMSTATUS status;
CHIP_ERROR err = CHIP_NO_ERROR;
// Check if Name is to long
if (name != nullptr)
{
if (APP_NAME_MAX_LENGTH < strlen((char *) name))
{
SILABS_LOG("App Name too long");
return CHIP_ERROR_INVALID_ARGUMENT;
}
else
{
strcpy((char *) mName, (char *) name);
}
}
/* Enable the memory lcd */
#if (SLI_SI91X_MCU_INTERFACE)
sl_memlcd_display_enable();
#else
status = sl_board_enable_display();
if (status != SL_STATUS_OK)
{
// sl-temp: S3 display isn't working yet, don't crash app for this.
SILABS_LOG("Board Display enable fail %d", status);
}
#endif
/* Initialize the DMD module for the DISPLAY device driver. */
status = DMD_init(0);
if (DMD_OK != status)
{
SILABS_LOG("DMD init failed %d", status);
err = CHIP_ERROR_INTERNAL;
}
/* Initialize the glib context */
status = GLIB_contextInit(&glibContext);
if (GLIB_OK != status)
{
SILABS_LOG("Glib context init failed %d", status);
err = CHIP_ERROR_INTERNAL;
}
glibContext.backgroundColor = White;
glibContext.foregroundColor = Black;
status = GLIB_clear(&glibContext);
if (GLIB_OK != status)
{
SILABS_LOG("Glib clear failed %d", status);
err = CHIP_ERROR_INTERNAL;
}
demoUIInit(&glibContext);
dState.mainState = initialState;
return err;
}
/* This function is necessary because currently glib.h cannot be used within a C++ context. */
void * SilabsLCD::Context()
{
return (void *) &glibContext;
}
int SilabsLCD::Clear()
{
return GLIB_clear(&glibContext);
}
int SilabsLCD::DrawPixel(void * pContext, int32_t x, int32_t y)
{
return GLIB_drawPixel((GLIB_Context_t *) pContext, x, y);
}
int SilabsLCD::Update(void)
{
return updateDisplay();
}
void SilabsLCD::WriteDemoUI(bool state)
{
if (mCurrentScreen != DemoScreen)
{
mCurrentScreen = DemoScreen;
}
dState.mainState = state;
WriteDemoUI();
}
void SilabsLCD::WriteDemoUI()
{
Clear();
if (customUI != nullptr)
{
customUI(&glibContext);
}
else
{
demoUIClearMainScreen(mName);
demoUIDisplayApp(dState.mainState);
}
}
void SilabsLCD::WriteStatus()
{
uint8_t lineNb = 0;
char str[20];
GLIB_clear(&glibContext);
sprintf(str, "# Fabrics : %d", mStatus.nbFabric);
GLIB_drawStringOnLine(&glibContext, str, lineNb++, GLIB_ALIGN_LEFT, 0, 0, true);
if (strlen(mStatus.networkName) >= sizeof(str))
{
memcpy(str, mStatus.networkName, sizeof(str) - 1);
str[sizeof(str) - 1] = '\0';
}
else
{
memcpy(str, mStatus.networkName, sizeof(str));
}
#if SL_WIFI
GLIB_drawStringOnLine(&glibContext, "SSID : ", lineNb++, GLIB_ALIGN_LEFT, 0, 0, true);
GLIB_drawStringOnLine(&glibContext, str, lineNb++, GLIB_ALIGN_LEFT, 0, 0, true);
#else
GLIB_drawStringOnLine(&glibContext, "PANID : ", lineNb, GLIB_ALIGN_LEFT, 0, 0, true);
GLIB_drawStringOnLine(&glibContext, str, lineNb++, GLIB_ALIGN_LEFT, 64, 0, true);
#if CHIP_DEVICE_CONFIG_THREAD_FTD
GLIB_drawStringOnLine(&glibContext, "OT Type : FTD", lineNb++, GLIB_ALIGN_LEFT, 0, 0, true);
#else
GLIB_drawStringOnLine(&glibContext, "OT Type : MTD", lineNb++, GLIB_ALIGN_LEFT, 0, 0, true);
#endif // FTD
#endif
GLIB_drawStringOnLine(&glibContext, "", lineNb++, GLIB_ALIGN_LEFT, 0, 0, true);
sprintf(str, "Connected : %c", mStatus.connected ? 'Y' : 'N');
GLIB_drawStringOnLine(&glibContext, str, lineNb++, GLIB_ALIGN_LEFT, 0, 0, true);
sprintf(str, "Advertising : %c", mStatus.advertising ? 'Y' : 'N');
GLIB_drawStringOnLine(&glibContext, str, lineNb++, GLIB_ALIGN_LEFT, 0, 0, true);
if (mStatus.icdMode != NotICD)
{
sprintf(str, "ICD : %s", mStatus.icdMode == SIT ? "SIT" : "LIT");
GLIB_drawStringOnLine(&glibContext, str, lineNb++, GLIB_ALIGN_LEFT, 0, 0, true);
}
updateDisplay();
}
void SilabsLCD::SetCustomUI(customUICB cb)
{
customUI = cb;
}
void SilabsLCD::GetScreen(Screen_e & screen)
{
screen = static_cast<Screen_e>(mCurrentScreen);
}
void SilabsLCD::SetScreen(Screen_e screen)
{
if (screen >= InvalidScreen)
{
return;
}
switch (screen)
{
case DemoScreen:
WriteDemoUI();
break;
case StatusScreen:
WriteStatus();
break;
#ifdef QR_CODE_ENABLED
case QRCodeScreen:
WriteQRCode();
break;
#endif
default:
break;
}
mCurrentScreen = screen;
}
void SilabsLCD::CycleScreens(void)
{
#ifdef QR_CODE_ENABLED
if (mCurrentScreen < QRCodeScreen)
#else
if (mCurrentScreen < StatusScreen)
#endif
{
mCurrentScreen++;
}
else
{
mCurrentScreen = DemoScreen;
}
SetScreen(static_cast<Screen_e>(mCurrentScreen));
}
void SilabsLCD::SetStatus(DisplayStatus_t & status)
{
mStatus = status;
}
#ifdef QR_CODE_ENABLED
void SilabsLCD::WriteQRCode()
{
if (!qrcodegen_encodeText((const char *) mQRCodeBuffer, workBuffer, qrCode, qrcodegen_Ecc_LOW, QR_CODE_VERSION, QR_CODE_VERSION,
qrcodegen_Mask_AUTO, true))
{
SILABS_LOG("qrcodegen_encodeText() failed");
return;
}
const int size = qrcodegen_getSize(qrCode);
GLIB_clear(&glibContext);
const int displaySize = (2 * QR_CODE_BORDER_SIZE + size) * QR_CODE_MODULE_SIZE;
const int displayX = (LCD_SIZE - displaySize) / 2;
const int displayY = displayX;
for (int y = 0; y < size; ++y)
{
for (int x = 0; x < size; ++x)
{
if (qrcodegen_getModule(qrCode, x, y))
{
LCDFillRect(displayX + (QR_CODE_BORDER_SIZE + x) * QR_CODE_MODULE_SIZE,
displayY + (QR_CODE_BORDER_SIZE + y) * QR_CODE_MODULE_SIZE, QR_CODE_MODULE_SIZE, QR_CODE_MODULE_SIZE);
}
}
}
SilabsLCD::Update();
}
void SilabsLCD::SetQRCode(uint8_t * str, uint32_t size)
{
if (size < chip::QRCodeBasicSetupPayloadGenerator::kMaxQRCodeBase38RepresentationLength + 1)
{
memcpy(mQRCodeBuffer, str, size);
}
}
void SilabsLCD::ShowQRCode(bool show)
{
if (mCurrentScreen != QRCodeScreen)
{
mCurrentScreen = QRCodeScreen;
}
WriteQRCode();
}
void SilabsLCD::LCDFillRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h)
{
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
GLIB_drawPixel(&glibContext, x + j, y + i);
}
}
}
#endif // QR_CODE_ENABLED