Skip to content

Commit

Permalink
Fix packet fragmentation in python tutorials (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcamise-gpsw committed May 9, 2024
1 parent 419e8d4 commit 8c5fbf1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import argparse
import asyncio
import sys
from typing import Final

from rich.console import Console
Expand Down Expand Up @@ -39,6 +40,7 @@ async def poll_for_status() -> bool:
response = (await gopro.http_command.webcam_status()).data
if response.error != WebcamError.SUCCESS:
# Something bad happened
console.print(f"[yellow]Received webcam error: {response.error}")
return False
if response.status in statuses:
# We found the desired status
Expand All @@ -51,7 +53,7 @@ async def poll_for_status() -> bool:
return False


async def main(args: argparse.Namespace) -> None:
async def main(args: argparse.Namespace) -> int:
logger = setup_logging(__name__, args.log)
gopro: GoProBase | None = None

Expand Down Expand Up @@ -79,7 +81,9 @@ async def main(args: argparse.Namespace) -> None:
await wait_for_webcam_status(gopro, {WebcamStatus.OFF})

console.print("[blue]Starting webcam...")
await gopro.http_command.webcam_start()
if (status := (await gopro.http_command.webcam_start()).data.error) != WebcamError.SUCCESS:
console.print(f"[red]Couldn't start webcam: {status}")
return -1
await wait_for_webcam_status(gopro, {WebcamStatus.HIGH_POWER_PREVIEW})

# Start player
Expand All @@ -97,6 +101,8 @@ async def main(args: argparse.Namespace) -> None:
if gopro:
await gopro.close()

return 0


def parse_arguments() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Setup and view a GoPro webcam using TS protocol.")
Expand All @@ -117,7 +123,7 @@ def parse_arguments() -> argparse.Namespace:

# Needed for poetry scripts defined in pyproject.toml
def entrypoint() -> None:
asyncio.run(main(parse_arguments()))
sys.exit(asyncio.run(main(parse_arguments())))


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ def yield_fragmented_packets(payload: bytes) -> Generator[bytes, None, None]:
is_first_packet = True

# Build initial length header
if length < (2**5 - 1):
header = bytearray([length])
elif length < (2**13 - 1):
if length < (2**13 - 1):
header = bytearray((length | 0x2000).to_bytes(2, "big", signed=False))
elif length < (2**16 - 1):
header = bytearray((length | 0x6400).to_bytes(2, "big", signed=False))
Expand Down

0 comments on commit 8c5fbf1

Please sign in to comment.