Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start of higher level SDK #11

Closed
wants to merge 11 commits into from
65 changes: 58 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,61 @@ As simple as:

```python
import asyncio
from frameutils import Bluetooth
from frameutils import Frame


async def main():
bluetooth = Bluetooth()
await bluetooth.connect()
# the with statement handles the connection and disconnection to Frame
async with Frame() as f:
# you can access the lower-level bluetooth connection via f.bluetooth, although you shouldn't need to do this often
print(f"Connected: {f.bluetooth.is_connected()}")

print(await bluetooth.send_lua("print('hello world')", await_print=True))
print(await bluetooth.send_lua("print(1 + 2)", await_print=True))
# let's get the current battery level
print(f"Frame battery: {await f.get_battery_level()}%")

# let's write (or overwrite) the file greeting.txt with "Hello world".
# You can provide a bytes object or convert a string with .encode()
await f.files.write_file("greeting.txt", b"Hello world")

# And now we read that file back.
# Note that we should convert the bytearray to a string via the .decode() method.
print((await f.files.read_file("greeting.txt")).decode())

# run_lua will automatically handle scripts that are too long for the MTU, so you don't need to worry about it.
# It will also automatically handle responses that are too long for the MTU automatically.
await f.run_lua("frame.display.text('Hello world', 50, 100);frame.display.show()")

# evaluate is equivalent to f.run_lua("print(\"1+2\"), await_print=True)
# It will also automatically handle responses that are too long for the MTU automatically.
print(await f.evaluate("1+2"))

# take a photo and save to disk
await f.display.show_text("Taking photo...", 200, 150)
await f.camera.save_photo("frame-test-photo.jpg")
await f.display.show_text("Photo saved!", 200, 150)
# or with more control
await f.camera.save_photo("frame-test-photo-2.jpg", autofocus_seconds=3, quality=f.camera.HIGH_QUALITY, autofocus_type=f.camera.AUTOFOCUS_TYPE_CENTER_WEIGHTED)
# or get the raw bytes
photo_bytes = await f.camera.take_photo(autofocus_seconds=1)

# Show the full palette
width = 640 // 4
height = 400 // 4
for color in range(0, 16):
tile_x = (color % 4)
tile_y = (color // 4)
await f.display.draw_rect(tile_x*width+1, tile_y*height+1, width, height, color)
await f.display.write_text(f"{color}", tile_x*width+width//2+1, tile_y*height+height//2+1)
await f.display.show()
await asyncio.sleep(5)

# scroll some long text
await f.display.scroll_text("Never gonna give you up\nNever gonna let you down\nNever gonna run around and desert you\nNever gonna make you cry\nNever gonna say goodbye\nNever gonna tell a lie and hurt you")



print("disconnected")

await bluetooth.disconnect()


asyncio.run(main())
Expand All @@ -33,8 +77,15 @@ asyncio.run(main())

## Tests

To run the unit tests, ensure you have an unconnected Frame device in range, and then run:
To run the unit tests, ensure you have pytest installed:

```sh
pip3 install pytest
```

With an unconnected Frame device in range, run:

```sh
python3 -m pytest tests/test_bluetooth.py
python3 -m pytest tests/test_files.py
```
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ classifiers = [
"Programming Language :: Python :: 3",
"Operating System :: OS Independent",
]
dependencies = ["bleak", "numpy", "scikit-learn"]
dependencies = ["bleak", "numpy", "scikit-learn", "exif"]

[project.scripts]
frameutils = "frameutils.cli:main"
Expand Down
4 changes: 3 additions & 1 deletion src/frameutils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
__all__ = ["bluetooth"]
__all__ = ["bluetooth", "files", "frame"]

from .bluetooth import Bluetooth
from .files import FrameFileSystem
from .frame import Frame
Loading