-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
711 lines (603 loc) · 26.4 KB
/
Copy pathapp.py
File metadata and controls
711 lines (603 loc) · 26.4 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
# Author: William Liu <liwi@ohsu.edu>
import asyncio
import logging
import os
from datetime import datetime
from bleak import BleakScanner
from bleak.backends.device import BLEDevice
from open_gopro import GoProResp, WirelessGoPro, constants, proto
from open_gopro.exceptions import ConnectFailed, FailedToFindDevice
from pynput import keyboard
from rich.console import Console
from rich.prompt import Confirm, Prompt
from rich.table import Table
async def scan_for_cameras() -> dict[str, BLEDevice]:
"""Scan for available GoPro cameras.
Returns
-------
dict[str, BLEDevice]
Dictionary of GoPro cameras that were found during scanning.
"""
devices: dict[str, BLEDevice] = dict()
def scan_callback(device, advertising_data):
if device.name and device.name != "Unknown":
logger.info(f"Discovered {device}")
devices[device.name] = device
async with BleakScanner(
scan_callback, service_uuids=["0000fea6-0000-1000-8000-00805f9b34fb"]
):
for i in range(1, 100):
await asyncio.sleep(0.1)
return devices
def device_table(devices: dict[str, BLEDevice]) -> None:
"""Display table with GoPro cameras found by scanning.
Parameters
----------
devices : dict[str, BLEDevice]
Dictionary of GoPro cameras found during scanning.
"""
table = Table(show_header=True, header_style="bold magenta")
table.add_column("Device Name")
table.add_column("Bluetooth Address", style="dim")
for name, device in devices.items():
table.add_row(name, device.address)
console.print(table)
async def get_camera_model(cam: WirelessGoPro) -> str:
"""Get the camera model number."""
camera_info = await cam.ble_command.get_hardware_info()
return camera_info.data.model_name
async def get_camera_battery(cam: WirelessGoPro) -> int:
"""Get current battery level in percent."""
batt = await (cam.ble_status.internal_battery_percentage).get_value()
return batt.data
async def verify_battery(cam: WirelessGoPro) -> tuple[bool, int]:
"""Check if battery is above 15%."""
batt = await get_camera_battery(cam)
if batt <= 15:
return False, batt
return True, batt
async def get_camera_remaining_storage(cam: WirelessGoPro) -> int:
"""Get remaining space on SD card, in kilobytes."""
storage = await (cam.ble_status.sd_card_remaining).get_value()
return storage.data
async def verify_storage(cam: WirelessGoPro) -> tuple[bool, int]:
"""Check if storage is above 1 GB."""
storage = await get_camera_remaining_storage(cam)
if storage <= 1e6:
return False, storage
return True, storage
async def connected_camera_table(connected_cameras: dict[str, WirelessGoPro]) -> None:
"""Display a table with infor about each connected GoPro.
Parameters
----------
connected_cameras : dict[str, WirelessGoPro]
Dictionary of currently connected cameras, where key is the camera name
and value is the `WirelessGoPro` instance.
"""
table = Table(show_header=True, header_style="bold magenta")
table.add_column("Camera Name")
table.add_column("Battery Level")
table.add_column("Space Remaining on SD Card (mb)")
for name, camera in connected_cameras.items():
batt = await get_camera_battery(camera)
sdcard = await get_camera_remaining_storage(camera)
table.add_row(name, str(batt) + "%", str(sdcard / 1000))
console.print(table)
def prompt_device_selection(devices: dict[str, BLEDevice]) -> str | None:
"""Prompt user to choose which device(s) to connect to.
Parameters
----------
devices : dict[str, BLEDevice]
Dictionary of GoPro cameras found during scanning.
Returns
-------
str | None
A string representing the user's choice, or `None` if no cameras were
found during scanning.
"""
if devices:
choices = ["All", "None"]
choices.extend([n for n in devices.keys()])
prompt = Prompt.ask(
"Do you want to connect to any of the found devices?",
console=console,
choices=choices,
)
return prompt
else:
console.print(
"""No cameras were found. Make sure:
1) All cameras are turned on
2) The computer's bluetooth is turned on
3) If this is the first time connecting to a camera, it must be in pairing mode"""
)
return "None"
async def connect_camera(
found_devices: dict[str, BLEDevice],
connected_cameras: dict[str, WirelessGoPro],
connect_prompt: str,
) -> None:
"""Connect to camera(s) based on user input.
Parameters
----------
found_devices : dict[str, BLEDevice]
Dictionary of GoPro cameras that were discovered during scanning.
connected_cameras : dict[str, WirelessGoPro]
Dictionary of currently connected cameras, where key is the camera name
and value is the `WirelessGoPro` instance.
connect_prompt : str
User input from the connect prompt.
"""
if connect_prompt == "None":
pass
else:
retry: bool = True
while retry:
missed_connections: list = []
retry = False
with console.status("Connecting to cameras...", spinner="bouncingBar"):
if connect_prompt == "All":
for name in found_devices.keys():
if name in connected_cameras:
console.print(f"{name} is already connected.")
continue
try:
cam = WirelessGoPro(target=name, enable_wifi=False)
await cam.open()
console.print(f"Connected to {name}")
connected_cameras[name] = cam
except FailedToFindDevice:
logging.error(f"Failed to find {name}.")
missed_connections.append(name)
except ConnectFailed:
logging.error(f"Failed to connect to {name}.")
missed_connections.append(name)
else:
if connect_prompt in connected_cameras:
console.print(f"{connect_prompt} is already connected.")
retry = False
else:
try:
cam = WirelessGoPro(
target=connect_prompt, enable_wifi=False
)
await cam.open()
console.print(f"Connected to {connect_prompt}")
connected_cameras[connect_prompt] = cam
except FailedToFindDevice:
logging.error(f"Failed to find {connect_prompt}.")
missed_connections.append(connect_prompt)
except ConnectFailed:
logging.error(f"Failed to connect to {connect_prompt}.")
missed_connections.append(connect_prompt)
if missed_connections:
retry = Confirm.ask(
f"Could not connect to the following camera(s): {missed_connections}. Retry?"
)
async def disconnect_cameras(
connected_cameras: dict[str, WirelessGoPro], quit_flag: bool = False
) -> None:
"""Disconnect all currently connected GoPro cameras.
It is very important to call the close() method on each WirelessGoPro
instance. If the connection is not closed gracefully it can cause issues
the next time you try to connect to the camera. Then you have to reset the
connections from the camera and re-pair with the computer.
Parameters
----------
connected_cameras : dict[str, WirelessGoPro]
Dictionary of currently connected cameras, where key is the camera name
and value is the `WirelessGoPro` instance.
quit_flag : bool
Indicates whether this method was called when user is trying to quit the
application or merely disconnect the cameras without quitting.
"""
if connected_cameras:
with console.status("Disconnecting from cameras...", spinner="bouncingBar"):
disconnected = list()
for cam in connected_cameras:
await connected_cameras[cam].close()
disconnected.append(cam)
logging.info(f"Disconnected from {cam}.")
console.print(f"Disconnected from {cam}")
for cam in disconnected:
del connected_cameras[cam]
else:
if not quit_flag:
console.print("No cameras are currently connected")
async def ready_to_record(connected_cameras: dict[str, WirelessGoPro]) -> bool:
"""Make sure cameras are ready to start recording.
This includes checking for battery life and SD card space remaining, to
prevent starting a recording where the camera might run out of battery or
SD card space.
Parameters
----------
connected_cameras : dict[str, WirelessGoPro]
Dictionary of currently connected cameras, where key is the camera name
and value is the `WirelessGoPro` instance.
"""
if connected_cameras:
not_ready: int = 0
for name, cam in connected_cameras.items():
logging.info(f"Checking if {name} is ready...")
ready: bool = False
batt_ready: bool = False
sdcard_ready: bool = False
for i in range(10):
status = await (cam.ble_status.ready).get_value()
logging.info(f"Status is {str(status)} on retry# {i + 1}.")
if (status.status == constants.ErrorCode.SUCCESS) and (status.data):
ready = True
batt_ready, batt_percent = await verify_battery(cam)
sdcard_ready, sdcard_remaining = await verify_storage(cam)
break
await asyncio.sleep(1)
if (ready, batt_ready, sdcard_ready) == (True, True, True):
logging.info(
f"{name} is ready, the battery is at {batt_percent}%, and the SD card has"
f" {sdcard_remaining} kb remaining."
)
console.print(f"{name} is ready!")
elif (ready, batt_ready, sdcard_ready) == (True, True, False):
not_ready += 1
logging.info(
f"{name} is ready, the battery is at {batt_percent}%, and the SD card has"
f" {sdcard_remaining} kb remaining."
)
console.print(
f"{name} only has {sdcard_remaining / 1e6} GB remaining, quit the app and remove some of the video"
" files before proceeding."
)
elif (ready, batt_ready, sdcard_ready) == (True, False, False):
not_ready += 1
logging.info(
f"{name} is ready, the battery is at {batt_percent}%, and the SD card has"
f" {sdcard_remaining} kb remaining."
)
console.print(
f"{name} only has {sdcard_remaining / 1e6} GB remaining and the battery is at {batt_percent}%."
" Quit the app, remove some of the video files, and change the battery before proceeding."
)
elif (ready, batt_ready, sdcard_ready) == (True, False, True):
not_ready += 1
logging.info(
f"{name} is ready, the battery is at {batt_percent}%, and the SD card has"
f" {sdcard_remaining} kb remaining."
)
console.print(
f"{name} only has battery at {batt_percent}%."
" Quit the app and change the battery before proceeding."
)
else:
not_ready += 1
logging.info(f"{name} is not ready.")
console.print(f"{name} is not ready. Please try again.")
if not_ready != 0:
return False
else:
return True
else:
console.print("No cameras are connected!")
return False
async def record(
connected_cameras: dict[str, WirelessGoPro], timeout: float | None = None
) -> None:
"""Listen for the Page Down keycode to start/stop a recording.
Parameters
----------
connected_cameras : dict[str, WirelessGoPro]
Dictionary containining the WirelessGoPro instances of the currently
connected cameras.
timeout : int | float
Maximum time, in seconds, to wait for the key press before cancelling.
"""
cancel_flag: bool = False
def _on_press(key):
logging.info(f"Key {key} was pressed.")
def _on_release(key):
logging.info(f"Key {key} was released.")
if key == keyboard.Key.page_down:
return False # stop the listener
elif key == keyboard.Key.esc:
nonlocal cancel_flag
cancel_flag = True
return False # stop the listener
# The key release after pressing ENTER can trigger the event listner, so sleep for a second to ensure
# no keys are actively being pressed
await asyncio.sleep(1)
with console.status(
"Switch application focus to Mobility Lab, then press > on remote to start recording. Press ESC to cancel.",
spinner="bouncingBar",
):
logging.info("Starting keyboard listener, waiting for start trigger.")
with keyboard.Listener(on_press=_on_press, on_release=_on_release) as listener:
listener.join()
logging.info(f"The value of cancel_flag is {cancel_flag}.")
if cancel_flag:
console.print("Recording cancelled.")
logging.info("Recording cancelled.")
return
tasks: list[asyncio.Task] = []
async with (
asyncio.TaskGroup() as tg
): # once context manager exits all tasks are awaited
for cam in connected_cameras.values():
tasks.append(
tg.create_task(
cam.ble_command.set_shutter(shutter=constants.Toggle.ENABLE)
)
)
logging.info("Recording started.")
# Wait for stop trigger
with console.status(
"Recording... Press > on remote to stop recording.", spinner="bouncingBar"
):
logging.info("Starting keyboard listener, waiting for stop trigger.")
with keyboard.Listener(on_press=_on_press, on_release=_on_release) as listener:
listener.join()
tasks = []
async with (
asyncio.TaskGroup() as tg
): # once context manager exits all tasks are awaited
for cam in connected_cameras.values():
tasks.append(
tg.create_task(
cam.ble_command.set_shutter(shutter=constants.Toggle.DISABLE)
)
)
async def enforce_camera_settings(
connected_cameras: dict[str, WirelessGoPro], retries: int = 5
) -> None:
"""Ensure all cameras are recording with the same video settings.
The standard settings for all users are:
Parameters
----------
connected_cameras : dict[str, WirelessGoPro]
Dictionary containining the WirelessGoPro instances of the currently
connected cameras.
"""
for name, cam in connected_cameras.items():
# Load the video preset group to ensure we are in video mode
for i in range(retries):
resp = await cam.ble_command.load_preset_group(
group=proto.EnumPresetGroup.PRESET_GROUP_ID_VIDEO
)
if _check_setting_set_response(resp, "load_preset_group", name, i):
break
if i == (retries - 1):
logging.warning(
f"{name} did not succeed in changing the preset group to Video."
)
console.print(
f"{name} did not succeed in changing the preset group to Video. Try re-connecting to the cameras."
)
model = await get_camera_model(cam)
if model == "HERO13 Black":
settings = await _hero13_settings(name, cam, retries)
elif model == "HERO12 Black":
settings = await _hero12_settings(name, cam, retries)
else:
raise ValueError(f"Camera model {model} not supported!")
with console.status("Verifying camera settings...", spinner="bouncingBar"):
for setting in settings:
for i in range(retries):
if setting == "load_preset_group": # ensure camera is in video mode
resp = await cam.ble_command.load_preset_group(
group=settings[setting]
)
else:
resp = await (getattr(cam.ble_setting, setting)).set(
settings[setting]
)
if _check_setting_set_response(resp, setting, name, i):
break
if i == (retries - 1):
logging.warning(
f"{name} did not succeed in changing the {setting}."
)
console.print(
f"{name} did not succeed in changing the {setting}. Try re-connecting to the cameras."
)
async def _hero12_settings(name: str, cam: WirelessGoPro, retries: int) -> dict:
# Check the Anti Flicker settings
success = False
for i in range(retries):
resp = await cam.ble_setting.anti_flicker.get_value()
if _check_setting_get_response(resp, "Anti_Flicker", name, i):
if resp.data == constants.settings.Anti_Flicker.NUM_60HZ:
fps = constants.settings.FramesPerSecond.NUM_30_0
elif resp.data == constants.settings.Anti_Flicker.NUM_50HZ:
fps = constants.settings.FramesPerSecond.NUM_50_0
else:
logging.error(
f"Unknown value {resp.data} encountered for Anti_Flicker for {name}."
)
success = True
break
if not success:
logging.error(f"Could not get Anti_Flicker value for {name}.")
# Check the lens FOV, allow for using values other than the default (linear)
success = False
for i in range(retries):
resp = await cam.ble_setting.video_lens.get_value()
lens = resp.data
if _check_setting_get_response(resp, "Video_Lens", name, i):
if lens != constants.settings.VideoLens.LINEAR:
console.print(
f"[bold blue]Heads up![/bold blue] {name} is using {str(resp.data)} field of view."
)
prompt = Prompt.ask(
"Do you want to switch to the default (linear) field of view?",
console=console,
choices=["Yes", "No"],
)
if prompt == "Yes":
lens = constants.settings.VideoLens.LINEAR
success = True
break
if not success:
logging.error(f"Could not get Video_Lens value for {name}.")
# Create settings dictionary
settings = {
"load_preset_group": proto.EnumPresetGroup.PRESET_GROUP_ID_VIDEO, # video mode
"controls": constants.settings.Controls.PRO,
"profiles": constants.settings.Profiles.STANDARD,
"video_lens": lens,
"video_aspect_ratio": constants.settings.VideoAspectRatio.NUM_16_9,
"video_resolution": constants.settings.VideoResolution.NUM_1080,
"frames_per_second": fps,
"hypersmooth": constants.settings.Hypersmooth.OFF,
"hindsight": constants.settings.Hindsight.OFF,
"bit_depth": constants.settings.BitDepth.NUM_8_BIT,
"video_bit_rate": constants.settings.VideoBitRate.HIGH,
"auto_power_down": constants.settings.AutoPowerDown.NUM_30_MIN,
}
return settings
async def _hero13_settings(name: str, cam: WirelessGoPro, retries: int) -> dict:
# Check the Anti Flicker settings
success = False
for i in range(retries):
resp = await cam.ble_setting.anti_flicker.get_value()
if _check_setting_get_response(resp, "Anti_Flicker", name, i):
if resp.data == constants.settings.Anti_Flicker.NTSC:
frame_rate = constants.settings.FrameRate.NUM_30_0
elif resp.data == constants.settings.Anti_Flicker.PAL:
frame_rate = constants.settings.FrameRate.NUM_50_0
else:
logging.error(
f"Unknown value {resp.data} encountered for Anti_Flicker for {name}."
)
success = True
break
if not success:
logging.error(f"Could not get Anti_Flicker value for {name}.")
# Check the lens FOV, allow for using values other than the default (linear)
success = False
for i in range(retries):
resp = await cam.ble_setting.video_lens.get_value()
lens = resp.data
if _check_setting_get_response(resp, "Video_Lens", name, i):
if lens != constants.settings.VideoLens.LINEAR:
console.print(
f"[bold blue]Heads up![/bold blue] {name} is using {str(resp.data)} field of view."
)
prompt = Prompt.ask(
"Do you want to switch to the default (linear) field of view?",
console=console,
choices=["Yes", "No"],
)
if prompt == "Yes":
lens = constants.settings.VideoLens.LINEAR
success = True
break
if not success:
logging.error(f"Could not get Video_Lens value for {name}.")
# Create settings dictionary
settings = {
"load_preset_group": proto.EnumPresetGroup.PRESET_GROUP_ID_VIDEO, # video mode
"controls": constants.settings.Controls.PRO,
"profiles": constants.settings.Profiles.STANDARD,
"video_lens": lens,
"video_framing": constants.settings.VideoFraming.NUM_16_9,
"video_resolution": constants.settings.VideoResolution.NUM_1080,
"frame_rate": frame_rate,
"hypersmooth": constants.settings.Hypersmooth.OFF,
"hindsight": constants.settings.Hindsight.OFF,
"bit_depth": constants.settings.BitDepth.NUM_8_BIT,
"video_bit_rate": constants.settings.VideoBitRate.HIGH,
}
return settings
def _check_setting_get_response(
resp: GoProResp, setting: str, name: str, retry: int
) -> bool:
if resp.status != constants.ErrorCode.SUCCESS:
logging.error(
f"{name} did not succeed in getting the value for {setting} on try #{retry + 1}."
)
return False
logging.info(
f"{name} got the value {resp.data} for {setting} successfully on try #{retry + 1}."
)
return True
def _check_setting_set_response(
resp: GoProResp, setting: str, name: str, retry: int
) -> bool:
if resp.status != constants.ErrorCode.SUCCESS:
logging.error(
f"{name} did not succeed in setting the value of {setting} on try #{retry + 1}."
)
return False
logging.info(f"{name} set the value of {setting} successfully on try #{retry + 1}.")
return True
async def main() -> None:
"""Entrypoint for the asynchronous event loop."""
console.rule("Welcome to the GoPro Camera Control App")
logging.info("Entered main, starting app.")
running = True
connected_cameras: dict[str, WirelessGoPro] = dict()
logging.info(f"The following cameras are connected {connected_cameras}.")
ready: bool = False # flag to set when cameras are connected and not busy
logging.info(f"The ready_to_record status is {ready}")
while running:
first_action = Prompt.ask(
"What would you like to do?",
choices=["Connect", "Disconnect", "View", "Record", "Quit"],
)
logging.info(f"First action prompt was displayed, response was {first_action}.")
if first_action == "Connect":
found_devices: dict[str, BLEDevice] = dict()
with console.status("Scanning for cameras..", spinner="bouncingBar"):
found_devices = await scan_for_cameras()
logging.info(f"Scanning found the following cameras: {found_devices}.")
if found_devices:
device_table(found_devices)
connect_prompt = prompt_device_selection(found_devices)
logging.info(
f"The connect prompt was displayed, user chose: {connect_prompt}."
)
if connect_prompt != "None":
await connect_camera(found_devices, connected_cameras, connect_prompt)
await enforce_camera_settings(connected_cameras)
elif first_action == "Disconnect":
await disconnect_cameras(connected_cameras)
elif first_action == "View":
if connected_cameras:
await connected_camera_table(connected_cameras)
else:
console.print("No cameras currently connected")
elif first_action == "Record":
if connected_cameras:
ready = await ready_to_record(connected_cameras)
logging.info(f"Ready to record is {ready}.")
if ready:
await record(connected_cameras)
else:
console.print(
"At least one of the cameras is not ready to receive commands. Try again."
)
else:
console.print("No cameras currently connected")
elif first_action == "Quit":
await disconnect_cameras(connected_cameras, quit_flag=True)
logging.info("Quitting application.")
console.print("Goodbye!")
await asyncio.sleep(1)
running = False
cwd = os.getcwd()
log_folder = os.path.join(cwd, "log")
if not os.path.exists(log_folder):
os.mkdir(log_folder)
now = datetime.now()
now_str = now.strftime("%Y-%m-%d_%H-%M-%S")
log_fname = os.path.join(log_folder, f"{now_str}.log")
logger = logging.getLogger()
logging.basicConfig(
filename=log_fname,
encoding="utf-8",
format="%(asctime)s:%(levelname)s:%(message)s",
level=logging.INFO,
)
RELEASE_VERSION = "v0.3.2"
logging.info(f"Using gopro-sync version: {RELEASE_VERSION}.")
console = Console()
loop = asyncio.new_event_loop()
loop.run_until_complete(main())