Skip to content

Commit dadb0af

Browse files
TinyuZhaolbuque
authored andcommitted
libs/module: Add Module ASR docs and example.
Signed-off-by: tinyu <tinyu.zhao@gmail.com>
1 parent 54e8246 commit dadb0af

7 files changed

Lines changed: 191 additions & 6 deletions

File tree

docs/en/module/asr.rst

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
ASR Module
3+
============
4+
.. sku:M147
5+
.. include:: ../refs/module.asr.ref
6+
7+
This is the driver library of ASR Module.
8+
9+
Support the following products:
10+
11+
|ASRModule|
12+
13+
UiFlow2 Example
14+
---------------
15+
16+
ASR Example
17+
^^^^^^^^^^^^^
18+
19+
Open the |asr_core2_example.m5f2| project in UiFlow2.
20+
21+
This example shows how to use Module ASR to get the current command word, command number, and trigger an event when you say hello to do something you want to do.
22+
23+
UiFlow2 Code Block:
24+
25+
|example.png|
26+
27+
Example output:
28+
29+
None
30+
31+
MicroPython Example
32+
-------------------
33+
34+
ASR Example
35+
^^^^^^^^^^^^^
36+
37+
This example shows how to use Module ASR to get the current command word, command number, and trigger an event when you say hello to do something you want to do.
38+
39+
MicroPython Code Block:
40+
41+
.. literalinclude:: ../../../examples/module/asr/asr_core2_example.py
42+
:language: python
43+
:linenos:
44+
45+
Example output:
46+
47+
None
48+
49+
**API**
50+
-------
51+
52+
ASRModule
53+
^^^^^^^^^
54+
55+
.. autoclass:: module.asr.ASRModule
56+
:members:
57+
:member-order: bysource

docs/en/module/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Module
66

77
4in8out.rst
88
ain4.rst
9+
asr.rst
910
audio.rst
1011
bala2.rst
1112
cc1101.rst

docs/en/refs/module.asr.ref

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
.. |ASRModule| image:: https://m5stack-doc.oss-cn-shenzhen.aliyuncs.com/635/U914_01.webp
3+
:target: https://docs.m5stack.com/en/module/M147
4+
:height: 200px
5+
:width: 200px
6+
7+
.. |init.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/asr/init.png
8+
.. |_handler.png| image:: https://static-cdn.m5stack.com/mpy_docs/unit/asr/_handler.png
9+
.. |get_received_status.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/asr/get_received_status.png
10+
.. |send_message.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/asr/send_message.png
11+
.. |get_current_raw_message.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/asr/get_current_raw_message.png
12+
.. |get_current_command_word.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/asr/get_current_command_word.png
13+
.. |get_current_command_num.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/asr/get_current_command_num.png
14+
.. |get_command_handler.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/asr/get_command_handler.png
15+
.. |add_command_word.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/asr/add_command_word.png
16+
.. |event.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/asr/event.png
17+
.. |remove_command_word.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/asr/remove_command_word.png
18+
.. |search_command_num.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/asr/search_command_num.png
19+
.. |search_command_word.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/asr/search_command_word.png
20+
.. |get_command_list.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/asr/get_command_list.png
21+
22+
.. |example.png| image:: https://static-cdn.m5stack.com/mpy_docs/module/asr/example.png
23+
24+
.. |asr_core2_example.m5f2| raw:: html
25+
26+
<a
27+
href="https://uiflow2.m5stack.com/?example=https://raw.githubusercontent.com/m5stack/uiflow-micropython/develop/examples/module/asr/asr_core2_example.m5f2"
28+
target="_blank"
29+
>
30+
asr_core2_example.m5f2
31+
</a>
32+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":"V2.0","versionNumber":"V2.3.8","type":"core2","components":[{"name":"page0","type":"lvgl_page","layer":0,"screenId":"builtin","screenName":"","id":"dT7^4p`VUvYRRoaV","createTime":1764211017876,"backgroundColor":"#ffffff","isLVGL":true,"isSelected":true},{"name":"label0","type":"lvgl_label","layer":1,"screenId":"builtin","screenName":"","id":"lV1xyPF8daYDk0Xs","createTime":1764211783917,"x":1,"y":69,"color":"#000000","backgroundColor":"#ffffff","bg_opacity":0,"text":"label0","font":"lv.font_montserrat_14","pageId":"dT7^4p`VUvYRRoaV","isLVGL":true,"isSelected":false,"width":43,"height":15},{"name":"label1","type":"lvgl_label","layer":2,"screenId":"builtin","screenName":"","id":"xARoaeA39aVbe&Jj","createTime":1764211785795,"x":1,"y":103,"color":"#000000","backgroundColor":"#ffffff","bg_opacity":0,"text":"label1","font":"lv.font_montserrat_14","pageId":"dT7^4p`VUvYRRoaV","isLVGL":true,"isSelected":false,"width":39,"height":15},{"name":"label2","type":"lvgl_label","layer":3,"screenId":"builtin","screenName":"","id":"w=u`pG!QV8&RaQos","createTime":1764211792130,"x":1,"y":138,"color":"#000000","backgroundColor":"#ffffff","bg_opacity":0,"text":"label2","font":"lv.font_montserrat_14","pageId":"dT7^4p`VUvYRRoaV","isLVGL":true,"isSelected":false,"width":42,"height":15}],"resources":[{"hardware":["hardware_button","hardware_pin_button","imu","speaker","touch","mic","sdcard"]},{"module":["module_asr"]}],"units":[],"hats":[],"caps":[],"chains":[],"bases":[],"plcmodules":[],"i2cs":[],"chainBus":[],"blockly":"<block type=\"basic_on_setup\" id=\"setup_block\" deletable=\"false\" x=\"50\" y=\"54\"><mutation isBegin=\"true\"></mutation><field name=\"UPDATEOP\">true</field><statement name=\"FUNC\"><block type=\"system_m5_begin\" id=\"PkGi~+o_ap/UH#K%1V|9\"><next><block type=\"lvgl_page_screen_load\" id=\"%;bWR4amq(WGGIpM=DK/\"><field name=\"NAME\">page0</field><next><block type=\"module_asr_init\" id=\")KxLUWWG%%}i*i@DnMh(\"><field name=\"NAME\">module_asr_0</field><value name=\"UART\"><shadow type=\"module_asr_id_option\" id=\"^|QvQQZgAdVOF-v6(B9W\"><field name=\"VALUE\">2</field></shadow></value><value name=\"TX\"><shadow type=\"module_asr_tx_option\" id=\"l5iO*htp2$Y=l^YR:9~,\"><field name=\"VALUE\">27</field></shadow></value><value name=\"RX\"><shadow type=\"module_asr_rx_option\" id=\"1xjK1[,LeJjZpuzMwSsK\"><field name=\"VALUE\">34</field></shadow></value><next><block type=\"module_asr_add_command_word\" id=\"qwk^9^3@j-VT6o/]Mc?X\"><mutation wordValue=\"hello\"></mutation><field name=\"NAME\">module_asr_0</field><field name=\"WORD\">hello</field><value name=\"NUM\"><block type=\"module_asr_search_command_num\" id=\"rrEKX$M3`ZO99(A;Et%C\"><field name=\"NAME\">module_asr_0</field><value name=\"VALUE\"><shadow type=\"text\" id=\"r[xb[8d_^mJSD-9gsqYg\"><field name=\"TEXT\">hello</field></shadow></value></block></value></block></next></block></next></block></next></block></statement></block><block type=\"basic_on_loop\" id=\"loop_block\" deletable=\"false\" x=\"50\" y=\"370\"><mutation isUpdate=\"true\"></mutation><field name=\"UPDATEOP\">true</field><statement name=\"FUNC\"><block type=\"system_m5_update\" id=\"p_@s_VUlO*zvd9R+8I1F\"><next><block type=\"lvgl_label_set_text\" id=\"MoSX16M~;3?-|YR-SlQ`\"><field name=\"NAME\">label0</field><value name=\"VALUE\"><shadow type=\"text\" id=\"y-q9=]imklL6UvHj82HE\"><field name=\"TEXT\">hello M5</field></shadow><block type=\"text_add_str\" id=\"j!~ZF*~W]JCJMj!{HnY+\"><value name=\"VALUE1\"><shadow type=\"text\" id=\"64A,w:Qt#TF1U@@~{{hS\"><field name=\"TEXT\">Is Receive:</field></shadow></value><value name=\"VALUE2\"><block type=\"module_asr_get_received_status\" id=\"~K[xTB{LP^={wKV)]1vW\"><field name=\"NAME\">module_asr_0</field></block></value></block></value><next><block type=\"lvgl_label_set_text\" id=\"W=[n-bloj6xzen}VoT#*\"><field name=\"NAME\">label1</field><value name=\"VALUE\"><shadow type=\"text\" id=\"y-q9=]imklL6UvHj82HE\"><field name=\"TEXT\">hello M5</field></shadow><block type=\"text_add_str\" id=\"sfsB,o7WM.=Pe(mapJ;V\"><value name=\"VALUE1\"><shadow type=\"text\" id=\"fyB3g6rD-7AObvT(N@Vi\"><field name=\"TEXT\">Command Num:</field></shadow></value><value name=\"VALUE2\"><block type=\"module_asr_get_current_command_num\" id=\"MNmRyKKZ+`(VuL!qO38(\"><field name=\"NAME\">module_asr_0</field></block></value></block></value><next><block type=\"lvgl_label_set_text\" id=\"3$j%^i+lK@C,K1D+5O4?\"><field name=\"NAME\">label2</field><value name=\"VALUE\"><shadow type=\"text\" id=\"y-q9=]imklL6UvHj82HE\"><field name=\"TEXT\">hello M5</field></shadow><block type=\"text_add_str\" id=\"NzAL!ZC@Di94%z|D]=km\"><value name=\"VALUE1\"><shadow type=\"text\" id=\"dV=,|zh6jlmW`oSti/Kj\"><field name=\"TEXT\">Command Word:</field></shadow></value><value name=\"VALUE2\"><block type=\"module_asr_get_current_command_word\" id=\"rP,@Dqe);f2#hFlkY?GT\"><field name=\"NAME\">module_asr_0</field></block></value></block></value><next><block type=\"time_sleep_second\" id=\"zs3maL-]|[[B%[y0~`6d\"><value name=\"SECOND\"><shadow type=\"math_number\" id=\"L^/yfy4o=6CLjFG5AZN+\"><mutation max=\"Infinity\" min=\"0\" precision=\"0\"></mutation><field name=\"NUM\">1</field></shadow></value></block></next></block></next></block></next></block></next></block></statement></block><block type=\"module_asr_event\" id=\"S00tps_4y=rq%3/*tyX+\" x=\"50\" y=\"670\"><field name=\"NAME\">module_asr_0</field><value name=\"WORD\"><shadow type=\"text\" id=\"vRI:qyuX)W+sxZT6zsky\"><field name=\"TEXT\">hello</field></shadow></value><statement name=\"FUNC\"><block type=\"module_asr_send_message\" id=\"+b6N(D_Wfzx:Dw@`sI.r\"><field name=\"NAME\">module_asr_0</field><value name=\"VALUE\"><shadow type=\"math_number\" id=\"xTa.0F#t85a@,0?;Jyv@\"><mutation max=\"Infinity\" min=\"-Infinity\" precision=\"0\"></mutation><field name=\"NUM\">0x5a</field></shadow></value></block></statement></block>","screen":[{"simulationName":"Built-in","type":"builtin","width":320,"height":240,"scale":0.77,"screenName":"","blockId":"","screenColorType":0,"rotation":1,"id":"builtin","createTime":1764211017874}],"logicWhenNum":0,"customList":[]}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import os, sys, io
6+
import M5
7+
from M5 import *
8+
import m5ui
9+
import lvgl as lv
10+
from module import ASRModule
11+
import time
12+
13+
14+
page0 = None
15+
label0 = None
16+
label1 = None
17+
label2 = None
18+
module_asr_0 = None
19+
20+
21+
def module_asr_0_hello_event(args):
22+
global page0, label0, label1, label2, module_asr_0
23+
module_asr_0.send_message(0x5A)
24+
25+
26+
def setup():
27+
global page0, label0, label1, label2, module_asr_0
28+
29+
M5.begin()
30+
Widgets.setRotation(1)
31+
m5ui.init()
32+
page0 = m5ui.M5Page(bg_c=0xFFFFFF)
33+
label0 = m5ui.M5Label(
34+
"label0",
35+
x=1,
36+
y=69,
37+
text_c=0x000000,
38+
bg_c=0xFFFFFF,
39+
bg_opa=0,
40+
font=lv.font_montserrat_14,
41+
parent=page0,
42+
)
43+
label1 = m5ui.M5Label(
44+
"label1",
45+
x=1,
46+
y=103,
47+
text_c=0x000000,
48+
bg_c=0xFFFFFF,
49+
bg_opa=0,
50+
font=lv.font_montserrat_14,
51+
parent=page0,
52+
)
53+
label2 = m5ui.M5Label(
54+
"label2",
55+
x=1,
56+
y=138,
57+
text_c=0x000000,
58+
bg_c=0xFFFFFF,
59+
bg_opa=0,
60+
font=lv.font_montserrat_14,
61+
parent=page0,
62+
)
63+
64+
page0.screen_load()
65+
module_asr_0 = ASRModule(2, tx=27, rx=34)
66+
module_asr_0.add_command_word(
67+
module_asr_0.search_command_num("hello"), "hello", module_asr_0_hello_event
68+
)
69+
70+
71+
def loop():
72+
global page0, label0, label1, label2, module_asr_0
73+
M5.update()
74+
label0.set_text(str((str("Is Receive:") + str((module_asr_0.get_received_status())))))
75+
label1.set_text(str((str("Command Num:") + str((module_asr_0.get_current_command_num())))))
76+
label2.set_text(str((str("Command Word:") + str((module_asr_0.get_current_command_word())))))
77+
time.sleep(1)
78+
79+
80+
if __name__ == "__main__":
81+
try:
82+
setup()
83+
while True:
84+
loop()
85+
except (Exception, KeyboardInterrupt) as e:
86+
try:
87+
m5ui.deinit()
88+
from utility import print_error_msg
89+
90+
print_error_msg(e)
91+
except ImportError:
92+
print("please update to latest firmware")

m5stack/libs/module/asr.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
class ASRModule(ASRUnit):
88
"""Voice recognition hardware module.
99
10-
:param int id: UART port ID for communication. Default is 1.
11-
:param list|tuple port: Tuple containing TX and RX pin numbers.
10+
:param int id: UART port ID for communication. Default is 2.
11+
:param int tx: TX pin number.
12+
:param int rx: RX pin number.
13+
:param bool verbose: Enable verbose output. Default is False.
1214
1315
UiFlow2 Code Block:
1416
@@ -21,8 +23,8 @@ class ASRModule(ASRUnit):
2123
from unit import ASRUnit
2224
2325
# Initialize with UART1, TX on pin 2, RX on pin 1
24-
asr = ASRUnit(id=1, port=(1, 2))
26+
asr = ASRModule(id=2, tx=2, rx=1)
2527
"""
2628

27-
def __init__(self, id: Literal[0, 1, 2] = 1, tx=-1, rx=-1, verbose: bool = False):
29+
def __init__(self, id=1, tx=-1, rx=-1, verbose: bool = False):
2830
super().__init__(id, port=(rx, tx), verbose=verbose)

m5stack/libs/unit/asr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class ASRUnit:
1313
"""Voice recognition hardware module.
1414
15-
:param int id: UART port ID for communication. Default is 1.
15+
:param int id: UART port ID for communication. Default is 2.
1616
:param list|tuple port: Tuple containing TX and RX pin numbers.
1717
1818
UiFlow2 Code Block:
@@ -26,7 +26,7 @@ class ASRUnit:
2626
from unit import ASRUnit
2727
2828
# Initialize with UART1, TX on pin 2, RX on pin 1
29-
asr = ASRUnit(id=1, port=(1, 2))
29+
asr = ASRUnit(id=2, port=(1, 2))
3030
"""
3131

3232
_COMMAND_LIST = {

0 commit comments

Comments
 (0)