Skip to content

Commit 6db46a1

Browse files
icyqwqlbuque
authored andcommitted
libs/unit: Support JoystickV2.
Signed-off-by: icyqwq <[email protected]>
1 parent f4d8d0b commit 6db46a1

File tree

6 files changed

+594
-0
lines changed

6 files changed

+594
-0
lines changed

docs/en/refs/unit.joystickv2.ref

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
.. |JoystickV2Unit| image::
3+
:target:
4+
:height: 200px
5+
:width: 200px
6+
7+
.. |init.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2unit/init.svg
8+
.. |set_axis_x_invert.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2unit/set_axis_x_invert.svg
9+
.. |set_axis_y_invert.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2unit/set_axis_y_invert.svg
10+
.. |set_axis_swap.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2unit/set_axis_swap.svg
11+
.. |read_adc_value.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2unit/read_adc_value.svg
12+
.. |read_button_status.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2unit/read_button_status.svg
13+
.. |set_rgb_led.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2unit/set_rgb_led.svg
14+
.. |get_rgb_led.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2unit/get_rgb_led.svg
15+
.. |set_axis_x_mapping.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2unit/set_axis_x_mapping.svg
16+
.. |set_axis_y_mapping.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2unit/set_axis_y_mapping.svg
17+
.. |set_deadzone_adc.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2unit/set_deadzone_adc.svg
18+
.. |set_deadzone_position.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2unit/set_deadzone_position.svg
19+
.. |read_axis_position.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2unit/read_axis_position.svg
20+
.. |set_address.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2unit/set_address.svg
21+
.. |read_fw_version.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2unit/read_fw_version.svg
22+
23+
.. |example.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2unit/example.svg
24+

docs/en/units/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,4 @@ Unit
7171
watering.rst
7272
weight_i2c.rst
7373
zigbee.rst
74+
joystickv2.rst

docs/en/units/joystickv2.rst

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
2+
JoystickV2Unit
3+
==============
4+
5+
.. include:: ../refs/unit.joystickv2unit.ref
6+
7+
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.
8+
9+
Support the following products:
10+
11+
|JoystickV2Unit|
12+
13+
Micropython Example::
14+
15+
import os, sys, io
16+
import M5
17+
from M5 import *
18+
from unit import JoystickV2Unit
19+
from hardware import *
20+
i2c = I2C(1, scl=22, sda=21)
21+
joystick = JoystickV2Unit(i2c)
22+
joystick.read_adc_value()
23+
joystick.read_button_status()
24+
joystick.set_rgb_led(255, 0, 0)
25+
joystick.get_rgb_led()
26+
joystick.set_deadzone_position(200, 200)
27+
while True:
28+
joystick.read_axis_position()
29+
30+
UIFLOW2 Example:
31+
32+
|example.svg|
33+
34+
.. only:: builder_html
35+
36+
class JoystickV2Unit
37+
--------------------
38+
39+
Constructors
40+
------------
41+
42+
.. class:: JoystickV2Unit(i2c, address)
43+
44+
Initialize the JoystickV2 Unit.
45+
46+
:param I2C i2c: I2C port to use.
47+
:param int address: I2C address of the JoystickV2 Unit.
48+
49+
UIFLOW2:
50+
51+
|init.svg|
52+
53+
54+
Methods
55+
-------
56+
57+
.. method:: JoystickV2Unit.set_axis_x_invert(invert)
58+
59+
Invert the X-axis of the joystick.
60+
61+
:param bool invert: Whether to invert the X-axis.
62+
63+
UIFLOW2:
64+
65+
|set_axis_x_invert.svg|
66+
67+
.. method:: JoystickV2Unit.set_axis_y_invert(invert)
68+
69+
Invert the Y-axis of the joystick.
70+
71+
:param bool invert: Whether to invert the Y-axis.
72+
73+
UIFLOW2:
74+
75+
|set_axis_y_invert.svg|
76+
77+
.. method:: JoystickV2Unit.set_axis_swap(swap)
78+
79+
Swap the X-axis and Y-axis of the joystick.
80+
81+
:param bool swap: Whether to swap the X-axis and Y-axis.
82+
83+
UIFLOW2:
84+
85+
|set_axis_swap.svg|
86+
87+
.. method:: JoystickV2Unit.read_adc_value()
88+
89+
Read the ADC value of the joystick.
90+
91+
92+
UIFLOW2:
93+
94+
|read_adc_value.svg|
95+
96+
.. method:: JoystickV2Unit.read_button_status()
97+
98+
Read the button status of the joystick.
99+
100+
101+
UIFLOW2:
102+
103+
|read_button_status.svg|
104+
105+
.. method:: JoystickV2Unit.set_rgb_led(r, g, b)
106+
107+
Set the RGB LED color of the joystick.
108+
109+
:param int r: The red value (0-255).
110+
:param int g: The green value (0-255).
111+
:param int b: The blue value (0-255).
112+
113+
UIFLOW2:
114+
115+
|set_rgb_led.svg|
116+
117+
.. method:: JoystickV2Unit.get_rgb_led()
118+
119+
Get the RGB LED color of the joystick.
120+
121+
122+
UIFLOW2:
123+
124+
|get_rgb_led.svg|
125+
126+
.. method:: JoystickV2Unit.set_axis_x_mapping(adc_neg_min, adc_neg_max, adc_pos_min, adc_pos_max)
127+
128+
Set the mapping parameters of the X-axis.
129+
130+
ADC Raw 0 65536
131+
|------------------------------------------------------|
132+
Mapped -4096 0 0 4096
133+
|---------------------|-dead zone-|--------------------|
134+
adc_neg_min adc_neg_max adc_pos_min adc_pos_max
135+
136+
137+
:param int adc_neg_min: The minimum ADC value of the negative range.
138+
:param int adc_neg_max: The maximum ADC value of the negative range.
139+
:param int adc_pos_min: The minimum ADC value of the positive range.
140+
:param int adc_pos_max: The maximum ADC value of the positive range.
141+
142+
UIFLOW2:
143+
144+
|set_axis_x_mapping.svg|
145+
146+
.. method:: JoystickV2Unit.set_axis_y_mapping(adc_neg_min, adc_neg_max, adc_pos_min, adc_pos_max)
147+
148+
Set the mapping parameters of the Y-axis.
149+
150+
ADC Raw 0 65536
151+
|------------------------------------------------------|
152+
Mapped -4096 0 0 4096
153+
|---------------------|-dead zone-|--------------------|
154+
adc_neg_min adc_neg_max adc_pos_min adc_pos_max
155+
156+
157+
:param int adc_neg_min: The minimum ADC value of the negative range.
158+
:param int adc_neg_max: The maximum ADC value of the negative range.
159+
:param int adc_pos_min: The minimum ADC value of the positive range.
160+
:param int adc_pos_max: The maximum ADC value of the positive range.
161+
162+
UIFLOW2:
163+
164+
|set_axis_y_mapping.svg|
165+
166+
.. method:: JoystickV2Unit.set_deadzone_adc(x_adc_raw, y_adc_raw)
167+
168+
Set the dead zone of the joystick.
169+
170+
:param int x_adc_raw: The dead zone of the X-axis. Range is 0 to 32768.
171+
:param int y_adc_raw: The dead zone of the Y-axis. Range is 0 to 32768.
172+
173+
UIFLOW2:
174+
175+
|set_deadzone_adc.svg|
176+
177+
.. method:: JoystickV2Unit.set_deadzone_position(x_pos, y_pos)
178+
179+
Set the dead zone of the joystick.
180+
181+
:param int x_pos: The dead zone of the X-axis. Range is 0 to 4096.
182+
:param int y_pos: The dead zone of the Y-axis. Range is 0 to 4096.
183+
184+
UIFLOW2:
185+
186+
|set_deadzone_position.svg|
187+
188+
.. method:: JoystickV2Unit.read_axis_position()
189+
190+
Read the position of the joystick.
191+
192+
193+
UIFLOW2:
194+
195+
|read_axis_position.svg|
196+
197+
.. method:: JoystickV2Unit.set_address(address)
198+
199+
Set the I2C address of the JoystickV2 Unit.
200+
201+
:param int address: The I2C address to set.
202+
203+
UIFLOW2:
204+
205+
|set_address.svg|
206+
207+
.. method:: JoystickV2Unit.read_fw_version()
208+
209+
Read the firmware version of the JoystickV2 Unit.
210+
211+
212+
UIFLOW2:
213+
214+
|read_fw_version.svg|
215+
216+
217+

m5stack/libs/unit/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"IMUUnit": "imu",
5858
"IRUnit": "ir",
5959
"JoystickUnit": "joystick",
60+
"JoystickV2Unit": "joystickv2",
6061
"KeyUnit": "key",
6162
"KMeterISOUnit": "kmeter_iso",
6263
"KMeterUnit": "kmeter",

0 commit comments

Comments
 (0)