idotmatrix-api-client 0.2.2__tar.gz → 0.2.3__tar.gz

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.
Files changed (65) hide show
  1. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/PKG-INFO +4 -1
  2. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/README.md +3 -0
  3. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/client.py +9 -0
  4. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/connection_manager.py +23 -6
  5. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/modules/gif.py +20 -8
  6. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/modules/image.py +31 -6
  7. idotmatrix_api_client-0.2.3/idotmatrix/modules/materials.py +120 -0
  8. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/util/command_ack.py +1 -0
  9. idotmatrix_api_client-0.2.3/idotmatrix/util/device_material.py +36 -0
  10. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/pyproject.toml +1 -1
  11. idotmatrix_api_client-0.2.2/idotmatrix/util/device_material.py +0 -16
  12. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/LICENSE +0 -0
  13. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/__init__.py +0 -0
  14. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/apps/__init__.py +0 -0
  15. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/apps/digital_picture_frame.py +0 -0
  16. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/const.py +0 -0
  17. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/16-ErbosDraco1StOpenNbpRegular-l5wX.ttf +0 -0
  18. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/16-MinecraftStandard.otf +0 -0
  19. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/16-MinecraftStandardBoldOblique.otf +0 -0
  20. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/16-PixelOperator8-Bold.ttf +0 -0
  21. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/16-PixelOperator8.ttf +0 -0
  22. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/16-PixelifySans-Regular.ttf +0 -0
  23. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/16-PressStart2P-Regular.ttf +0 -0
  24. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/16-dogica.ttf +0 -0
  25. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/16-dogicabold.ttf +0 -0
  26. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/16-dogicapixel.ttf +0 -0
  27. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/16-dogicapixelbold.ttf +0 -0
  28. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/16_32-HomeVideo-BLG6G.ttf +0 -0
  29. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/16_32-HomeVideoBold-R90Dv.ttf +0 -0
  30. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/16_32-MayfairNbpBold-gAA4.ttf +0 -0
  31. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/32-Jersey10-Regular.ttf +0 -0
  32. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/CC-BY-SA-4.0-License.txt +0 -0
  33. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/ErbosDraco-License.txt +0 -0
  34. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/HomeVideo-License.txt +0 -0
  35. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/Jersey10-Regular-License.txt +0 -0
  36. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/MayfairNbpBold-License.txt +0 -0
  37. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/MinecraftStandard-License.txt +0 -0
  38. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/PixelOperator-License.txt +0 -0
  39. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/PixelifySans-License.txt +0 -0
  40. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/PressStart2P-Regular-License.txt +0 -0
  41. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/Rain-DRM3-License.txt +0 -0
  42. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/Rain-DRM3.otf +0 -0
  43. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/fonts/dogica_license.txt +0 -0
  44. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/modules/__init__.py +0 -0
  45. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/modules/chronograph.py +0 -0
  46. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/modules/clock.py +0 -0
  47. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/modules/common.py +0 -0
  48. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/modules/countdown.py +0 -0
  49. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/modules/eco.py +0 -0
  50. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/modules/effect.py +0 -0
  51. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/modules/fullscreen_color.py +0 -0
  52. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/modules/graffiti.py +0 -0
  53. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/modules/music_sync.py +0 -0
  54. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/modules/scoreboard.py +0 -0
  55. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/modules/system.py +0 -0
  56. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/modules/text.py +0 -0
  57. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/protocol.py +0 -0
  58. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/screensize.py +0 -0
  59. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/transport.py +0 -0
  60. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/util/__init__.py +0 -0
  61. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/util/color_utils.py +0 -0
  62. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/util/device_info.py +0 -0
  63. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/util/file_watch.py +0 -0
  64. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/util/image_utils.py +0 -0
  65. {idotmatrix_api_client-0.2.2 → idotmatrix_api_client-0.2.3}/idotmatrix/util/upload_ack.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: idotmatrix-api-client
3
- Version: 0.2.2
3
+ Version: 0.2.3
4
4
  Summary: A library for controlling iDotMatrix displays.
5
5
  License-Expression: GPL-3.0-or-later
6
6
  License-File: LICENSE
@@ -151,6 +151,9 @@ await client.image.show_image_file("photo.jpg", resize_mode=ResizeMode.FILL)
151
151
 
152
152
  # show an animated GIF
153
153
  await client.gif.upload_gif_file("animation.gif")
154
+
155
+ # store files in the slots of the device, which then cycles through them on its own (no uploads during the show)
156
+ await client.materials.store(["a.gif", "b.png", "c.gif"], slots=[1, 2, 3], display_time=30)
154
157
  ```
155
158
 
156
159
  Both can be mixed freely, no mode switching is needed in between. They work like the official app, which waits
@@ -133,6 +133,9 @@ await client.image.show_image_file("photo.jpg", resize_mode=ResizeMode.FILL)
133
133
 
134
134
  # show an animated GIF
135
135
  await client.gif.upload_gif_file("animation.gif")
136
+
137
+ # store files in the slots of the device, which then cycles through them on its own (no uploads during the show)
138
+ await client.materials.store(["a.gif", "b.png", "c.gif"], slots=[1, 2, 3], display_time=30)
136
139
  ```
137
140
 
138
141
  Both can be mixed freely, no mode switching is needed in between. They work like the official app, which waits
@@ -15,6 +15,7 @@ from idotmatrix.modules.fullscreen_color import FullscreenColorModule
15
15
  from idotmatrix.modules.gif import GifModule
16
16
  from idotmatrix.modules.graffiti import GraffitiModule
17
17
  from idotmatrix.modules.image import ImageModule
18
+ from idotmatrix.modules.materials import MaterialsModule
18
19
  from idotmatrix.modules.music_sync import MusicSyncModule
19
20
  from idotmatrix.modules.scoreboard import ScoreboardModule
20
21
  from idotmatrix.modules.system import SystemModule
@@ -152,6 +153,14 @@ class IDotMatrixClient:
152
153
  screen_size=self.screen_size
153
154
  )
154
155
 
156
+ @cached_property
157
+ def materials(self) -> MaterialsModule:
158
+ return MaterialsModule(
159
+ connection_manager=self._connection_manager,
160
+ gif=self.gif,
161
+ image=self.image,
162
+ )
163
+
155
164
  @cached_property
156
165
  def music_sync(self) -> MusicSyncModule:
157
166
  return MusicSyncModule(
@@ -462,20 +462,33 @@ class ConnectionManager:
462
462
  f"sending {len(packets)} packet(s) in chunks of size {len(packets[0][0])} bytes to device, for a total size of {total_byte_count} bytes"
463
463
  )
464
464
 
465
- # All but the last BLE packet of each packet are sent without response,
465
+ wait_for_ack = wait_for_ack and self._notifications_available
466
+ # Uploads write every BLE packet with response, like the official app: each write is answered by the device,
467
+ # so the Bluetooth stack (f.e. an ESPHome proxy) cannot drop packets of a burst, which the device answers
468
+ # by never acknowledging the chunk or by rebooting.
469
+ every_packet_with_response = wait_for_ack and response
470
+
471
+ # Otherwise all but the last BLE packet of each packet are sent without response,
466
472
  # so every one of them has to fit into a single write-without-response.
467
- ble_packet_size = await self.get_max_bytes_per_chunk(response=False)
473
+ ble_packet_size = await self.get_max_bytes_per_chunk(response=every_packet_with_response)
474
+ if every_packet_with_response:
475
+ ble_packet_size = min(ble_packet_size, await self.get_max_bytes_per_chunk(response=False))
468
476
  self.logging.debug(f"ble_packet_size size is {ble_packet_size} bytes")
469
477
  packets = self._split_oversized_ble_packets(packets, ble_packet_size)
470
478
 
471
- wait_for_ack = wait_for_ack and self._notifications_available
472
479
  if wait_for_ack:
473
480
  # leftovers of earlier commands must not be taken for the acknowledgement of the first packet
474
481
  self._notifications = asyncio.Queue()
475
482
 
476
483
  for i, packet in enumerate(packets):
477
484
  deadline = asyncio.get_running_loop().time() + UPLOAD_ACK_TIMEOUT_SECONDS
478
- await self._write_chunk(packet, response, paced=wait_for_ack, label=f"{i + 1} of {len(packets)}")
485
+ await self._write_chunk(
486
+ packet,
487
+ response,
488
+ all_with_response=every_packet_with_response,
489
+ paced=wait_for_ack and not every_packet_with_response,
490
+ label=f"{i + 1} of {len(packets)}",
491
+ )
479
492
  if wait_for_ack:
480
493
  status = await self._wait_for_upload_status(deadline)
481
494
  if status == UploadStatus.FINISHED and i < len(packets) - 1:
@@ -483,12 +496,15 @@ class ConnectionManager:
483
496
  self.logging.debug("the device reports the upload as finished, skipping the remaining packets")
484
497
  return
485
498
 
486
- async def _write_chunk(self, packet: List[bytearray | bytes], response: bool, paced: bool, label: str):
499
+ async def _write_chunk(
500
+ self, packet: List[bytearray | bytes], response: bool, all_with_response: bool, paced: bool, label: str
501
+ ):
487
502
  """
488
503
  Writes the BLE packets of one packet. Only the last one is written with a response, if requested.
489
504
  Args:
490
505
  packet: The BLE packets to write.
491
506
  response: If True, the last BLE packet is written with response.
507
+ all_with_response: If True, every BLE packet is written with response.
492
508
  paced: If True, the writes are spaced `UPLOAD_WRITE_INTERVAL_SECONDS` apart.
493
509
  label: Describes the packet in the log.
494
510
  """
@@ -496,7 +512,8 @@ class ConnectionManager:
496
512
  if paced and j > 0:
497
513
  await asyncio.sleep(UPLOAD_WRITE_INTERVAL_SECONDS)
498
514
  self.logging.debug(f"sending packet {label}.{j + 1} of {len(packet)}")
499
- await self._transport.write(ble_packet, response=response if j == len(packet) - 1 else False)
515
+ with_response = all_with_response or (response and j == len(packet) - 1)
516
+ await self._transport.write(ble_packet, response=with_response)
500
517
 
501
518
  @staticmethod
502
519
  def _split_oversized_ble_packets(
@@ -13,7 +13,9 @@ from idotmatrix.modules import IDotMatrixModule
13
13
  from idotmatrix.protocol import build_command
14
14
  from idotmatrix.screensize import ScreenSize
15
15
  from idotmatrix.util import image_utils, color_utils
16
- from idotmatrix.util.device_material import INDEX_SHOW_NOW, convert_display_time
16
+ from idotmatrix.util.device_material import (
17
+ DEFAULT_DISPLAY_TIME_SECONDS, INDEX_SHOW_NOW, convert_display_time, validate_display_time, validate_slot,
18
+ )
17
19
  from idotmatrix.util.image_utils import ResizeMode
18
20
 
19
21
  ANIMATION_MAX_FRAME_COUNT = 64 # Maximum number of frames in a GIF animation
@@ -47,9 +49,11 @@ class GifModule(IDotMatrixModule):
47
49
  palletize: bool = True,
48
50
  background_color: Tuple[int, int, int] | int | str = (0, 0, 0),
49
51
  duration_per_frame_in_ms: int = None,
52
+ slot: int | None = None,
53
+ display_time: int = DEFAULT_DISPLAY_TIME_SECONDS,
50
54
  ):
51
55
  """
52
- Uploads a GIF file to the device.
56
+ Uploads a GIF file to the device. It is shown right away, unless a slot is given.
53
57
 
54
58
  Args:
55
59
  file_path (str): path to the image file
@@ -58,7 +62,13 @@ class GifModule(IDotMatrixModule):
58
62
  high detail (like photos) but good for pixel-art or other content with high contrasts. Defaults to True.
59
63
  background_color (Tuple[int, int, int]): RGB color to fill transparent pixels. Defaults to black (0, 0, 0).
60
64
  duration_per_frame_in_ms (int, optional): Duration of each frame in milliseconds. If not provided, defaults to the duration specified in the GIF file, or 200ms if not set.
65
+ slot (int, optional): If given (0 to 11), the GIF is stored in that slot of the device instead of being shown
66
+ right away. The device cycles through the stored items by itself, see `MaterialsModule`.
67
+ display_time (int): Seconds the stored item stays on screen in that cycle. Only used with a slot.
61
68
  """
69
+ if slot is not None:
70
+ validate_slot(slot)
71
+ validate_display_time(display_time)
62
72
  screen_width = self.screen_size.value[0] # assuming square canvas, so width == height
63
73
  background_color = color_utils.parse_color_rgb(background_color)
64
74
 
@@ -82,8 +92,9 @@ class GifModule(IDotMatrixModule):
82
92
  # this might be the index that this GIF will be stored within the device's memory :think:
83
93
  # it doesn't seem to have an effect when sending a single GIF like it is done here though
84
94
  # shows the GIF right away, like the official app does
85
- gif_type=INDEX_SHOW_NOW,
95
+ gif_type=INDEX_SHOW_NOW if slot is None else slot,
86
96
  time_sign=0,
97
+ display_time=None if slot is None else display_time,
87
98
  )
88
99
  await self._send_packets(packets=packets, response=True, wait_for_ack=True)
89
100
 
@@ -208,7 +219,8 @@ class GifModule(IDotMatrixModule):
208
219
  gif_data: bytes,
209
220
  gif_type: int,
210
221
  time_sign: int, # Assuming this is the raw time signature before DeviceMaterialTimeConvert.ConvertTime
211
- ble_device_mtu_enabled: bool = True
222
+ ble_device_mtu_enabled: bool = True,
223
+ display_time: int | None = None,
212
224
  ) -> list[list[bytearray]]:
213
225
  """
214
226
  Creates packets for sending GIF data, mirroring the Java GifAgreement logic.
@@ -217,6 +229,7 @@ class GifModule(IDotMatrixModule):
217
229
  gif_data: The raw byte array of the GIF data.
218
230
  gif_type: The type parameter (e.g., 12 or other values from sendImageData). Values up until 19 display an image on the device.
219
231
  time_sign: 0: "0", 1: "10", 2: "30", 3: "60", 4: "300", else: "5".
232
+ display_time: The display time in seconds. If given it is used instead of the one `time_sign` stands for.
220
233
  ble_device_mtu_enabled: Boolean indicating if MTU is enabled on the BLE device.
221
234
 
222
235
  Returns:
@@ -269,11 +282,10 @@ class GifModule(IDotMatrixModule):
269
282
  else:
270
283
  # Java: DeviceMaterialTimeConvert.ConvertTime(AppData.getInstance().getTimeSign())
271
284
  # 'time_sign' passed to create_gif_data_packets is the input_key
272
- converted_time_value = convert_display_time(time_sign) # time_sign is the key (0,1,2,3,4...)
285
+ converted_time_value = display_time if display_time is not None else convert_display_time(time_sign)
273
286
 
274
- time_sign_bytes_be = bytearray(converted_time_value.to_bytes(2, byteorder='big'))
275
- header[13] = time_sign_bytes_be[0]
276
- header[14] = time_sign_bytes_be[1]
287
+ # little endian, like every other number of the header (the app does the same)
288
+ header[13:15] = converted_time_value.to_bytes(2, byteorder='little')
277
289
 
278
290
  header[15] = gif_type & 0xFF # Ensure it's a byte
279
291
 
@@ -13,7 +13,9 @@ from idotmatrix.connection_manager import ConnectionManager
13
13
  from idotmatrix.modules import IDotMatrixModule
14
14
  from idotmatrix.screensize import ScreenSize
15
15
  from idotmatrix.util import image_utils, color_utils
16
- from idotmatrix.util.device_material import INDEX_SHOW_NOW, convert_display_time
16
+ from idotmatrix.util.device_material import (
17
+ DEFAULT_DISPLAY_TIME_SECONDS, INDEX_SHOW_NOW, convert_display_time, validate_display_time, validate_slot,
18
+ )
17
19
 
18
20
  MTU_SIZE_IF_ENABLED = 509
19
21
  MTU_SIZE_IF_DISABLED = 18
@@ -142,9 +144,12 @@ class ImageModule(IDotMatrixModule):
142
144
  palletize: bool = False,
143
145
  background_color: Tuple[int, int, int] | int | str = (0, 0, 0),
144
146
  png: bool = False,
147
+ slot: int | None = None,
148
+ display_time: int = DEFAULT_DISPLAY_TIME_SECONDS,
145
149
  ) -> None:
146
150
  """
147
- Shows an image file on the device, the way the official app does it for photos.
151
+ Shows an image file on the device, the way the official app does it for photos. It is shown right away, unless
152
+ a slot is given.
148
153
 
149
154
  In contrast to `upload_image_file` this does not need `set_mode`, and it can be mixed with
150
155
  GIF uploads without any mode switching in between.
@@ -159,7 +164,13 @@ class ImageModule(IDotMatrixModule):
159
164
  png (bool): If True, the image is sent as a PNG file instead of raw RGB data. That is smaller (about 25-45%
160
165
  for photos, more for pixel-art, and a palette PNG with `palletize`), which saves a little upload time.
161
166
  Experimental: tested on a 64x64 device only, the official app sends raw RGB. Defaults to False.
167
+ slot (int, optional): If given (0 to 11), the image is stored in that slot of the device instead of being
168
+ shown right away. The device cycles through the stored items by itself, see `MaterialsModule`.
169
+ display_time (int): Seconds the stored item stays on screen in that cycle. Only used with a slot.
162
170
  """
171
+ if slot is not None:
172
+ validate_slot(slot)
173
+ validate_display_time(display_time)
163
174
  background_color = color_utils.parse_color_rgb(background_color)
164
175
  # reading and converting the file takes a while, the event loop must not be blocked
165
176
  pixel_data = await asyncio.to_thread(
@@ -170,7 +181,7 @@ class ImageModule(IDotMatrixModule):
170
181
  palletize=palletize,
171
182
  background_color=background_color,
172
183
  )
173
- await self._send_static_image_data(pixel_data, png=png, palette=palletize)
184
+ await self._send_static_image_data(pixel_data, png=png, palette=palletize, slot=slot, display_time=display_time)
174
185
 
175
186
  async def show_image_pixeldata(
176
187
  self,
@@ -202,10 +213,21 @@ class ImageModule(IDotMatrixModule):
202
213
  img.save(buffer, "PNG", optimize=True)
203
214
  return bytearray(buffer.getvalue())
204
215
 
205
- async def _send_static_image_data(self, pixel_data: bytearray, png: bool = False, palette: bool = False) -> None:
216
+ async def _send_static_image_data(
217
+ self,
218
+ pixel_data: bytearray,
219
+ png: bool = False,
220
+ palette: bool = False,
221
+ slot: int | None = None,
222
+ display_time: int = DEFAULT_DISPLAY_TIME_SECONDS,
223
+ ) -> None:
206
224
  if png:
207
225
  pixel_data = self._encode_png(pixel_data, palette=palette)
208
- packets = self._create_static_image_data_packets(pixel_data)
226
+ packets = self._create_static_image_data_packets(
227
+ pixel_data,
228
+ index=INDEX_SHOW_NOW if slot is None else slot,
229
+ display_time=None if slot is None else display_time,
230
+ )
209
231
  await self._send_packets(packets, response=True, wait_for_ack=True)
210
232
 
211
233
  def _create_static_image_data_packets(
@@ -213,6 +235,7 @@ class ImageModule(IDotMatrixModule):
213
235
  image_data: bytearray,
214
236
  index: int = INDEX_SHOW_NOW,
215
237
  time_sign: int = 0,
238
+ display_time: int | None = None,
216
239
  ) -> List[List[bytearray]]:
217
240
  """
218
241
  Creates the packets of a static image upload. This corresponds to `sendImageData` of the official app,
@@ -234,7 +257,9 @@ class ImageModule(IDotMatrixModule):
234
257
  header[4] = 2 if i > 0 else 0 # continuation / first chunk
235
258
  header[5:9] = total_length
236
259
  header[9:13] = crc32
237
- header[13:15] = convert_display_time(time_sign).to_bytes(2, "big")
260
+ header[13:15] = (
261
+ display_time if display_time is not None else convert_display_time(time_sign)
262
+ ).to_bytes(2, "little")
238
263
  header[15] = index
239
264
 
240
265
  packets.append(self._create_ble_packets(bytes(header) + chunk))
@@ -0,0 +1,120 @@
1
+ import asyncio
2
+ import logging
3
+ from os import PathLike
4
+ from typing import Iterable, Sequence, Tuple
5
+
6
+ from PIL import Image as PILImage
7
+
8
+ from idotmatrix import protocol
9
+ from idotmatrix.connection_manager import ConnectionManager
10
+ from idotmatrix.modules import IDotMatrixModule
11
+ from idotmatrix.modules.gif import GifModule
12
+ from idotmatrix.modules.image import ImageModule
13
+ from idotmatrix.protocol import build_command
14
+ from idotmatrix.util import image_utils
15
+ from idotmatrix.util.device_material import (
16
+ DEFAULT_DISPLAY_TIME_SECONDS, SLOTS, validate_display_time, validate_slot,
17
+ )
18
+
19
+
20
+ class MaterialsModule(IDotMatrixModule):
21
+ """
22
+ The slots of the device ("device materials" in the official app). The device keeps the items in its slots and
23
+ cycles through them by itself, in the order of the slots, so after the upload no Bluetooth traffic is needed and the
24
+ animation is not interrupted by an upload, which takes seconds for a GIF. Based on `DeviceMaterialChildFragment` of
25
+ the official app, which deletes the slots and then sends the items one after another.
26
+ """
27
+ logging = logging.getLogger(__name__)
28
+
29
+ def __init__(self, connection_manager: ConnectionManager, gif: GifModule, image: ImageModule) -> None:
30
+ super().__init__(connection_manager=connection_manager)
31
+ self._gif = gif
32
+ self._image = image
33
+
34
+ async def clear(self, slots: Iterable[int] | None = None) -> None:
35
+ """
36
+ Deletes the items in the given slots.
37
+
38
+ Args:
39
+ slots: The slots to empty (0 to 11). All of them if not given.
40
+ """
41
+ slots = self._validate_slots(slots)
42
+ # `11 00 02 01 <number of slots> <slot>...`, acknowledged with `05 00 02 01 01` (verified on a 64x64)
43
+ await self._send_bytes(data=build_command(protocol.DELETE_MATERIALS, len(slots), *slots), response=True)
44
+
45
+ async def store(
46
+ self,
47
+ files: Sequence[PathLike | str],
48
+ slots: Iterable[int] | None = None,
49
+ display_time: int = DEFAULT_DISPLAY_TIME_SECONDS,
50
+ resize_mode: image_utils.ResizeMode = image_utils.ResizeMode.FIT,
51
+ palletize: bool | None = None,
52
+ background_color: Tuple[int, int, int] | int | str = (0, 0, 0),
53
+ clear: bool = True,
54
+ ) -> list[int]:
55
+ """
56
+ Stores image and GIF files in the slots of the device, one file per slot, and ends up with the device cycling
57
+ through them. Other slots are not touched, so a list of slots protects the others.
58
+
59
+ Args:
60
+ files: The files, in the order they should be shown in. Animated GIFs are stored as GIFs, the rest as images.
61
+ slots: The slots the device may use (0 to 11), all of them if not given. The files go into them in the order
62
+ of the slot numbers, which is the order of the cycle.
63
+ display_time: Seconds each item stays on screen.
64
+ resize_mode: How the files are scaled to the screen.
65
+ palletize: Reduce the colors to a palette. The default is on for GIFs and off for images.
66
+ background_color: Color of the bars if a file does not have the shape of the screen.
67
+ clear: Delete the used slots before the upload, like the official app does.
68
+
69
+ Returns:
70
+ The slots that were used, one for each file.
71
+
72
+ Raises:
73
+ ValueError: If there are more files than slots, or a slot or the display time is invalid.
74
+ """
75
+ slots = self._validate_slots(slots)
76
+ validate_display_time(display_time)
77
+ if len(files) > len(slots):
78
+ raise ValueError(f"{len(files)} files do not fit into {len(slots)} slots")
79
+ used = slots[:len(files)]
80
+ if not used:
81
+ return []
82
+
83
+ if clear:
84
+ await self.clear(used)
85
+ for slot, file in zip(used, files):
86
+ if await asyncio.to_thread(self._is_animated, file):
87
+ await self._gif.upload_gif_file(
88
+ file_path=file,
89
+ resize_mode=resize_mode,
90
+ palletize=True if palletize is None else palletize,
91
+ background_color=background_color,
92
+ slot=slot,
93
+ display_time=display_time,
94
+ )
95
+ else:
96
+ await self._image.show_image_file(
97
+ file_path=file,
98
+ resize_mode=resize_mode,
99
+ palletize=bool(palletize),
100
+ background_color=background_color,
101
+ slot=slot,
102
+ display_time=display_time,
103
+ )
104
+ return used
105
+
106
+ @staticmethod
107
+ def _validate_slots(slots: Iterable[int] | None) -> list[int]:
108
+ if slots is None:
109
+ return list(SLOTS)
110
+ slots = sorted(validate_slot(slot) for slot in slots)
111
+ if len(set(slots)) != len(slots):
112
+ raise ValueError(f"slots must not contain duplicates, got: {slots}")
113
+ if not slots:
114
+ raise ValueError("slots must not be empty")
115
+ return slots
116
+
117
+ @staticmethod
118
+ def _is_animated(file: PathLike | str) -> bool:
119
+ with PILImage.open(file) as img:
120
+ return getattr(img, "n_frames", 1) > 1
@@ -15,6 +15,7 @@ ACKNOWLEDGED_COMMANDS = frozenset({
15
15
  protocol.CHRONOGRAPH,
16
16
  protocol.SCOREBOARD,
17
17
  protocol.MICROPHONE_MODE,
18
+ protocol.DELETE_MATERIALS,
18
19
  })
19
20
 
20
21
 
@@ -0,0 +1,36 @@
1
+ # Index (the last byte of the header) of an upload to the device. 0 to 11 are slots the device keeps
2
+ # and cycles through, 13 is the one the official app uses to show something on the device right away.
3
+ INDEX_SHOW_NOW = 13
4
+
5
+ # The slots (0 to 11, verified on a 64x64: items stay stored, are cycled through in the order of the slots, and an upload
6
+ # of the same data again is skipped by the device) that can hold items.
7
+ SLOTS = tuple(range(12))
8
+ DEFAULT_DISPLAY_TIME_SECONDS = 5
9
+ MAX_DISPLAY_TIME_SECONDS = 0xFFFF
10
+
11
+
12
+ def validate_slot(slot: int) -> int:
13
+ """Raises a ValueError for a slot the device does not have."""
14
+ if isinstance(slot, bool) or not isinstance(slot, int) or slot not in SLOTS:
15
+ raise ValueError(f"slot must be one of 0 to {SLOTS[-1]}, got: {slot!r}")
16
+ return slot
17
+
18
+
19
+ def validate_display_time(seconds: int) -> int:
20
+ """Raises a ValueError for a display time that does not fit the two bytes of the header."""
21
+ if isinstance(seconds, bool) or not isinstance(seconds, int) or not 1 <= seconds <= MAX_DISPLAY_TIME_SECONDS:
22
+ raise ValueError(f"display_time must be between 1 and {MAX_DISPLAY_TIME_SECONDS} seconds, got: {seconds!r}")
23
+ return seconds
24
+
25
+
26
+ def convert_display_time(time_sign: int) -> int:
27
+ """
28
+ Python equivalent of DeviceMaterialTimeConvert.ConvertTime(int i).
29
+ The time sign corresponds to AppData.getInstance().getTimeSign() of the official app, which is the
30
+ display time of an item in the list of stored materials of the device. It originates from
31
+ SharedPreferences, and 0 is the default.
32
+
33
+ Returns:
34
+ The display time in seconds.
35
+ """
36
+ return {1: 10, 2: 30, 3: 60, 4: 300}.get(time_sign, 5)
@@ -4,7 +4,7 @@ description = "A library for controlling iDotMatrix displays."
4
4
  license = "GPL-3.0-or-later"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.12,<3.15"
7
- version = "0.2.2"
7
+ version = "0.2.3"
8
8
  classifiers = [
9
9
  "Intended Audience :: Developers",
10
10
  ]
@@ -1,16 +0,0 @@
1
- # Index (the last byte of the header) of an upload to the device. 0 to 11 are slots the device keeps
2
- # and cycles through, 13 is the one the official app uses to show something on the device right away.
3
- INDEX_SHOW_NOW = 13
4
-
5
-
6
- def convert_display_time(time_sign: int) -> int:
7
- """
8
- Python equivalent of DeviceMaterialTimeConvert.ConvertTime(int i).
9
- The time sign corresponds to AppData.getInstance().getTimeSign() of the official app, which is the
10
- display time of an item in the list of stored materials of the device. It originates from
11
- SharedPreferences, and 0 is the default.
12
-
13
- Returns:
14
- The display time in seconds.
15
- """
16
- return {1: 10, 2: 30, 3: 60, 4: 300}.get(time_sign, 5)