ExtEncoder Unit

支持以下产品:

ExtEncoderUnit

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 ExtEncoderUnit
10
11
12label0 = None
13i2c0 = None
14extencoder_0 = None
15
16
17def setup():
18    global label0, i2c0, extencoder_0
19
20    M5.begin()
21    Widgets.fillScreen(0x222222)
22    label0 = Widgets.Label("label0", 132, 109, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
23
24    i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
25    extencoder_0 = ExtEncoderUnit(i2c0, 0x59)
26
27
28def loop():
29    global label0, i2c0, extencoder_0
30    M5.update()
31    if extencoder_0.get_rotary_status():
32        label0.setText(str(extencoder_0.get_rotary_value()))
33
34
35if __name__ == "__main__":
36    try:
37        setup()
38        while True:
39            loop()
40    except (Exception, KeyboardInterrupt) as e:
41        try:
42            from utility import print_error_msg
43
44            print_error_msg(e)
45        except ImportError:
46            print("please update to latest firmware")

UiFlow2 应用示例

example.png

cores3_extencoder_example.m5f2

class ExtEncoderUnit

Constructors

class ExtEncoderUnit(i2c, address: int | list | tuple = 0x59)

创建 ExtEncoderUnit 对象。

UiFlow2

init.png

Methods

ExtEncoderUnit.get_rotary_status() bool

获取 ExtEncoderUnit 对象的旋转状态。

UiFlow2

get_rotary_status.png

ExtEncoderUnit.get_rotary_value() int

获取 ExtEncoderUnit 对象的旋转值。

UiFlow2

get_rotary_value.png

ExtEncoderUnit.get_rotary_increments() int

获取 ExtEncoderUnit 对象的旋转增量。可用于判断旋转方向。

UiFlow2

get_rotary_increments.png

ExtEncoderUnit.reset_rotary_value() None

重置 ExtEncoderUnit 对象的旋转值。

UiFlow2

reset_rotary_value.png

ExtEncoderUnit.set_rotary_value(new_value: int) None

设置 ExtEncoderUnit 对象的旋转值。

参数:

new_value (int) – 调整当前值。

UiFlow2

set_rotary_value.png

ExtEncoderUnit.get_perimeter() int

获取 ExtEncoderUnit 对象的周长。单位为毫米。

UiFlow2

get_perimeter.png

ExtEncoderUnit.set_perimeter(perimeter: int) None

设置 ExtEncoderUnit 对象的周长。

参数:

perimeter (int) – ExtEncoderUnit 对象的周长。单位为毫米。

UiFlow2

set_perimeter.png

ExtEncoderUnit.get_pulse() int

每转脉冲数。

UiFlow2

get_pulse.png

ExtEncoderUnit.set_pulse(pulse: int) None

设置每圈的脉冲数。

参数:

pulse (int) – 每圈的脉冲数。

UiFlow2

set_pulse.png

ExtEncoderUnit.get_zero_mode() int

获取 ExtEncoderUnit 对象的零点模式。

UiFlow2

get_zero_mode.png

ExtEncoderUnit.set_zero_mode(mode: int) None

设置 ExtEncoderUnit 对象的零位模式。

参数:

mode (int) – ExtEncoderUnit 对象的零点模式。

UiFlow2

set_zero_mode.png

ExtEncoderUnit.get_meter_value() int

获取 ExtEncoderUnit 对象的计数值。单位为毫米。

UiFlow2

get_meter_value.png

ExtEncoderUnit.get_zero_pulse_value() int

获取 ExtEncoderUnit 对象的零脉冲值。

UiFlow2

get_zero_pulse_value.png

ExtEncoderUnit.set_zero_pulse_value(value: int) None

设置 ExtEncoderUnit 对象的零脉冲值。

参数:

value (int) – ExtEncoderUnit 对象的零脉冲值。

UiFlow2

set_zero_pulse_value.png

ExtEncoderUnit.get_firmware_version() int

获取 ExtEncoderUnit 对象的固件版本。

UiFlow2

get_firmware_version.png

ExtEncoderUnit.set_address(address) int

设置 ExtEncoderUnit 对象的 I2C 地址。

UiFlow2

set_address.png