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

Added vector drawing functionality #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "frame-sdk"
version = "1.2.1"
version = "1.2.2"
authors = [{ name = "Roger Pincombe", email = "[email protected]" },{ name = "Brilliant Labs", email = "[email protected]" }]
description = "Python Developer SDK for Brilliant Frame glasses"
readme = "readme.md"
Expand Down
84 changes: 84 additions & 0 deletions src/frame_sdk/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,90 @@ async def draw_rect(self, x: int, y: int, w: int, h: int, color: PaletteColors =

w = w // 8 * 8
await self.frame.run_lua(self._draw_rect_lua(x, y, w, h, color))

def _draw_vector_lua(self, x: int, y: int, w: int, h: int, color: PaletteColors):
"""Helper function to generate Lua code for vector drawing using bitmap."""
if isinstance(color, PaletteColors):
color = color.value

# Determine color mode based on color value
if color <= 1:
# 2-color mode: 8 pixels per byte
color_mode = 2
pixels_per_byte = 8
elif color <= 3:
# 4-color mode: 4 pixels per byte
color_mode = 4
pixels_per_byte = 4
else:
# 16-color mode: 2 pixels per byte
color_mode = 16
pixels_per_byte = 2

# Adjust width based on color mode
w = w // pixels_per_byte * pixels_per_byte

# Calculate bytes needed based on color mode
bytes_needed = (w // pixels_per_byte) * h

if color_mode == 2:
pattern = "\\xFF"
elif color_mode == 4:
pattern = "\\xFF"
else:
pattern = "\\xFF"

return f"frame.display.bitmap({x},{y},{w},{color_mode},{color},string.rep(\"{pattern}\",{bytes_needed}))"

async def draw_vector(self, x1: int, y1: int, x2: int, y2: int, color: PaletteColors = PaletteColors.WHITE):
"""
Draws a vector (including diagonals) using bitmap on the display.

Args:
x1 (int): The x coordinate of the start point.
y1 (int): The y coordinate of the start point.
x2 (int): The x coordinate of the end point.
y2 (int): The y coordinate of the end point.
color (PaletteColors): The color of the line (uses palette offset)
"""
if isinstance(color, PaletteColors):
color = color.value

# Determine color mode and minimum bitmap width
if color <= 1:
min_width = 8 # 2-color mode: 8 pixels per byte
elif color <= 3:
min_width = 4 # 4-color mode: 4 pixels per byte
else:
min_width = 2 # 16-color mode: 2 pixels per byte

dx = abs(x2 - x1)
dy = abs(y2 - y1)
sx = 1 if x1 < x2 else -1
sy = 1 if y1 < y2 else -1

if dx > dy:
# More horizontal movement
error = dx / 2
while x1 != x2:
# Draw bitmap with appropriate width
await self.frame.run_lua(self._draw_vector_lua(x1, y1, min_width, 1, color))
error -= dy
if error < 0:
y1 += sy
error += dx
x1 += sx
else:
# More vertical movement
error = dy / 2
while y1 != y2:
# Draw bitmap with appropriate width
await self.frame.run_lua(self._draw_vector_lua(x1, y1, min_width, 1, color))
error -= dx
if error < 0:
x1 += sx
error += dy
y1 += sy

async def draw_rect_filled(self, x: int, y: int, w: int, h: int, border_width: int, border_color: PaletteColors, fill_color: PaletteColors):
"""
Expand Down