esp_qrcode_main.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #include <stdio.h>
  15. #include <esp_err.h>
  16. #include "esp_log.h"
  17. #include "qrcodegen.h"
  18. #include "qrcode.h"
  19. static const char *TAG = "QRCODE";
  20. static const char *lt[] = {
  21. /* 0 */ " ",
  22. /* 1 */ "\u2580 ",
  23. /* 2 */ " \u2580",
  24. /* 3 */ "\u2580\u2580",
  25. /* 4 */ "\u2584 ",
  26. /* 5 */ "\u2588 ",
  27. /* 6 */ "\u2584\u2580",
  28. /* 7 */ "\u2588\u2580",
  29. /* 8 */ " \u2584",
  30. /* 9 */ "\u2580\u2584",
  31. /* 10 */ " \u2588",
  32. /* 11 */ "\u2580\u2588",
  33. /* 12 */ "\u2584\u2584",
  34. /* 13 */ "\u2588\u2584",
  35. /* 14 */ "\u2584\u2588",
  36. /* 15 */ "\u2588\u2588",
  37. };
  38. void esp_qrcode_print_console(esp_qrcode_handle_t qrcode)
  39. {
  40. int size = qrcodegen_getSize(qrcode);
  41. int border = 2;
  42. unsigned char num = 0;
  43. for (int y = -border; y < size + border; y+=2) {
  44. for (int x = -border; x < size + border; x+=2) {
  45. num = 0;
  46. if (qrcodegen_getModule(qrcode, x, y)) {
  47. num |= 1 << 0;
  48. }
  49. if ((x < size + border) && qrcodegen_getModule(qrcode, x+1, y)) {
  50. num |= 1 << 1;
  51. }
  52. if ((y < size + border) && qrcodegen_getModule(qrcode, x, y+1)) {
  53. num |= 1 << 2;
  54. }
  55. if ((x < size + border) && (y < size + border) && qrcodegen_getModule(qrcode, x+1, y+1)) {
  56. num |= 1 << 3;
  57. }
  58. printf("%s", lt[num]);
  59. }
  60. printf("\n");
  61. }
  62. printf("\n");
  63. }
  64. esp_err_t esp_qrcode_generate(esp_qrcode_config_t *cfg, const char *text)
  65. {
  66. enum qrcodegen_Ecc ecc_lvl;
  67. uint8_t *qrcode, *tempbuf;
  68. esp_err_t err = ESP_FAIL;
  69. qrcode = calloc(1, qrcodegen_BUFFER_LEN_FOR_VERSION(cfg->max_qrcode_version));
  70. if (!qrcode) {
  71. return ESP_ERR_NO_MEM;
  72. }
  73. tempbuf = calloc(1, qrcodegen_BUFFER_LEN_FOR_VERSION(cfg->max_qrcode_version));
  74. if (!tempbuf) {
  75. free(qrcode);
  76. return ESP_ERR_NO_MEM;
  77. }
  78. switch(cfg->qrcode_ecc_level) {
  79. case ESP_QRCODE_ECC_LOW:
  80. ecc_lvl = qrcodegen_Ecc_LOW;
  81. break;
  82. case ESP_QRCODE_ECC_MED:
  83. ecc_lvl = qrcodegen_Ecc_MEDIUM;
  84. break;
  85. case ESP_QRCODE_ECC_QUART:
  86. ecc_lvl = qrcodegen_Ecc_QUARTILE;
  87. break;
  88. case ESP_QRCODE_ECC_HIGH:
  89. ecc_lvl = qrcodegen_Ecc_HIGH;
  90. break;
  91. default:
  92. ecc_lvl = qrcodegen_Ecc_LOW;
  93. break;
  94. }
  95. ESP_LOGI(TAG, "Encoding below text with ECC LVL %d & QR Code Version %d",
  96. ecc_lvl, cfg->max_qrcode_version);
  97. ESP_LOGI(TAG, "%s", text);
  98. // Make and print the QR Code symbol
  99. bool ok = qrcodegen_encodeText(text, tempbuf, qrcode, ecc_lvl,
  100. qrcodegen_VERSION_MIN, cfg->max_qrcode_version,
  101. qrcodegen_Mask_AUTO, true);
  102. if (ok && cfg->display_func) {
  103. cfg->display_func((esp_qrcode_handle_t)qrcode);
  104. err = ESP_OK;
  105. }
  106. free(qrcode);
  107. free(tempbuf);
  108. return err;
  109. }