Browse Source

Add 'CARDPUTER Arduino/qrcodei2c.ino'

cube 7 months ago
parent
commit
194835193c
1 changed files with 70 additions and 0 deletions
  1. 70
    0
      CARDPUTER Arduino/qrcodei2c.ino

+ 70
- 0
CARDPUTER Arduino/qrcodei2c.ino View File

@@ -0,0 +1,70 @@
1
+/**
2
+ * @file i2c_mode.ino
3
+ * @author SeanKwok (shaoxiang@m5stack.com)
4
+ * @brief Unit QRCode I2C Mode Example
5
+ * @version 0.1
6
+ * @date 2024-01-30
7
+ *
8
+ *
9
+ * @Hardwares: M5Core + Unit Synth
10
+ * @Platform Version: Arduino M5Stack Board Manager v2.1.0
11
+ * @Dependent Library:
12
+ * M5Unified: https://github.com/m5stack/M5Unified
13
+ * M5GFX: https://github.com/m5stack/M5GFX
14
+ * M5UnitQRCode: https://github.com/m5stack/M5Unit-QRCode
15
+ */
16
+#include <M5Unified.h>
17
+#include <M5GFX.h>
18
+#include "M5UnitQRCode.h"
19
+
20
+M5Canvas canvas(&M5.Display);
21
+
22
+M5UnitQRCodeI2C qrcode;
23
+
24
+void setup() {
25
+    M5.begin();
26
+
27
+    canvas.setColorDepth(1);  // mono color
28
+    canvas.createSprite(M5.Display.width(), M5.Display.height());
29
+    canvas.setTextSize((float)canvas.width() / 160);
30
+    canvas.setTextScroll(true);
31
+
32
+    while (!qrcode.begin(&Wire, UNIT_QRCODE_ADDR, G2, G1, 100000U)) {
33
+        canvas.println("Unit QRCode I2C Init Fail");
34
+        Serial.println("Unit QRCode I2C Init Fail");
35
+        canvas.pushSprite(0, 0);
36
+        delay(1000);
37
+    }
38
+
39
+    canvas.println("Unit QRCode I2C Init Success");
40
+    Serial.println("Unit QRCode I2C Init Success");
41
+    canvas.println("Manual Scan Mode");
42
+    canvas.pushSprite(0, 0);
43
+    qrcode.setTriggerMode(MANUAL_SCAN_MODE);
44
+}
45
+
46
+void loop() {
47
+    if (qrcode.getDecodeReadyStatus() == 1) {
48
+        uint8_t buffer[512] = {0};
49
+        uint16_t length     = qrcode.getDecodeLength();
50
+        Serial.printf("len:%d\r\n", length);
51
+        qrcode.getDecodeData(buffer, length);
52
+        Serial.printf("decode data:");
53
+        for (int i = 0; i < length; i++) {
54
+            Serial.printf("%c", buffer[i]);
55
+            canvas.printf("%c", buffer[i]);
56
+        }
57
+        Serial.println();
58
+        canvas.println();
59
+        canvas.pushSprite(0, 0);
60
+    }
61
+    M5.update();
62
+    if (M5.BtnA.wasPressed()) {
63
+        // start scan
64
+        qrcode.setDecodeTrigger(1);
65
+    }
66
+    if (M5.BtnB.wasPressed()) {
67
+        // stop scan
68
+        qrcode.setDecodeTrigger(0);
69
+    }
70
+}