idotmatrix-api-client 1.0.0__tar.gz → 1.2.0__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.
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/PKG-INFO +7 -5
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/README.md +6 -4
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/client.py +9 -0
- idotmatrix_api_client-1.2.0/idotmatrix/modules/alarm.py +226 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/gif.py +6 -5
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/image.py +12 -12
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/protocol.py +1 -0
- idotmatrix_api_client-1.2.0/idotmatrix/screensize.py +23 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/app.py +7 -6
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/canvas.py +23 -15
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/convert.py +5 -5
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/draw.py +13 -13
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/preview.py +33 -21
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/previews.py +1 -1
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/util/device_info.py +1 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/util/image_utils.py +37 -19
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/util/upload_ack.py +5 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/pyproject.toml +2 -1
- idotmatrix_api_client-1.0.0/idotmatrix/screensize.py +0 -8
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/LICENSE +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/__init__.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/apps/__init__.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/apps/digital_picture_frame.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/connection_manager.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/const.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-ErbosDraco1StOpenNbpRegular-l5wX.ttf +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-MinecraftStandard.otf +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-MinecraftStandardBoldOblique.otf +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-PixelOperator8-Bold.ttf +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-PixelOperator8.ttf +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-PixelifySans-Regular.ttf +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-PressStart2P-Regular.ttf +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-dogica.ttf +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-dogicabold.ttf +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-dogicapixel.ttf +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16-dogicapixelbold.ttf +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16_32-HomeVideo-BLG6G.ttf +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16_32-HomeVideoBold-R90Dv.ttf +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/16_32-MayfairNbpBold-gAA4.ttf +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/32-Jersey10-Regular.ttf +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/CC-BY-SA-4.0-License.txt +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/ErbosDraco-License.txt +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/HomeVideo-License.txt +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/Jersey10-Regular-License.txt +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/MayfairNbpBold-License.txt +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/MinecraftStandard-License.txt +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/PixelOperator-License.txt +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/PixelifySans-License.txt +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/PressStart2P-Regular-License.txt +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/Rain-DRM3-License.txt +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/Rain-DRM3.otf +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/fonts/dogica_license.txt +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/__init__.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/chronograph.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/clock.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/common.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/countdown.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/eco.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/effect.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/fullscreen_color.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/graffiti.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/materials.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/music_sync.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/scoreboard.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/system.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/modules/text.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/transport.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/__init__.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/__main__.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/colorwheel.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/controls.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/devicebar.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/devices.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/dialogs.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/features.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/files.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/forms.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/pages.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/scanner.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/scoreboard.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/settings.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/tui/widgets.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/util/__init__.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/util/color_utils.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/util/command_ack.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/util/device_material.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/util/file_watch.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/util/overlay.py +0 -0
- {idotmatrix_api_client-1.0.0 → idotmatrix_api_client-1.2.0}/idotmatrix/util/quality.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: idotmatrix-api-client
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.2.0
|
|
4
4
|
Summary: A library for controlling iDotMatrix displays.
|
|
5
5
|
License-Expression: GPL-3.0-or-later
|
|
6
6
|
License-File: LICENSE
|
|
@@ -28,7 +28,7 @@ Description-Content-Type: text/markdown
|
|
|
28
28
|
<h3 align="center">idotmatrix-api-client</h3>
|
|
29
29
|
|
|
30
30
|
<p align="center">
|
|
31
|
-
Control 16x16, 32x32 and
|
|
31
|
+
Control 16x16, 32x32, 64x64 and 24x48 sized iDotMatrix Pixel Displays
|
|
32
32
|
</p>
|
|
33
33
|
|
|
34
34
|
<p align="center">
|
|
@@ -54,11 +54,12 @@ this library.
|
|
|
54
54
|
* Scrolling text with the bundled pixel fonts
|
|
55
55
|
* Music sync with the microphone of the device
|
|
56
56
|
* Brightness, eco mode, power and time
|
|
57
|
+
* Alarms, which make the device beep (untested, see [the protocol notes](docs/DEVICE-PROTOCOL.md#alarm-and-buzzer))
|
|
57
58
|
* Digital picture frame for a folder of pictures
|
|
58
59
|
* Terminal UI to try all of it
|
|
59
|
-
* Works with the 16x16, 32x32 and 64x64 displays, with `bleak` or with the Bluetooth stack of Home Assistant
|
|
60
|
+
* Works with the 16x16, 32x32 and 64x64 displays and the 24x48 (48 pixels wide, 24 high), with `bleak` or with the Bluetooth stack of Home Assistant
|
|
60
61
|
|
|
61
|
-
Open ends (
|
|
62
|
+
Open ends (the cloud API, password protection, firmware update) are collected in the
|
|
62
63
|
[device protocol notes](docs/DEVICE-PROTOCOL.md#not-understood-yet).
|
|
63
64
|
|
|
64
65
|
<br clear="right"/>
|
|
@@ -79,7 +80,8 @@ pip install 'idotmatrix-api-client[tui]'
|
|
|
79
80
|
idotmatrix-tui /path/to/pictures
|
|
80
81
|
```
|
|
81
82
|
|
|
82
|
-
See the [TUI notes](docs/TUI.md) for the keys and options.
|
|
83
|
+
See the [TUI notes](docs/TUI.md) for the keys and options. The terminal UI and its dependencies (textual) are optional:
|
|
84
|
+
`pip install idotmatrix-api-client` installs only the API client, which never loads them.
|
|
83
85
|
|
|
84
86
|
### API Client
|
|
85
87
|
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
<h3 align="center">idotmatrix-api-client</h3>
|
|
8
8
|
|
|
9
9
|
<p align="center">
|
|
10
|
-
Control 16x16, 32x32 and
|
|
10
|
+
Control 16x16, 32x32, 64x64 and 24x48 sized iDotMatrix Pixel Displays
|
|
11
11
|
</p>
|
|
12
12
|
|
|
13
13
|
<p align="center">
|
|
@@ -33,11 +33,12 @@ this library.
|
|
|
33
33
|
* Scrolling text with the bundled pixel fonts
|
|
34
34
|
* Music sync with the microphone of the device
|
|
35
35
|
* Brightness, eco mode, power and time
|
|
36
|
+
* Alarms, which make the device beep (untested, see [the protocol notes](docs/DEVICE-PROTOCOL.md#alarm-and-buzzer))
|
|
36
37
|
* Digital picture frame for a folder of pictures
|
|
37
38
|
* Terminal UI to try all of it
|
|
38
|
-
* Works with the 16x16, 32x32 and 64x64 displays, with `bleak` or with the Bluetooth stack of Home Assistant
|
|
39
|
+
* Works with the 16x16, 32x32 and 64x64 displays and the 24x48 (48 pixels wide, 24 high), with `bleak` or with the Bluetooth stack of Home Assistant
|
|
39
40
|
|
|
40
|
-
Open ends (
|
|
41
|
+
Open ends (the cloud API, password protection, firmware update) are collected in the
|
|
41
42
|
[device protocol notes](docs/DEVICE-PROTOCOL.md#not-understood-yet).
|
|
42
43
|
|
|
43
44
|
<br clear="right"/>
|
|
@@ -58,7 +59,8 @@ pip install 'idotmatrix-api-client[tui]'
|
|
|
58
59
|
idotmatrix-tui /path/to/pictures
|
|
59
60
|
```
|
|
60
61
|
|
|
61
|
-
See the [TUI notes](docs/TUI.md) for the keys and options.
|
|
62
|
+
See the [TUI notes](docs/TUI.md) for the keys and options. The terminal UI and its dependencies (textual) are optional:
|
|
63
|
+
`pip install idotmatrix-api-client` installs only the API client, which never loads them.
|
|
62
64
|
|
|
63
65
|
### API Client
|
|
64
66
|
|
|
@@ -5,6 +5,7 @@ from typing import Optional
|
|
|
5
5
|
from bleak.backends.device import BLEDevice
|
|
6
6
|
|
|
7
7
|
from idotmatrix.connection_manager import ConnectionManager, ConnectionListener
|
|
8
|
+
from idotmatrix.modules.alarm import AlarmModule
|
|
8
9
|
from idotmatrix.modules.chronograph import ChronographModule
|
|
9
10
|
from idotmatrix.modules.clock import ClockModule
|
|
10
11
|
from idotmatrix.modules.common import CommonModule
|
|
@@ -91,6 +92,14 @@ class IDotMatrixClient:
|
|
|
91
92
|
def screen_size(self, screen_size: ScreenSize) -> None:
|
|
92
93
|
self._screen_size = screen_size
|
|
93
94
|
|
|
95
|
+
@cached_property
|
|
96
|
+
def alarm(self) -> AlarmModule:
|
|
97
|
+
return AlarmModule(
|
|
98
|
+
connection_manager=self._connection_manager,
|
|
99
|
+
gif=self.gif,
|
|
100
|
+
common=self.common,
|
|
101
|
+
)
|
|
102
|
+
|
|
94
103
|
@cached_property
|
|
95
104
|
def chronograph(self) -> ChronographModule:
|
|
96
105
|
return ChronographModule(
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import binascii
|
|
3
|
+
import logging
|
|
4
|
+
from collections.abc import Callable, Iterable
|
|
5
|
+
from datetime import datetime
|
|
6
|
+
from enum import IntEnum
|
|
7
|
+
from os import PathLike
|
|
8
|
+
from typing import Tuple
|
|
9
|
+
|
|
10
|
+
from idotmatrix import protocol
|
|
11
|
+
from idotmatrix.connection_manager import ConnectionManager
|
|
12
|
+
from idotmatrix.modules import IDotMatrixModule
|
|
13
|
+
from idotmatrix.modules.common import CommonModule
|
|
14
|
+
from idotmatrix.modules.gif import CHUNK_SIZE_4096, GifModule
|
|
15
|
+
from idotmatrix.protocol import build_command
|
|
16
|
+
from idotmatrix.util import color_utils
|
|
17
|
+
from idotmatrix.util.image_utils import ResizeMode
|
|
18
|
+
|
|
19
|
+
# The alarms the official app offers (0 to 9, according to issue #51). Only slot 0 was seen to ring, on a 16x16
|
|
20
|
+
# (docs/DEVICE-PROTOCOL.md).
|
|
21
|
+
ALARM_SLOTS = tuple(range(10))
|
|
22
|
+
# The ring durations the official app offers, in seconds. The header has two bytes for it.
|
|
23
|
+
APP_RING_DURATIONS_SECONDS = (10, 30, 60, 300, 900)
|
|
24
|
+
DEFAULT_RING_DURATION_SECONDS = 10
|
|
25
|
+
MAX_RING_DURATION_SECONDS = 0xFFFF
|
|
26
|
+
|
|
27
|
+
HEADER_SIZE_ALARM = 24
|
|
28
|
+
# the type of the picture the alarm shows (byte 10 of the header): 1 = GIF, 2 = raw 16x16 RGB, 3 = text
|
|
29
|
+
PICTURE_TYPE_GIF = 1
|
|
30
|
+
# the official app gives the picture of an alarm the id slot + 20 (the last byte of the header)
|
|
31
|
+
PICTURE_ID_OFFSET = 20
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class Weekday(IntEnum):
|
|
35
|
+
"""The days an alarm repeats on, numbered like `datetime.weekday()`."""
|
|
36
|
+
MONDAY = 0
|
|
37
|
+
TUESDAY = 1
|
|
38
|
+
WEDNESDAY = 2
|
|
39
|
+
THURSDAY = 3
|
|
40
|
+
FRIDAY = 4
|
|
41
|
+
SATURDAY = 5
|
|
42
|
+
SUNDAY = 6
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class AlarmModule(IDotMatrixModule):
|
|
46
|
+
"""
|
|
47
|
+
The alarms of the device. When an alarm goes off the device shows its picture and, if the buzzer is on, beeps.
|
|
48
|
+
This is the only known way to make the device beep: there is no command that just beeps.
|
|
49
|
+
|
|
50
|
+
Based on `TimerAgreement.sendData` of the official Android app (2.1.7), as described by a third party who
|
|
51
|
+
confirmed the bytes with a capture of the app and made a 16x16 beep with them (issue #51). Not tested by us yet,
|
|
52
|
+
see docs/DEVICE-PROTOCOL.md.
|
|
53
|
+
|
|
54
|
+
The device keeps the time only while it is powered, and an alarm only goes off at the real time: set the time
|
|
55
|
+
(the default of `set_alarm`) and then the alarm for a later minute.
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
logging = logging.getLogger(__name__)
|
|
59
|
+
|
|
60
|
+
def __init__(self, connection_manager: ConnectionManager, gif: GifModule, common: CommonModule) -> None:
|
|
61
|
+
super().__init__(connection_manager=connection_manager)
|
|
62
|
+
self._gif = gif
|
|
63
|
+
self._common = common
|
|
64
|
+
|
|
65
|
+
async def set_alarm(
|
|
66
|
+
self,
|
|
67
|
+
slot: int,
|
|
68
|
+
hour: int,
|
|
69
|
+
minute: int,
|
|
70
|
+
file_path: PathLike | str,
|
|
71
|
+
weekdays: Iterable[Weekday | int] | None = None,
|
|
72
|
+
ring_duration: int = DEFAULT_RING_DURATION_SECONDS,
|
|
73
|
+
buzzer: bool = True,
|
|
74
|
+
sync_time: bool = True,
|
|
75
|
+
resize_mode: ResizeMode = ResizeMode.FIT,
|
|
76
|
+
background_color: Tuple[int, int, int] | int | str = (0, 0, 0),
|
|
77
|
+
progress: Callable[[int, int], None] | None = None,
|
|
78
|
+
) -> None:
|
|
79
|
+
"""
|
|
80
|
+
Sets an alarm. The device shows its picture right away as a preview (replacing what is on screen), and again
|
|
81
|
+
when the alarm goes off.
|
|
82
|
+
|
|
83
|
+
Args:
|
|
84
|
+
slot (int): The alarm to set (0 to 9). Only slot 0 is known to ring, slot 9 did not on a 16x16.
|
|
85
|
+
hour (int): The hour the alarm goes off (0 to 23).
|
|
86
|
+
minute (int): The minute the alarm goes off (0 to 59).
|
|
87
|
+
file_path (PathLike | str): The picture shown while the alarm rings, a GIF or any other picture. It is
|
|
88
|
+
converted like for `GifModule.upload_gif_file`.
|
|
89
|
+
weekdays (Iterable[Weekday | int], optional): The days the alarm repeats on (0 = Monday to 6 = Sunday).
|
|
90
|
+
The alarm goes off once if none are given.
|
|
91
|
+
ring_duration (int): How long the alarm rings, in seconds (1 to 65535). The official app offers 10, 30,
|
|
92
|
+
60, 300 and 900.
|
|
93
|
+
buzzer (bool): Whether the device beeps while the alarm rings. Defaults to True.
|
|
94
|
+
sync_time (bool): Whether to set the time of the device to the current time first. An alarm only goes off
|
|
95
|
+
if the device knows the time, which it loses when it is powered off. Defaults to True.
|
|
96
|
+
resize_mode (ResizeMode): How the picture is fitted to the screen.
|
|
97
|
+
background_color (Tuple[int, int, int] | int | str): The color of transparent pixels. Defaults to black.
|
|
98
|
+
progress (Callable[[int, int], None], optional): Called with (chunks sent, chunks in total) while the
|
|
99
|
+
picture is transferred, see `ConnectionManager.send_packets`.
|
|
100
|
+
|
|
101
|
+
Raises:
|
|
102
|
+
UploadError: If the device does not acknowledge a part in time. Only the acknowledgement that accepts a
|
|
103
|
+
part is known, so a rejection shows up as a missing acknowledgement.
|
|
104
|
+
|
|
105
|
+
For a while after an alarm went off the device keeps showing its picture and ignores new uploads (about
|
|
106
|
+
3 minutes on a 16x16).
|
|
107
|
+
"""
|
|
108
|
+
flags = self._flags(enabled=True, weekdays=weekdays)
|
|
109
|
+
self._validate(slot=slot, hour=hour, minute=minute, ring_duration=ring_duration)
|
|
110
|
+
background_color = color_utils.parse_color_rgb(background_color)
|
|
111
|
+
|
|
112
|
+
# reading and converting the file takes a while, the event loop must not be blocked
|
|
113
|
+
gif_data = await asyncio.to_thread(
|
|
114
|
+
self._gif._load_gif_and_adapt_to_canvas,
|
|
115
|
+
file_path=file_path,
|
|
116
|
+
canvas_size=self._gif.screen_size.value,
|
|
117
|
+
resize_mode=resize_mode,
|
|
118
|
+
background_color=background_color,
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
if sync_time:
|
|
122
|
+
await self._common.set_time(datetime.now())
|
|
123
|
+
|
|
124
|
+
packets = self._create_alarm_packets(
|
|
125
|
+
gif_data=gif_data, slot=slot, flags=flags, hour=hour, minute=minute,
|
|
126
|
+
ring_duration=ring_duration, buzzer=buzzer,
|
|
127
|
+
)
|
|
128
|
+
await self._send_packets(packets=packets, response=True, wait_for_ack=True, progress=progress)
|
|
129
|
+
|
|
130
|
+
async def disable_alarm(
|
|
131
|
+
self,
|
|
132
|
+
slot: int,
|
|
133
|
+
hour: int,
|
|
134
|
+
minute: int,
|
|
135
|
+
weekdays: Iterable[Weekday | int] | None = None,
|
|
136
|
+
ring_duration: int = DEFAULT_RING_DURATION_SECONDS,
|
|
137
|
+
buzzer: bool = True,
|
|
138
|
+
) -> None:
|
|
139
|
+
"""
|
|
140
|
+
Switches an alarm off. The official app sends the settings of the alarm with the "enabled" bit cleared and
|
|
141
|
+
without a picture. Whether the device needs the settings of the alarm or only its slot is not known, so pass
|
|
142
|
+
the ones it was set with to be safe. Untested.
|
|
143
|
+
|
|
144
|
+
Args:
|
|
145
|
+
slot (int): The alarm to switch off (0 to 9).
|
|
146
|
+
hour (int): The hour of the alarm (0 to 23).
|
|
147
|
+
minute (int): The minute of the alarm (0 to 59).
|
|
148
|
+
weekdays (Iterable[Weekday | int], optional): The days the alarm repeats on (0 = Monday to 6 = Sunday).
|
|
149
|
+
ring_duration (int): How long the alarm rings, in seconds (1 to 65535).
|
|
150
|
+
buzzer (bool): Whether the alarm beeps.
|
|
151
|
+
"""
|
|
152
|
+
flags = self._flags(enabled=False, weekdays=weekdays)
|
|
153
|
+
self._validate(slot=slot, hour=hour, minute=minute, ring_duration=ring_duration)
|
|
154
|
+
data = build_command(
|
|
155
|
+
protocol.ALARM, slot, flags, hour, minute, *ring_duration.to_bytes(2, byteorder="little"),
|
|
156
|
+
PICTURE_TYPE_GIF, 1 if buzzer else 0,
|
|
157
|
+
)
|
|
158
|
+
await self._send_bytes(data=data)
|
|
159
|
+
|
|
160
|
+
@staticmethod
|
|
161
|
+
def _create_alarm_packets(
|
|
162
|
+
gif_data: bytes,
|
|
163
|
+
slot: int,
|
|
164
|
+
flags: int,
|
|
165
|
+
hour: int,
|
|
166
|
+
minute: int,
|
|
167
|
+
ring_duration: int,
|
|
168
|
+
buzzer: bool,
|
|
169
|
+
) -> list[list[bytearray]]:
|
|
170
|
+
"""
|
|
171
|
+
Creates the packets of an alarm with a GIF: the picture split into parts of 4096 bytes, each with a header
|
|
172
|
+
of 24 bytes in front (`TimerAgreement.sendData`). The header has the length of the packet, the command
|
|
173
|
+
(`00 80`), the settings of the alarm, whether it is the first part, the size and CRC-32 of the whole picture
|
|
174
|
+
and the id of the picture.
|
|
175
|
+
|
|
176
|
+
Returns:
|
|
177
|
+
One packet per part, each a list of BLE packets.
|
|
178
|
+
"""
|
|
179
|
+
if not gif_data:
|
|
180
|
+
raise ValueError("gif_data cannot be empty")
|
|
181
|
+
|
|
182
|
+
total_length = len(gif_data).to_bytes(4, byteorder="little")
|
|
183
|
+
crc32 = (binascii.crc32(gif_data) & 0xFFFFFFFF).to_bytes(4, byteorder="little")
|
|
184
|
+
|
|
185
|
+
packets = []
|
|
186
|
+
for offset in range(0, len(gif_data), CHUNK_SIZE_4096):
|
|
187
|
+
part = gif_data[offset:offset + CHUNK_SIZE_4096]
|
|
188
|
+
header = bytearray(HEADER_SIZE_ALARM)
|
|
189
|
+
header[0:2] = (HEADER_SIZE_ALARM + len(part)).to_bytes(2, byteorder="little")
|
|
190
|
+
header[2] = protocol.ALARM.opcode
|
|
191
|
+
header[3] = protocol.ALARM.subcommand
|
|
192
|
+
header[4] = slot
|
|
193
|
+
header[5] = flags
|
|
194
|
+
header[6] = hour
|
|
195
|
+
header[7] = minute
|
|
196
|
+
header[8:10] = ring_duration.to_bytes(2, byteorder="little")
|
|
197
|
+
header[10] = PICTURE_TYPE_GIF
|
|
198
|
+
header[11] = 1 if buzzer else 0
|
|
199
|
+
header[12] = 2 if offset else 0 # 0 for the first part, 2 for the following ones
|
|
200
|
+
header[13:17] = total_length
|
|
201
|
+
header[17:21] = crc32
|
|
202
|
+
# bytes 21 and 22 stay 0
|
|
203
|
+
header[23] = slot + PICTURE_ID_OFFSET
|
|
204
|
+
packets.append(GifModule._create_ble_packets(bytes(header) + part))
|
|
205
|
+
return packets
|
|
206
|
+
|
|
207
|
+
@staticmethod
|
|
208
|
+
def _flags(enabled: bool, weekdays: Iterable[Weekday | int] | None) -> int:
|
|
209
|
+
"""Bit 0: the alarm is on, bits 1 to 7: the days it repeats on (Monday to Sunday), none for once."""
|
|
210
|
+
flags = 1 if enabled else 0
|
|
211
|
+
for day in weekdays or ():
|
|
212
|
+
if isinstance(day, bool) or not isinstance(day, int) or not 0 <= day <= 6:
|
|
213
|
+
raise ValueError(f"weekdays must be between 0 (Monday) and 6 (Sunday), got: {day!r}")
|
|
214
|
+
flags |= 1 << (day + 1)
|
|
215
|
+
return flags
|
|
216
|
+
|
|
217
|
+
@staticmethod
|
|
218
|
+
def _validate(slot: int, hour: int, minute: int, ring_duration: int) -> None:
|
|
219
|
+
for name, value, valid in (
|
|
220
|
+
("slot", slot, range(len(ALARM_SLOTS))),
|
|
221
|
+
("hour", hour, range(24)),
|
|
222
|
+
("minute", minute, range(60)),
|
|
223
|
+
("ring_duration", ring_duration, range(1, MAX_RING_DURATION_SECONDS + 1)),
|
|
224
|
+
):
|
|
225
|
+
if isinstance(value, bool) or not isinstance(value, int) or value not in valid:
|
|
226
|
+
raise ValueError(f"{name} must be between {valid[0]} and {valid[-1]}, got: {value!r}")
|
|
@@ -105,14 +105,13 @@ class GifModule(IDotMatrixModule):
|
|
|
105
105
|
if slot is not None:
|
|
106
106
|
validate_slot(slot)
|
|
107
107
|
validate_display_time(display_time)
|
|
108
|
-
screen_width = self.screen_size.value[0] # assuming square canvas, so width == height
|
|
109
108
|
background_color = color_utils.parse_color_rgb(background_color)
|
|
110
109
|
|
|
111
110
|
# reading and converting the file takes a while, the event loop must not be blocked
|
|
112
111
|
gif_data = await asyncio.to_thread(
|
|
113
112
|
self._load_gif_and_adapt_to_canvas,
|
|
114
113
|
file_path=file_path,
|
|
115
|
-
canvas_size=
|
|
114
|
+
canvas_size=self.screen_size.value,
|
|
116
115
|
resize_mode=resize_mode,
|
|
117
116
|
palletize=palletize,
|
|
118
117
|
background_color=background_color,
|
|
@@ -150,7 +149,7 @@ class GifModule(IDotMatrixModule):
|
|
|
150
149
|
def _load_gif_and_adapt_to_canvas(
|
|
151
150
|
self,
|
|
152
151
|
file_path: PathLike | str,
|
|
153
|
-
canvas_size: int,
|
|
152
|
+
canvas_size: int | Tuple[int, int],
|
|
154
153
|
resize_mode: ResizeMode,
|
|
155
154
|
palletize: bool = True,
|
|
156
155
|
background_color: Tuple[int, int, int] = (0, 0, 0),
|
|
@@ -166,7 +165,8 @@ class GifModule(IDotMatrixModule):
|
|
|
166
165
|
|
|
167
166
|
Args:
|
|
168
167
|
file_path (PathLike): Path to the GIF file.
|
|
169
|
-
canvas_size (int): Size of the
|
|
168
|
+
canvas_size (int | Tuple[int, int]): Size of the device's canvas in pixels, one number for a square
|
|
169
|
+
canvas or (width, height).
|
|
170
170
|
resize_mode (ResizeMode): The mode to resize the image. Options are:
|
|
171
171
|
- ResizeMode.FIT: Resize to fit within the canvas while maintaining aspect ratio.
|
|
172
172
|
- ResizeMode.FILL: Resize to fill the canvas, may crop the image.
|
|
@@ -183,6 +183,7 @@ class GifModule(IDotMatrixModule):
|
|
|
183
183
|
Returns:
|
|
184
184
|
bytes: A byte representation of the GIF file, adapted to fit the pixel size.
|
|
185
185
|
"""
|
|
186
|
+
canvas_size = image_utils.canvas_dimensions(canvas_size)
|
|
186
187
|
from PIL import GifImagePlugin
|
|
187
188
|
GifImagePlugin.LOADING_STRATEGY = GifImagePlugin.LoadingStrategy.RGB_AFTER_DIFFERENT_PALETTE_ONLY
|
|
188
189
|
|
|
@@ -196,7 +197,7 @@ class GifModule(IDotMatrixModule):
|
|
|
196
197
|
source_durations.append(img.info.get("duration"))
|
|
197
198
|
frame = img.copy()
|
|
198
199
|
|
|
199
|
-
if frame.size !=
|
|
200
|
+
if frame.size != canvas_size:
|
|
200
201
|
# needs to use NEAREST to to avoid color distortion
|
|
201
202
|
resample_mode = PILImage.Resampling.NEAREST
|
|
202
203
|
frame = image_utils.resize_image(
|
|
@@ -85,7 +85,7 @@ class ImageModule(IDotMatrixModule):
|
|
|
85
85
|
pixel_data = await asyncio.to_thread(
|
|
86
86
|
self._load_image_and_adapt_to_canvas,
|
|
87
87
|
file_path=file_path,
|
|
88
|
-
canvas_size=self.screen_size.value
|
|
88
|
+
canvas_size=self.screen_size.value,
|
|
89
89
|
resize_mode=resize_mode,
|
|
90
90
|
palletize=palletize,
|
|
91
91
|
background_color=background_color,
|
|
@@ -95,7 +95,7 @@ class ImageModule(IDotMatrixModule):
|
|
|
95
95
|
@staticmethod
|
|
96
96
|
def _load_image_and_adapt_to_canvas(
|
|
97
97
|
file_path: PathLike | str,
|
|
98
|
-
canvas_size: int,
|
|
98
|
+
canvas_size: int | Tuple[int, int],
|
|
99
99
|
resize_mode: image_utils.ResizeMode,
|
|
100
100
|
palletize: bool,
|
|
101
101
|
background_color: Tuple[int, int, int],
|
|
@@ -103,17 +103,18 @@ class ImageModule(IDotMatrixModule):
|
|
|
103
103
|
overlay: TextOverlay | None = None,
|
|
104
104
|
) -> bytearray:
|
|
105
105
|
"""
|
|
106
|
-
Loads an image from a file, resizes it to
|
|
106
|
+
Loads an image from a file, resizes it to the size of the canvas,
|
|
107
107
|
and applies a black background if the image is smaller than the canvas size.
|
|
108
108
|
Args:
|
|
109
109
|
file_path (str): Path to the image file.
|
|
110
|
-
canvas_size (int): Size of the
|
|
110
|
+
canvas_size (int | Tuple[int, int]): Size of the canvas in pixels, one number for a square canvas or
|
|
111
|
+
(width, height).
|
|
111
112
|
palletize (bool): If True, the image will be converted to a palette-based image.
|
|
112
113
|
background_color (Tuple[int, int, int]): RGB color for the background, which is only visible if the input
|
|
113
114
|
colors (int): The number of colors of the palette that `palletize` reduces the image to.
|
|
114
115
|
overlay (TextOverlay, optional): A text that is drawn on top of the image.
|
|
115
116
|
Returns:
|
|
116
|
-
bytearray: A bytearray containing the raw pixel data of the processed image in RGB format.
|
|
117
|
+
bytearray: A bytearray containing the raw pixel data of the processed image in RGB format, row by row.
|
|
117
118
|
"""
|
|
118
119
|
if background_color is None or len(background_color) != 3:
|
|
119
120
|
raise ValueError("background_color must be a tuple of three integers (R, G, B)")
|
|
@@ -199,7 +200,7 @@ class ImageModule(IDotMatrixModule):
|
|
|
199
200
|
pixel_data = await asyncio.to_thread(
|
|
200
201
|
self._load_image_and_adapt_to_canvas,
|
|
201
202
|
file_path=file_path,
|
|
202
|
-
canvas_size=self.screen_size.value
|
|
203
|
+
canvas_size=self.screen_size.value,
|
|
203
204
|
resize_mode=resize_mode,
|
|
204
205
|
palletize=palletize,
|
|
205
206
|
background_color=background_color,
|
|
@@ -232,8 +233,7 @@ class ImageModule(IDotMatrixModule):
|
|
|
232
233
|
palette: If True, the PNG uses a palette of up to 256 colors, which is smaller, but changes the colors of
|
|
233
234
|
images with more than 256 of them.
|
|
234
235
|
"""
|
|
235
|
-
|
|
236
|
-
img = PILImage.frombytes("RGB", (size, size), bytes(pixel_data))
|
|
236
|
+
img = PILImage.frombytes("RGB", self.screen_size.value, bytes(pixel_data))
|
|
237
237
|
if palette:
|
|
238
238
|
img = img.quantize(colors, dither=PILImage.Dither.NONE)
|
|
239
239
|
buffer = io.BytesIO()
|
|
@@ -303,17 +303,17 @@ class ImageModule(IDotMatrixModule):
|
|
|
303
303
|
Args:
|
|
304
304
|
pixel_colors (List[Tuple[int, int, int]]): List of tuples representing RGB pixel values.
|
|
305
305
|
Each tuple should contain three integers (R, G, B) in the range 0-255.
|
|
306
|
-
|
|
306
|
+
One for each pixel of the screen (width * height), row by row.
|
|
307
307
|
"""
|
|
308
308
|
pixel_data = self._pixel_colors_to_bytes(pixel_colors)
|
|
309
309
|
|
|
310
310
|
return await self._send_diy_image_data(pixel_data)
|
|
311
311
|
|
|
312
312
|
def _pixel_colors_to_bytes(self, pixel_colors: List[Tuple[int, int, int] | int | str]) -> bytearray:
|
|
313
|
-
|
|
314
|
-
if len(pixel_colors) !=
|
|
313
|
+
width, height = self.screen_size.value
|
|
314
|
+
if len(pixel_colors) != width * height:
|
|
315
315
|
raise ValueError(
|
|
316
|
-
f"pixel_data must contain exactly {
|
|
316
|
+
f"pixel_data must contain exactly {width * height} pixels ({width}x{height}), got: {len(pixel_colors)}"
|
|
317
317
|
)
|
|
318
318
|
|
|
319
319
|
# parse the pixel_colors to ensure they are in RGB format
|
|
@@ -21,6 +21,7 @@ SCREEN_FLIP = Command(6, 0x80)
|
|
|
21
21
|
SCREEN_ON_OFF = Command(7, 1)
|
|
22
22
|
SET_JOINT = Command(12, 0x80) # not acknowledged
|
|
23
23
|
DELETE_MATERIALS = Command(2, 1)
|
|
24
|
+
ALARM = Command(0, 0x80) # with a picture behind a 24 byte header, see AlarmModule
|
|
24
25
|
|
|
25
26
|
# modes
|
|
26
27
|
CLOCK = Command(6, 1)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from enum import Enum
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class ScreenSize(Enum):
|
|
5
|
+
"""
|
|
6
|
+
Enum for screen sizes. The value is (width, height) in pixels.
|
|
7
|
+
|
|
8
|
+
The names follow the device's own rows x columns naming, so SIZE_24x48 is 24 pixels high and 48 wide.
|
|
9
|
+
"""
|
|
10
|
+
SIZE_64x64 = (64, 64)
|
|
11
|
+
SIZE_32x32 = (32, 32)
|
|
12
|
+
SIZE_16x16 = (16, 16)
|
|
13
|
+
SIZE_24x48 = (48, 24)
|
|
14
|
+
|
|
15
|
+
@property
|
|
16
|
+
def width(self) -> int:
|
|
17
|
+
"""The number of pixels in a row."""
|
|
18
|
+
return self.value[0]
|
|
19
|
+
|
|
20
|
+
@property
|
|
21
|
+
def height(self) -> int:
|
|
22
|
+
"""The number of rows."""
|
|
23
|
+
return self.value[1]
|
|
@@ -158,7 +158,7 @@ class IDotMatrixApp(App):
|
|
|
158
158
|
self._client_factory = client_factory
|
|
159
159
|
self.client: IDotMatrixClient | None = None
|
|
160
160
|
self.connection = "connecting"
|
|
161
|
-
self.canvas_size = DEFAULT_CANVAS_SIZE
|
|
161
|
+
self.canvas_size = (DEFAULT_CANVAS_SIZE, DEFAULT_CANVAS_SIZE) # (width, height)
|
|
162
162
|
self.marked: list[Path] = []
|
|
163
163
|
self.text_filter = ""
|
|
164
164
|
self._busy: Worker | None = None
|
|
@@ -609,11 +609,12 @@ class IDotMatrixApp(App):
|
|
|
609
609
|
warning = self.main_screen.query_one("#preview-warning", Static)
|
|
610
610
|
except NoMatches: # the app is closing
|
|
611
611
|
return
|
|
612
|
-
|
|
612
|
+
(shown_width, shown_height), (actual_width, actual_height) = event.shown, event.actual
|
|
613
|
+
too_small = actual_width > shown_width > 0
|
|
613
614
|
warning.set_class(too_small, "visible")
|
|
614
615
|
if too_small:
|
|
615
|
-
warning.update(f"⚠ too small: drawn at {
|
|
616
|
-
"enlarge the terminal for an exact picture")
|
|
616
|
+
warning.update(f"⚠ too small: drawn at {shown_width}x{shown_height} of {actual_width}x{actual_height} "
|
|
617
|
+
"pixels, enlarge the terminal for an exact picture")
|
|
617
618
|
|
|
618
619
|
def _update_info(self) -> None:
|
|
619
620
|
path = self._previewed
|
|
@@ -638,7 +639,7 @@ class IDotMatrixApp(App):
|
|
|
638
639
|
text.append(symbols.get(self.connection, "○"), style=STATUS_COLORS.get(self.connection, "grey62"))
|
|
639
640
|
text.append(f" {self.connection}")
|
|
640
641
|
if self.connection == "connected":
|
|
641
|
-
text.append(f" {self.canvas_size}x{self.canvas_size}")
|
|
642
|
+
text.append(f" {self.canvas_size[0]}x{self.canvas_size[1]}")
|
|
642
643
|
if self.session.address:
|
|
643
644
|
known = self.session.device(self.session.address)
|
|
644
645
|
name = self._device_names.get(self.session.address) or (known.name if known else "")
|
|
@@ -660,7 +661,7 @@ class IDotMatrixApp(App):
|
|
|
660
661
|
self.client = self._client_factory(self.session.address)
|
|
661
662
|
await self._connect_client()
|
|
662
663
|
size = self.client.screen_size
|
|
663
|
-
self.canvas_size = size.value
|
|
664
|
+
self.canvas_size = size.value if isinstance(size, ScreenSize) else (DEFAULT_CANVAS_SIZE, DEFAULT_CANVAS_SIZE)
|
|
664
665
|
self.connection = "connected"
|
|
665
666
|
address = self.session.address
|
|
666
667
|
if address:
|
|
@@ -1,26 +1,34 @@
|
|
|
1
1
|
from PIL import Image as PILImage
|
|
2
2
|
|
|
3
|
+
from idotmatrix.util.image_utils import canvas_dimensions
|
|
4
|
+
|
|
3
5
|
Color = tuple[int, int, int]
|
|
4
6
|
BLACK: Color = (0, 0, 0)
|
|
5
7
|
MAX_UNDO = 100
|
|
6
8
|
|
|
7
9
|
|
|
8
10
|
class PixelCanvas:
|
|
9
|
-
"""A
|
|
11
|
+
"""A picture to draw on, with undo, and a record of what changed since it was last sent to the display."""
|
|
10
12
|
|
|
11
|
-
def __init__(self, size: int):
|
|
12
|
-
|
|
13
|
-
self.
|
|
13
|
+
def __init__(self, size: int | tuple[int, int]):
|
|
14
|
+
"""`size` is one number for a square canvas, or (width, height)."""
|
|
15
|
+
self.width, self.height = canvas_dimensions(size)
|
|
16
|
+
self._pixels: list[list[Color]] = [[BLACK] * self.width for _ in range(self.height)]
|
|
14
17
|
self._undo: list[list[list[Color]]] = []
|
|
15
18
|
self._redo: list[list[list[Color]]] = []
|
|
16
19
|
self._dirty: set[tuple[int, int]] = set()
|
|
17
20
|
self._touched: set[int] = set()
|
|
18
21
|
|
|
22
|
+
@property
|
|
23
|
+
def size(self) -> tuple[int, int]:
|
|
24
|
+
"""(width, height)"""
|
|
25
|
+
return self.width, self.height
|
|
26
|
+
|
|
19
27
|
def get(self, x: int, y: int) -> Color:
|
|
20
28
|
return self._pixels[y][x]
|
|
21
29
|
|
|
22
30
|
def inside(self, x: int, y: int) -> bool:
|
|
23
|
-
return 0 <= x < self.
|
|
31
|
+
return 0 <= x < self.width and 0 <= y < self.height
|
|
24
32
|
|
|
25
33
|
# changing
|
|
26
34
|
|
|
@@ -79,17 +87,17 @@ class PixelCanvas:
|
|
|
79
87
|
stack.extend(((px + 1, py), (px - 1, py), (px, py + 1), (px, py - 1)))
|
|
80
88
|
|
|
81
89
|
def clear(self) -> None:
|
|
82
|
-
for y in range(self.
|
|
83
|
-
for x in range(self.
|
|
90
|
+
for y in range(self.height):
|
|
91
|
+
for x in range(self.width):
|
|
84
92
|
self.set(x, y, BLACK)
|
|
85
93
|
|
|
86
94
|
def load(self, image: PILImage.Image) -> None:
|
|
87
95
|
"""Replaces the picture, scaled to the canvas without smoothing, so pixel art stays sharp."""
|
|
88
96
|
rgb = image.convert("RGB")
|
|
89
|
-
if rgb.size !=
|
|
90
|
-
rgb = rgb.resize(
|
|
91
|
-
for y in range(self.
|
|
92
|
-
for x in range(self.
|
|
97
|
+
if rgb.size != self.size:
|
|
98
|
+
rgb = rgb.resize(self.size, PILImage.Resampling.NEAREST)
|
|
99
|
+
for y in range(self.height):
|
|
100
|
+
for x in range(self.width):
|
|
93
101
|
self.set(x, y, rgb.getpixel((x, y)))
|
|
94
102
|
|
|
95
103
|
# undo
|
|
@@ -105,8 +113,8 @@ class PixelCanvas:
|
|
|
105
113
|
return False
|
|
106
114
|
target.append([row[:] for row in self._pixels])
|
|
107
115
|
state = source.pop()
|
|
108
|
-
for y in range(self.
|
|
109
|
-
for x in range(self.
|
|
116
|
+
for y in range(self.height):
|
|
117
|
+
for x in range(self.width):
|
|
110
118
|
self.set(x, y, state[y][x])
|
|
111
119
|
return True
|
|
112
120
|
|
|
@@ -126,12 +134,12 @@ class PixelCanvas:
|
|
|
126
134
|
|
|
127
135
|
def everything(self) -> dict[tuple[int, int], Color]:
|
|
128
136
|
self._dirty.clear()
|
|
129
|
-
return {(x, y): self._pixels[y][x] for y in range(self.
|
|
137
|
+
return {(x, y): self._pixels[y][x] for y in range(self.height) for x in range(self.width)}
|
|
130
138
|
|
|
131
139
|
# as a picture
|
|
132
140
|
|
|
133
141
|
def to_image(self) -> PILImage.Image:
|
|
134
|
-
image = PILImage.new("RGB",
|
|
142
|
+
image = PILImage.new("RGB", self.size)
|
|
135
143
|
image.putdata([pixel for row in self._pixels for pixel in row])
|
|
136
144
|
return image
|
|
137
145
|
|
|
@@ -36,13 +36,14 @@ def is_animated(path: Path) -> bool:
|
|
|
36
36
|
return image_utils.is_animated(img)
|
|
37
37
|
|
|
38
38
|
|
|
39
|
-
def convert(path: Path, canvas_size: int, options: Options) -> Preview:
|
|
39
|
+
def convert(path: Path, canvas_size: int | tuple[int, int], options: Options) -> Preview:
|
|
40
40
|
"""
|
|
41
41
|
Converts a file the way an upload does, with the options, and decodes the result. Slow for a long animation, so
|
|
42
|
-
not for the UI thread.
|
|
42
|
+
not for the UI thread. `canvas_size` is one number for a square screen, or (width, height).
|
|
43
43
|
"""
|
|
44
|
+
canvas_size = image_utils.canvas_dimensions(canvas_size)
|
|
45
|
+
screen = next((size for size in ScreenSize if size.value == canvas_size), ScreenSize.SIZE_64x64)
|
|
44
46
|
if is_animated(path):
|
|
45
|
-
screen = next((size for size in ScreenSize if size.value[0] == canvas_size), ScreenSize.SIZE_64x64)
|
|
46
47
|
module = GifModule(connection_manager=None, screen_size=screen)
|
|
47
48
|
data = module._load_gif_and_adapt_to_canvas(
|
|
48
49
|
file_path=path, canvas_size=canvas_size, resize_mode=options.resize,
|
|
@@ -63,9 +64,8 @@ def convert(path: Path, canvas_size: int, options: Options) -> Preview:
|
|
|
63
64
|
pixels = ImageModule._load_image_and_adapt_to_canvas(
|
|
64
65
|
file_path=path, canvas_size=canvas_size, resize_mode=options.resize, palletize=palletize,
|
|
65
66
|
background_color=options.background_rgb, colors=colors, overlay=options.overlay_for(path))
|
|
66
|
-
image = PILImage.frombytes("RGB",
|
|
67
|
+
image = PILImage.frombytes("RGB", canvas_size, bytes(pixels))
|
|
67
68
|
if options.png:
|
|
68
|
-
screen = next((size for size in ScreenSize if size.value[0] == canvas_size), ScreenSize.SIZE_64x64)
|
|
69
69
|
encoder = ImageModule(connection_manager=None, screen_size=screen)
|
|
70
70
|
size = len(encoder._encode_png(pixels, palette=palletize or colors < 256, colors=colors))
|
|
71
71
|
else:
|