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:
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:
class Joystick2Unit
Constructors
Methods
- Joystick2Unit.set_axis_x_invert(invert)
Invert the X-axis of the joystick.
- Parameters:
invert (bool) – Whether to invert the X-axis.
UIFLOW2:
- Joystick2Unit.set_axis_y_invert(invert)
Invert the Y-axis of the joystick.
- Parameters:
invert (bool) – Whether to invert the Y-axis.
UIFLOW2:
- 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:
- 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:
- 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:
- Joystick2Unit.set_led_brightness(brightness)
Set the brightness of the RGB LED.
- Parameters:
brightness (float) – The brightness value (0-100).
UIFLOW2:
- Joystick2Unit.fill_color(v)
Set the RGB LED color of the joystick.
- Parameters:
v – The RGB value (0x000000-0xFFFFFF).
UIFLOW2:
- Joystick2Unit.fill_color_rgb(r, g, b)
Set the RGB LED color of the joystick.
- 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:
UIFLOW2:
- 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:
UIFLOW2:
- Joystick2Unit.set_deadzone_adc(x_adc_raw, y_adc_raw)
Set the dead zone of the joystick.
- Parameters:
UIFLOW2:
- Joystick2Unit.set_deadzone_position(x_pos, y_pos)
Set the dead zone of the joystick.
- Parameters:
UIFLOW2:
- 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:
- 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:
- Joystick2Unit.get_x_raw()
Read the raw X-axis value of the joystick.
- Return (int):
Returns the raw X-axis value.
UIFLOW2:
- Joystick2Unit.get_y_raw()
Read the raw Y-axis value of the joystick.
- Return (int):
Returns the raw Y-axis value.
UIFLOW2:
- Joystick2Unit.get_x_position()
Read the X-axis position of the joystick.
- Return (int):
Returns the X-axis position.
UIFLOW2:
- Joystick2Unit.get_y_position()
Read the Y-axis position of the joystick.
- Return (int):
Returns the Y-axis position.
UIFLOW2: