Chain Mic

MicChain 是链式总线上麦克风设备的辅助类。它提供了读取 ADC 值、配置阈值、设置触发周期和监控麦克风触发事件的方法。

支持以下产品:

Chain MIC

UiFlow2 应用示例

麦克风声音检测

在 UiFlow2 中打开 m5core_chain_mic_basic_example.m5f2 项目。

本示例演示如何从 Chain MIC 传感器读取 ADC 值并监控声音检测。

UiFlow2 代码块:

m5core_chain_mic_basic_example.png

示例输出:

None

MicroPython 应用示例

麦克风声音检测

本示例演示如何从 Chain MIC 传感器读取 ADC 值并监控声音检测。

MicroPython 代码块:

 1# SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from chain import MicChain
 9import time
10from chain import ChainBus
11
12
13title0 = None
14label_adc = None
15label_status = None
16bus2 = None
17chain_mic_0 = None
18last_trigger_time = None
19
20
21def chain_mic_0_low_to_high_event(args):
22    global title0, label_adc, label_status, bus2, chain_mic_0, last_trigger_time
23    last_trigger_time = time.ticks_ms()
24    label_status.setVisible(True)
25
26
27def chain_mic_0_high_to_low_event(args):
28    global title0, label_adc, label_status, bus2, chain_mic_0, last_trigger_time
29    last_trigger_time = time.ticks_ms()
30    label_status.setVisible(True)
31
32
33def setup():
34    global title0, label_adc, label_status, bus2, chain_mic_0, last_trigger_time
35
36    M5.begin()
37    Widgets.setRotation(1)
38    Widgets.fillScreen(0x000000)
39    title0 = Widgets.Title("Chain MIC Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu24)
40    label_adc = Widgets.Label("ADC: --", 95, 76, 1.0, 0xFFFFFF, 0x000000, Widgets.FONTS.DejaVu24)
41    label_status = Widgets.Label(
42        "voice trigger", 82, 158, 1.0, 0x4CEB18, 0x000000, Widgets.FONTS.DejaVu24
43    )
44    bus2 = ChainBus(2, tx=21, rx=22)
45    chain_mic_0 = MicChain(bus2, 1)
46    chain_mic_0.set_trigger_callback(MicChain.TRIGGER_LOW_TO_HIGH, chain_mic_0_low_to_high_event)
47    chain_mic_0.set_trigger_callback(MicChain.TRIGGER_HIGH_TO_LOW, chain_mic_0_high_to_low_event)
48    chain_mic_0.set_trigger(True)
49    chain_mic_0.set_rgb_color(0x000064)
50    label_status.setVisible(False)
51
52
53def loop():
54    global title0, label_adc, label_status, bus2, chain_mic_0, last_trigger_time
55    M5.update()
56    label_adc.setText(str((str("ADC: ") + str((chain_mic_0.get_adc12())))))
57    time.sleep_ms(100)
58    if (time.ticks_diff((time.ticks_ms()), last_trigger_time)) > 3000:
59        label_status.setVisible(False)
60
61
62if __name__ == "__main__":
63    try:
64        setup()
65        while True:
66            loop()
67    except (Exception, KeyboardInterrupt) as e:
68        try:
69            bus2.deinit()
70            from utility import print_error_msg
71
72            print_error_msg(e)
73        except ImportError:
74            print("please update to latest firmware")

示例输出:

None

API 参考

MicChain

class chain.mic.MicChain(bus, device_id)

基类:KeyChain

用于通过链式总线与麦克风设备交互的 Mic Chain 类。

参数:
  • bus (ChainBus) – Chain 总线实例。

  • device_id (int) – Chain Bus 上麦克风的设备 ID。

UiFlow2 代码块:

init.png

MicroPython 代码块:

from chain import ChainBus
from chain import MicChain

bus2 = ChainBus(2, tx=21, rx=22)
chain_mic_0 = MicChain(bus2, 1)

其他按钮和一些通用方法,请参考 ChainKey 类。

get_adc8()

获取麦克风的 8 位 ADC 值。

返回:

8 位 ADC 值(0-255),失败时返回 None。

返回类型:

int

UiFlow2 代码块:

get_adc8.png

MicroPython 代码块:

value = chain_mic_0.get_adc8()
get_adc12()

获取麦克风的 12 位 ADC 值。

返回:

12 位 ADC 值(0-4095),失败时返回 None。

返回类型:

int

UiFlow2 代码块:

get_adc12.png

MicroPython 代码块:

value = chain_mic_0.get_adc12()
set_trigger(enable)

启用或禁用阈值触发报告。

参数:

enable (bool) – True 启用阈值触发报告,False 禁用。

返回:

操作成功返回 True,否则返回 False。

返回类型:

bool

UiFlow2 代码块:

set_trigger.png

MicroPython 代码块:

success = chain_mic_0.set_trigger(True)
get_trigger()

获取是否启用阈值触发报告。

返回:

启用阈值触发报告返回 True,禁用返回 False。失败时返回 False。

返回类型:

bool

UiFlow2 代码块:

get_trigger.png

MicroPython 代码块:

enabled = chain_mic_0.get_trigger()
set_trigger_thresh(threshold, save=False)

设置麦克风触发阈值。

参数:
  • threshold (int) – 阈值(0-4095)。

  • save (bool) – 是否将阈值保存到闪存。默认值:False。

返回:

操作成功返回 True,否则返回 False。

返回类型:

bool

UiFlow2 代码块:

set_trigger_thresh.png

MicroPython 代码块:

success = chain_mic_0.set_trigger_thresh(2000, True)
get_trigger_thresh()

获取麦克风触发阈值。

返回:

阈值(0-4095),失败时返回 None。

返回类型:

int

UiFlow2 代码块:

get_trigger_thresh.png

MicroPython 代码块:

threshold = chain_mic_0.get_trigger_thresh()
set_trigger_interval(interval_ms)

设置最小触发间隔(去抖动时间)。

参数:

interval_ms (int) – 触发之间的最小时间间隔(毫秒)。范围:300-1000。

返回:

操作成功返回 True,否则返回 False。

返回类型:

bool

UiFlow2 代码块:

set_trigger_interval.png

MicroPython 代码块:

success = chain_mic_0.set_trigger_interval(500)  # Set 500ms minimum interval between triggers
get_trigger_interval()

获取最小触发间隔(去抖动时间)。

返回:

最小触发间隔(毫秒),失败时返回 None。

返回类型:

int

UiFlow2 代码块:

get_trigger_interval.png

MicroPython 代码块:

interval = chain_mic_0.get_trigger_interval()
set_trigger_callback(trigger_type, callback)

设置麦克风触发事件的回调。

参数:
  • trigger_type (int) – 要监听的触发类型。使用 MicChain.TRIGGER_LOW_TO_HIGH (0) 或 MicChain.TRIGGER_HIGH_TO_LOW (1)。

  • callback – 当麦克风触发时将被调用的回调函数。

返回类型:

None

备注

在回调函数中不能调用链式相关方法。

UiFlow2 代码块:

set_trigger_callback.png

MicroPython 代码块:

def mic_trigger_callback():
    print("Sound detected")

# Listen for low-to-high trigger only
chain_mic_0.set_trigger_callback(MicChain.TRIGGER_LOW_TO_HIGH, mic_trigger_callback)

# Listen for high-to-low trigger only
chain_mic_0.set_trigger_callback(MicChain.TRIGGER_HIGH_TO_LOW, mic_trigger_callback)