Encoder8 Unit

UNIT 8Encoder is a set of 8 rotary encoders as one of the input unit, the internal use of STM32 single-chip microcomputer as the acquisition and communication processor, and the host computer using I2C communication interface, each rotary encoder corresponds to 1 RGB LED light, encoder in addition to left and right rotation, but also radially pressed, in addition to a physical toggle switch and its corresponding RGB LED light, including 5V->3V3 DCDC circuit.

Support the following products:

Encoder8Unit

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 Encoder8Unit
10
11
12label0 = None
13title0 = None
14label1 = None
15label2 = None
16i2c0 = None
17encoder8_0 = None
18
19
20def setup():
21    global label0, title0, label1, label2, i2c0, encoder8_0
22
23    M5.begin()
24    Widgets.fillScreen(0x222222)
25    label0 = Widgets.Label("label0", 2, 72, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
26    title0 = Widgets.Title(
27        "8EncoderUnit CoreS3 Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18
28    )
29    label1 = Widgets.Label("label1", 2, 116, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
30    label2 = Widgets.Label("label2", 2, 161, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
31
32    i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
33    encoder8_0 = Encoder8Unit(i2c0, 0x41)
34    encoder8_0.set_led_rgb_from(1, 8, 0x33FF33)
35    encoder8_0.set_counter_value(1, 0)
36
37
38def loop():
39    global label0, title0, label1, label2, i2c0, encoder8_0
40    M5.update()
41    label0.setText(str((str("CH1 Counter Value:") + str((encoder8_0.get_counter_value(1))))))
42    label1.setText(str((str("CH1 Button State:") + str((encoder8_0.get_button_status(1))))))
43    label2.setText(str((str("Switch State:") + str((encoder8_0.get_switch_status())))))
44
45
46if __name__ == "__main__":
47    try:
48        setup()
49        while True:
50            loop()
51    except (Exception, KeyboardInterrupt) as e:
52        try:
53            from utility import print_error_msg
54
55            print_error_msg(e)
56        except ImportError:
57            print("please update to latest firmware")

UIFLOW2 Example:

example.png

encoder8_cores3_example.m5f2

class Encoder8Unit

Constructors

class Encoder8Unit(i2c, slave_addr, address)

Initialize the Encoder8 Unit with the specified I2C interface and address.

Parameters:
  • i2c – The I2C interface or PAHUBUnit instance for communication.

  • slave_addr (int) – Deprecated parameter, kept for backward compatibility.

  • address (int) – The I2C address of the Encoder8 Unit. Default is 0x59.

UIFLOW2:

init.png

Methods

Encoder8Unit.init_i2c_address(slave_addr)

Set or change the I2C address of the Encoder8 Unit.

Parameters:

slave_addr (int) – The new I2C address to set.

Encoder8Unit.available()

Check if the Encoder8 Unit is connected on the I2C bus.

Encoder8Unit.get_counter_value(channel)

Get the current counter value of the specified channel.

Parameters:

channel (int) – The encoder channel (1-8). Default is 1.

Returns:

The current counter value as an integer.

UIFLOW2:

get_counter_value.png

Encoder8Unit.set_counter_value(channel, value)

Set the counter value for the specified channel.

Parameters:
  • channel (int) – The encoder channel (1-8). Default is 1.

  • value (int) – The counter value to set.

UIFLOW2:

set_counter_value.png

Encoder8Unit.get_increment_value(channel)

Get the incremental value of the specified channel.

Parameters:

channel (int) – The encoder channel (1-8). Default is 1.

Returns:

The incremental value as an integer.

UIFLOW2:

get_increment_value.png

Encoder8Unit.reset_counter_value(channel)

Reset the counter value for the specified channel.

Parameters:

channel (int) – The encoder channel (1-8). Default is 1.

UIFLOW2:

reset_counter_value.png

Encoder8Unit.get_button_status(channel)

Get the button status for the specified channel.

Parameters:

channel (int) – The encoder channel (1-8). Default is 1.

Returns:

True if the button is pressed, False otherwise.

UIFLOW2:

get_button_status.png

Encoder8Unit.get_switch_status()

Get the status of the global switch.

Returns:

True if the switch is on, False otherwise.

UIFLOW2:

get_switch_status.png

Encoder8Unit.set_led_rgb(channel, rgb)

Set the RGB color of the specified channel's LED.

Parameters:
  • channel (int) – The encoder channel (1-8). Default is 1.

  • rgb (int) – The RGB color value (0-0xFFFFFF). Default is 0.

UIFLOW2:

set_led_rgb.png

Encoder8Unit.set_led_rgb_from(begin, end, rgb)

Set the RGB color for a range of channels' LEDs.

Parameters:
  • begin (int) – The starting channel index. Default is 0.

  • end (int) – The ending channel index. Default is 0.

  • rgb (int) – The RGB color value (0-0xFFFFFF). Default is 0.

UIFLOW2:

set_led_rgb_from.png

Encoder8Unit.get_device_status(mode)

Get the device firmware version or I2C address.

Parameters:

mode (int) – The mode to read. 0xFE for firmware version, 0xFF for I2C address. Default is 0xFE.

Returns:

The value read from the specified mode register.

UIFLOW2:

get_device_status.png

Encoder8Unit.set_i2c_address(addr)

Set a new I2C address for the device.

Parameters:

addr (int) – The new I2C address. Default is 0x41.

UIFLOW2:

set_i2c_address.png

Encoder8Unit.read_reg_data(reg, num)

Read data from a specified register.

Parameters:
  • reg (int) – The register address to read from.

  • num (int) – The number of bytes to read.

Encoder8Unit.write_reg_data(reg, byte_lst)

Write data to a specified register.

Parameters:
  • reg – The register address to write to.

  • byte_lst – A list of bytes to write to the register.

Encoder8Unit.deinit()

Deinitialize the Encoder8 Unit instance.