Encoder Unit

The following products are supported:

Encoder

Micropython Example:

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

UIFLOW2 Example:

example.svg

stickc_plus2_encoder_example.m5f2

class EncoderUnit

Constructors

class EncoderUnit(i2c, address: int | list | tuple = 0x40)

Creates a Rotary object.

参数:
  • i2c – I2C object.

  • address – I2C address, Default is 0x40.

UIFLOW2:

init.svg

Methods

EncoderUnit.get_rotary_status() bool

Gets the rotation status of the Rotary object.

UIFLOW2:

get_rotary_status.svg

EncoderUnit.get_rotary_value() int

Gets the rotation value of the Rotary object.

UIFLOW2:

get_rotary_value.svg

EncoderUnit.get_rotary_increments() int

Gets the rotation increment of the Rotary object. Can be used to determine the direction of rotation.

UIFLOW2:

get_rotary_increments.svg

EncoderUnit.reset_rotary_value() None

Resets the rotation value of the Rotary object.

UIFLOW2:

reset_rotary_value.svg

EncoderUnit.set_rotary_value(new_value: int) None

Sets the rotation value of the Rotary object.

参数:

new_value (int) – adjust the current value.

UIFLOW2:

set_rotary_value.svg

EncoderUnit.get_button_status() bool

Get the current status of the rotary encoder keys.

UIFLOW2:

get_button_status.svg

EncoderUnit.set_color(index, rgb: int) None

Set the color of the LED

参数:
  • index (int) – the index of the LED, 1 or 2.

  • rgb (int) – the color of the LED, 0x000000 - 0xFFFFFF.

UIFLOW2:

set_color.svg

EncoderUnit.fill_color(rgb: int) None

Set the color of the LED

参数:

rgb (int) – the color of the LED, 0x000000 - 0xFFFFFF.

UIFLOW2:

fill_color.svg