振动器 Unit

支持以下产品:

振动器

MicroPython 应用示例

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from hardware import *
 9from unit import VibratorUnit
10
11
12label0 = None
13vibrator_0 = None
14
15
16def btnB_wasClicked_event(state):  # noqa: N802
17    global label0, vibrator_0
18    vibrator_0.once(freq=1000, duty=15, duration=50)
19
20
21def setup():
22    global label0, vibrator_0
23
24    M5.begin()
25    Widgets.fillScreen(0x222222)
26    label0 = Widgets.Label("play", 127, 210, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24)
27
28    BtnB.setCallback(type=BtnB.CB_TYPE.WAS_CLICKED, cb=btnB_wasClicked_event)
29
30    vibrator_0 = VibratorUnit((36, 26))
31
32
33def loop():
34    global label0, vibrator_0
35    M5.update()
36
37
38if __name__ == "__main__":
39    try:
40        setup()
41        while True:
42            loop()
43    except (Exception, KeyboardInterrupt) as e:
44        try:
45            from utility import print_error_msg
46
47            print_error_msg(e)
48        except ImportError:
49            print("please update to latest firmware")

UiFlow2 应用示例

example.png

core_vibrator_example.m5f2

class VibratorUnit

Constructors

class VibratorUnit(port: tuple = (26, 0))

创建一个 VibratorUnit 对象。

参数:

port – VibratorUnit 连接的端口。

UIFLOW2:

init.png

Methods

VibratorUnit.once(freq=10, duty=50, duration=50) None

在电机上播放一次触觉效果。

参数:
  • freq (int) – 振动频率范围为 10-55 Hz。

  • duty (int) – 振动的占空比范围为 0-100,表示相应的百分比。

  • duration (int) – 振动效果的持续时间,单位为毫秒。

UIFLOW2:

once.png

VibratorUnit.set_freq(freq)

设置振动频率。

参数:

freq (int) – 振动频率范围为 10-55 Hz。

UIFLOW2:

set_freq.png

VibratorUnit.set_duty(freq) None

设置振动占空比。

参数:

duty (int) – 振动的占空比范围为 0-100,表示相应的百分比。

UIFLOW2:

set_duty.png

VibratorUnit.turn_off() None

关闭电机。

UIFLOW2:

turn_off.png

VibratorUnit.deint() None

反初始化电机。

UIFLOW2:

deinit.png