QRCodeReader.ino 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Based on https://drive.google.com/file/d/1w8hJ5NhNik3qo2xl6eDrlgG4n6R4LuJ_/view
  2. #include "quirc.h"
  3. struct QRCodeData
  4. {
  5. bool valid;
  6. int dataType;
  7. uint8_t payload[1024];
  8. int payloadLen;
  9. };
  10. // creating a task handle
  11. TaskHandle_t QRCodeReader_Task;
  12. struct quirc *q = NULL;
  13. uint8_t *image = NULL;
  14. camera_fb_t * fb = NULL;
  15. struct quirc_code code;
  16. struct quirc_data data;
  17. quirc_decode_error_t err;
  18. struct QRCodeData qrCodeData;
  19. String QRCodeResult = "";
  20. void qr_reader_setup() {
  21. // camera init
  22. camera_config_t config;
  23. config.ledc_channel = LEDC_CHANNEL_0;
  24. config.ledc_timer = LEDC_TIMER_0;
  25. config.pin_d0 = Y2_GPIO_NUM;
  26. config.pin_d1 = Y3_GPIO_NUM;
  27. config.pin_d2 = Y4_GPIO_NUM;
  28. config.pin_d3 = Y5_GPIO_NUM;
  29. config.pin_d4 = Y6_GPIO_NUM;
  30. config.pin_d5 = Y7_GPIO_NUM;
  31. config.pin_d6 = Y8_GPIO_NUM;
  32. config.pin_d7 = Y9_GPIO_NUM;
  33. config.pin_xclk = XCLK_GPIO_NUM;
  34. config.pin_pclk = PCLK_GPIO_NUM;
  35. config.pin_vsync = VSYNC_GPIO_NUM;
  36. config.pin_href = HREF_GPIO_NUM;
  37. config.pin_sscb_sda = SIOD_GPIO_NUM;
  38. config.pin_sscb_scl = SIOC_GPIO_NUM;
  39. config.pin_pwdn = PWDN_GPIO_NUM;
  40. config.pin_reset = RESET_GPIO_NUM;
  41. config.xclk_freq_hz = 10000000;
  42. config.pixel_format = PIXFORMAT_GRAYSCALE;
  43. config.frame_size = FRAMESIZE_QVGA;
  44. config.jpeg_quality = 15;
  45. config.fb_count = 1;
  46. /*#if defined(CAMERA_MODEL_ESP_EYE)
  47. pinMode(13, INPUT_PULLUP);
  48. pinMode(14, INPUT_PULLUP);
  49. #endif*/
  50. esp_err_t err = esp_camera_init(&config);
  51. if (err != ESP_OK) {
  52. Serial.printf("Camera init failed with error 0x%x", err);
  53. ESP.restart();
  54. }
  55. sensor_t * s = esp_camera_sensor_get();
  56. s->set_framesize(s, FRAMESIZE_QVGA);
  57. //Serial.println("Configure and initialize the camera successfully.");
  58. //Serial.println();
  59. /* ---------------------------------------- */
  60. /* ---------------------------------------- create "QRCodeReader_Task" using the xTaskCreatePinnedToCore() function */
  61. xTaskCreatePinnedToCore(
  62. QRCodeReader, /* Task function. */
  63. "QRCodeReader_Task", /* name of task. */
  64. 10000, /* Stack size of task */
  65. NULL, /* parameter of the task */
  66. 1, /* priority of the task */
  67. &QRCodeReader_Task, /* Task handle to keep track of created task */
  68. 0); /* pin task to core 0 */
  69. /* ---------------------------------------- */
  70. }
  71. void qr_reader_loop() {
  72. delay(1);
  73. }
  74. void QRCodeReader( void * pvParameters ){
  75. /* ---------------------------------------- */
  76. Serial.println("Waiting for QR code");
  77. //Serial.print("QRCodeReader running on core ");
  78. //Serial.println(xPortGetCoreID());
  79. //Serial.println();
  80. /* ---------------------------------------- */
  81. /* ---------------------------------------- Loop to read QR Code in real time. */
  82. while(1){
  83. q = quirc_new();
  84. if (q == NULL){
  85. Serial.print("can't create quirc object\r\n");
  86. continue;
  87. }
  88. fb = esp_camera_fb_get();
  89. if (!fb)
  90. {
  91. Serial.println("Camera capture failed");
  92. continue;
  93. }
  94. quirc_resize(q, fb->width, fb->height);
  95. image = quirc_begin(q, NULL, NULL);
  96. memcpy(image, fb->buf, fb->len);
  97. quirc_end(q);
  98. int count = quirc_count(q);
  99. if (count > 0) {
  100. quirc_extract(q, 0, &code);
  101. err = quirc_decode(&code, &data);
  102. if (err){
  103. Serial.println("Decoding FAILED");
  104. QRCodeResult = "Decoding FAILED";
  105. } else {
  106. //Serial.printf("Decoding successful:\n");
  107. dumpData(&data);
  108. }
  109. Serial.println();
  110. }
  111. esp_camera_fb_return(fb);
  112. fb = NULL;
  113. image = NULL;
  114. quirc_destroy(q);
  115. }
  116. /* ---------------------------------------- */
  117. }
  118. void dumpData(const struct quirc_data *data)
  119. {
  120. /*Serial.printf("Version: %d\n", data->version);
  121. Serial.printf("ECC level: %c\n", "MLHQ"[data->ecc_level]);
  122. Serial.printf("Mask: %d\n", data->mask);
  123. Serial.printf("Length: %d\n", data->payload_len);*/
  124. Serial.printf("Payload: %s\n", data->payload);
  125. QRCodeResult = (const char *)data->payload;
  126. }