idotmatrix-api-client 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- idotmatrix/__init__.py +15 -0
- idotmatrix/apps/__init__.py +0 -0
- idotmatrix/apps/digital_picture_frame.py +541 -0
- idotmatrix/client.py +284 -0
- idotmatrix/connection_manager.py +702 -0
- idotmatrix/const.py +30 -0
- idotmatrix/fonts/16-ErbosDraco1StOpenNbpRegular-l5wX.ttf +0 -0
- idotmatrix/fonts/16-MinecraftStandard.otf +0 -0
- idotmatrix/fonts/16-MinecraftStandardBoldOblique.otf +0 -0
- idotmatrix/fonts/16-PixelOperator8-Bold.ttf +0 -0
- idotmatrix/fonts/16-PixelOperator8.ttf +0 -0
- idotmatrix/fonts/16-PixelifySans-Regular.ttf +0 -0
- idotmatrix/fonts/16-PressStart2P-Regular.ttf +0 -0
- idotmatrix/fonts/16-dogica.ttf +0 -0
- idotmatrix/fonts/16-dogicabold.ttf +0 -0
- idotmatrix/fonts/16-dogicapixel.ttf +0 -0
- idotmatrix/fonts/16-dogicapixelbold.ttf +0 -0
- idotmatrix/fonts/16_32-HomeVideo-BLG6G.ttf +0 -0
- idotmatrix/fonts/16_32-HomeVideoBold-R90Dv.ttf +0 -0
- idotmatrix/fonts/16_32-MayfairNbpBold-gAA4.ttf +0 -0
- idotmatrix/fonts/32-Jersey10-Regular.ttf +0 -0
- idotmatrix/fonts/CC-BY-SA-4.0-License.txt +428 -0
- idotmatrix/fonts/ErbosDraco-License.txt +3 -0
- idotmatrix/fonts/HomeVideo-License.txt +2 -0
- idotmatrix/fonts/Jersey10-Regular-License.txt +93 -0
- idotmatrix/fonts/MayfairNbpBold-License.txt +3 -0
- idotmatrix/fonts/MinecraftStandard-License.txt +5 -0
- idotmatrix/fonts/PixelOperator-License.txt +121 -0
- idotmatrix/fonts/PixelifySans-License.txt +93 -0
- idotmatrix/fonts/PressStart2P-Regular-License.txt +93 -0
- idotmatrix/fonts/Rain-DRM3-License.txt +44 -0
- idotmatrix/fonts/Rain-DRM3.otf +0 -0
- idotmatrix/fonts/dogica_license.txt +94 -0
- idotmatrix/modules/__init__.py +64 -0
- idotmatrix/modules/chronograph.py +50 -0
- idotmatrix/modules/clock.py +101 -0
- idotmatrix/modules/common.py +176 -0
- idotmatrix/modules/countdown.py +70 -0
- idotmatrix/modules/eco.py +63 -0
- idotmatrix/modules/effect.py +79 -0
- idotmatrix/modules/fullscreen_color.py +55 -0
- idotmatrix/modules/gif.py +403 -0
- idotmatrix/modules/graffiti.py +71 -0
- idotmatrix/modules/image.py +383 -0
- idotmatrix/modules/music_sync.py +67 -0
- idotmatrix/modules/scoreboard.py +38 -0
- idotmatrix/modules/system.py +65 -0
- idotmatrix/modules/text.py +232 -0
- idotmatrix/protocol.py +52 -0
- idotmatrix/screensize.py +8 -0
- idotmatrix/transport.py +137 -0
- idotmatrix/util/__init__.py +0 -0
- idotmatrix/util/color_utils.py +64 -0
- idotmatrix/util/command_ack.py +23 -0
- idotmatrix/util/device_info.py +38 -0
- idotmatrix/util/device_material.py +16 -0
- idotmatrix/util/file_watch.py +57 -0
- idotmatrix/util/image_utils.py +139 -0
- idotmatrix/util/upload_ack.py +42 -0
- idotmatrix_api_client-0.2.0.dist-info/METADATA +375 -0
- idotmatrix_api_client-0.2.0.dist-info/RECORD +63 -0
- idotmatrix_api_client-0.2.0.dist-info/WHEEL +4 -0
- idotmatrix_api_client-0.2.0.dist-info/licenses/LICENSE +674 -0
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import binascii
|
|
3
|
+
import io
|
|
4
|
+
import logging
|
|
5
|
+
import struct
|
|
6
|
+
from enum import Enum
|
|
7
|
+
from os import PathLike
|
|
8
|
+
from typing import List, Tuple
|
|
9
|
+
|
|
10
|
+
from PIL import Image as PILImage, ImageOps
|
|
11
|
+
|
|
12
|
+
from idotmatrix.connection_manager import ConnectionManager
|
|
13
|
+
from idotmatrix.modules import IDotMatrixModule
|
|
14
|
+
from idotmatrix.screensize import ScreenSize
|
|
15
|
+
from idotmatrix.util import image_utils, color_utils
|
|
16
|
+
from idotmatrix.util.device_material import INDEX_SHOW_NOW, convert_display_time
|
|
17
|
+
|
|
18
|
+
MTU_SIZE_IF_ENABLED = 509
|
|
19
|
+
MTU_SIZE_IF_DISABLED = 18
|
|
20
|
+
CHUNK_SIZE_4096 = 4096
|
|
21
|
+
HEADER_SIZE_STATIC_IMAGE = 16
|
|
22
|
+
TYPE_STATIC_IMAGE = 2
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class ImageMode(Enum):
|
|
26
|
+
"""Enum for image modes."""
|
|
27
|
+
DisableDIY = 0 # Disable DIY mode
|
|
28
|
+
EnableDIY = 1 # Enable DIY mode
|
|
29
|
+
Unknown2 = 2 # Unknown mode 2
|
|
30
|
+
Unknown3 = 3 # Unknown mode 3
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class ImageModule(IDotMatrixModule):
|
|
34
|
+
logging = logging.getLogger(__name__)
|
|
35
|
+
|
|
36
|
+
def __init__(
|
|
37
|
+
self,
|
|
38
|
+
connection_manager: ConnectionManager,
|
|
39
|
+
screen_size: ScreenSize,
|
|
40
|
+
):
|
|
41
|
+
super().__init__(connection_manager=connection_manager)
|
|
42
|
+
self.screen_size = screen_size
|
|
43
|
+
|
|
44
|
+
async def set_mode(
|
|
45
|
+
self,
|
|
46
|
+
mode: ImageMode | int = ImageMode.EnableDIY,
|
|
47
|
+
):
|
|
48
|
+
"""
|
|
49
|
+
Enter the DIY draw mode of the iDotMatrix device.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
mode (int): 0 = disable DIY, 1 = enable DIY, 2 = ?, 3 = ?. Defaults to 1.
|
|
53
|
+
"""
|
|
54
|
+
if isinstance(mode, ImageMode):
|
|
55
|
+
mode = mode.value
|
|
56
|
+
|
|
57
|
+
data = bytearray([5, 0, 4, 1, mode % 256])
|
|
58
|
+
await self._send_bytes(data=data, response=True)
|
|
59
|
+
|
|
60
|
+
async def upload_image_file(
|
|
61
|
+
self,
|
|
62
|
+
file_path: PathLike | str,
|
|
63
|
+
resize_mode: image_utils.ResizeMode = image_utils.ResizeMode.FIT,
|
|
64
|
+
palletize: bool = False,
|
|
65
|
+
background_color: Tuple[int, int, int] | int | str = (0, 0, 0), # default to black background
|
|
66
|
+
) -> None:
|
|
67
|
+
"""
|
|
68
|
+
Uploads a file processed and makes sure everything is correct before uploading to the device.
|
|
69
|
+
|
|
70
|
+
Args:
|
|
71
|
+
file_path (str): path-like object to the image file
|
|
72
|
+
resize_mode (image_utils.ResizeMode): The mode to use for resizing the image (ResizeMode.FIT, ResizeMode.FILL, ResizeMode.STRETCH).
|
|
73
|
+
palletize (bool): If True, the image will be converted to a palette-based image. Usually bad for images with
|
|
74
|
+
high detail (like photos) but good for pixel-art or other content with high contrasts. Defaults to False.
|
|
75
|
+
background_color (Tuple[int, int, int]): RGB color for the background, which is only visible if the input
|
|
76
|
+
image doesn't match the devices aspect ratio. Defaults to black (0, 0, 0).
|
|
77
|
+
"""
|
|
78
|
+
background_color = color_utils.parse_color_rgb(background_color)
|
|
79
|
+
# reading and converting the file takes a while, the event loop must not be blocked
|
|
80
|
+
pixel_data = await asyncio.to_thread(
|
|
81
|
+
self._load_image_and_adapt_to_canvas,
|
|
82
|
+
file_path=file_path,
|
|
83
|
+
canvas_size=self.screen_size.value[0], # assuming square canvas, so width == height
|
|
84
|
+
resize_mode=resize_mode,
|
|
85
|
+
palletize=palletize,
|
|
86
|
+
background_color=background_color,
|
|
87
|
+
)
|
|
88
|
+
await self._send_diy_image_data(pixel_data)
|
|
89
|
+
|
|
90
|
+
@staticmethod
|
|
91
|
+
def _load_image_and_adapt_to_canvas(
|
|
92
|
+
file_path: PathLike | str,
|
|
93
|
+
canvas_size: int,
|
|
94
|
+
resize_mode: image_utils.ResizeMode,
|
|
95
|
+
palletize: bool,
|
|
96
|
+
background_color: Tuple[int, int, int],
|
|
97
|
+
) -> bytearray:
|
|
98
|
+
"""
|
|
99
|
+
Loads an image from a file, resizes it to fit within a square canvas of pixel_size x pixel_size,
|
|
100
|
+
and applies a black background if the image is smaller than the canvas size.
|
|
101
|
+
Args:
|
|
102
|
+
file_path (str): Path to the image file.
|
|
103
|
+
canvas_size (int): Size of the square canvas in pixels.
|
|
104
|
+
palletize (bool): If True, the image will be converted to a palette-based image.
|
|
105
|
+
background_color (Tuple[int, int, int]): RGB color for the background, which is only visible if the input
|
|
106
|
+
Returns:
|
|
107
|
+
bytearray: A bytearray containing the raw pixel data of the processed image in RGB format.
|
|
108
|
+
"""
|
|
109
|
+
if background_color is None or len(background_color) != 3:
|
|
110
|
+
raise ValueError("background_color must be a tuple of three integers (R, G, B)")
|
|
111
|
+
|
|
112
|
+
with PILImage.open(file_path) as img:
|
|
113
|
+
# rotate image based on EXIF data if available
|
|
114
|
+
img = ImageOps.exif_transpose(img)
|
|
115
|
+
|
|
116
|
+
# LANCZOS leads to a more pleasing result for images with high detail,
|
|
117
|
+
# NEAREST is better for pixel-art images, as it preserves the pixel structure.
|
|
118
|
+
resample_mode = PILImage.Resampling.NEAREST if palletize else PILImage.Resampling.LANCZOS
|
|
119
|
+
img = image_utils.resize_image(
|
|
120
|
+
image=img,
|
|
121
|
+
canvas_size=canvas_size,
|
|
122
|
+
resize_mode=resize_mode,
|
|
123
|
+
resample_mode=resample_mode,
|
|
124
|
+
mode="RGB", # ensure the image is in RGB mode
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
if palletize:
|
|
128
|
+
img = image_utils.palettize(img)
|
|
129
|
+
|
|
130
|
+
# Convert to RGB if not already in that mode, to get the pixel data in RGB format
|
|
131
|
+
mode = "RGB"
|
|
132
|
+
if img.mode != mode:
|
|
133
|
+
img = img.convert(mode)
|
|
134
|
+
|
|
135
|
+
png_buffer = bytearray(img.tobytes())
|
|
136
|
+
return png_buffer
|
|
137
|
+
|
|
138
|
+
async def show_image_file(
|
|
139
|
+
self,
|
|
140
|
+
file_path: PathLike | str,
|
|
141
|
+
resize_mode: image_utils.ResizeMode = image_utils.ResizeMode.FIT,
|
|
142
|
+
palletize: bool = False,
|
|
143
|
+
background_color: Tuple[int, int, int] | int | str = (0, 0, 0),
|
|
144
|
+
png: bool = False,
|
|
145
|
+
) -> None:
|
|
146
|
+
"""
|
|
147
|
+
Shows an image file on the device, the way the official app does it for photos.
|
|
148
|
+
|
|
149
|
+
In contrast to `upload_image_file` this does not need `set_mode`, and it can be mixed with
|
|
150
|
+
GIF uploads without any mode switching in between.
|
|
151
|
+
|
|
152
|
+
Args:
|
|
153
|
+
file_path (str): path-like object to the image file
|
|
154
|
+
resize_mode (image_utils.ResizeMode): The mode to use for resizing the image.
|
|
155
|
+
palletize (bool): If True, the image will be converted to a palette-based image. Usually bad for images with
|
|
156
|
+
high detail (like photos) but good for pixel-art or other content with high contrasts. Defaults to False.
|
|
157
|
+
background_color (Tuple[int, int, int]): RGB color for the background, which is only visible if the input
|
|
158
|
+
image doesn't match the devices aspect ratio. Defaults to black (0, 0, 0).
|
|
159
|
+
png (bool): If True, the image is sent as a PNG file instead of raw RGB data. That is smaller (about 25-45%
|
|
160
|
+
for photos, more for pixel-art, and a palette PNG with `palletize`), which saves a little upload time.
|
|
161
|
+
Experimental: tested on a 64x64 device only, the official app sends raw RGB. Defaults to False.
|
|
162
|
+
"""
|
|
163
|
+
background_color = color_utils.parse_color_rgb(background_color)
|
|
164
|
+
# reading and converting the file takes a while, the event loop must not be blocked
|
|
165
|
+
pixel_data = await asyncio.to_thread(
|
|
166
|
+
self._load_image_and_adapt_to_canvas,
|
|
167
|
+
file_path=file_path,
|
|
168
|
+
canvas_size=self.screen_size.value[0], # assuming square canvas, so width == height
|
|
169
|
+
resize_mode=resize_mode,
|
|
170
|
+
palletize=palletize,
|
|
171
|
+
background_color=background_color,
|
|
172
|
+
)
|
|
173
|
+
await self._send_static_image_data(pixel_data, png=png, palette=palletize)
|
|
174
|
+
|
|
175
|
+
async def show_image_pixeldata(
|
|
176
|
+
self,
|
|
177
|
+
pixel_colors: List[Tuple[int, int, int] | int | str],
|
|
178
|
+
png: bool = False,
|
|
179
|
+
) -> None:
|
|
180
|
+
"""
|
|
181
|
+
Shows pixel data on the device, the way the official app does it for photos. See `show_image_file`.
|
|
182
|
+
Args:
|
|
183
|
+
pixel_colors: One color for each pixel of the screen, row by row.
|
|
184
|
+
png (bool): If True, the pixels are sent as a PNG file. See `show_image_file`.
|
|
185
|
+
"""
|
|
186
|
+
await self._send_static_image_data(self._pixel_colors_to_bytes(pixel_colors), png=png)
|
|
187
|
+
|
|
188
|
+
def _encode_png(self, pixel_data: bytearray, palette: bool = False) -> bytearray:
|
|
189
|
+
"""
|
|
190
|
+
Encodes raw RGB pixel data of the whole screen as a PNG file.
|
|
191
|
+
|
|
192
|
+
Args:
|
|
193
|
+
pixel_data: RGB bytes, row by row.
|
|
194
|
+
palette: If True, the PNG uses a palette of up to 256 colors, which is smaller, but changes the colors of
|
|
195
|
+
images with more than 256 of them.
|
|
196
|
+
"""
|
|
197
|
+
size = self.screen_size.value[0]
|
|
198
|
+
img = PILImage.frombytes("RGB", (size, size), bytes(pixel_data))
|
|
199
|
+
if palette:
|
|
200
|
+
img = img.quantize(256, dither=PILImage.Dither.NONE)
|
|
201
|
+
buffer = io.BytesIO()
|
|
202
|
+
img.save(buffer, "PNG", optimize=True)
|
|
203
|
+
return bytearray(buffer.getvalue())
|
|
204
|
+
|
|
205
|
+
async def _send_static_image_data(self, pixel_data: bytearray, png: bool = False, palette: bool = False) -> None:
|
|
206
|
+
if png:
|
|
207
|
+
pixel_data = self._encode_png(pixel_data, palette=palette)
|
|
208
|
+
packets = self._create_static_image_data_packets(pixel_data)
|
|
209
|
+
await self._send_packets(packets, response=True, wait_for_ack=True)
|
|
210
|
+
|
|
211
|
+
def _create_static_image_data_packets(
|
|
212
|
+
self,
|
|
213
|
+
image_data: bytearray,
|
|
214
|
+
index: int = INDEX_SHOW_NOW,
|
|
215
|
+
time_sign: int = 0,
|
|
216
|
+
) -> List[List[bytearray]]:
|
|
217
|
+
"""
|
|
218
|
+
Creates the packets of a static image upload. This corresponds to `sendImageData` of the official app,
|
|
219
|
+
which looks like `sendDIYImageData` with a longer header, that contains a CRC32 and the index of the image.
|
|
220
|
+
|
|
221
|
+
Returns:
|
|
222
|
+
A list of lists of byte arrays. The outer list represents the "4K chunks"
|
|
223
|
+
and the inner lists contain the actual BLE packets for each 4K chunk.
|
|
224
|
+
"""
|
|
225
|
+
total_length = len(image_data).to_bytes(4, "little")
|
|
226
|
+
crc32 = (binascii.crc32(image_data) & 0xFFFFFFFF).to_bytes(4, "little")
|
|
227
|
+
|
|
228
|
+
packets = []
|
|
229
|
+
for i, chunk in enumerate(self.chunk_data_by_size(image_data, CHUNK_SIZE_4096)):
|
|
230
|
+
header = bytearray(HEADER_SIZE_STATIC_IMAGE)
|
|
231
|
+
header[0:2] = (len(chunk) + HEADER_SIZE_STATIC_IMAGE).to_bytes(2, "little")
|
|
232
|
+
header[2] = TYPE_STATIC_IMAGE
|
|
233
|
+
header[3] = 0
|
|
234
|
+
header[4] = 2 if i > 0 else 0 # continuation / first chunk
|
|
235
|
+
header[5:9] = total_length
|
|
236
|
+
header[9:13] = crc32
|
|
237
|
+
header[13:15] = convert_display_time(time_sign).to_bytes(2, "big")
|
|
238
|
+
header[15] = index
|
|
239
|
+
|
|
240
|
+
packets.append(self._create_ble_packets(bytes(header) + chunk))
|
|
241
|
+
return packets
|
|
242
|
+
|
|
243
|
+
async def upload_image_pixeldata(
|
|
244
|
+
self,
|
|
245
|
+
pixel_colors: List[Tuple[int, int, int] | int | str],
|
|
246
|
+
) -> None:
|
|
247
|
+
"""
|
|
248
|
+
Uploads pixel data to the iDotMatrix device.
|
|
249
|
+
Args:
|
|
250
|
+
pixel_colors (List[Tuple[int, int, int]]): List of tuples representing RGB pixel values.
|
|
251
|
+
Each tuple should contain three integers (R, G, B) in the range 0-255.
|
|
252
|
+
The length of this list must match the square of the screen size (pixel_size * pixel_size).
|
|
253
|
+
"""
|
|
254
|
+
pixel_data = self._pixel_colors_to_bytes(pixel_colors)
|
|
255
|
+
|
|
256
|
+
return await self._send_diy_image_data(pixel_data)
|
|
257
|
+
|
|
258
|
+
def _pixel_colors_to_bytes(self, pixel_colors: List[Tuple[int, int, int] | int | str]) -> bytearray:
|
|
259
|
+
pixel_size = self.screen_size.value[0] # assuming square canvas, so width == height
|
|
260
|
+
if len(pixel_colors) != pixel_size * pixel_size:
|
|
261
|
+
raise ValueError(
|
|
262
|
+
f"pixel_data must contain exactly {pixel_size * pixel_size} pixels (quared pixel_size), got: {len(pixel_colors)}"
|
|
263
|
+
)
|
|
264
|
+
|
|
265
|
+
# parse the pixel_colors to ensure they are in RGB format
|
|
266
|
+
pixel_colors = color_utils.parse_color_rgb_list(pixel_colors)
|
|
267
|
+
|
|
268
|
+
# Convert pixel_data to a bytearray in RGB format
|
|
269
|
+
pixel_data = bytearray()
|
|
270
|
+
for r, g, b in pixel_colors:
|
|
271
|
+
if not (0 <= r <= 255 and 0 <= g <= 255 and 0 <= b <= 255):
|
|
272
|
+
raise ValueError(f"Invalid RGB value: ({r}, {g}, {b}). Values must be between 0 and 255.")
|
|
273
|
+
pixel_data.extend([r, g, b])
|
|
274
|
+
return pixel_data
|
|
275
|
+
|
|
276
|
+
async def _send_diy_image_data(
|
|
277
|
+
self, pixel_data: bytearray,
|
|
278
|
+
) -> None:
|
|
279
|
+
packets = self._create_diy_image_data_packets(pixel_data)
|
|
280
|
+
await self._send_packets(packets, response=True, wait_for_ack=True)
|
|
281
|
+
|
|
282
|
+
@staticmethod
|
|
283
|
+
def _short_to_bytes_le(value: int) -> bytes:
|
|
284
|
+
"""Converts a short (2 bytes) to little-endian bytes."""
|
|
285
|
+
return struct.pack('<h', value)
|
|
286
|
+
|
|
287
|
+
@staticmethod
|
|
288
|
+
def _int_to_bytes_le(value: int) -> bytes:
|
|
289
|
+
"""Converts an int (4 bytes) to little-endian bytes."""
|
|
290
|
+
return struct.pack('<i', value)
|
|
291
|
+
|
|
292
|
+
@staticmethod
|
|
293
|
+
def chunk_data_by_size(data: bytearray | bytes, chunk_size: int) -> List[bytearray]:
|
|
294
|
+
"""
|
|
295
|
+
Chunks the input data into smaller pieces of a specified size.
|
|
296
|
+
|
|
297
|
+
Args:
|
|
298
|
+
data: The byte array to be chunked.
|
|
299
|
+
chunk_size: The maximum size of each chunk.
|
|
300
|
+
|
|
301
|
+
Returns:
|
|
302
|
+
A list of byte arrays, where each is a chunk of the original data.
|
|
303
|
+
"""
|
|
304
|
+
chunks = []
|
|
305
|
+
for i in range(0, len(data), chunk_size):
|
|
306
|
+
chunks.append(data[i:i + chunk_size])
|
|
307
|
+
return chunks
|
|
308
|
+
|
|
309
|
+
def _create_ble_packets(self, data_chunk, ble_device_mtu_enabled=True):
|
|
310
|
+
"""
|
|
311
|
+
Splits a data chunk into smaller BLE packets based on MTU size.
|
|
312
|
+
This corresponds to the inner chunking logic in `getSendData`.
|
|
313
|
+
|
|
314
|
+
Args:
|
|
315
|
+
data_chunk: A byte array representing a single "large" chunk (e.g., 4K).
|
|
316
|
+
ble_device_mtu_enabled: Boolean indicating if MTU is enabled on the BLE device.
|
|
317
|
+
|
|
318
|
+
Returns:
|
|
319
|
+
A list of byte arrays, where each is a BLE packet.
|
|
320
|
+
"""
|
|
321
|
+
mtu = MTU_SIZE_IF_ENABLED if ble_device_mtu_enabled else MTU_SIZE_IF_DISABLED
|
|
322
|
+
return self.chunk_data_by_size(data_chunk, mtu)
|
|
323
|
+
|
|
324
|
+
def _create_diy_image_data_packets(
|
|
325
|
+
self,
|
|
326
|
+
image_data: bytearray,
|
|
327
|
+
ble_device_mtu_enabled=True
|
|
328
|
+
) -> List[List[bytearray]]:
|
|
329
|
+
"""
|
|
330
|
+
Recreates the sendData3 structure for DIY image data.
|
|
331
|
+
This corresponds to the logic in `sendDIYImageData`.
|
|
332
|
+
|
|
333
|
+
Args:
|
|
334
|
+
image_data: The raw byte array of the RGB image data.
|
|
335
|
+
ble_device_mtu_enabled: Boolean indicating if MTU is enabled on the BLE device.
|
|
336
|
+
|
|
337
|
+
Returns:
|
|
338
|
+
A list of lists of byte arrays. The outer list represents the "4K chunks"
|
|
339
|
+
and the inner lists contain the actual BLE packets for each 4K chunk.
|
|
340
|
+
"""
|
|
341
|
+
send_data_3 = []
|
|
342
|
+
|
|
343
|
+
image_data_len_bytes = self._int_to_bytes_le(len(image_data))
|
|
344
|
+
|
|
345
|
+
# 1. Chunk the image_data into 4096-byte chunks (or smaller for the last one)
|
|
346
|
+
# This corresponds to `getSendData4096`
|
|
347
|
+
chunks_4096 = self.chunk_data_by_size(image_data, CHUNK_SIZE_4096)
|
|
348
|
+
|
|
349
|
+
# 2. Process each 4096-byte chunk to create the "large packet" structure
|
|
350
|
+
# This corresponds to the loop creating `arrayList` in `sendDIYImageData`
|
|
351
|
+
processed_large_packets = []
|
|
352
|
+
for i, chunk in enumerate(chunks_4096):
|
|
353
|
+
packet_length = len(chunk) + 9 # 9 bytes for the header
|
|
354
|
+
packet_length_bytes = self._short_to_bytes_le(packet_length)
|
|
355
|
+
|
|
356
|
+
# Construct the header for this large packet
|
|
357
|
+
header = bytearray(9)
|
|
358
|
+
header[0] = packet_length_bytes[0] # Little-endian short
|
|
359
|
+
header[1] = packet_length_bytes[1]
|
|
360
|
+
header[2] = 0 # Command or type
|
|
361
|
+
header[3] = 0 # Sub-command or subtype
|
|
362
|
+
|
|
363
|
+
if i > 0:
|
|
364
|
+
header[4] = 2 # Continuation packet
|
|
365
|
+
else:
|
|
366
|
+
header[4] = 0 # First packet
|
|
367
|
+
|
|
368
|
+
header[5] = image_data_len_bytes[0] # Total image data length (little-endian int)
|
|
369
|
+
header[6] = image_data_len_bytes[1]
|
|
370
|
+
header[7] = image_data_len_bytes[2]
|
|
371
|
+
header[8] = image_data_len_bytes[3]
|
|
372
|
+
|
|
373
|
+
# Combine header and the current 4096B chunk data
|
|
374
|
+
large_packet_data = bytes(header) + chunk
|
|
375
|
+
processed_large_packets.append(large_packet_data)
|
|
376
|
+
|
|
377
|
+
# 3. Split each "large packet" into smaller BLE packets
|
|
378
|
+
# This corresponds to the loop calling `getSendData` and adding to `sendData3`
|
|
379
|
+
for i, large_packet in enumerate(processed_large_packets):
|
|
380
|
+
ble_packets_for_chunk = self._create_ble_packets(large_packet, ble_device_mtu_enabled)
|
|
381
|
+
send_data_3.append(ble_packets_for_chunk)
|
|
382
|
+
|
|
383
|
+
return send_data_3
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
from idotmatrix import protocol
|
|
4
|
+
from idotmatrix.modules import IDotMatrixModule
|
|
5
|
+
from idotmatrix.protocol import build_command
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class MusicSyncModule(IDotMatrixModule):
|
|
9
|
+
logging = logging.getLogger(__name__)
|
|
10
|
+
|
|
11
|
+
async def set_mic_type(self, type: int, sensitivity: int = 50):
|
|
12
|
+
"""
|
|
13
|
+
Switches the device to music sync with its built-in microphone, which then reacts to sound on its own,
|
|
14
|
+
without any audio data being sent. This is what the "microphone" screen of the iDotMatrix Android App does
|
|
15
|
+
(`BleProtocolN.sendMicCommand1`, which sends the 1-based mode and the sensitivity).
|
|
16
|
+
|
|
17
|
+
Tested on a 64x64: modes 1 to 5 show different scenes (a figure, a heart, two frog-like faces, ...) that move
|
|
18
|
+
to sound and knocks. There is no command to leave the mode, show something else (f.e. the clock) instead.
|
|
19
|
+
|
|
20
|
+
Args:
|
|
21
|
+
type (int): The scene, starting at 1.
|
|
22
|
+
sensitivity (int): How sensitive the microphone is, 0 to 100. Defaults to 50, like the app.
|
|
23
|
+
"""
|
|
24
|
+
if not 0 <= sensitivity <= 100:
|
|
25
|
+
raise ValueError("sensitivity must be between 0 and 100")
|
|
26
|
+
data = build_command(protocol.MICROPHONE_MODE, type % 256, sensitivity)
|
|
27
|
+
await self._send_bytes(data=data)
|
|
28
|
+
|
|
29
|
+
async def send_image_rythm(self, value1: int):
|
|
30
|
+
"""
|
|
31
|
+
Shows one frame of the "image rhythm" animation. The Android App sends this about every 60 ms with the loudness
|
|
32
|
+
(1 to 7) of the sound its microphone records, so the animation only moves if it is sent over and over with
|
|
33
|
+
changing values. Sent once it just shows a still frame. To let the device react to sound on its own use
|
|
34
|
+
`set_mic_type`.
|
|
35
|
+
|
|
36
|
+
Args:
|
|
37
|
+
value1 (int): The loudness, 1 to 7 in the App. Larger values were tested up to 10 and show other frames.
|
|
38
|
+
"""
|
|
39
|
+
data = build_command(protocol.IMAGE_RHYTHM, value1 % 256, 1)
|
|
40
|
+
await self._send_bytes(data=data)
|
|
41
|
+
|
|
42
|
+
async def send_rhythm(
|
|
43
|
+
self, mode: int, byteArray: bytearray
|
|
44
|
+
):
|
|
45
|
+
"""
|
|
46
|
+
Used to send synchronized Microphone sound data to the device and visualizing it. Is handled in MicrophoneActivity.java of the
|
|
47
|
+
iDotMatrix Android App. Will not be implemented here because there are no plans to support the computer microphone. The device
|
|
48
|
+
has an integrated microphone which is able to react to sound.
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
mode (int): mode of the rhythm.
|
|
52
|
+
byteArray (bytearray): actual microphone sound data for the visualization.
|
|
53
|
+
"""
|
|
54
|
+
# Assuming `mode` is intended to be used in future or within `byteArray` preparation.
|
|
55
|
+
data = byteArray
|
|
56
|
+
await self._send_bytes(data=data)
|
|
57
|
+
|
|
58
|
+
async def stop_rythm(self):
|
|
59
|
+
"""
|
|
60
|
+
Stops the Microphone Rhythm on the iDotMatrix device. Tested on a 64x64: this does not leave the animation that
|
|
61
|
+
`set_mic_type` or `send_image_rythm` started, show something else (f.e. the clock) to leave it.
|
|
62
|
+
|
|
63
|
+
Returns:
|
|
64
|
+
bytearray: Byte array of the command which needs to be sent to the device.
|
|
65
|
+
"""
|
|
66
|
+
data = build_command(protocol.IMAGE_RHYTHM, 0, 0)
|
|
67
|
+
await self._send_bytes(data=data)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import struct
|
|
3
|
+
|
|
4
|
+
from idotmatrix import protocol
|
|
5
|
+
from idotmatrix.modules import IDotMatrixModule
|
|
6
|
+
from idotmatrix.protocol import build_command
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ScoreboardModule(IDotMatrixModule):
|
|
10
|
+
"""This class contains the Scoreboard management of the iDotMatrix device."""
|
|
11
|
+
|
|
12
|
+
logging = logging.getLogger(__name__)
|
|
13
|
+
|
|
14
|
+
async def show(self, count1: int, count2: int):
|
|
15
|
+
"""
|
|
16
|
+
Set the scoreboard of the device.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
count1 (int): first counter, max: 999 (buffer overflow if more! -> might lead to unintended behavior)
|
|
20
|
+
count2 (int): second counter, max: 999 (buffer overflow if more! -> might lead to unintended behavior)
|
|
21
|
+
"""
|
|
22
|
+
# Packing counts into two bytes (big-endian) each, assuming the counts are not exceeding 16-bit max value.
|
|
23
|
+
bytearray_count1 = struct.pack(
|
|
24
|
+
"!H", max(0, min(999, count1))
|
|
25
|
+
) # Clamping the value to be between 0 and 999
|
|
26
|
+
bytearray_count2 = struct.pack(
|
|
27
|
+
"!H", max(0, min(999, count2))
|
|
28
|
+
) # Clamping the value to be between 0 and 999
|
|
29
|
+
|
|
30
|
+
# Preparing the data to be sent, adjusting the indices according to the byte order.
|
|
31
|
+
data = build_command(
|
|
32
|
+
protocol.SCOREBOARD,
|
|
33
|
+
bytearray_count1[1], # Lower byte of count1
|
|
34
|
+
bytearray_count1[0], # Upper byte of count1
|
|
35
|
+
bytearray_count2[1], # Lower byte of count2
|
|
36
|
+
bytearray_count2[0], # Upper byte of count2
|
|
37
|
+
)
|
|
38
|
+
await self._send_bytes(data=data)
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
from cryptography.fernet import Fernet
|
|
4
|
+
|
|
5
|
+
from idotmatrix import protocol
|
|
6
|
+
from idotmatrix.modules import IDotMatrixModule
|
|
7
|
+
from idotmatrix.protocol import build_command
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class SystemModule(IDotMatrixModule):
|
|
11
|
+
"""This class contains system calls for the iDotMatrix device."""
|
|
12
|
+
|
|
13
|
+
logging = logging.getLogger(__name__)
|
|
14
|
+
|
|
15
|
+
async def delete_device_data(self):
|
|
16
|
+
"""
|
|
17
|
+
Deletes the device data and resets it to defaults.
|
|
18
|
+
"""
|
|
19
|
+
data = build_command(protocol.DELETE_MATERIALS, 12, 0, *range(1, 12))
|
|
20
|
+
await self._send_bytes(data=data)
|
|
21
|
+
|
|
22
|
+
@staticmethod
|
|
23
|
+
def _encrypt_aes(data: bytes, key: bytes) -> bytes:
|
|
24
|
+
"""
|
|
25
|
+
Encrypts data using AES encryption with the given key.
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
data (bytes): Data to be encrypted.
|
|
29
|
+
key (bytes): Encryption key.
|
|
30
|
+
|
|
31
|
+
Returns:
|
|
32
|
+
bytes: Encrypted data.
|
|
33
|
+
"""
|
|
34
|
+
f = Fernet(key)
|
|
35
|
+
encrypted_data = f.encrypt(data)
|
|
36
|
+
return encrypted_data
|
|
37
|
+
|
|
38
|
+
async def get_device_location(self):
|
|
39
|
+
"""
|
|
40
|
+
Gets the device location (untested yet). Missing some AES encryption stuff of iDotMatrix to work.
|
|
41
|
+
"""
|
|
42
|
+
# TODO: implement Aes encryption according to iDotMatrix Android App
|
|
43
|
+
command = bytearray(
|
|
44
|
+
[
|
|
45
|
+
6,
|
|
46
|
+
76,
|
|
47
|
+
79,
|
|
48
|
+
67,
|
|
49
|
+
65,
|
|
50
|
+
84,
|
|
51
|
+
69,
|
|
52
|
+
0,
|
|
53
|
+
0,
|
|
54
|
+
0,
|
|
55
|
+
0,
|
|
56
|
+
0,
|
|
57
|
+
0,
|
|
58
|
+
0,
|
|
59
|
+
0,
|
|
60
|
+
0,
|
|
61
|
+
]
|
|
62
|
+
)
|
|
63
|
+
key = Fernet.generate_key()
|
|
64
|
+
data = self._encrypt_aes(bytes(command), key)
|
|
65
|
+
await self._send_bytes(data=data)
|