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