Joystick2 Unit

The joystick is an input unit for control, utilizing an I2C communication interface and supporting three-axis control signals (X/Y-axis analog input for displacement and Z-axis digital input for key presses). It is ideal for applications like gaming and robot control.

Support the following products:

Joystick2Unit

Micropython Example:

import os, sys, io
import M5
from M5 import *
from unit import Joystick2Unit
from hardware import *
i2c = I2C(1, scl=22, sda=21)
joystick = Joystick2Unit(i2c)
joystick.read_adc_value()
joystick.read_button_status()
joystick.set_rgb_led(255, 0, 0)
joystick.get_rgb_led()
joystick.set_deadzone_position(200, 200)
while True:
    joystick.read_axis_position()

UIFLOW2 Example:

example.png

cores3_joystick_example.m5f2

class Joystick2Unit

Constructors

class Joystick2Unit(i2c, address)

Initialize the Joystick Unit.

Parameters:
  • i2c (I2C) – I2C port to use.

  • address (int) – I2C address of the Joystick Unit.

UIFLOW2:

init.png

Methods

Joystick2Unit.set_axis_x_invert(invert)

Invert the X-axis of the joystick.

Parameters:

invert (bool) – Whether to invert the X-axis.

UIFLOW2:

set_axis_x_invert.png

Joystick2Unit.set_axis_y_invert(invert)

Invert the Y-axis of the joystick.

Parameters:

invert (bool) – Whether to invert the Y-axis.

UIFLOW2:

set_axis_y_invert.png

Joystick2Unit.set_axis_swap(swap)

Swap the X-axis and Y-axis of the joystick.

Parameters:

swap (bool) – Whether to swap the X-axis and Y-axis.

UIFLOW2:

set_axis_swap.png

Joystick2Unit.get_adc_value()

Read the ADC value of the joystick.

Return (tuple):

Returns a tuple of the X-axis and Y-axis ADC values, from 0 to 65535

UIFLOW2:

get_adc_value.png

Joystick2Unit.get_button_status()

Read the button status of the joystick.

Return (bool):

Returns the button status. True if pressed, False if not pressed.

UIFLOW2:

get_button_status.png

Joystick2Unit.set_led_brightness(brightness)

Set the brightness of the RGB LED.

Parameters:

brightness (float) – The brightness value (0-100).

UIFLOW2:

set_led_brightness.png

Joystick2Unit.fill_color(v)

Set the RGB LED color of the joystick.

Parameters:

v – The RGB value (0x000000-0xFFFFFF).

UIFLOW2:

fill_color.png

Joystick2Unit.fill_color_rgb(r, g, b)

Set the RGB LED color of the joystick.

Parameters:
  • r (int) – The red value (0-255).

  • g (int) – The green value (0-255).

  • b (int) – The blue value (0-255).

Joystick2Unit.set_axis_x_mapping(adc_neg_min, adc_neg_max, adc_pos_min, adc_pos_max)
Set the mapping parameters of the X-axis.

ADC Raw     0                                                    65536
            |------------------------------------------------------|
Mapped    -4096                   0           0                   4096
            |---------------------|-dead zone-|--------------------|
      adc_neg_min        adc_neg_max        adc_pos_min         adc_pos_max
Parameters:
  • adc_neg_min (int) – The minimum ADC value of the negative range.

  • adc_neg_max (int) – The maximum ADC value of the negative range.

  • adc_pos_min (int) – The minimum ADC value of the positive range.

  • adc_pos_max (int) – The maximum ADC value of the positive range.

UIFLOW2:

set_axis_x_mapping.png

Joystick2Unit.set_axis_y_mapping(adc_neg_min, adc_neg_max, adc_pos_min, adc_pos_max)
Set the mapping parameters of the Y-axis.

ADC Raw     0                                                    65536
            |------------------------------------------------------|
Mapped    -4096                   0           0                   4096
            |---------------------|-dead zone-|--------------------|
      adc_neg_min        adc_neg_max        adc_pos_min         adc_pos_max
Parameters:
  • adc_neg_min (int) – The minimum ADC value of the negative range.

  • adc_neg_max (int) – The maximum ADC value of the negative range.

  • adc_pos_min (int) – The minimum ADC value of the positive range.

  • adc_pos_max (int) – The maximum ADC value of the positive range.

UIFLOW2:

set_axis_y_mapping.png

Joystick2Unit.set_deadzone_adc(x_adc_raw, y_adc_raw)

Set the dead zone of the joystick.

Parameters:
  • x_adc_raw (int) – The dead zone of the X-axis. Range is 0 to 32768.

  • y_adc_raw (int) – The dead zone of the Y-axis. Range is 0 to 32768.

UIFLOW2:

set_deadzone_adc.png

Joystick2Unit.set_deadzone_position(x_pos, y_pos)

Set the dead zone of the joystick.

Parameters:
  • x_pos (int) – The dead zone of the X-axis. Range is 0 to 4096.

  • y_pos (int) – The dead zone of the Y-axis. Range is 0 to 4096.

UIFLOW2:

set_deadzone_position.png

Joystick2Unit.get_axis_position()

Read the position of the joystick.

Return (tuple):

Returns a tuple of the X-axis and Y-axis positions. The range is -4096 to 4096.

UIFLOW2:

get_axis_position.png

Joystick2Unit.set_address(address)

Set the I2C address of the Joystick Unit.

Parameters:

address (int) – The I2C address to set.

Joystick2Unit.get_firmware_version()

Read the firmware version of the Joystick Unit.

Return (int):

Returns the firmware version.

UIFLOW2:

get_firmware_version.png

Joystick2Unit.get_x_raw()

Read the raw X-axis value of the joystick.

Return (int):

Returns the raw X-axis value.

UIFLOW2:

get_x_raw.png

Joystick2Unit.get_y_raw()

Read the raw Y-axis value of the joystick.

Return (int):

Returns the raw Y-axis value.

UIFLOW2:

get_y_raw.png

Joystick2Unit.get_x_position()

Read the X-axis position of the joystick.

Return (int):

Returns the X-axis position.

UIFLOW2:

get_x_position.png

Joystick2Unit.get_y_position()

Read the Y-axis position of the joystick.

Return (int):

Returns the Y-axis position.

UIFLOW2:

get_y_position.png