Skip to content

Commit a73ac75

Browse files
icyqwqlbuque
authored andcommitted
libs/unit: Rename methods, add new api.
Signed-off-by: icyqwq <[email protected]>
1 parent 40df468 commit a73ac75

File tree

3 files changed

+130
-60
lines changed

3 files changed

+130
-60
lines changed

docs/en/refs/unit.joystickv2.ref

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@
44
:height: 200px
55
:width: 200px
66

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
7+
.. |init.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2/init.svg
8+
.. |set_axis_x_invert.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2/set_axis_x_invert.svg
9+
.. |set_axis_y_invert.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2/set_axis_y_invert.svg
10+
.. |set_axis_swap.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2/set_axis_swap.svg
11+
.. |get_adc_value.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2/get_adc_value.svg
12+
.. |get_button_status.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2/get_button_status.svg
13+
.. |fill_color.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2/fill_color.svg
14+
.. |set_axis_x_mapping.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2/set_axis_x_mapping.svg
15+
.. |set_axis_y_mapping.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2/set_axis_y_mapping.svg
16+
.. |set_deadzone_adc.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2/set_deadzone_adc.svg
17+
.. |set_deadzone_position.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2/set_deadzone_position.svg
18+
.. |get_axis_position.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2/get_axis_position.svg
19+
.. |set_address.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2/set_address.svg
20+
.. |get_firmware_version.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2/get_firmware_version.svg
21+
.. |get_x_raw.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2/get_x_raw.svg
22+
.. |get_y_raw.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2/get_y_raw.svg
23+
.. |get_x_position.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2/get_x_position.svg
24+
.. |get_y_position.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2/get_y_position.svg
2225

23-
.. |example.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2unit/example.svg
26+
.. |example.svg| image:: https://static-cdn.m5stack.com/mpy_docs/unit/joystickv2/example.svg
2427

docs/en/units/joystickv2.rst

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Methods
8787

8888
|set_axis_swap.svg|
8989

90-
.. method:: JoystickV2Unit.read_adc_value()
90+
.. method:: JoystickV2Unit.get_adc_value()
9191

9292
Read the ADC value of the joystick.
9393

@@ -96,9 +96,9 @@ Methods
9696

9797
UIFLOW2:
9898

99-
|read_adc_value.svg|
99+
|get_adc_value.svg|
100100

101-
.. method:: JoystickV2Unit.read_button_status()
101+
.. method:: JoystickV2Unit.get_button_status()
102102

103103
Read the button status of the joystick.
104104

@@ -107,9 +107,9 @@ Methods
107107

108108
UIFLOW2:
109109

110-
|read_button_status.svg|
110+
|get_button_status.svg|
111111

112-
.. method:: JoystickV2Unit.set_rgb_led(r, g, b)
112+
.. method:: JoystickV2Unit.fill_color(r, g, b)
113113

114114
Set the RGB LED color of the joystick.
115115

@@ -120,18 +120,7 @@ Methods
120120

121121
UIFLOW2:
122122

123-
|set_rgb_led.svg|
124-
125-
.. method:: JoystickV2Unit.get_rgb_led()
126-
127-
Get the RGB LED color of the joystick.
128-
129-
:return (tuple): Returns a tuple of the RGB LED color values.
130-
131-
132-
UIFLOW2:
133-
134-
|get_rgb_led.svg|
123+
|fill_color.svg|
135124

136125
.. method:: JoystickV2Unit.set_axis_x_mapping(adc_neg_min, adc_neg_max, adc_pos_min, adc_pos_max)
137126

@@ -203,7 +192,7 @@ Methods
203192

204193
|set_deadzone_position.svg|
205194

206-
.. method:: JoystickV2Unit.read_axis_position()
195+
.. method:: JoystickV2Unit.get_axis_position()
207196

208197
Read the position of the joystick.
209198

@@ -212,7 +201,7 @@ Methods
212201

213202
UIFLOW2:
214203

215-
|read_axis_position.svg|
204+
|get_axis_position.svg|
216205

217206
.. method:: JoystickV2Unit.set_address(address)
218207

@@ -225,7 +214,7 @@ Methods
225214

226215
|set_address.svg|
227216

228-
.. method:: JoystickV2Unit.read_fw_version()
217+
.. method:: JoystickV2Unit.get_firmware_version()
229218

230219
Read the firmware version of the JoystickV2 Unit.
231220

@@ -234,7 +223,51 @@ Methods
234223

235224
UIFLOW2:
236225

237-
|read_fw_version.svg|
226+
|get_firmware_version.svg|
227+
228+
.. method:: JoystickV2Unit.get_x_raw()
229+
230+
Read the raw X-axis value of the joystick.
231+
232+
:return (int): Returns the raw X-axis value.
233+
234+
235+
UIFLOW2:
236+
237+
|get_x_raw.svg|
238+
239+
.. method:: JoystickV2Unit.get_y_raw()
240+
241+
Read the raw Y-axis value of the joystick.
242+
243+
:return (int): Returns the raw Y-axis value.
244+
245+
246+
UIFLOW2:
247+
248+
|get_y_raw.svg|
249+
250+
.. method:: JoystickV2Unit.get_x_position()
251+
252+
Read the X-axis position of the joystick.
253+
254+
:return (int): Returns the X-axis position.
255+
256+
257+
UIFLOW2:
258+
259+
|get_x_position.svg|
260+
261+
.. method:: JoystickV2Unit.get_y_position()
262+
263+
Read the Y-axis position of the joystick.
264+
265+
:return (int): Returns the Y-axis position.
266+
267+
268+
UIFLOW2:
269+
270+
|get_y_position.svg|
238271

239272

240273

m5stack/libs/unit/joystickv2.py

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ def __init__(self, i2c: I2C, address: int | list | tuple = 0x63):
5353
self._swap = False
5454
self._x_mapping = [0, 0, 0, 0]
5555
self._y_mapping = [0, 0, 0, 0]
56-
self._available()
57-
58-
def _available(self) -> None:
5956
if self._addr not in self._i2c.scan():
6057
raise Exception("JoystickV2Unit not found, please check if it's properly connected.")
6158

@@ -117,7 +114,7 @@ def set_axis_swap(self, swap: bool = True) -> None:
117114
"""
118115
self._swap = swap
119116

120-
def read_adc_value(self) -> tuple:
117+
def get_adc_value(self) -> tuple:
121118
"""
122119
note: Read the ADC value of the joystick.
123120
@@ -138,7 +135,7 @@ def read_adc_value(self) -> tuple:
138135
x, y = y, x
139136
return (x, y)
140137

141-
def read_button_status(self) -> bool:
138+
def get_button_status(self) -> bool:
142139
"""
143140
note: Read the button status of the joystick.
144141
@@ -151,7 +148,7 @@ def read_button_status(self) -> bool:
151148
"""
152149
return not bool(self._read_reg_data(0x20, 1)[0])
153150

154-
def set_rgb_led(self, r: int, g: int, b: int) -> None:
151+
def fill_color(self, r: int, g: int, b: int) -> None:
155152
"""
156153
note: Set the RGB LED color of the joystick.
157154
@@ -169,21 +166,6 @@ def set_rgb_led(self, r: int, g: int, b: int) -> None:
169166
"""
170167
self._write_reg_data(0x30, [b, g, r])
171168

172-
def get_rgb_led(self) -> tuple:
173-
"""
174-
note: Get the RGB LED color of the joystick.
175-
176-
label:
177-
en: "%1 get RGB LED color of JoystickV2 Unit"
178-
cn: "%1 获取 JoystickV2 Unit 的 RGB LED 颜色"
179-
180-
return:
181-
note: Returns a tuple of the RGB LED color values.
182-
"""
183-
buf = self._read_reg_data(0x30, 3)
184-
(b, g, r) = struct.unpack("<BBB", buf)
185-
return (r, g, b)
186-
187169
def _set_mapping(
188170
self, adc_neg_min: int, adc_neg_max: int, adc_pos_min: int, adc_pos_max: int, axis_x=True
189171
):
@@ -300,7 +282,7 @@ def set_deadzone_position(self, x_pos: int, y_pos: int) -> None:
300282
time.sleep_ms(100)
301283
self._set_mapping(*self._y_mapping, False)
302284

303-
def read_axis_position(self) -> tuple:
285+
def get_axis_position(self) -> tuple:
304286
"""
305287
note: Read the position of the joystick.
306288
@@ -336,7 +318,7 @@ def set_address(self, address: int) -> None:
336318
self._write_reg_data(0xFF, [address])
337319
self._addr = address
338320

339-
def read_fw_version(self) -> int:
321+
def get_firmware_version(self) -> int:
340322
"""
341323
note: Read the firmware version of the JoystickV2 Unit.
342324
@@ -348,3 +330,55 @@ def read_fw_version(self) -> int:
348330
note: Returns the firmware version.
349331
"""
350332
return self._read_reg_data(0xFE, 1)[0]
333+
334+
def get_x_raw(self) -> int:
335+
"""
336+
note: Read the raw X-axis value of the joystick.
337+
338+
label:
339+
en: "%1 read raw X-axis ADC value"
340+
cn: "%1 读取原始X轴ADC值"
341+
342+
return:
343+
note: Returns the raw X-axis value.
344+
"""
345+
return self.get_adc_value()[0]
346+
347+
def get_y_raw(self) -> int:
348+
"""
349+
note: Read the raw Y-axis value of the joystick.
350+
351+
label:
352+
en: "%1 read raw Y-axis ADC value"
353+
cn: "%1 读取原始Y轴ADC值"
354+
355+
return:
356+
note: Returns the raw Y-axis value.
357+
"""
358+
return self.get_adc_value()[1]
359+
360+
def get_x_position(self) -> int:
361+
"""
362+
note: Read the X-axis position of the joystick.
363+
364+
label:
365+
en: "%1 read X-axis position"
366+
cn: "%1 读取X轴位置"
367+
368+
return:
369+
note: Returns the X-axis position.
370+
"""
371+
return self.get_axis_position()[0]
372+
373+
def get_y_position(self) -> int:
374+
"""
375+
note: Read the Y-axis position of the joystick.
376+
377+
label:
378+
en: "%1 read Y-axis position"
379+
cn: "%1 读取Y轴位置"
380+
381+
return:
382+
note: Returns the Y-axis position.
383+
"""
384+
return self.get_axis_position()[1]

0 commit comments

Comments
 (0)