From 9880317523810be21930451f769db4e4c0ba4719 Mon Sep 17 00:00:00 2001 From: 0xrushi <0xrushi@gmail.com> Date: Mon, 4 Nov 2024 08:54:11 -0500 Subject: [PATCH 1/2] add vector --- pyproject.toml | 2 +- src/frame_sdk/display.py | 55 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 99f9e01..76fbb0d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 = "pip@betechie.com" },{ name = "Brilliant Labs", email = "info@brilliant.xyz" }] description = "Python Developer SDK for Brilliant Frame glasses" readme = "readme.md" diff --git a/src/frame_sdk/display.py b/src/frame_sdk/display.py index 3ef5a78..4a38219 100644 --- a/src/frame_sdk/display.py +++ b/src/frame_sdk/display.py @@ -519,6 +519,61 @@ 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 + + # Ensure width is multiple of 8 since we're using 2-color mode (8 pixels per byte) + w = w // 8 * 8 + + # Calculate the number of bytes needed (each byte = 8 pixels in 2-color mode) + bytes_needed = (w // 8) * h + + return f"frame.display.bitmap({x},{y},{w},2,{color},string.rep(\"\\xFF\",{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 + + 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 8x1 bitmap + await self.frame.run_lua(f"frame.display.bitmap({x1},{y1},8,2,{color},\"\\xFF\")") + error -= dy + if error < 0: + y1 += sy + error += dx + x1 += sx + else: + # More vertical movement + error = dy / 2 + while y1 != y2: + # Draw 8x1 bitmap + await self.frame.run_lua(f"frame.display.bitmap({x1},{y1},8,2,{color},\"\\xFF\")") + 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): """ From 88230035a4cfa952b6c876f6deb12927ca78df22 Mon Sep 17 00:00:00 2001 From: 0xrushi <0xrushi@gmail.com> Date: Mon, 4 Nov 2024 09:01:02 -0500 Subject: [PATCH 2/2] color modes --- src/frame_sdk/display.py | 53 +++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/src/frame_sdk/display.py b/src/frame_sdk/display.py index 4a38219..55689ba 100644 --- a/src/frame_sdk/display.py +++ b/src/frame_sdk/display.py @@ -524,14 +524,35 @@ 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 - # Ensure width is multiple of 8 since we're using 2-color mode (8 pixels per byte) - w = w // 8 * 8 - - # Calculate the number of bytes needed (each byte = 8 pixels in 2-color mode) - bytes_needed = (w // 8) * h + # Calculate bytes needed based on color mode + bytes_needed = (w // pixels_per_byte) * h - return f"frame.display.bitmap({x},{y},{w},2,{color},string.rep(\"\\xFF\",{bytes_needed}))" + 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): """ @@ -546,18 +567,26 @@ async def draw_vector(self, x1: int, y1: int, x2: int, y2: int, color: PaletteCo """ 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 8x1 bitmap - await self.frame.run_lua(f"frame.display.bitmap({x1},{y1},8,2,{color},\"\\xFF\")") + # 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 @@ -567,8 +596,8 @@ async def draw_vector(self, x1: int, y1: int, x2: int, y2: int, color: PaletteCo # More vertical movement error = dy / 2 while y1 != y2: - # Draw 8x1 bitmap - await self.frame.run_lua(f"frame.display.bitmap({x1},{y1},8,2,{color},\"\\xFF\")") + # 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