Joystick Unit
The following products are supported:
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 JoystickUnit 10 11 12circle0 = None 13circle1 = None 14label0 = None 15label1 = None 16label2 = None 17label3 = None 18i2c0 = None 19joystick_0 = None 20 21 22x = None 23value = None 24in_min = None 25in_max = None 26out_min = None 27out_range = None 28y = None 29last_x = None 30last_y = None 31 32 33# Describe this function... 34def map_to_range(value, in_min, in_max, out_min, out_range): 35 global x, y, last_x, last_y, circle0, circle1, label0, label1, label2, label3, i2c0, joystick_0 36 return int((value - in_min) * out_range / (in_max - in_min) + out_min) 37 38 39def setup(): 40 global \ 41 circle0, \ 42 circle1, \ 43 label0, \ 44 label1, \ 45 label2, \ 46 label3, \ 47 i2c0, \ 48 joystick_0, \ 49 x, \ 50 out_range, \ 51 out_min, \ 52 y, \ 53 value, \ 54 in_min, \ 55 in_max, \ 56 last_x, \ 57 last_y 58 59 M5.begin() 60 circle0 = Widgets.Circle(67, 120, 50, 0xFFFFFF, 0x000000) 61 circle1 = Widgets.Circle(67, 120, 4, 0xFFFFFF, 0xFFFFFF) 62 label0 = Widgets.Label("X:", 6, 185, 1.0, 0x74F707, 0x222222, Widgets.FONTS.DejaVu18) 63 label1 = Widgets.Label("Y:", 6, 212, 1.0, 0x74F707, 0x222222, Widgets.FONTS.DejaVu18) 64 label2 = Widgets.Label("0", 25, 185, 1.0, 0x74F707, 0x222222, Widgets.FONTS.DejaVu18) 65 label3 = Widgets.Label("0", 21, 212, 1.0, 0x74F707, 0x222222, Widgets.FONTS.DejaVu18) 66 67 i2c0 = I2C(0, scl=Pin(33), sda=Pin(32), freq=100000) 68 joystick_0 = JoystickUnit(i2c0, 0x52) 69 joystick_0.swap_x(True) 70 last_x = 67 71 last_y = 120 72 73 74def loop(): 75 global \ 76 circle0, \ 77 circle1, \ 78 label0, \ 79 label1, \ 80 label2, \ 81 label3, \ 82 i2c0, \ 83 joystick_0, \ 84 x, \ 85 out_range, \ 86 out_min, \ 87 y, \ 88 value, \ 89 in_min, \ 90 in_max, \ 91 last_x, \ 92 last_y 93 M5.update() 94 x = last_x + map_to_range(joystick_0.get_x(), -128, 127, -43, 86) 95 y = last_y + map_to_range(joystick_0.get_y(), -128, 127, -43, 86) 96 circle1.setCursor(x=x, y=y) 97 if joystick_0.get_button_status(): 98 circle1.setColor(color=0xFF0000, fill_c=0xFF0000) 99 else: 100 circle1.setColor(color=0xFFFFFF, fill_c=0x6600CC) 101 label2.setText(str(joystick_0.get_x())) 102 label3.setText(str(joystick_0.get_y())) 103 104 105if __name__ == "__main__": 106 try: 107 setup() 108 while True: 109 loop() 110 except (Exception, KeyboardInterrupt) as e: 111 try: 112 from utility import print_error_msg 113 114 print_error_msg(e) 115 except ImportError: 116 print("please update to latest firmware")
UIFLOW2 Example:
joystick_stickcplus2_example.m5f2
class JoystickUnit
Constructors
Methods
- JoystickUnit.swap_x(swap: bool = True) None
Swap x-axis direction
- Parameters:
swap – True or False
UIFLOW2: