USB Module
Module USB 是使用 SPI 接口实现扩展 USB 功能的模块,使用 MAX3421E 实现。
Support the following products:
Micropython Example
备注
使用下列案例前请检查模块拨码开关,确保案例使用相关引脚与模块拨码开关位置一致,具体配置请查看产品说明手册页面。SPI 配置已在内部实现,用户无需关心。
输入/输出引脚控制
模块通过排针引出 5 个 IN(输入)引脚和 5 个 OUT (输出引脚),本案例控制输出引脚高低电平切换,读取并打印输入引脚电平状态。
1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD 2# 3# SPDX-License-Identifier: MIT 4import os, sys, io 5import M5 6from M5 import * 7from module import USBModule 8import time 9 10 11 12module_usb_0 = None 13last_time = None 14i = None 15last_set_time = None 16value = None 17state = None 18toggle = None 19 20 21def setup(): 22 global module_usb_0, last_time, last_set_time, value, state, toggle, i 23 M5.begin() 24 Widgets.fillScreen(0x222222) 25 module_usb_0 = USBModule(pin_cs=1, pin_int=10) 26 module_usb_0.write_gpout(0, 0) 27 module_usb_0.write_gpout(1, 1) 28 module_usb_0.write_gpout(2, 0) 29 module_usb_0.write_gpout(3, 1) 30 module_usb_0.write_gpout(4, 0) 31 state = [0] * 5 32 toggle = True 33 34 35def loop(): 36 global module_usb_0, last_time, last_set_time, value, state, toggle, i 37 M5.update() 38 module_usb_0.poll_data() 39 if (time.ticks_diff((time.ticks_ms()), last_time)) > 200: 40 last_time = time.ticks_ms() 41 for i in range(5): 42 value = module_usb_0.read_gpin(i) 43 state[int((i + 1) - 1)] = value 44 print(state) 45 if (time.ticks_diff((time.ticks_ms()), last_set_time)) > 1000: 46 last_set_time = time.ticks_ms() 47 if toggle: 48 for i in range(5): 49 module_usb_0.write_gpout(i, 1) 50 else: 51 for i in range(5): 52 module_usb_0.write_gpout(i, 0) 53 toggle = not toggle 54 55 56if __name__ == '__main__': 57 try: 58 setup() 59 while True: 60 loop() 61 except (Exception, KeyboardInterrupt) as e: 62 try: 63 from utility import print_error_msg 64 print_error_msg(e) 65 except ImportError: 66 print("please update to latest firmware") 67
鼠标
案例实现 USB 主机,获取鼠标输入
1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD 2# 3# SPDX-License-Identifier: MIT 4import os, sys, io 5import M5 6from M5 import * 7from module import USBModule 8 9 10 11title0 = None 12label_x = None 13label_y = None 14module_usb_0 = None 15x = None 16y = None 17mouse_move = None 18dx = None 19dy = None 20 21 22def setup(): 23 global title0, label_x, label_y, module_usb_0, x, y, mouse_move, dx, dy 24 M5.begin() 25 Widgets.fillScreen(0x222222) 26 title0 = Widgets.Title("Module USB Example mouse", 3, 0xffffff, 0x0000FF, Widgets.FONTS.DejaVu18) 27 label_x = Widgets.Label("x", 130, 90, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18) 28 label_y = Widgets.Label("y", 180, 90, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18) 29 module_usb_0 = USBModule(pin_cs=1, pin_int=10) 30 x = 0 31 y = 0 32 33 34def loop(): 35 global title0, label_x, label_y, module_usb_0, x, y, mouse_move, dx, dy 36 module_usb_0.poll_data() 37 if module_usb_0.is_left_btn_pressed(): 38 print('click left') 39 if module_usb_0.is_right_btn_pressed(): 40 print('click right') 41 if module_usb_0.is_middle_btn_pressed(): 42 print('click middle') 43 mouse_move = module_usb_0.read_mouse_move() 44 dx = mouse_move[0] 45 dy = mouse_move[1] 46 if dx != 0 or dy != 0: 47 print((str('move: ') + str(((str(dx) + str(((str(', ') + str(dy))))))))) 48 x = min(max(x + dx, 0), 320) 49 y = min(max(y + dy, 0), 240) 50 label_x.setText(str(x)) 51 label_y.setText(str(y)) 52 53 54if __name__ == '__main__': 55 try: 56 setup() 57 while True: 58 loop() 59 except (Exception, KeyboardInterrupt) as e: 60 try: 61 from utility import print_error_msg 62 print_error_msg(e) 63 except ImportError: 64 print("please update to latest firmware") 65
键盘
案例实现 USB 主机,获取键盘输入
1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD 2# 3# SPDX-License-Identifier: MIT 4import os, sys, io 5import M5 6from M5 import * 7from module import USBModule 8 9 10 11module_usb_0 = None 12modifier = None 13indata = None 14 15 16def setup(): 17 global module_usb_0, modifier, indata 18 M5.begin() 19 Widgets.fillScreen(0x222222) 20 module_usb_0 = USBModule(pin_cs=1, pin_int=10) 21 22 23def loop(): 24 global module_usb_0, modifier, indata 25 M5.update() 26 module_usb_0.poll_data() 27 modifier = module_usb_0.read_kb_modifier() 28 if modifier & 0x01: 29 print('Left Control pressed') 30 if modifier & 0x02: 31 print('Left Shift pressed') 32 if modifier & 0x04: 33 print('Left Alt pressed') 34 if modifier & 0x08: 35 print('Left GUI pressed') 36 if modifier & 0x10: 37 print('Right Control pressed') 38 if modifier & 0x20: 39 print('Right Shift pressed') 40 if modifier & 0x40: 41 print('Right Alt pressed') 42 if modifier & 0x80: 43 print('Right GUI pressed') 44 indata = module_usb_0.read_kb_input(True) 45 if indata: 46 print(indata) 47 48 49if __name__ == '__main__': 50 try: 51 setup() 52 while True: 53 loop() 54 except (Exception, KeyboardInterrupt) as e: 55 try: 56 from utility import print_error_msg 57 print_error_msg(e) 58 except ImportError: 59 print("please update to latest firmware") 60
UIFlow2.0 Example
输入/输出引脚控制
鼠标
键盘
class USBModule
Constructors
- poll_data()
poll data
Note: It needs to be called in the main loop
UIFlow2.0

- read_mouse_move() tuple[int, int]
读取鼠标光标移动
返回包含鼠标水平位移 x 和垂直位移 y 的元组 (x, y);x 范围: -127~127,静止为0,负值为向左移动,正值为向右移动;y 范围: -127~127,静止为0,负值为向上移动,正值为向下移动。
Example:
move = usb_module.read_mouse_move() x = move[0] y = move[1]
UIFlow2.0

- read_kb_input(convert: bool = True) list
读取键盘输入
convert是否转为 HID Keycode 为对应的字符串。
返回一个包含键盘输入的列表(元素个数最大为6,即一次最多输入6个按键值)
Example:
res = usb_module.read_kb_input(convert=True) # output ['a', 'b', 'Enter'] res = usb_module.read_kb_input(convert=False) # output [0x04, 0x05, 0x28]
UIFlow2.0

- read_kb_modifier() int
读取键盘修饰键,即 “Ctrl”,“Shift”,“Alt”,“Win” 按键。
返回键盘修饰键的状态,通常是通过位掩码来表示不同修饰键的状态。0x01: 左侧控制键(Left Control)
0x02: 左侧 Shift 键(Left Shift)
0x04: 左侧 Alt 键(Left Alt)
0x08: 左侧 Windows 键(Left GUI)
0x10: 右侧控制键(Right Control)
0x20: 右侧 Shift 键(Right Shift)
0x40: 右侧 Alt 键(Right Alt)
0x80: 右侧 Windows 键(Right GUI)
Example:
modifier = module_usb.read_kb_modifier() if modifier & 0x01: print("left ctrl key pressed")
UIFlow2.0

- write_gpout(pin, value)
设置输出引脚电平
pin引脚号返回1 为高电平,0 为低电平
UIFlow2.0












