Repository navigation
Expand file tree
/
Copy pathBTGUI6.py
More file actions
213 lines (184 loc) · 5.86 KB
/
Copy pathBTGUI6.py
File metadata and controls
213 lines (184 loc) · 5.86 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
import tkinter as tk
import threading
import serial
import asyncio
from bleak import BleakClient, BleakScanner
# ------------------ SETTINGS ------------------
SERIAL_PORT = "COM5"
BAUD_RATE = 115200
BLE_NAME = "Group3-RPi"
BLE_CHAR_UUID = "00002a67-0000-1000-8000-00805f9b34fb"
# ------------------ FIELDS ------------------
fields = [
"Satellite Count",
"Latitude",
"Longitude",
"Elevation",
"Accel X",
"Accel Y",
"Accel Z",
"Gyro X",
"Gyro Y",
"Gyro Z",
"Mag X",
"Mag Y",
"Mag Z"
]
usb_latest = {f: "---" for f in fields}
ble_latest = {f: "---" for f in fields}
# ------------------ USB SERIAL ------------------
ser = None
running_usb = False
def parse_usb_csv(line):
"""Parse CSV formatted USB data line"""
try:
parts = line.strip().split(",")
if len(parts) < 15:
return None
clean = [p.strip() for p in parts]
data = {
"Satellite Count": clean[2],
"Latitude": clean[3],
"Longitude": clean[4],
"Elevation": clean[5],
"Accel X": clean[6],
"Accel Y": clean[7],
"Accel Z": clean[8],
"Gyro X": clean[9],
"Gyro Y": clean[10],
"Gyro Z": clean[11],
"Mag X": clean[12],
"Mag Y": clean[13],
"Mag Z": clean[14]
}
return data
except:
return None
def usb_thread():
global usb_latest
while running_usb:
try:
line = ser.readline().decode(errors="ignore").strip()
if not line:
continue
data = parse_usb_csv(line)
if data:
usb_latest.update(data)
except:
pass
def start_usb():
global ser, running_usb
try:
ser = serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=1)
except Exception as e:
tk.messagebox.showerror("USB Error", str(e))
return
running_usb = True
threading.Thread(target=usb_thread, daemon=True).start()
start_usb_btn.config(state="disabled")
stop_usb_btn.config(state="normal")
def stop_usb():
global running_usb, ser
running_usb = False
if ser:
ser.close()
start_usb_btn.config(state="normal")
stop_usb_btn.config(state="disabled")
# ------------------ BLE PARSING ------------------
running_ble = False
ble_buffer = ""
ble_index = -1
async def ble_task():
global running_ble
device = await BleakScanner.find_device_by_name(BLE_NAME)
if not device:
print("BLE device not found")
return
async with BleakClient(device) as client:
print("Connected to BLE!")
def ble_callback(sender, data):
# Properly decode bytes to string
clean = data.decode(errors="ignore").replace("\r", "").replace("\n", "")
process_ble_bytes(clean)
await client.start_notify(BLE_CHAR_UUID, ble_callback)
while running_ble:
await asyncio.sleep(0.1)
def process_ble_bytes(text):
"""Collect bracketed packets, update fields in real time"""
global ble_buffer, ble_index
for ch in text:
if ch == "[":
ble_buffer = ""
ble_index = 0
continue
if ch == "]":
ble_buffer = ""
ble_index = -1
continue
if ble_index >= 0:
if ch == ",":
assign_ble_value(ble_buffer)
ble_buffer = ""
ble_index += 1
else:
ble_buffer += ch
def assign_ble_value(val):
"""
BLE packet positions:
0 ignored
1 ignored
2 Satellite Count
...
"""
global ble_index
if ble_index < 2: # ignore timestamp fields
return
field_idx = ble_index - 2
if field_idx >= len(fields):
return
field = fields[field_idx]
# Remove any leftover quotes or b'' formatting
val_clean = val.strip().replace("b'", "").replace("'", "")
ble_latest[field] = val_clean
def ble_thread_func():
asyncio.run(ble_task())
def start_ble():
global running_ble
running_ble = True
threading.Thread(target=ble_thread_func, daemon=True).start()
start_ble_btn.config(state="disabled")
stop_ble_btn.config(state="normal")
def stop_ble():
global running_ble
running_ble = False
start_ble_btn.config(state="normal")
stop_ble_btn.config(state="disabled")
# ------------------ UI ------------------
root = tk.Tk()
root.title("USB + BLE Sensor Viewer")
main = tk.Frame(root, padx=10, pady=10)
main.pack()
start_usb_btn = tk.Button(main, text="Start USB", command=start_usb)
stop_usb_btn = tk.Button(main, text="Stop USB", command=stop_usb, state="disabled")
start_ble_btn = tk.Button(main, text="Start BLE", command=start_ble)
stop_ble_btn = tk.Button(main, text="Stop BLE", command=stop_ble, state="disabled")
start_usb_btn.grid(row=0, column=0, padx=5, pady=5)
stop_usb_btn.grid(row=0, column=1, padx=5, pady=5)
start_ble_btn.grid(row=1, column=0, padx=5, pady=5)
stop_ble_btn.grid(row=1, column=1, padx=5, pady=5)
usb_labels = {}
ble_labels = {}
for i, f in enumerate(fields):
tk.Label(main, text=f"USB {f}:").grid(row=i+2, column=0, sticky="e")
usb_labels[f] = tk.Label(main, text="---")
usb_labels[f].grid(row=i+2, column=1, sticky="w")
tk.Label(main, text=f"BLE {f}:").grid(row=i+2, column=2, sticky="e")
ble_labels[f] = tk.Label(main, text="---")
ble_labels[f].grid(row=i+2, column=3, sticky="w")
def update_display():
for f in fields:
usb_labels[f].config(text=usb_latest.get(f, "---"))
ble_labels[f].config(text=ble_latest.get(f, "---"))
root.after(100, update_display)
root.after(100, update_display)
root.mainloop()