Code de-clutter

This commit is contained in:
MassiveBox 2025-11-17 22:11:23 +01:00
parent e82ecadbb3
commit 1e267dbdb6
Signed by: massivebox
GPG key ID: 9B74D3A59181947D
4 changed files with 6 additions and 21 deletions

View file

@ -2,7 +2,7 @@
Gicisky - A Python library for interacting with Gicisky electronic ink display tags. Gicisky - A Python library for interacting with Gicisky electronic ink display tags.
""" """
__version__ = "0.1.0" __version__ = "0.1.1"
__author__ = "MassiveBox" __author__ = "MassiveBox"
__email__ = "box@massive.box" __email__ = "box@massive.box"

View file

@ -5,20 +5,13 @@ from typing import Optional
from ble.interface import BLEInterface, Advertisement from ble.interface import BLEInterface, Advertisement
from logger.logger import GiciskyLogger, LogCategory from logger.logger import GiciskyLogger, LogCategory
# Default UUIDs for Gicisky tags
BASE_SERVICE = 0xFEF0 BASE_SERVICE = 0xFEF0
SERVICE_UUID = f"0000{BASE_SERVICE:04x}-0000-1000-8000-00805f9b34fb" SERVICE_UUID = f"0000{BASE_SERVICE:04x}-0000-1000-8000-00805f9b34fb"
CHAR_CMD_UUID = f"0000{BASE_SERVICE+1:04x}-0000-1000-8000-00805f9b34fb" CHAR_CMD_UUID = f"0000{BASE_SERVICE+1:04x}-0000-1000-8000-00805f9b34fb"
CHAR_IMG_UUID = f"0000{BASE_SERVICE+2:04x}-0000-1000-8000-00805f9b34fb" CHAR_IMG_UUID = f"0000{BASE_SERVICE+2:04x}-0000-1000-8000-00805f9b34fb"
# Constants CHUNK_SIZE = 240
CHUNK_SIZE = 240 # 480 hex chars (as seen in the JS uploader) MTU_WAIT = 0.03
MTU_WAIT = 0.03 # delay between chunks (adjustable)
DEBUG = True
def log(msg):
if DEBUG:
print(f"[LOG] {msg}", flush=True)
class GiciskyProtocol: class GiciskyProtocol:
@ -42,6 +35,7 @@ class GiciskyProtocol:
self.logger.info("Starting image upload", LogCategory.DATA_TRANSFER) self.logger.info("Starting image upload", LogCategory.DATA_TRANSFER)
await self.send_command(3) await self.send_command(3)
elif hx.startswith("05"): elif hx.startswith("05"):
# Image upload loop
self.logger.debug(f"Handling response: {hx}, err: {hx[2:4]}, part: {hx[4:12]}", LogCategory.NOTIFICATION) self.logger.debug(f"Handling response: {hx}, err: {hx[2:4]}, part: {hx[4:12]}", LogCategory.NOTIFICATION)
err = hx[2:4] err = hx[2:4]
if err == "00": # continue sending chunks if err == "00": # continue sending chunks

View file

@ -66,27 +66,21 @@ def convert_to_gicisky_bytes(img: Image.Image, model: ModelId, lum_threshold: in
Returns: Returns:
bytes: Processed image data in Gicisky format bytes: Processed image data in Gicisky format
""" """
# Get device specifications
specs = DEVICE_SPECS.get(model) specs = DEVICE_SPECS.get(model)
if not specs: if not specs:
raise ValueError("Unknown model") raise ValueError("Unknown model")
width, height = specs.size width, height = specs.size
# Resize image to device dimensions
img = img.convert("RGB").resize((width, height), Image.Resampling.LANCZOS) img = img.convert("RGB").resize((width, height), Image.Resampling.LANCZOS)
# Apply TFT transformation if enabled
if specs.tft: if specs.tft:
# Resize to half width and double height # Resize to half width and double height
img = img.resize((width // 2, height * 2), Image.Resampling.LANCZOS) img = img.resize((width // 2, height * 2), Image.Resampling.LANCZOS)
width, height = img.size width, height = img.size
# Apply mirroring if enabled
if specs.mirror: if specs.mirror:
img = img.transpose(Image.FLIP_TOP_BOTTOM) img = img.transpose(Image.FLIP_TOP_BOTTOM)
# Convert to numpy array
arr = np.array(img) arr = np.array(img)
# Process pixels - column-major order # Process pixels - column-major order
@ -99,7 +93,6 @@ def convert_to_gicisky_bytes(img: Image.Image, model: ModelId, lum_threshold: in
r, g, b = arr[y, x] # Note: numpy uses [y, x] indexing r, g, b = arr[y, x] # Note: numpy uses [y, x] indexing
luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b
# Apply thresholding based on compression setting
if specs.compression: if specs.compression:
# When compression is enabled, dark pixels set bits # When compression is enabled, dark pixels set bits
if luminance < lum_threshold: if luminance < lum_threshold:
@ -125,7 +118,6 @@ def convert_to_gicisky_bytes(img: Image.Image, model: ModelId, lum_threshold: in
byte_data.append(current_byte) byte_data.append(current_byte)
red_byte_data.append(current_red_byte) red_byte_data.append(current_red_byte)
# Apply compression if enabled
if specs.compression: if specs.compression:
byte_data_compressed = _apply_compression(byte_data, red_byte_data, width, height, specs.second_color) byte_data_compressed = _apply_compression(byte_data, red_byte_data, width, height, specs.second_color)
else: else:
@ -156,7 +148,6 @@ def _apply_compression(byte_data: List[int], red_byte_data: List[int],
byte_per_line = height // 8 byte_per_line = height // 8
current_pos = 0 current_pos = 0
# Process black/white data
for i in range(width): for i in range(width):
# Add line header # Add line header
byte_data_compressed.extend([ byte_data_compressed.extend([

View file

@ -8,7 +8,7 @@ with open("requirements.txt", "r", encoding="utf-8") as fh:
setup( setup(
name="gicisky", name="gicisky",
version="0.1.0", version="0.1.1",
author="MassiveBox", author="MassiveBox",
author_email="box@massive.box", author_email="box@massive.box",
description="A Python library for interacting with Gicisky E-Ink display tags via Bluetooth Low Energy", description="A Python library for interacting with Gicisky E-Ink display tags via Bluetooth Low Energy",