3 * Copyright (c) 2020 Project CHIP Authors
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include "qrcodegen.h"
30 #define QR_CODE_VERSION 4
31 #define QR_CODE_MODULE_SIZE 3
32 #define QR_CODE_BORDER_SIZE 0
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)];
38 static void LCDFillRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h);
44 /* Initialize the DMD module for the DISPLAY device driver. */
48 EFR32_LOG("DMD init failed %d", status);
51 /* Initialize the glib context */
52 status = GLIB_contextInit(&glibContext);
53 if (GLIB_OK != status)
55 EFR32_LOG("Glib context init failed %d", status);
58 glibContext.backgroundColor = White;
59 glibContext.foregroundColor = Black;
60 status = GLIB_clear(&glibContext);
61 if (GLIB_OK != status)
63 EFR32_LOG("Glib clear failed %d", status);
67 void LCDWriteQRCode(uint8_t * str)
69 if (!qrcodegen_encodeText((const char *) str, workBuffer, qrCode, qrcodegen_Ecc_LOW, QR_CODE_VERSION, QR_CODE_VERSION,
70 qrcodegen_Mask_AUTO, true))
72 EFR32_LOG("qrcodegen_encodeText() failed");
76 const int size = qrcodegen_getSize(qrCode);
78 GLIB_clear(&glibContext);
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;
84 for (int y = 0; y < size; ++y)
86 for (int x = 0; x < size; ++x)
88 if (qrcodegen_getModule(qrCode, x, y))
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);
99 void LCDFillRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h)
101 for (int i = 0; i < h; i++)
103 for (int j = 0; j < w; j++)
105 GLIB_drawPixel(&glibContext, x + j, y + i);