Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit781c710

Browse files
Fix joystick angle
1 parent63850c7 commit781c710

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

‎internal_filesystem/boot_fri3d-2024.py‎

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
importmachine
66
importcst816s
77
importi2c
8+
importmath
89

910
importlvglaslv
1011
importtask_handler
@@ -117,6 +118,34 @@ def read_joystick():
117118
returnmapping['key']
118119
returnNone# No key triggered
119120

121+
# Rotate: UP = 0°, RIGHT = 90°, DOWN = 180°, LEFT = 270°
122+
defread_joystick_angle(threshold=0.1):
123+
# Read ADC values
124+
val_up_down=adc_up_down.read()
125+
val_left_right=adc_left_right.read()
126+
127+
iftime.time()<60:
128+
print(f"val_up_down:{val_up_down}")
129+
print(f"val_left_right:{val_left_right}")
130+
131+
# Normalize to [-1, 1]
132+
x= (val_left_right-2048)/2048# Positive x = RIGHT
133+
y= (val_up_down-2048)/2048# Positive y = UP
134+
iftime.time()<60:
135+
print(f"x,y ={x},{y}")
136+
137+
# Check if joystick is near center
138+
magnitude=math.sqrt(x*x+y*y)
139+
iftime.time()<60:
140+
print(f"magnitude:{magnitude}")
141+
ifmagnitude<threshold:
142+
returnNone# Neutral position
143+
144+
# Calculate angle in degrees with UP = 0°, clockwise
145+
angle_rad=math.atan2(-x,y)
146+
angle_deg=math.degrees(angle_rad)
147+
angle_deg= (angle_deg+360)%360# Normalize to [0, 360)
148+
returnangle_deg
120149

121150
# Key repeat configuration
122151
# This whole debounce logic is only necessary because LVGL 9.2.2 seems to have an issue where
@@ -155,15 +184,18 @@ def keypad_read_cb(indev, data):
155184
current_key=lv.KEY.END
156185
else:
157186
# Check joystick
158-
joystick=read_joystick()
159-
ifjoystick=="LEFT":
160-
current_key=lv.KEY.LEFT
161-
elifjoystick=="RIGHT":
162-
current_key=lv.KEY.RIGHT
163-
elifjoystick=="UP":
164-
current_key=lv.KEY.UP
165-
elifjoystick=="DOWN":
166-
current_key=lv.KEY.DOWN
187+
joystick=read_joystick_angle(0.25)
188+
ifjoystick:
189+
iftime.time()<60:
190+
print(f"joystick angle:{joystick}")
191+
ifjoystick=="LEFT":
192+
current_key=lv.KEY.LEFT
193+
elifjoystick=="RIGHT":
194+
current_key=lv.KEY.RIGHT
195+
elifjoystick=="UP":
196+
current_key=lv.KEY.UP
197+
elifjoystick=="DOWN":
198+
current_key=lv.KEY.DOWN
167199

168200
# Key repeat logic
169201
ifcurrent_key:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp