Skip to content

Commit fdb30b4

Browse files
committed
Updated to release 2.1.3 of XRPLib
1 parent efec19f commit fdb30b4

6 files changed

Lines changed: 48 additions & 23 deletions

File tree

lib/XRPExamples/gamepad_example.blocks

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ while not (gp.is_button_pressed(gp.BACK)):
1111

1212

1313

14-
## [2025-05-30 23:46:21]
15-
##XRPBLOCKS {"blocks":{"languageVersion":0,"blocks":[{"type":"controls_whileUntil","id":"#/DDnbE2JxVsLQo`C`e^","x":-363,"y":16,"fields":{"MODE":"WHILE"},"inputs":{"BOOL":{"block":{"type":"logic_negate","id":"+k{^!vL*n}Km]oQ8u,ye","inputs":{"BOOL":{"block":{"type":"xrp_gp_button_pressed","id":"DtLypJc;SU2xT4A~S3nV","fields":{"GPBUTTON":"BACK"}}}}}},"DO":{"block":{"type":"xrp_arcade","id":"(|]Iln#JYa9hzgEz+(4g","inputs":{"STRAIGHT":{"shadow":{"type":"math_number","id":"FOj1uvC$:~x/+cX}d(:|","fields":{"NUM":0.8}},"block":{"type":"xrp_gp_get_value","id":"s#GS_Dt)B8Cb9+4ctpwk","fields":{"GPVALUE":"Y1"}}},"TURN":{"shadow":{"type":"math_number","id":"n1jAKrjST!+3#bfChh$d","fields":{"NUM":0.2}},"block":{"type":"xrp_gp_get_value","id":"*]`U9XHyYnA?Gp%U;$NF","fields":{"GPVALUE":"X1"}}}}}}}}]}}
14+
## [2025-07-13 01:36:24]
15+
##XRPBLOCKS {"blocks":{"languageVersion":0,"blocks":[{"type":"controls_whileUntil","id":"#/DDnbE2JxVsLQo`C`e^","x":-363,"y":16,"fields":{"MODE":"UNTIL"},"inputs":{"BOOL":{"block":{"type":"xrp_gp_button_pressed","id":"DtLypJc;SU2xT4A~S3nV","fields":{"GPBUTTON":"BACK"}}},"DO":{"block":{"type":"xrp_arcade","id":"(|]Iln#JYa9hzgEz+(4g","inputs":{"STRAIGHT":{"shadow":{"type":"math_number","id":"FOj1uvC$:~x/+cX}d(:|","fields":{"NUM":0.8}},"block":{"type":"xrp_gp_get_value","id":"s#GS_Dt)B8Cb9+4ctpwk","fields":{"GPVALUE":"Y1"}}},"TURN":{"shadow":{"type":"math_number","id":"n1jAKrjST!+3#bfChh$d","fields":{"NUM":0.2}},"block":{"type":"xrp_gp_get_value","id":"*]`U9XHyYnA?Gp%U;$NF","fields":{"GPVALUE":"X1"}}}}}}}}]}}

lib/XRPLib/gamepad.py

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Gamepad:
2525
DPAD_L = const(16)
2626
DPAD_R = const(17)
2727

28-
joyData = [
28+
_joyData = [
2929
0.0,
3030
0.0,
3131
0.0,
@@ -35,7 +35,7 @@ class Gamepad:
3535
@classmethod
3636
def get_default_gamepad(cls):
3737
"""
38-
Get the default XRP bluetooth joystick instance. This is a singleton, so only one instance of the reflectance sensor will ever exist.
38+
Get the default XRP bluetooth joystick instance. This is a singleton, so only one instance of the gamepad sensor will ever exist.
3939
"""
4040
if cls._DEFAULT_GAMEPAD_INSTANCE is None:
4141
cls._DEFAULT_GAMEPAD_INSTANCE = cls()
@@ -44,30 +44,54 @@ def get_default_gamepad(cls):
4444

4545
def __init__(self):
4646
"""
47-
"""
47+
Manages communication with gamepad data coming from a remote computer via bluetooth
4848
49+
"""
4950
def start(self):
50-
for i in range(len(self.joyData)):
51-
self.joyData[i] = 0.0
51+
"""
52+
Signals the remote computer to begin sending gamepad data packets.
53+
"""
54+
for i in range(len(self._joyData)):
55+
self._joyData[i] = 0.0
5256
uart.set_data_callback(self._data_callback)
5357
sys.stdout.write(chr(27))
5458
sys.stdout.write(chr(101))
5559

5660

5761
def stop(self):
58-
uart.clear_data_callback()
62+
"""
63+
Signals the remote computer to stop sending gamepad data packets.
64+
"""
5965
sys.stdout.write(chr(27))
6066
sys.stdout.write(chr(102))
6167

62-
def get_value(self, index):
63-
return -self.joyData[index] #returning the negative to make normal for user
68+
def get_value(self, index:int) -> float:
69+
"""
70+
Get the current value of a joystick axis
71+
72+
:param index: The joystick axis index
73+
Gamepad.X1, Gamepad.Y1, Gamepad.X2, Gamepad.Y2
74+
:type int
75+
:returns: The value of the joystick between -1 and 1
76+
:rtype: float
77+
"""
78+
return -self._joyData[index] #returning the negative to make normal for user
6479

65-
def is_button_pressed(self, index):
66-
return self.joyData[index] > 0
80+
def is_button_pressed(self, index:int) -> bool:
81+
"""
82+
Checks if a specific button is currently pressed.
83+
84+
:param index: The button index
85+
Gamepad.BUTTON_A, Gamepad.TRIGGER_L, Gamepad.DPAD_UP, etc
86+
:type int
87+
:returns: The value of the button 1 or 0
88+
:rtype: bool
89+
"""
90+
return self._joyData[index] > 0
6791

6892
def _data_callback(self, data):
6993
if(data[0] == 0x55 and len(data) == data[1] + 2):
7094
for i in range(2, data[1] + 2, 2):
71-
self.joyData[data[i]] = round(data[i + 1]/127.5 - 1, 2)
95+
self._joyData[data[i]] = round(data[i + 1]/127.5 - 1, 2)
7296

7397

lib/XRPLib/imu.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from .imu_defs import *
88
from uctypes import struct, addressof
99
except (TypeError, ModuleNotFoundError):
10+
LSM_ADDR_PRIMARY = 0x6A
1011
# Import wrapped in a try/except so that autodoc generation can process properly
1112
pass
1213
from machine import I2C, Pin, Timer, disable_irq, enable_irq

lib/XRPLib/resetbot.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def reset_hard():
4444
reset_led()
4545
reset_servos()
4646
reset_webserver()
47-
47+
4848
if "XRPLib.gamepad" in sys.modules:
4949
reset_gamepad()
5050

@@ -59,3 +59,4 @@ def reset_hard():
5959

6060
if "XRPLib.webserver" in sys.modules:
6161
reset_webserver()
62+

lib/XRPLib/timeout.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import time
22

33
class Timeout:
4-
def __init__(self, timeout):
4+
def __init__(self, timeout: float):
55
"""
66
Starts a timer that will expire after the given timeout.
77
88
:param timeout: The timeout, in seconds
99
:type timeout: float
1010
"""
11-
self.timeout = timeout
12-
self.start_time = time.time()
11+
self.timeout = timeout*1000
12+
self.start_time = time.ticks_ms()
1313

1414
def is_done(self):
1515
"""
1616
:return: True if the timeout has expired, False otherwise
1717
"""
1818
if self.timeout is None:
1919
return False
20-
return time.time() - self.start_time > self.timeout
20+
return time.ticks_ms() - self.start_time > self.timeout

lib/package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@
2121
["XRPLib/webserver.py", "github:Open-STEM/XRP_Micropython/XRPLib/webserver.py"],
2222
["XRPExamples/__init__.py", "github:Open-STEM/XRP_Micropython/XRPExamples/__init__.py"],
2323
["XRPExamples/drive_examples.py", "github:Open-STEM/XRP_Micropython/XRPExamples/drive_examples.py"],
24+
["XRPExamples/gamepad_example.blocks", "github:Open-STEM/XRP_Micropython/XRPExamples/gamepad_example.blocks"],
2425
["XRPExamples/installation_verification.py", "github:Open-STEM/XRP_Micropython/XRPExamples/installation_verification.py"],
25-
["XRPExamples/led_example.py", "github:Open-STEM/XRP_Micropython/XRPExamples/misc_examples.py"],
26+
["XRPExamples/led_example.py", "github:Open-STEM/XRP_Micropython/XRPExamples/led_example.py"],
2627
["XRPExamples/sensor_examples.py", "github:Open-STEM/XRP_Micropython/XRPExamples/sensor_examples.py"],
27-
["XRPExamples/webserver_example.py", "github:Open-STEM/XRP_Micropython/XRPExamples/webserver_example.py"],
28-
["XRPExamples/gamepad_example.blocks", "github:Open-STEM/XRP_Micropython/XRPExamples/gamepad_example.blocks"]
29-
28+
["XRPExamples/webserver_example.py", "github:Open-STEM/XRP_Micropython/XRPExamples/webserver_example.py"]
3029
],
3130
"deps": [
3231
["github:pimoroni/phew", "latest"]
3332
],
34-
"version": "2.1.2"
33+
"version": "2.1.3"
3534
}

0 commit comments

Comments
 (0)