Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / platform / efr32 / display / lcd.c
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    All rights reserved.
5  *
6  *    Licensed under the Apache License, Version 2.0 (the "License");
7  *    you may not use this file except in compliance with the License.
8  *    You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *    Unless required by applicable law or agreed to in writing, software
13  *    distributed under the License is distributed on an "AS IS" BASIS,
14  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *    See the License for the specific language governing permissions and
16  *    limitations under the License.
17  */
18
19 #include <stdio.h>
20 #include <string.h>
21
22 #include "lcd.h"
23
24 #include "display.h"
25 #include "dmd.h"
26 #include "glib.h"
27 #include "qrcodegen.h"
28
29 #define LCD_SIZE 128
30 #define QR_CODE_VERSION 4
31 #define QR_CODE_MODULE_SIZE 3
32 #define QR_CODE_BORDER_SIZE 0
33
34 static GLIB_Context_t glibContext;
35 static uint8_t qrCode[qrcodegen_BUFFER_LEN_FOR_VERSION(QR_CODE_VERSION)];
36 static uint8_t workBuffer[qrcodegen_BUFFER_LEN_FOR_VERSION(QR_CODE_VERSION)];
37
38 static void LCDFillRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h);
39
40 void initLCD(void)
41 {
42     EMSTATUS status;
43
44     /* Initialize the DMD module for the DISPLAY device driver. */
45     status = DMD_init(0);
46     if (DMD_OK != status)
47     {
48         EFR32_LOG("DMD init failed %d", status);
49     }
50
51     /* Initialize the glib context */
52     status = GLIB_contextInit(&glibContext);
53     if (GLIB_OK != status)
54     {
55         EFR32_LOG("Glib context init failed %d", status);
56     }
57
58     glibContext.backgroundColor = White;
59     glibContext.foregroundColor = Black;
60     status                      = GLIB_clear(&glibContext);
61     if (GLIB_OK != status)
62     {
63         EFR32_LOG("Glib clear failed %d", status);
64     }
65 }
66
67 void LCDWriteQRCode(uint8_t * str)
68 {
69     if (!qrcodegen_encodeText((const char *) str, workBuffer, qrCode, qrcodegen_Ecc_LOW, QR_CODE_VERSION, QR_CODE_VERSION,
70                               qrcodegen_Mask_AUTO, true))
71     {
72         EFR32_LOG("qrcodegen_encodeText() failed");
73         return;
74     }
75
76     const int size = qrcodegen_getSize(qrCode);
77
78     GLIB_clear(&glibContext);
79
80     const int displaySize = (2 * QR_CODE_BORDER_SIZE + size) * QR_CODE_MODULE_SIZE;
81     const int displayX    = (LCD_SIZE - displaySize) / 2;
82     const int displayY    = displayX;
83
84     for (int y = 0; y < size; ++y)
85     {
86         for (int x = 0; x < size; ++x)
87         {
88             if (qrcodegen_getModule(qrCode, x, y))
89             {
90                 LCDFillRect(displayX + (QR_CODE_BORDER_SIZE + x) * QR_CODE_MODULE_SIZE,
91                             displayY + (QR_CODE_BORDER_SIZE + y) * QR_CODE_MODULE_SIZE, QR_CODE_MODULE_SIZE, QR_CODE_MODULE_SIZE);
92             }
93         }
94     }
95
96     DMD_updateDisplay();
97 }
98
99 void LCDFillRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h)
100 {
101     for (int i = 0; i < h; i++)
102     {
103         for (int j = 0; j < w; j++)
104         {
105             GLIB_drawPixel(&glibContext, x + j, y + i);
106         }
107     }
108 }