eried 2 лет назад
Родитель
Сommit
a8abf25a17
2 измененных файлов с 159 добавлено и 6 удалено
  1. 146 0
      esp32cam_marauder/QRCodeReader.ino
  2. 13 6
      esp32cam_marauder/esp32cam_marauder.ino

+ 146 - 0
esp32cam_marauder/QRCodeReader.ino

@@ -0,0 +1,146 @@
+// Based on https://drive.google.com/file/d/1w8hJ5NhNik3qo2xl6eDrlgG4n6R4LuJ_/view
+#include "quirc.h"
+
+struct QRCodeData
+{
+  bool valid;
+  int dataType;
+  uint8_t payload[1024];
+  int payloadLen;
+};
+
+// creating a task handle
+TaskHandle_t QRCodeReader_Task; 
+
+struct quirc *q = NULL;
+uint8_t *image = NULL;  
+camera_fb_t * fb = NULL;
+struct quirc_code code;
+struct quirc_data data;
+quirc_decode_error_t err;
+struct QRCodeData qrCodeData;  
+String QRCodeResult = "";
+
+void qr_reader_setup() {
+  // camera init
+
+  camera_config_t config;
+  config.ledc_channel = LEDC_CHANNEL_0;
+  config.ledc_timer = LEDC_TIMER_0;
+  config.pin_d0 = Y2_GPIO_NUM;
+  config.pin_d1 = Y3_GPIO_NUM;
+  config.pin_d2 = Y4_GPIO_NUM;
+  config.pin_d3 = Y5_GPIO_NUM;
+  config.pin_d4 = Y6_GPIO_NUM;
+  config.pin_d5 = Y7_GPIO_NUM;
+  config.pin_d6 = Y8_GPIO_NUM;
+  config.pin_d7 = Y9_GPIO_NUM;
+  config.pin_xclk = XCLK_GPIO_NUM;
+  config.pin_pclk = PCLK_GPIO_NUM;
+  config.pin_vsync = VSYNC_GPIO_NUM;
+  config.pin_href = HREF_GPIO_NUM;
+  config.pin_sscb_sda = SIOD_GPIO_NUM;
+  config.pin_sscb_scl = SIOC_GPIO_NUM;
+  config.pin_pwdn = PWDN_GPIO_NUM;
+  config.pin_reset = RESET_GPIO_NUM;
+  config.xclk_freq_hz = 10000000;
+  config.pixel_format = PIXFORMAT_GRAYSCALE;
+  config.frame_size = FRAMESIZE_QVGA;
+  config.jpeg_quality = 15;
+  config.fb_count = 1;
+  
+  /*#if defined(CAMERA_MODEL_ESP_EYE)
+    pinMode(13, INPUT_PULLUP);
+    pinMode(14, INPUT_PULLUP);
+  #endif*/
+
+  esp_err_t err = esp_camera_init(&config);
+  if (err != ESP_OK) {
+    Serial.printf("Camera init failed with error 0x%x", err);
+    ESP.restart();
+  }
+  
+  sensor_t * s = esp_camera_sensor_get();
+  s->set_framesize(s, FRAMESIZE_QVGA);
+  
+  Serial.println("Configure and initialize the camera successfully.");
+  Serial.println();
+  /* ---------------------------------------- */
+
+  /* ---------------------------------------- create "QRCodeReader_Task" using the xTaskCreatePinnedToCore() function */
+  xTaskCreatePinnedToCore(
+             QRCodeReader,          /* Task function. */
+             "QRCodeReader_Task",   /* name of task. */
+             10000,                 /* Stack size of task */
+             NULL,                  /* parameter of the task */
+             1,                     /* priority of the task */
+             &QRCodeReader_Task,    /* Task handle to keep track of created task */
+             0);                    /* pin task to core 0 */
+  /* ---------------------------------------- */
+}
+
+void qr_reader_loop() {
+  delay(1);
+}
+
+void QRCodeReader( void * pvParameters ){
+  /* ---------------------------------------- */
+  Serial.println("QRCodeReader is ready.");
+  Serial.print("QRCodeReader running on core ");
+  Serial.println(xPortGetCoreID());
+  Serial.println();
+  /* ---------------------------------------- */
+
+  /* ---------------------------------------- Loop to read QR Code in real time. */
+  while(1){
+      q = quirc_new();
+      if (q == NULL){
+        Serial.print("can't create quirc object\r\n");  
+        continue;
+      }
+    
+      fb = esp_camera_fb_get();
+      if (!fb)
+      {
+        Serial.println("Camera capture failed");
+        continue;
+      }   
+      
+      quirc_resize(q, fb->width, fb->height);
+      image = quirc_begin(q, NULL, NULL);
+      memcpy(image, fb->buf, fb->len);
+      quirc_end(q);
+      
+      int count = quirc_count(q);
+      if (count > 0) {
+        quirc_extract(q, 0, &code);
+        err = quirc_decode(&code, &data);
+    
+        if (err){
+          Serial.println("Decoding FAILED");
+          QRCodeResult = "Decoding FAILED";
+        } else {
+          //Serial.printf("Decoding successful:\n");
+          dumpData(&data);
+        } 
+        Serial.println();
+      } 
+      
+      esp_camera_fb_return(fb);
+      fb = NULL;
+      image = NULL;  
+      quirc_destroy(q);
+  }
+  /* ---------------------------------------- */
+}
+
+void dumpData(const struct quirc_data *data)
+{
+  /*Serial.printf("Version: %d\n", data->version);
+  Serial.printf("ECC level: %c\n", "MLHQ"[data->ecc_level]);
+  Serial.printf("Mask: %d\n", data->mask);
+  Serial.printf("Length: %d\n", data->payload_len);*/
+  Serial.printf("Payload: %s\n", data->payload);
+  
+  QRCodeResult = (const char *)data->payload;
+}

+ 13 - 6
esp32cam_marauder/esp32cam_marauder.ino

@@ -160,13 +160,20 @@ void setup()
 
   unsigned long waitForStreamMode = millis() + 1000;
 
-  while (waitForStreamMode > millis())
-  {
-    if (Serial.available()) // if we receive anything, just switch to stream mode
+  while (waitForStreamMode > millis()) {
+    if (Serial.available())  // if we receive anything, just switch to another mode
     {
-      cam_stream_setup();
-      for (;;)
-        cam_stream_loop();
+      switch (Serial.read()) {
+        case 'q':  // QR code reader mode
+          qr_reader_setup();
+          for (;;)
+            qr_reader_loop();
+
+        default:  // Camera stream
+          cam_stream_setup();
+          for (;;)
+            cam_stream_loop();
+      }
     }
   }
   //Serial.begin(115200);