-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
239 lines (182 loc) · 6.82 KB
/
Copy pathmain.py
File metadata and controls
239 lines (182 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
'''
Gesture Control System for Mouse Scrolling and Media Volume
- Uses ultrasonic distance sensor for gesture recognition
- Uses IR button for mode switching and tap actions
'''
import serial
import pyautogui
import time
from collections import deque
pyautogui.PAUSE = 0.0 # zero delay for instant responsiveness
PORT = 'COM12' # change to your Arduino's serial port
BAUD_RATE = 115200
COOLDOWN = 0.05
TAP_THRESHOLD = 0.4
HOLD_THRESHOLD = 1.5
SCROLL_STRENGTH = 25 #
VOLUME_STEPS = 2
MID_ZONE = 14.0
DEADZONE = 3.0 # deadzone for stable scrolling/volume control
MAX_DIST = 45.0 # maximum distance to consider for gestures
mode = "mouse"
ir_pressed_time = None
mode_switched = False
last_action_time = 0
local_volume = 50
raw_history = deque(maxlen=5)
# LCD state tracking to minimize lcd spam and ensure smooth updates
last_lcd_message = ""
last_lcd_update = 0
event_expire_time = 0
persistent_line2 = "Ready..."
LCD_COOLDOWN = 0.4
EVENT_DISPLAY_TIME = 1.5
# Initialization
print(f"Initializing Gesture Control System [{mode.upper()} MODE]")
try:
arduino = serial.Serial(PORT, BAUD_RATE, timeout=1)
time.sleep(2)
print("✅ Hardware Serial Connection Established!")
except Exception as e:
print(f"❌ Serial Connection Failed: {e}")
exit()
# LCD COMMUNICATION
def send_lcd(line1, line2, force=False):
global last_lcd_message
global last_lcd_update
now = time.time()
message = f"{line1}|{line2}"
# Prevent unnecessary LCD spam
if not force:
# Skip duplicate updates
if message == last_lcd_message:
return
# Enforce cooldown between updates to prevent flickering
if now - last_lcd_update < LCD_COOLDOWN:
return
try:
arduino.write((message + "\n").encode())
last_lcd_message = message
last_lcd_update = now
except:
pass
# Event display helper
def show_event(line2):
global event_expire_time
send_lcd(
f"{mode.upper()} MODE",
line2
)
event_expire_time = time.time() + EVENT_DISPLAY_TIME
# basic median filter for distance readings to smooth out noise
def process_filtered_distance(val):
if val <= 1.5 or val >= 200.0:
return 999.0
raw_history.append(val)
sorted_history = sorted(list(raw_history))
return sorted_history[len(sorted_history) // 2] # return median value
# Initial boot screen
send_lcd(
"GESTURE CTRL",
"System Ready",
force=True
)
time.sleep(2)
send_lcd(
f"{mode.upper()} MODE",
persistent_line2,
force=True
)
# ----------------------- MAIN LOOP ---------------------------
while True:
try:
# Restore default LCD line if no event is active
if time.time() > event_expire_time:
desired_message = f"{mode.upper()} MODE|{persistent_line2}"
if last_lcd_message != desired_message:
send_lcd(
f"{mode.upper()} MODE",
persistent_line2
)
# Read serial data
if arduino.in_waiting > 0:
data = arduino.readline().decode('utf-8').strip()
if not data or "," not in data:
continue
parts = data.split(",")
if len(parts) != 2:
continue
raw_dist, raw_ir = parts
dist = process_filtered_distance(float(raw_dist))
ir = int(raw_ir)
now = time.time()
action = "IDLE"
# IR button handling
if ir == 1:
if ir_pressed_time is None:
ir_pressed_time = now
mode_switched = False
duration = now - ir_pressed_time
# Mode switch - Hold for HOLD_THRESHOLD sec to switch modes
if duration >= HOLD_THRESHOLD and not mode_switched:
mode = "media" if mode == "mouse" else "mouse"
print(f"\n🔁 MODE SWITCHED → {mode.upper()}\n")
show_event("MODE CHANGED")
mode_switched = True
else:
if ir_pressed_time is not None:
duration = now - ir_pressed_time
# TAP ACTION
if duration < TAP_THRESHOLD and not mode_switched:
if mode == "mouse":
pyautogui.click()
action = "MOUSE CLICK"
show_event("CLICK")
else:
pyautogui.press("playpause")
action = "MEDIA PLAY/PAUSE"
show_event("PLAY/PAUSE")
ir_pressed_time = None
mode_switched = False
# Ultrasonic gesture handling
if dist <= MAX_DIST:
if now - last_action_time > COOLDOWN:
# Near distance behavior
if dist < (MID_ZONE - DEADZONE):
if mode == "mouse":
pyautogui.scroll(-SCROLL_STRENGTH)
action = f"SCROLL DOWN ({dist:.1f}cm)"
show_event("SCROLL DOWN")
else:
pyautogui.press("volumedown")
action = "VOLUME DOWN"
show_event("VOL DOWN")
# Far distance behavior
elif dist > (MID_ZONE + DEADZONE):
if mode == "mouse":
pyautogui.scroll(SCROLL_STRENGTH)
action = "SCROLL UP"
show_event("SCROLL UP")
else:
pyautogui.press("volumeup")
action = "VOLUME UP"
show_event("VOL UP")
else:
action = f"NEUTRAL DEADZONE ({dist:.1f}cm)"
last_action_time = now
else:
if action == "IDLE" and len(raw_history) > 0:
raw_history.clear() # remove old readings when hand is removed to prevent jumps during next gesture
# Debug console output if there's an IR event or any action taken
if ir == 1 or action != "IDLE":
print(
f"Mode: {mode.upper():5s} | "
f"Dist: {dist:5.1f}cm | "
f"IR: {ir} | "
f"Action: {action}"
)
except KeyboardInterrupt:
print("\n interrupted, exiting...")
break
except Exception:
continue