omnipython-lvgl 9.6.1.dev1__cp311-cp311-win_amd64.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.
fs_driver.py ADDED
@@ -0,0 +1,168 @@
1
+ # SPDX-FileCopyrightText: 2024 Brad Barnett
2
+ # SPDX-FileCopyrightText: mhepp (original, from lv_binding_micropython; MIT)
3
+ #
4
+ # SPDX-License-Identifier: MIT
5
+
6
+ """
7
+ fs_driver.py - register a Python-backed LVGL filesystem driver.
8
+
9
+ This is the one copy, in PyDevices/omnipython-lvgl (``python/fs_driver.py``).
10
+ The MicroPython and CircuitPython ports freeze it and the CPython wheel
11
+ installs it.
12
+
13
+ Bridges LVGL's ``lv_fs`` API to the host's ``open()``, so LVGL features that
14
+ stream from files work against the platform filesystem (MicroPython VFS,
15
+ CircuitPython storage, or CPython) without enabling any ``LV_USE_FS_*`` C
16
+ driver. The main customers are runtime-loadable binary fonts and images::
17
+
18
+ import lvgl as lv
19
+ import fs_driver
20
+
21
+ fs_driver.register("S")
22
+ font = lv.binfont_create("S:fonts/montserrat_20.bin")
23
+ label.set_style_text_font(font, 0)
24
+
25
+ Streaming through this driver keeps only LVGL's parsed structures in the
26
+ heap; the source file is read incrementally (never fully buffered), which
27
+ matters for large fonts on small boards. ``lv.binfont_create_from_buffer``
28
+ remains the no-driver alternative when holding the whole file in RAM is
29
+ acceptable.
30
+
31
+ Adapted from lv_binding_micropython's ``lib/fs_driver.py`` (author mhepp).
32
+ Changes for PyDevices: portable across the three generated bindings and a
33
+ ``register()`` convenience that owns the ``lv.fs_drv_t`` instance.
34
+
35
+ Portability note: LVGL stores the handle returned by ``open_cb`` as a raw
36
+ ``void *``. The MicroPython-style runtimes pass a dict through unchanged and
37
+ ``Blob.__cast__()`` recovers it; the CPython runtime instead returns the
38
+ object's address as an int from ``__cast__()``. ``_handles`` keys every open
39
+ handle by ``id()`` (== that address on both runtimes) so one lookup works
40
+ everywhere — and it doubles as the reference that keeps the handle alive on
41
+ CPython, where LVGL's ``void *`` holds no reference.
42
+ """
43
+
44
+ import struct
45
+
46
+ import lvgl as lv
47
+
48
+ _handles = {}
49
+
50
+
51
+ def _handle(fs_file):
52
+ obj = fs_file.__cast__()
53
+ if isinstance(obj, int): # CPython runtime: raw address
54
+ return _handles[obj]
55
+ return obj # MicroPython-style runtime: the dict itself
56
+
57
+
58
+ def fs_open_cb(drv, path, mode):
59
+ if mode == lv.FS_MODE.WR:
60
+ p_mode = "wb"
61
+ elif mode == lv.FS_MODE.RD:
62
+ p_mode = "rb"
63
+ elif mode == lv.FS_MODE.WR | lv.FS_MODE.RD:
64
+ p_mode = "rb+"
65
+ else:
66
+ raise RuntimeError(
67
+ "fs_open_cb() - open mode error, %s is invalid mode" % mode
68
+ )
69
+
70
+ try:
71
+ f = open(path, p_mode) # noqa: SIM115 - closed via fs_close_cb
72
+ except OSError as e:
73
+ raise RuntimeError("fs_open_cb(%s) exception: %s" % (path, e))
74
+
75
+ handle = {"file": f, "path": path}
76
+ _handles[id(handle)] = handle
77
+ return handle
78
+
79
+
80
+ def fs_close_cb(drv, fs_file):
81
+ handle = _handle(fs_file)
82
+ _handles.pop(id(handle), None)
83
+ try:
84
+ handle["file"].close()
85
+ except OSError as e:
86
+ raise RuntimeError("fs_close_cb(%s) exception: %s" % (handle["path"], e))
87
+ return lv.FS_RES.OK
88
+
89
+
90
+ def fs_read_cb(drv, fs_file, buf, btr, br):
91
+ handle = _handle(fs_file)
92
+ try:
93
+ data = handle["file"].read(btr)
94
+ buf.__dereference__(btr)[0 : len(data)] = data
95
+ br.__dereference__(4)[0:4] = struct.pack("<L", len(data))
96
+ except OSError as e:
97
+ raise RuntimeError("fs_read_cb(%s) exception: %s" % (handle["path"], e))
98
+ return lv.FS_RES.OK
99
+
100
+
101
+ def fs_seek_cb(drv, fs_file, pos, whence):
102
+ handle = _handle(fs_file)
103
+ try:
104
+ handle["file"].seek(pos, whence)
105
+ except OSError as e:
106
+ raise RuntimeError("fs_seek_cb(%s) exception: %s" % (handle["path"], e))
107
+ return lv.FS_RES.OK
108
+
109
+
110
+ def fs_tell_cb(drv, fs_file, pos):
111
+ handle = _handle(fs_file)
112
+ try:
113
+ tpos = handle["file"].tell()
114
+ pos.__dereference__(4)[0:4] = struct.pack("<L", tpos)
115
+ except OSError as e:
116
+ raise RuntimeError("fs_tell_cb(%s) exception: %s" % (handle["path"], e))
117
+ return lv.FS_RES.OK
118
+
119
+
120
+ def fs_write_cb(drv, fs_file, buf, btw, bw):
121
+ handle = _handle(fs_file)
122
+ try:
123
+ wr = handle["file"].write(buf.__dereference__(btw)[0:btw])
124
+ bw.__dereference__(4)[0:4] = struct.pack("<L", wr)
125
+ except OSError as e:
126
+ raise RuntimeError("fs_write_cb(%s) exception: %s" % (handle["path"], e))
127
+ return lv.FS_RES.OK
128
+
129
+
130
+ def fs_register(fs_drv, letter, cache_size=500):
131
+ """Wire the callbacks into a caller-owned ``lv.fs_drv_t`` and register it.
132
+
133
+ Upstream-compatible entry point (same signature as
134
+ lv_binding_micropython). ``letter`` is the drive prefix used in LVGL
135
+ paths (``"S"`` registers ``S:...``).
136
+ """
137
+ fs_drv.init()
138
+ fs_drv.letter = ord(letter)
139
+ fs_drv.open_cb = fs_open_cb
140
+ fs_drv.read_cb = fs_read_cb
141
+ fs_drv.write_cb = fs_write_cb
142
+ fs_drv.seek_cb = fs_seek_cb
143
+ fs_drv.tell_cb = fs_tell_cb
144
+ fs_drv.close_cb = fs_close_cb
145
+
146
+ if cache_size >= 0:
147
+ fs_drv.cache_size = cache_size
148
+
149
+ fs_drv.register()
150
+ return fs_drv
151
+
152
+
153
+ # The lv.fs_drv_t must outlive the registration; keep them at module scope,
154
+ # one per drive letter.
155
+ _registered = {}
156
+
157
+
158
+ def register(letter="S", cache_size=500):
159
+ """Register drive ``letter`` backed by the platform filesystem.
160
+
161
+ Idempotent per letter: repeat calls return the existing driver. Paths
162
+ after the colon are passed to ``open()`` verbatim, so ``S:fonts/x.bin``
163
+ opens ``fonts/x.bin`` relative to the current directory and
164
+ ``S:/sd/fonts/x.bin`` opens an absolute path.
165
+ """
166
+ if letter not in _registered:
167
+ _registered[letter] = fs_register(lv.fs_drv_t(), letter, cache_size)
168
+ return _registered[letter]