1 // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <stdio.h>
16 #include <esp_err.h>
17 #include "esp_log.h"
18 
19 #include "qrcodegen.h"
20 #include "qrcode.h"
21 
22 static const char *TAG = "QRCODE";
23 
24 static const char *lt[] = {
25     /* 0 */ "  ",
26     /* 1 */ "\u2580 ",
27     /* 2 */ " \u2580",
28     /* 3 */ "\u2580\u2580",
29     /* 4 */ "\u2584 ",
30     /* 5 */ "\u2588 ",
31     /* 6 */ "\u2584\u2580",
32     /* 7 */ "\u2588\u2580",
33     /* 8 */ " \u2584",
34     /* 9 */ "\u2580\u2584",
35     /* 10 */ " \u2588",
36     /* 11 */ "\u2580\u2588",
37     /* 12 */ "\u2584\u2584",
38     /* 13 */ "\u2588\u2584",
39     /* 14 */ "\u2584\u2588",
40     /* 15 */ "\u2588\u2588",
41 };
42 
esp_qrcode_print_console(esp_qrcode_handle_t qrcode)43 void esp_qrcode_print_console(esp_qrcode_handle_t qrcode)
44 {
45     int size = qrcodegen_getSize(qrcode);
46     int border = 2;
47     unsigned char num = 0;
48 
49     for (int y = -border; y < size + border; y+=2) {
50         for (int x = -border; x < size + border; x+=2) {
51             num = 0;
52             if (qrcodegen_getModule(qrcode, x, y)) {
53                 num |= 1 << 0;
54             }
55             if ((x < size + border) && qrcodegen_getModule(qrcode, x+1, y)) {
56                 num |= 1 << 1;
57             }
58             if ((y < size + border) && qrcodegen_getModule(qrcode, x, y+1)) {
59                 num |= 1 << 2;
60             }
61             if ((x < size + border) && (y < size + border) && qrcodegen_getModule(qrcode, x+1, y+1)) {
62                 num |= 1 << 3;
63             }
64             printf("%s", lt[num]);
65         }
66         printf("\n");
67     }
68     printf("\n");
69 }
70 
esp_qrcode_generate(esp_qrcode_config_t * cfg,const char * text)71 esp_err_t esp_qrcode_generate(esp_qrcode_config_t *cfg, const char *text)
72 {
73     enum qrcodegen_Ecc ecc_lvl;
74     uint8_t *qrcode, *tempbuf;
75     esp_err_t err = ESP_FAIL;
76 
77     qrcode = calloc(1, qrcodegen_BUFFER_LEN_FOR_VERSION(cfg->max_qrcode_version));
78     if (!qrcode) {
79         return ESP_ERR_NO_MEM;
80     }
81 
82     tempbuf = calloc(1, qrcodegen_BUFFER_LEN_FOR_VERSION(cfg->max_qrcode_version));
83     if (!tempbuf) {
84         free(qrcode);
85         return ESP_ERR_NO_MEM;
86     }
87 
88     switch(cfg->qrcode_ecc_level) {
89         case ESP_QRCODE_ECC_LOW:
90             ecc_lvl = qrcodegen_Ecc_LOW;
91             break;
92         case ESP_QRCODE_ECC_MED:
93             ecc_lvl = qrcodegen_Ecc_MEDIUM;
94             break;
95         case ESP_QRCODE_ECC_QUART:
96             ecc_lvl = qrcodegen_Ecc_QUARTILE;
97             break;
98         case ESP_QRCODE_ECC_HIGH:
99             ecc_lvl = qrcodegen_Ecc_HIGH;
100             break;
101         default:
102             ecc_lvl = qrcodegen_Ecc_LOW;
103             break;
104     }
105 
106     ESP_LOGI(TAG, "Encoding below text with ECC LVL %d & QR Code Version %d",
107              ecc_lvl, cfg->max_qrcode_version);
108     ESP_LOGI(TAG, "%s", text);
109     // Make and print the QR Code symbol
110     bool ok = qrcodegen_encodeText(text, tempbuf, qrcode, ecc_lvl,
111                                    qrcodegen_VERSION_MIN, cfg->max_qrcode_version,
112                                    qrcodegen_Mask_AUTO, true);
113     if (ok && cfg->display_func) {
114         cfg->display_func((esp_qrcode_handle_t)qrcode);
115         err = ESP_OK;
116     }
117 
118     free(qrcode);
119     free(tempbuf);
120     return err;
121 }
122