Skip to content

Commit fb4fa54

Browse files
committed
Merge branch 'task/change_qrcode' into 'master'
qrcode: Replacing the qrcode component with the one from IDF. See merge request app-frameworks/esp-rainmaker!261
2 parents 669b6f7 + 7815a10 commit fb4fa54

File tree

5 files changed

+167
-23
lines changed

5 files changed

+167
-23
lines changed

components/qrcode/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
idf_component_register(SRCS ./src/qrcodegen.c ./src/qrcode.c
1+
idf_component_register(SRCS ./src/qrcodegen.c ./src/esp_qrcode_main.c ./src/esp_qrcode_wrapper.c
22
INCLUDE_DIRS include
33
REQUIRES
44
PRIV_REQUIRES )

components/qrcode/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# QR Code generator component
2+
3+
This directory contains a QR code generator component written in C. This component is based on [QR-Code-generator](https://github.com/nayuki/QR-Code-generator).
4+
5+
To learn more about how to use this component, please check API Documentation from header file [qrcode.h](./include/qrcode.h).

components/qrcode/include/qrcode.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ extern "C" {
2626
* @attention 1. Can successfully encode a UTF-8 string of up to 2953 bytes or an alphanumeric
2727
* string of up to 4296 characters or any digit string of up to 7089 characters
2828
*
29+
* @note This API is kept for backward compatibility
30+
*
2931
* @param text string to encode into a QR Code.
3032
*
3133
* @return
@@ -35,6 +37,86 @@ extern "C" {
3537
*/
3638
esp_err_t qrcode_display(const char *text);
3739

40+
/**
41+
* @brief QR Code handle used by the display function
42+
*/
43+
typedef const uint8_t * esp_qrcode_handle_t;
44+
45+
/**
46+
* @brief QR Code configuration options
47+
*/
48+
typedef struct {
49+
void (*display_func)(esp_qrcode_handle_t qrcode); /**< Function called for displaying the QR Code after encoding is complete */
50+
int max_qrcode_version; /**< Max QR Code Version to be used. Range: 2 - 40 */
51+
int qrcode_ecc_level; /**< Error Correction Level for QR Code */
52+
} esp_qrcode_config_t;
53+
54+
/**
55+
* @brief Error Correction Level in a QR Code Symbol
56+
*/
57+
enum {
58+
ESP_QRCODE_ECC_LOW, /**< QR Code Error Tolerance of 7% */
59+
ESP_QRCODE_ECC_MED, /**< QR Code Error Tolerance of 15% */
60+
ESP_QRCODE_ECC_QUART, /**< QR Code Error Tolerance of 25% */
61+
ESP_QRCODE_ECC_HIGH /**< QR Code Error Tolerance of 30% */
62+
};
63+
64+
/**
65+
* @brief Encodes the given string into a QR Code and calls the display function
66+
*
67+
* @attention 1. Can successfully encode a UTF-8 string of up to 2953 bytes or an alphanumeric
68+
* string of up to 4296 characters or any digit string of up to 7089 characters
69+
*
70+
* @param cfg Configuration used for QR Code encoding.
71+
* @param text String to encode into a QR Code.
72+
*
73+
* @return
74+
* - ESP_OK: succeed
75+
* - ESP_FAIL: Failed to encode string into a QR Code
76+
* - ESP_ERR_NO_MEM: Failed to allocate buffer for given max_qrcode_version
77+
*/
78+
esp_err_t esp_qrcode_generate(esp_qrcode_config_t *cfg, const char *text);
79+
80+
/**
81+
* @brief Displays QR Code on the console
82+
*
83+
* @param qrcode QR Code handle used by the display function.
84+
*/
85+
void esp_qrcode_print_console(esp_qrcode_handle_t qrcode);
86+
87+
/**
88+
* @brief Returns the side length of the given QR Code
89+
*
90+
* @param qrcode QR Code handle used by the display function.
91+
*
92+
* @return
93+
* - val[21, 177]: Side length of QR Code
94+
*/
95+
int esp_qrcode_get_size(esp_qrcode_handle_t qrcode);
96+
97+
/**
98+
* @brief Returns the Pixel value for the given coordinates
99+
* False indicates White and True indicates Black
100+
*
101+
* @attention 1. Coordinates for top left corner are (x=0, y=0)
102+
* @attention 2. For out of bound coordinates false (White) is returned
103+
*
104+
* @param qrcode QR Code handle used by the display function.
105+
* @param x X-Coordinate of QR Code module
106+
* @param y Y-Coordinate of QR Code module
107+
*
108+
* @return
109+
* - true: (x, y) Pixel is Black
110+
* - false: (x, y) Pixel is White
111+
*/
112+
bool esp_qrcode_get_module(esp_qrcode_handle_t qrcode, int x, int y);
113+
114+
#define ESP_QRCODE_CONFIG_DEFAULT() (esp_qrcode_config_t) { \
115+
.display_func = esp_qrcode_print_console, \
116+
.max_qrcode_version = 10, \
117+
.qrcode_ecc_level = ESP_QRCODE_ECC_LOW, \
118+
}
119+
38120
#ifdef __cplusplus
39121
}
40122
#endif

components/qrcode/src/qrcode.c renamed to components/qrcode/src/esp_qrcode_main.c

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
1+
// Copyright 2020 Espressif Systems (Shanghai) PTE LTD
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -14,10 +14,12 @@
1414

1515
#include <stdio.h>
1616
#include <esp_err.h>
17+
#include "esp_log.h"
1718

1819
#include "qrcodegen.h"
20+
#include "qrcode.h"
1921

20-
#define MAX_QRCODE_VERSION 5
22+
static const char *TAG = "qrcode";
2123

2224
static const char *lt[] = {
2325
/* 0 */ " ",
@@ -38,13 +40,8 @@ static const char *lt[] = {
3840
/* 15 */ "\u2588\u2588",
3941
};
4042

41-
void print_qr_char(unsigned char n)
43+
void esp_qrcode_print_console(esp_qrcode_handle_t qrcode)
4244
{
43-
printf("%s", lt[n]);
44-
}
45-
46-
extern void print_qr_char(unsigned char);
47-
static void printQr(const uint8_t qrcode[]) {
4845
int size = qrcodegen_getSize(qrcode);
4946
int border = 2;
5047
unsigned char num = 0;
@@ -64,38 +61,69 @@ static void printQr(const uint8_t qrcode[]) {
6461
if ((x < size + border) && (y < size + border) && qrcodegen_getModule(qrcode, x+1, y+1)) {
6562
num |= 1 << 3;
6663
}
67-
print_qr_char(num);
64+
printf("%s", lt[num]);
6865
}
6966
printf("\n");
7067
}
7168
printf("\n");
7269
}
7370

74-
esp_err_t qrcode_display(const char *text)
71+
esp_err_t esp_qrcode_generate(esp_qrcode_config_t *cfg, const char *text)
7572
{
76-
enum qrcodegen_Ecc errCorLvl = qrcodegen_Ecc_LOW;
77-
uint8_t *qrcode, *tempBuffer;
73+
enum qrcodegen_Ecc ecc_lvl;
74+
uint8_t *qrcode, *tempbuf;
7875
esp_err_t err = ESP_FAIL;
7976

80-
qrcode = calloc(1, qrcodegen_BUFFER_LEN_FOR_VERSION(MAX_QRCODE_VERSION));
81-
if (!qrcode)
77+
qrcode = calloc(1, qrcodegen_BUFFER_LEN_FOR_VERSION(cfg->max_qrcode_version));
78+
if (!qrcode) {
8279
return ESP_ERR_NO_MEM;
80+
}
8381

84-
tempBuffer = calloc(1, qrcodegen_BUFFER_LEN_FOR_VERSION(MAX_QRCODE_VERSION));
85-
if (!tempBuffer) {
82+
tempbuf = calloc(1, qrcodegen_BUFFER_LEN_FOR_VERSION(cfg->max_qrcode_version));
83+
if (!tempbuf) {
8684
free(qrcode);
8785
return ESP_ERR_NO_MEM;
8886
}
8987

90-
// Make and print the QR Code symbol
91-
bool ok = qrcodegen_encodeText(text, tempBuffer, qrcode, errCorLvl,
92-
qrcodegen_VERSION_MIN, MAX_QRCODE_VERSION, qrcodegen_Mask_AUTO, true);
93-
if (ok) {
94-
printQr(qrcode);
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_LOGD(TAG, "Encoding below text with ECC LVL %d & QR Code Version %d",
107+
ecc_lvl, cfg->max_qrcode_version);
108+
ESP_LOGD(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);
95115
err = ESP_OK;
96116
}
97117

98118
free(qrcode);
99-
free(tempBuffer);
119+
free(tempbuf);
100120
return err;
101121
}
122+
123+
esp_err_t qrcode_display(const char *text)
124+
{
125+
#define MAX_QRCODE_VERSION 5
126+
esp_qrcode_config_t cfg = ESP_QRCODE_CONFIG_DEFAULT();
127+
cfg.max_qrcode_version = MAX_QRCODE_VERSION;
128+
return esp_qrcode_generate(&cfg, text);
129+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
18+
#include "qrcodegen.h"
19+
#include "qrcode.h"
20+
21+
int esp_qrcode_get_size(esp_qrcode_handle_t qrcode)
22+
{
23+
return qrcodegen_getSize(qrcode);
24+
}
25+
26+
bool esp_qrcode_get_module(esp_qrcode_handle_t qrcode, int x, int y)
27+
{
28+
return qrcodegen_getModule(qrcode, x, y);
29+
}

0 commit comments

Comments
 (0)