Encoder Unit

The following products are supported:

Encoder

Micropython Example:

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

UIFLOW2 Example:

example.svg

class EncoderUnit

Constructors

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

Creates a Rotary object.

Parameters:
  • 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.

Parameters:

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

Parameters:
  • 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

Parameters:

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

UIFLOW2:

fill_color.svg