MiniEncoderC Hat

以下产品受支持:

Encoder

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 hat import MiniEncoderCHat
 10
 11
 12title0 = None
 13circle0 = None
 14circle1 = None
 15circle2 = None
 16label0 = None
 17i2c0 = None
 18i2c1 = None
 19hat_miniencoderc_0 = None
 20
 21
 22import math
 23
 24angle = None
 25x = None
 26y = None
 27direction = None
 28big_radius = None
 29small_radius = None
 30
 31
 32# Describe this function...
 33def move_small_circle():
 34    global \
 35        angle, \
 36        x, \
 37        y, \
 38        direction, \
 39        big_radius, \
 40        small_radius, \
 41        title0, \
 42        circle0, \
 43        circle1, \
 44        circle2, \
 45        label0, \
 46        i2c0, \
 47        i2c1, \
 48        hat_miniencoderc_0
 49    angle = ((hat_miniencoderc_0.get_rotary_value()) / 58) * 360
 50    x = big_radius * math.cos(angle / 180.0 * math.pi)
 51    y = big_radius * math.sin(angle / 180.0 * math.pi)
 52    circle0.setCursor(x=67, y=120)
 53    circle2.setCursor(x=(67 + (int(x))), y=(120 + (int(y))))
 54
 55
 56def setup():
 57    global \
 58        title0, \
 59        circle0, \
 60        circle1, \
 61        circle2, \
 62        label0, \
 63        i2c0, \
 64        i2c1, \
 65        hat_miniencoderc_0, \
 66        angle, \
 67        x, \
 68        y, \
 69        direction, \
 70        big_radius, \
 71        small_radius
 72
 73    M5.begin()
 74    title0 = Widgets.Title("Mini Encoder", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18)
 75    circle0 = Widgets.Circle(67, 120, 50, 0xFFFFFF, 0x000000)
 76    circle1 = Widgets.Circle(67, 120, 10, 0x00FF00, 0x00FF00)
 77    circle2 = Widgets.Circle(117, 120, 6, 0xFFFFFF, 0xFFFFFF)
 78    label0 = Widgets.Label("Count: 0", 4, 213, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
 79
 80    i2c0 = I2C(0, scl=Pin(33), sda=Pin(32), freq=100000)
 81    i2c1 = I2C(1, scl=Pin(26), sda=Pin(0), freq=100000)
 82    hat_miniencoderc_0 = MiniEncoderCHat(i2c0, 0x42)
 83    big_radius = 50
 84    small_radius = 6
 85
 86
 87def loop():
 88    global \
 89        title0, \
 90        circle0, \
 91        circle1, \
 92        circle2, \
 93        label0, \
 94        i2c0, \
 95        i2c1, \
 96        hat_miniencoderc_0, \
 97        angle, \
 98        x, \
 99        y, \
100        direction, \
101        big_radius, \
102        small_radius
103    M5.update()
104    if hat_miniencoderc_0.get_rotary_status():
105        direction = hat_miniencoderc_0.get_rotary_increments()
106        move_small_circle()
107        if direction < 0:
108            hat_miniencoderc_0.fill_color(0xFF0000)
109            label0.setText(str((str("Count: ") + str((hat_miniencoderc_0.get_rotary_value())))))
110        elif direction > 0:
111            hat_miniencoderc_0.fill_color(0x33FF33)
112            label0.setText(str((str("Count: ") + str((hat_miniencoderc_0.get_rotary_value())))))
113    else:
114        hat_miniencoderc_0.fill_color(0x000000)
115    if hat_miniencoderc_0.get_button_status():
116        circle1.setColor(color=0xFF0000, fill_c=0xFF0000)
117        move_small_circle()
118        hat_miniencoderc_0.reset_rotary_value()
119    else:
120        circle1.setColor(color=0x00FF00, fill_c=0x00FF00)
121
122
123if __name__ == "__main__":
124    try:
125        setup()
126        while True:
127            loop()
128    except (Exception, KeyboardInterrupt) as e:
129        try:
130            from utility import print_error_msg
131
132            print_error_msg(e)
133        except ImportError:
134            print("please update to latest firmware")

UiFlow2 应用示例

example.png

stickc_plus2_mini_encoder_example.m5f2

class MiniEncoderCHat

Constructors

class MiniEncoderCHat(i2c, address: int | list | tuple = 0x42)

创建一个 Rotary 对象。

参数:
  • i2c – I2C 对象。

  • address – I2C 地址,默认为 0x40。

UIFLOW2:

init.png

Methods

MiniEncoderCHat.get_rotary_status() bool

获取 Rotary 对象的旋转状态。

UIFLOW2:

get_rotary_status.png

MiniEncoderCHat.get_rotary_value() int

获取 Rotary 对象的旋转值。

UIFLOW2:

get_rotary_value.png

MiniEncoderCHat.get_rotary_increments() int

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

UIFLOW2:

get_rotary_increments.png

MiniEncoderCHat.reset_rotary_value() None

重置 Rotary 对象的旋转值。

UIFLOW2:

reset_rotary_value.png

MiniEncoderCHat.get_button_status() bool

获取旋转编码器按键的当前状态。

UIFLOW2:

get_button_status.png

MiniEncoderCHat.set_rotary_value(new_value: int) None

设置 Rotary 对象的旋转值。

参数:

new_value (int) – 调整当前值。

UIFLOW2:

set_rotary_value.png

MiniEncoderCHat.fill_color(rgb: int) None

设置 LED 的颜色

参数:

rgb (int) – LED 的颜色,取值范围为 0x000000 - 0xFFFFFF。

UIFLOW2:

fill_color.png

MiniEncoderCHat.read_fw_version() str

获取设备的固件版本。

UIFLOW2:

read_fw_version.png

MiniEncoderCHat.set_address(address) None

设置设备的 I2C 地址。

UIFLOW2:

set_address.png