qrcode.h 3.4 KB

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