|
5 | 5 | importmachine |
6 | 6 | importcst816s |
7 | 7 | importi2c |
| 8 | +importmath |
8 | 9 |
|
9 | 10 | importlvglaslv |
10 | 11 | importtask_handler |
@@ -117,6 +118,34 @@ def read_joystick(): |
117 | 118 | returnmapping['key'] |
118 | 119 | returnNone# No key triggered |
119 | 120 |
|
| 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 |
120 | 149 |
|
121 | 150 | # Key repeat configuration |
122 | 151 | # 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): |
155 | 184 | current_key=lv.KEY.END |
156 | 185 | else: |
157 | 186 | # 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 |
167 | 199 |
|
168 | 200 | # Key repeat logic |
169 | 201 | ifcurrent_key: |
|