QRCode Module

这个库是 Module13.2 QRCode 的驱动,该模块使用 UART 通信。

支持以下产品:

Module13.2 QRCode

UiFlow2 应用示例:

连续模式二维码识别

在 UiFlow2 上打开 cores3_qrcode_continuous_mode_example.m5f2 项目。

在连续模式下,按下一次按键模块开始解码,再次按下按键模块停止解码。

UiFlow2 代码块:

cores3_qrcode_continuous_mode_example.png

示例输出:

None

自动模式二维码识别

在 UiFlow2 上打开 cores3_qrcode_auto_mode_example.m5f2 项目。

在自动模式下,模块上电开始识别解码,不可停止。

UiFlow2 代码块:

cores3_qrcode_auto_mode_example.png

示例输出:

None

脉冲模式二维码识别

在 UiFlow2 上打开 cores3_qrcode_pulse_mode_example.m5f2 项目。

在脉冲模式下,设置触发引脚超过 20ms 的低电平触发解码一次。

UiFlow2 代码块:

cores3_qrcode_pulse_mode_example.png

示例输出:

None

移动感应模式二维码识别

在 UiFlow2 上打开 cores3_qrcode_motion_sensing_mode_example.m5f2 项目。

在感应模式下,模块基于视觉识别信息检测到场景变化时,会自动触发解码。

UiFlow2 代码块:

cores3_qrcode_motion_sensing_mode_example.png

示例输出:

None

MicroPython 应用示例:

连续模式二维码识别

在连续模式下,按下一次按键模块开始解码,再次按下按键模块停止解码。

MicroPython 代码块:

 1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from module import QRCodeModule
 9
10
11title0 = None
12label_status = None
13label_data = None
14module_qrcode_0 = None
15is_scanning = None
16data = None
17
18
19def btn_pwr_was_clicked_event(state):
20    global title0, label_status, label_data, module_qrcode_0, is_scanning, data
21    if is_scanning:
22        module_qrcode_0.stop_decode()
23        label_status.setColor(0xFFFFFF, 0x222222)
24        label_status.setText(str("stop scan"))
25    else:
26        module_qrcode_0.start_decode()
27        label_status.setColor(0x00FF00, 0x222222)
28        label_status.setText(str("scanning"))
29    is_scanning = not is_scanning
30
31
32def setup():
33    global title0, label_status, label_data, module_qrcode_0, is_scanning, data
34    M5.begin()
35    Widgets.fillScreen(0x222222)
36    title0 = Widgets.Title("QRCode", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu24)
37    label_status = Widgets.Label(
38        "stop scan", 5, 50, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24
39    )
40    label_data = Widgets.Label("data", 4, 100, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24)
41    BtnPWR.setCallback(type=BtnPWR.CB_TYPE.WAS_CLICKED, cb=btn_pwr_was_clicked_event)
42    module_qrcode_0 = QRCodeModule(1, tx=17, rx=18)
43    module_qrcode_0.set_trigger_mode(QRCodeModule.TRIGGER_MODE_CONTINUOUS)
44    module_qrcode_0.stop_decode()
45    is_scanning = False
46
47
48def loop():
49    global title0, label_status, label_data, module_qrcode_0, is_scanning, data
50    M5.update()
51    data = module_qrcode_0.read()
52    if data:
53        label_data.setText(str(data.decode()))
54        print(data.decode())
55
56
57if __name__ == "__main__":
58    try:
59        setup()
60        while True:
61            loop()
62    except (Exception, KeyboardInterrupt) as e:
63        try:
64            from utility import print_error_msg
65
66            print_error_msg(e)
67        except ImportError:
68            print("please update to latest firmware")

示例输出:

None

自动模式二维码识别

在自动模式下,模块上电开始识别解码,不可停止。

MicroPython 代码块:

 1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from module import QRCodeModule
 9
10
11title0 = None
12label_status = None
13label_data = None
14module_qrcode_0 = None
15data = None
16
17
18def setup():
19    global title0, label_status, label_data, module_qrcode_0, data
20    M5.begin()
21    Widgets.fillScreen(0x222222)
22    title0 = Widgets.Title("QRCode", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu24)
23    label_status = Widgets.Label(
24        "scanning", 5, 50, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24
25    )
26    label_data = Widgets.Label("data", 5, 100, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24)
27    module_qrcode_0 = QRCodeModule(2, tx=17, rx=18)
28    module_qrcode_0.set_trigger_mode(QRCodeModule.TRIGGER_MODE_AUTO)
29
30
31def loop():
32    global title0, label_status, label_data, module_qrcode_0, data
33    M5.update()
34    data = module_qrcode_0.read()
35    if data:
36        label_data.setText(str(data.decode()))
37
38
39if __name__ == "__main__":
40    try:
41        setup()
42        while True:
43            loop()
44    except (Exception, KeyboardInterrupt) as e:
45        try:
46            from utility import print_error_msg
47
48            print_error_msg(e)
49        except ImportError:
50            print("please update to latest firmware")

示例输出:

None

脉冲模式二维码识别

在脉冲模式下,设置触发引脚超过 20ms 的低电平触发解码一次。

MicroPython 代码块:

 1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from module import QRCodeModule
 9import time
10
11
12title0 = None
13label_status = None
14label_data = None
15module_qrcode_0 = None
16data = None
17
18
19def btn_pwr_was_clicked_event(state):
20    global title0, label_status, label_data, module_qrcode_0, data
21    module_qrcode_0.set_trig(0)
22    time.sleep_ms(20)
23    module_qrcode_0.set_trig(1)
24    label_status.setColor(0x00FF00, 0x222222)
25    label_status.setText(str("scanning"))
26
27
28def setup():
29    global title0, label_status, label_data, module_qrcode_0, data
30    M5.begin()
31    Widgets.fillScreen(0x222222)
32    title0 = Widgets.Title("QRCode", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu24)
33    label_status = Widgets.Label(
34        "scanning", 5, 50, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24
35    )
36    label_data = Widgets.Label("data", 5, 100, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24)
37    BtnPWR.setCallback(type=BtnPWR.CB_TYPE.WAS_CLICKED, cb=btn_pwr_was_clicked_event)
38    module_qrcode_0 = QRCodeModule(2, tx=17, rx=18)
39    module_qrcode_0.set_trigger_mode(QRCodeModule.TRIGGER_MODE_PULSE)
40
41
42def loop():
43    global title0, label_status, label_data, module_qrcode_0, data
44    M5.update()
45    data = module_qrcode_0.read()
46    if data:
47        label_data.setText(str(data.decode()))
48        label_status.setColor(0xFFFFFF, 0x222222)
49        label_status.setText(str("stop scan"))
50
51
52if __name__ == "__main__":
53    try:
54        setup()
55        while True:
56            loop()
57    except (Exception, KeyboardInterrupt) as e:
58        try:
59            from utility import print_error_msg
60
61            print_error_msg(e)
62        except ImportError:
63            print("please update to latest firmware")

示例输出:

None

移动感应模式二维码识别

在感应模式下,模块基于视觉识别信息检测到场景变化时,会自动触发解码。

MicroPython 代码块:

 1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from module import QRCodeModule
 9
10
11title0 = None
12label_status = None
13label_data = None
14module_qrcode_0 = None
15data = None
16
17
18def setup():
19    global title0, label_status, label_data, module_qrcode_0, data
20    M5.begin()
21    Widgets.fillScreen(0x222222)
22    title0 = Widgets.Title("QRCode", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu24)
23    label_status = Widgets.Label(
24        "detecting", 5, 50, 1.0, 0x00FF00, 0x222222, Widgets.FONTS.DejaVu24
25    )
26    label_data = Widgets.Label("data", 5, 100, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24)
27    module_qrcode_0 = QRCodeModule(2, tx=17, rx=18)
28    module_qrcode_0.set_trigger_mode(QRCodeModule.TRIGGER_MODE_MOTION_SENSING)
29    module_qrcode_0.set_motion_sensitivity(1)
30
31
32def loop():
33    global title0, label_status, label_data, module_qrcode_0, data
34    M5.update()
35    data = module_qrcode_0.read()
36    if data:
37        label_data.setText(str(data.decode()))
38
39
40if __name__ == "__main__":
41    try:
42        setup()
43        while True:
44            loop()
45    except (Exception, KeyboardInterrupt) as e:
46        try:
47            from utility import print_error_msg
48
49            print_error_msg(e)
50        except ImportError:
51            print("please update to latest firmware")

示例输出:

None

API应用

QRCodeModule

class module.qrcode.QRCodeModule

创建一个 QRCodeModule 对象。

参数:
  • id (int) – UART 端口号。

  • tx (int) – UART 发送引脚。

  • rx (int) – UART 接收引脚。

UiFlow2 代码块:

init.png

MicroPython 代码块:

from module import ModuleQRCode

module_qrcode = ModuleQRCode(id = 1, tx = 17, rx = 18)
set_power(enable)

设置二维码识别模块电源。

参数:

enable (bool) –

  • True : 打开。

  • False : 关闭。

UiFlow2 代码块:

set_power.png

MicroPython 代码块:

module_qrcode.set_power(enable)
set_trig(value)

设置触发引脚电平。

参数:

value (int) –

  • 0:低电平。

  • 1:高电平。

UiFlow2 代码块:

set_trig.png

MicroPython 代码块:

module_qrcode.set_trig(value)
start_decode()

开始解码。

UiFlow2 代码块:

start_decode.png

MicroPython 代码块:

module_qrcode.start_decode()
stop_decode()

停止解码。

UiFlow2 代码块:

stop_decode.png

MicroPython 代码块:

module_qrcode.stop_decode()
read()

读取二维码数据。

返回:

二维码数据。

返回类型:

None | bytes

如果未识别到,则返回 None。

UiFlow2 代码块:

read.png

MicroPython 代码块:

module_qrcode.read()
set_trigger_mode(mode)

设置触发模式。

参数:

mode (int) – 触发模式,可选项: - TRIGGER_MODE_KEY: 按键模式,触发引脚低电平开始解码,高电平停止解码。 - TRIGGER_MODE_CONTINUOUS: 连续模式: 调用 start_decode() 开始解码,调用 stop_decode() 停止解码。 - TRIGGER_MODE_AUTO: 自动模式: 上电开始解码,不可停止。 - TRIGGER_MODE_PULSE: 脉冲模式,触发引脚 20ms 以上低电平触发一次解码。 - TRIGGER_MODE_MOTION_SENSING: 移动感应模式,使用图像检测,当检测到场景发生变化时触发解码。

UiFlow2 代码块:

set_trigger_mode.png

MicroPython 代码块:

module_qrcode.set_trigger_mode(mode)
set_decode_delay(delay_ms)

设置命令解码延迟 (按键模式下)。

参数:

delay_ms (int) – 解码延迟时间(单位:毫秒),0 表示持续解码直到成功。

UiFlow2 代码块:

set_decode_delay.png

MicroPython 代码块:

module_qrcode.set_decode_delay(delay_ms)
set_trigger_timeout(timeout_ms)

设置触发超时。(脉冲模式下)。

参数:

timeout_ms – 触发超时时间(单位:毫秒),解码持续时间超过该值时自动停止解码。

UiFlow2 代码块:

set_trigger_timeout.png

MicroPython 代码块:

module_qrcode.set_trigger_timeout(timeout_ms)
set_motion_sensitivity(level)

设置移动感应灵敏度。(感应模式下)

参数:

level (int) – 灵敏度等级,范围 1-5,等级越高对场景变化越敏感。默认等级为 3。

UiFlow2 代码块:

set_motion_sensitivity.png

MicroPython 代码块:

module_qrcode.set_motion_sensitivity(level)
set_continuous_decode_delay(delay_100ms)

设置持续解码延迟时间。(感应模式下)

参数:

delay_ms (int) – 延迟时间,单位为 100 毫秒。值为 0 表示持续解码直到超时。

UiFlow2 代码块:

set_continuous_decode_delay.png

MicroPython 代码块:

module_qrcode.set_continuous_decode_delay(delay_ms)
set_trigger_decode_delay(delay_ms):

设置触发解码延迟时间(感应模式下)

触发解码延迟时间。当重新进入检测场景变化阶段,到检测到变化再次开始识读之间的延迟时长。

参数:

delay_ms (int) – 触发解码延迟时间,单位为毫秒。

UiFlow2 代码块:

set_trigger_decode_delay.png

MicroPython 代码块:

module_qrcode.set_trigger_decode_delay(delay_ms)
set_same_code_interval(interval_ms)

设置同码间隔时间。

参数:

interval_ms (int) – 相同条码的重复识读间隔时间,单位为毫秒。

UiFlow2 代码块:

set_same_code_interval.png

MicroPython 代码块:

module_qrcode.set_same_code_interval(interval_ms)
set_diff_code_interval(interval_ms)

设置异码间隔时间。

参数:

interval_ms (int) – 不同码的识读间隔时间,单位为毫秒。

UiFlow2 代码块:

set_diff_code_interval.png

MicroPython 代码块:

module_qrcode.set_diff_code_interval(interval_ms)
set_same_code_no_delay(enable)

设置同码非延迟输出。

参数:

enable (bool) – 是否开启同码非延迟输出,True 表示开启,False 表示关闭。

UiFlow2 代码块:

set_same_code_no_delay.png

MicroPython 代码块:

module_qrcode.set_same_code_no_delay(enable)
set_fill_light_mode(mode)

设置补光灯模式。

参数:

mode (int) – 补光灯模式,可选项:- FILL_LIGHT_OFF: 常灭。 - FILL_LIGHT_ON: 常亮。 - FILL_LIGHT_ON_DECODE: 解码时亮。

UiFlow2 代码块:

set_fill_light_mode.png

MicroPython 代码块:

module_qrcode.set_fill_light_mode(mode)
set_fill_light_brightness(brightness)

设置补光灯亮度。

参数:

brightness (int) – 补光灯亮度,范围:0~100。

UiFlow2 代码块:

set_fill_light_brightness.png

MicroPython 代码块:

module_qrcode.set_fill_light_brightness(brightness)
set_pos_light_mode(mode)

设置定位灯模式

参数:

mode (int) – 定位灯模式,可选项: - POS_LIGHT_OFF: 常灭。 - POS_LIGHT_ON_DECODE: 解码时亮。 - POS_LIGHT_FLASH_ON_DECODE: 解码时闪烁。

UiFlow2 代码块:

set_pos_light_mode.png

MicroPython 代码块:

module_qrcode.set_pos_light_mode(mode)
set_startup_tone(mode)

设置启动提示音。

参数:

mode (int) –

  • 0: 关闭启动提示音。

  • 1: 4 声。

  • 2: 2 声。

UiFlow2 代码块:

set_startup_tone.png

MicroPython 代码块:

module_qrcode.set_startup_tone(mode)
set_decode_success_beep(count)

设置解码成功提示音。

参数:

count (int) –

  • 0: 无提示音。

  • 1: 播放1次。

  • 2: 播放2次。

UiFlow2 代码块:

set_decode_success_beep.png

MicroPython 代码块:

module_qrcode.set_decode_success_beep(count)
set_case_conversion(mode)

设置大小写转换。

参数:

mode (int) –

  • 0: 不转换。

  • 1: 转为大写。

  • 2: 转为小写。

UiFlow2 代码块:

set_case_conversion.png

MicroPython 代码块:

module_qrcode.set_case_conversion(mode)
set_protocol_format(mode):
set_protocol_format(mode)
参数:

mode (int) –

  • 0`: 无协议 “

  • 1: 格式 1:[0x03] + 数据长度(2bytes) + 数据

  • 2: 格式 2:[0x03] + 数据长度 + 条码个数 + 条码 1 数据长度 + 条码 1 数据 +… + CRC

  • 3: 格式 3:[0x03] + 数据长度 + 条码个数 + 条码 1ID 号 + 条码 1 数据长度 + 条码 1 数据 + … + CRC

CRC 生成参考程序。

def crc16_calc(data: bytes) -> int:
    ca_crc = 0
    for byte in data:
        for i in range(7, -1, -1):
            if ca_crc & 0x8000:
                ca_crc = (ca_crc << 1) ^ 0x18005
            else:
                ca_crc <<= 1
            if (byte & (1 << i)) != 0:
                ca_crc ^= 0x18005
    return ca_crc & 0xFFFF

UiFlow2 代码块:

set_protocol_format.png

MicroPython 代码块:

module_qrcode.set_protocol_format(mode)