MiniEncoderC Hat

The following products are supported:

Encoder

Micropython Example:

  1import os, sys, io
  2import M5
  3from M5 import *
  4from hardware import *
  5from hat import MiniEncoderCHat
  6
  7
  8title0 = None
  9circle0 = None
 10circle1 = None
 11circle2 = None
 12label0 = None
 13i2c0 = None
 14i2c1 = None
 15hat_miniencoderc_0 = None
 16
 17
 18import math
 19
 20angle = None
 21x = None
 22y = None
 23direction = None
 24big_radius = None
 25small_radius = None
 26
 27
 28# Describe this function...
 29def move_small_circle():
 30    global \
 31        angle, \
 32        x, \
 33        y, \
 34        direction, \
 35        big_radius, \
 36        small_radius, \
 37        title0, \
 38        circle0, \
 39        circle1, \
 40        circle2, \
 41        label0, \
 42        i2c0, \
 43        i2c1, \
 44        hat_miniencoderc_0
 45    angle = ((hat_miniencoderc_0.get_rotary_value()) / 58) * 360
 46    x = big_radius * math.cos(angle / 180.0 * math.pi)
 47    y = big_radius * math.sin(angle / 180.0 * math.pi)
 48    circle0.setCursor(x=67, y=120)
 49    circle2.setCursor(x=(67 + (int(x))), y=(120 + (int(y))))
 50
 51
 52def setup():
 53    global \
 54        title0, \
 55        circle0, \
 56        circle1, \
 57        circle2, \
 58        label0, \
 59        i2c0, \
 60        i2c1, \
 61        hat_miniencoderc_0, \
 62        angle, \
 63        x, \
 64        y, \
 65        direction, \
 66        big_radius, \
 67        small_radius
 68
 69    M5.begin()
 70    title0 = Widgets.Title("Mini Encoder", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18)
 71    circle0 = Widgets.Circle(67, 120, 50, 0xFFFFFF, 0x000000)
 72    circle1 = Widgets.Circle(67, 120, 10, 0x00FF00, 0x00FF00)
 73    circle2 = Widgets.Circle(117, 120, 6, 0xFFFFFF, 0xFFFFFF)
 74    label0 = Widgets.Label("Count: 0", 4, 213, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
 75
 76    i2c0 = I2C(0, scl=Pin(33), sda=Pin(32), freq=100000)
 77    i2c1 = I2C(1, scl=Pin(26), sda=Pin(0), freq=100000)
 78    hat_miniencoderc_0 = MiniEncoderCHat(i2c0, 0x42)
 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        i2c1, \
 92        hat_miniencoderc_0, \
 93        angle, \
 94        x, \
 95        y, \
 96        direction, \
 97        big_radius, \
 98        small_radius
 99    M5.update()
100    if hat_miniencoderc_0.get_rotary_status():
101        direction = hat_miniencoderc_0.get_rotary_increments()
102        move_small_circle()
103        if direction < 0:
104            hat_miniencoderc_0.fill_color(0xFF0000)
105            label0.setText(str((str("Count: ") + str((hat_miniencoderc_0.get_rotary_value())))))
106        elif direction > 0:
107            hat_miniencoderc_0.fill_color(0x33FF33)
108            label0.setText(str((str("Count: ") + str((hat_miniencoderc_0.get_rotary_value())))))
109    else:
110        hat_miniencoderc_0.fill_color(0x000000)
111    if hat_miniencoderc_0.get_button_status():
112        circle1.setColor(color=0xFF0000, fill_c=0xFF0000)
113        move_small_circle()
114        hat_miniencoderc_0.reset_rotary_value()
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

class MiniEncoderCHat

Constructors

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

Creates a Rotary object.

Parameters:
  • i2c – I2C object.

  • address – I2C address, Default is 0x40.

UIFLOW2:

init.svg

Methods

MiniEncoderCHat.get_rotary_status() bool

Gets the rotation status of the Rotary object.

UIFLOW2:

get_rotary_status.svg

MiniEncoderCHat.get_rotary_value() int

Gets the rotation value of the Rotary object.

UIFLOW2:

get_rotary_value.svg

MiniEncoderCHat.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

MiniEncoderCHat.reset_rotary_value() None

Resets the rotation value of the Rotary object.

UIFLOW2:

reset_rotary_value.svg

MiniEncoderCHat.get_button_status() bool

Get the current status of the rotary encoder keys.

UIFLOW2:

get_button_status.svg

MiniEncoderCHat.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

MiniEncoderCHat.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

MiniEncoderCHat.read_fw_version() str

Get the firmware version of the device.

UIFLOW2:

read_fw_version.svg

MiniEncoderCHat.set_address(address) None

Set the I2C address of the device.

UIFLOW2:

set_address.svg