micropython-microbit-fs 0.1.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.
- micropython_microbit_fs/.DS_Store +0 -0
- micropython_microbit_fs/__init__.py +59 -0
- micropython_microbit_fs/api.py +114 -0
- micropython_microbit_fs/cli.py +246 -0
- micropython_microbit_fs/device_info.py +121 -0
- micropython_microbit_fs/exceptions.py +37 -0
- micropython_microbit_fs/file.py +62 -0
- micropython_microbit_fs/filesystem.py +464 -0
- micropython_microbit_fs/flash_regions.py +276 -0
- micropython_microbit_fs/hex_utils.py +131 -0
- micropython_microbit_fs/hexes/.DS_Store +0 -0
- micropython_microbit_fs/hexes/microbitv1/.DS_Store +0 -0
- micropython_microbit_fs/hexes/microbitv1/v1.1.1/micropython-microbit-v1.1.1.hex +14455 -0
- micropython_microbit_fs/hexes/microbitv2/.DS_Store +0 -0
- micropython_microbit_fs/hexes/microbitv2/v2.1.2/micropython-microbit-v2.1.2.hex +28186 -0
- micropython_microbit_fs/hexes.py +166 -0
- micropython_microbit_fs/py.typed +0 -0
- micropython_microbit_fs/uicr.py +89 -0
- micropython_microbit_fs-0.1.0.dist-info/METADATA +272 -0
- micropython_microbit_fs-0.1.0.dist-info/RECORD +22 -0
- micropython_microbit_fs-0.1.0.dist-info/WHEEL +4 -0
- micropython_microbit_fs-0.1.0.dist-info/entry_points.txt +3 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Bundled MicroPython hex files for micro:bit.
|
|
3
|
+
|
|
4
|
+
This module provides access to the bundled MicroPython Intel Hex files
|
|
5
|
+
for micro:bit V1 and V2. The hex files are organised in the `hexes/` directory
|
|
6
|
+
using the following folder structure:
|
|
7
|
+
|
|
8
|
+
hexes/
|
|
9
|
+
microbitv1/
|
|
10
|
+
v{semver}/
|
|
11
|
+
filename-v{semver}.hex
|
|
12
|
+
microbitv2/
|
|
13
|
+
v{semver}/
|
|
14
|
+
filename-v{semver}.hex
|
|
15
|
+
|
|
16
|
+
The micro:bit version and MicroPython version are determined by the folder path,
|
|
17
|
+
not the filename.
|
|
18
|
+
|
|
19
|
+
Example paths:
|
|
20
|
+
- hexes/microbitv1/v1.1.1/micropython-microbit-v1.1.1.hex
|
|
21
|
+
- hexes/microbitv2/v2.1.2/micropython-microbit-v2.1.2.hex
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from __future__ import annotations
|
|
25
|
+
|
|
26
|
+
import re
|
|
27
|
+
from dataclasses import dataclass
|
|
28
|
+
from importlib import resources
|
|
29
|
+
from pathlib import Path
|
|
30
|
+
from typing import Optional
|
|
31
|
+
|
|
32
|
+
from packaging.version import Version
|
|
33
|
+
|
|
34
|
+
from micropython_microbit_fs.exceptions import HexNotFoundError
|
|
35
|
+
|
|
36
|
+
# Regex patterns for device/version folders
|
|
37
|
+
DEVICE_FOLDER_PATTERN = re.compile(r"^microbitv(\d+)$")
|
|
38
|
+
VERSION_FOLDER_PATTERN = re.compile(r"^v(\d+\.\d+\.\d+)$")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@dataclass(frozen=True)
|
|
42
|
+
class MicroPythonHex:
|
|
43
|
+
"""Metadata and contents for a bundled MicroPython hex file."""
|
|
44
|
+
|
|
45
|
+
file_path: Path
|
|
46
|
+
version: str
|
|
47
|
+
device_version: int
|
|
48
|
+
content: str
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _get_hexes_dir() -> Path:
|
|
52
|
+
"""Get the path to the bundled hexes directory."""
|
|
53
|
+
# Use importlib.resources to access package data
|
|
54
|
+
return Path(str(resources.files("micropython_microbit_fs") / "hexes"))
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def list_bundled_versions(device_version: Optional[int] = None) -> dict[int, list[str]]:
|
|
58
|
+
"""List available MicroPython versions.
|
|
59
|
+
|
|
60
|
+
Example::
|
|
61
|
+
>>> list_bundled_versions()
|
|
62
|
+
{1: ['1.1.1'], 2: ['2.1.2']}
|
|
63
|
+
>>> list_bundled_versions(1)
|
|
64
|
+
{1: ['1.1.1']}
|
|
65
|
+
|
|
66
|
+
:param device_version: The micro:bit device version (1 or 2). ``None`` lists all.
|
|
67
|
+
:return: Mapping of device version to sorted versions (newest first).
|
|
68
|
+
"""
|
|
69
|
+
hexes_dir = _get_hexes_dir()
|
|
70
|
+
|
|
71
|
+
# Determine which device folders to inspect
|
|
72
|
+
target_devices: list[int]
|
|
73
|
+
if device_version is None:
|
|
74
|
+
target_devices = []
|
|
75
|
+
for entry in hexes_dir.iterdir():
|
|
76
|
+
if not entry.is_dir():
|
|
77
|
+
continue
|
|
78
|
+
match = DEVICE_FOLDER_PATTERN.match(entry.name)
|
|
79
|
+
if match:
|
|
80
|
+
target_devices.append(int(match.group(1)))
|
|
81
|
+
else:
|
|
82
|
+
target_devices = [device_version]
|
|
83
|
+
|
|
84
|
+
versions_by_device: dict[int, list[str]] = {}
|
|
85
|
+
|
|
86
|
+
for target in target_devices:
|
|
87
|
+
device_dir = hexes_dir / f"microbitv{target}"
|
|
88
|
+
versions: list[str] = []
|
|
89
|
+
|
|
90
|
+
if device_dir.exists():
|
|
91
|
+
for version_folder in device_dir.iterdir():
|
|
92
|
+
if not version_folder.is_dir():
|
|
93
|
+
continue
|
|
94
|
+
match = VERSION_FOLDER_PATTERN.match(version_folder.name)
|
|
95
|
+
if match:
|
|
96
|
+
semver = match.group(1)
|
|
97
|
+
# Hex filename is not important, but ensure exactly one exists
|
|
98
|
+
hex_files = [
|
|
99
|
+
p
|
|
100
|
+
for p in version_folder.iterdir()
|
|
101
|
+
if p.suffix.lower() == ".hex"
|
|
102
|
+
]
|
|
103
|
+
if len(hex_files) != 1:
|
|
104
|
+
raise HexNotFoundError(
|
|
105
|
+
f"Unexpected number of hex files found in path {version_folder} (should be exactly 1)."
|
|
106
|
+
)
|
|
107
|
+
versions.append(semver)
|
|
108
|
+
|
|
109
|
+
versions.sort(key=lambda v: Version(v), reverse=True)
|
|
110
|
+
versions_by_device[target] = versions
|
|
111
|
+
|
|
112
|
+
return versions_by_device
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def get_bundled_hex(
|
|
116
|
+
device_version: int, version: Optional[str] = None
|
|
117
|
+
) -> MicroPythonHex:
|
|
118
|
+
"""Get a bundled MicroPython hex file and metadata.
|
|
119
|
+
|
|
120
|
+
:param device_version: The micro:bit device version (1 or 2).
|
|
121
|
+
:param version: Optional MicroPython version string (e.g., "1.1.0").
|
|
122
|
+
If not provided, returns the latest available version.
|
|
123
|
+
:return: A frozen dataclass with filename, version string, and file contents.
|
|
124
|
+
:raises HexNotFoundError: If no hex file is found for the specified
|
|
125
|
+
device/version.
|
|
126
|
+
|
|
127
|
+
Example::
|
|
128
|
+
|
|
129
|
+
>>> hex_file = get_bundled_hex(1) # Get latest V1 hex
|
|
130
|
+
>>> hex_file.file_path.name
|
|
131
|
+
'micropython-microbit-v1.1.1.hex'
|
|
132
|
+
>>> hex_file.version
|
|
133
|
+
'1.1.1'
|
|
134
|
+
>>> hex_file.device_version
|
|
135
|
+
1
|
|
136
|
+
>>> hex_file.content[:2]
|
|
137
|
+
'::'
|
|
138
|
+
"""
|
|
139
|
+
available_versions_map = list_bundled_versions(device_version)
|
|
140
|
+
available_versions = available_versions_map.get(device_version, [])
|
|
141
|
+
if not available_versions:
|
|
142
|
+
raise HexNotFoundError(
|
|
143
|
+
f"No bundled MicroPython hex files found for micro:bit V{device_version}."
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
if version is None:
|
|
147
|
+
# Use the latest version (first in the sorted list)
|
|
148
|
+
selected_version = available_versions[0]
|
|
149
|
+
else:
|
|
150
|
+
if version not in available_versions:
|
|
151
|
+
raise HexNotFoundError(
|
|
152
|
+
f"MicroPython version '{version}' not found for micro:bit V{device_version}. "
|
|
153
|
+
f"Available versions: {', '.join(available_versions)}"
|
|
154
|
+
)
|
|
155
|
+
selected_version = version
|
|
156
|
+
|
|
157
|
+
# list_bundled_versions() ensures a single hex file exists in the version folder
|
|
158
|
+
hex_path = _get_hexes_dir() / f"microbitv{device_version}" / f"v{selected_version}"
|
|
159
|
+
hex_files = [p for p in hex_path.iterdir() if p.suffix.lower() == ".hex"]
|
|
160
|
+
hex_file = hex_files[0]
|
|
161
|
+
return MicroPythonHex(
|
|
162
|
+
file_path=hex_file,
|
|
163
|
+
version=selected_version,
|
|
164
|
+
device_version=device_version,
|
|
165
|
+
content=hex_file.read_text(),
|
|
166
|
+
)
|
|
File without changes
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
Parse UICR data from micro:bit MicroPython hex files.
|
|
4
|
+
|
|
5
|
+
The UICR (User Information Configuration Registers) contains MicroPython
|
|
6
|
+
specific data including:
|
|
7
|
+
- Magic value to identify MicroPython
|
|
8
|
+
- Flash page size
|
|
9
|
+
- Runtime start/end pages
|
|
10
|
+
- Version string location
|
|
11
|
+
|
|
12
|
+
For more info:
|
|
13
|
+
https://microbit-micropython.readthedocs.io/en/latest/devguide/hexformat.html
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
from intelhex import IntelHex
|
|
19
|
+
|
|
20
|
+
from micropython_microbit_fs.device_info import DEVICE_SPECS, DeviceInfo
|
|
21
|
+
from micropython_microbit_fs.hex_utils import (
|
|
22
|
+
read_string,
|
|
23
|
+
read_uint16,
|
|
24
|
+
read_uint32,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
# UICR addresses
|
|
28
|
+
UICR_START = 0x10001000
|
|
29
|
+
UICR_CUSTOMER_OFFSET = 0x80
|
|
30
|
+
UICR_CUSTOMER_UPY_OFFSET = 0x40
|
|
31
|
+
UICR_UPY_START = UICR_START + UICR_CUSTOMER_OFFSET + UICR_CUSTOMER_UPY_OFFSET
|
|
32
|
+
|
|
33
|
+
# UICR field addresses
|
|
34
|
+
UicrAddress = {
|
|
35
|
+
"MAGIC": UICR_UPY_START + 0, # 4 bytes - Magic value,
|
|
36
|
+
"END_MARKER": UICR_UPY_START + 4, # 4 bytes - End marker
|
|
37
|
+
"PAGE_SIZE": UICR_UPY_START + 8, # 4 bytes - Page size (log2)
|
|
38
|
+
"START_PAGE": UICR_UPY_START + 12, # 2 bytes - Start page number
|
|
39
|
+
"PAGES_USED": UICR_UPY_START + 14, # 2 bytes - Number of pages used
|
|
40
|
+
"DELIMITER": UICR_UPY_START + 16, # 4 bytes - Delimiter
|
|
41
|
+
"VERSION_LOC": UICR_UPY_START + 20, # 4 bytes - Address of version string
|
|
42
|
+
"REG_TERMINATOR": UICR_UPY_START + 24, # 4 bytes - Flash regions terminator
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def get_device_info_from_uicr(ih: IntelHex) -> DeviceInfo | None:
|
|
47
|
+
"""
|
|
48
|
+
Extract DeviceInfo from UICR data in an IntelHex object.
|
|
49
|
+
|
|
50
|
+
:param ih: IntelHex object containing the hex data.
|
|
51
|
+
:returns: DeviceInfo if valid MicroPython UICR data is found, None otherwise.
|
|
52
|
+
"""
|
|
53
|
+
magic = read_uint32(ih, UicrAddress["MAGIC"])
|
|
54
|
+
page_size_log2 = read_uint32(ih, UicrAddress["PAGE_SIZE"])
|
|
55
|
+
start_page = read_uint16(ih, UicrAddress["START_PAGE"])
|
|
56
|
+
pages_used = read_uint16(ih, UicrAddress["PAGES_USED"])
|
|
57
|
+
version_address = read_uint32(ih, UicrAddress["VERSION_LOC"])
|
|
58
|
+
|
|
59
|
+
for device in DEVICE_SPECS.values():
|
|
60
|
+
if device.uicr_magic == magic:
|
|
61
|
+
device_spec = device
|
|
62
|
+
break
|
|
63
|
+
else:
|
|
64
|
+
# Unknown magic value
|
|
65
|
+
return None
|
|
66
|
+
|
|
67
|
+
page_size = 2**page_size_log2
|
|
68
|
+
flash_size = device_spec.flash_size
|
|
69
|
+
flash_start = start_page * page_size
|
|
70
|
+
flash_end = flash_start + device_spec.flash_size
|
|
71
|
+
runtime_start = flash_start
|
|
72
|
+
runtime_end = pages_used * page_size
|
|
73
|
+
fs_start = runtime_end
|
|
74
|
+
fs_end = device_spec.fs_end_address
|
|
75
|
+
version = read_string(ih, version_address)
|
|
76
|
+
device_version = device_spec.device_version
|
|
77
|
+
|
|
78
|
+
return DeviceInfo(
|
|
79
|
+
flash_page_size=page_size,
|
|
80
|
+
flash_size=flash_size,
|
|
81
|
+
flash_start_address=flash_start,
|
|
82
|
+
flash_end_address=flash_end,
|
|
83
|
+
runtime_start_address=runtime_start,
|
|
84
|
+
runtime_end_address=runtime_end,
|
|
85
|
+
fs_start_address=fs_start,
|
|
86
|
+
fs_end_address=fs_end,
|
|
87
|
+
micropython_version=version,
|
|
88
|
+
device_version=device_version,
|
|
89
|
+
)
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: micropython-microbit-fs
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Library and cli app to add and extract files from MicroPython Hex files for micro:bit
|
|
5
|
+
Keywords: microbit,micropython,filesystem,intel-hex,embedded
|
|
6
|
+
Author: Carlos Pereira Atencio
|
|
7
|
+
Author-email: Carlos Pereira Atencio <carlosperate@embeddedlog.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
19
|
+
Classifier: Topic :: Software Development :: Embedded Systems
|
|
20
|
+
Requires-Dist: cyclopts>=3.24.0
|
|
21
|
+
Requires-Dist: intelhex>=2.3.0
|
|
22
|
+
Requires-Dist: packaging>=21.0
|
|
23
|
+
Requires-Dist: ruff>=0.8.0 ; extra == 'dev'
|
|
24
|
+
Requires-Dist: pytest>=8.0.0 ; extra == 'dev'
|
|
25
|
+
Requires-Dist: pytest-cov>=4.0.0 ; extra == 'dev'
|
|
26
|
+
Requires-Dist: pytest-xdist>=3.0.0 ; extra == 'dev'
|
|
27
|
+
Requires-Dist: pytest-sugar>=1.0.0 ; extra == 'dev'
|
|
28
|
+
Requires-Dist: mypy>=1.0.0 ; extra == 'dev'
|
|
29
|
+
Requires-Dist: mini-racer>=0.12.4 ; extra == 'dev'
|
|
30
|
+
Requires-Python: >=3.9
|
|
31
|
+
Project-URL: Changelog, https://github.com/carlosperate/python-microbit-fs/blob/main/CHANGELOG.md
|
|
32
|
+
Project-URL: Homepage, https://github.com/carlosperate/python-microbit-fs
|
|
33
|
+
Project-URL: Issues, https://github.com/carlosperate/python-microbit-fs/issues
|
|
34
|
+
Project-URL: Repository, https://github.com/carlosperate/python-microbit-fs.git
|
|
35
|
+
Provides-Extra: dev
|
|
36
|
+
Description-Content-Type: text/markdown
|
|
37
|
+
|
|
38
|
+
# micropython-microbit-fs
|
|
39
|
+
|
|
40
|
+
[](https://github.com/carlosperate/python-microbit-fs/actions/workflows/test.yml)
|
|
41
|
+
[](https://pypi.org/project/micropython-microbit-fs/)
|
|
42
|
+
[](LICENSE)
|
|
43
|
+
|
|
44
|
+
A Python library and command line tool to inject and extract files from
|
|
45
|
+
[MicroPython](https://microbit-micropython.readthedocs.io)
|
|
46
|
+
Intel Hex file for the [BBC micro:bit](https://microbit.org).
|
|
47
|
+
|
|
48
|
+
## Features
|
|
49
|
+
|
|
50
|
+
- **Inject files** into a MicroPython hex file for flashing to micro:bit.
|
|
51
|
+
- **Extract files** from an existing micro:bit MicroPython hex file.
|
|
52
|
+
- **Get device info** from hex, including filesystem size and MicroPython version.
|
|
53
|
+
- **Command-line interface** for easy scripting and automation.
|
|
54
|
+
- Includes the latest MicroPython releases for micro:bit V1 and V2 boards.
|
|
55
|
+
|
|
56
|
+
## Installation
|
|
57
|
+
|
|
58
|
+
To install this terminal tool we recommend using [uv](https://docs.astral.sh/uv/):
|
|
59
|
+
|
|
60
|
+
```
|
|
61
|
+
uv tool install micropython-microbit-fs
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
It can also be installed via pip as a normal Python package:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
pip install micropython-microbit-fs
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Command Line Interface
|
|
71
|
+
|
|
72
|
+
The package includes a `microbit-fs` command for working with hex files from
|
|
73
|
+
the terminal.
|
|
74
|
+
|
|
75
|
+
### Usage
|
|
76
|
+
|
|
77
|
+
Display device information:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
$ microbit-fs info micropython.hex
|
|
81
|
+
|
|
82
|
+
Device: micro:bit V2
|
|
83
|
+
MicroPython version: micro:bit v2.1.2+0697c6d on 2023-10-30; MicroPython v1.18 on 2023-10-30
|
|
84
|
+
Flash page size: 4096 bytes
|
|
85
|
+
Filesystem size: 20480 bytes
|
|
86
|
+
Filesystem start: 0x0006D000
|
|
87
|
+
Filesystem end: 0x00073000
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
List files in a hex file:
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
$ microbit-fs list micropython_with_files.hex
|
|
94
|
+
|
|
95
|
+
File Size
|
|
96
|
+
──────────────────────────────────────── ────────────
|
|
97
|
+
main.py 183 bytes
|
|
98
|
+
──────────────────────────────────────── ────────────
|
|
99
|
+
Total (1 files) 183 bytes
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Add files to a hex file:
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
# Add a single file using an existing MicroPython hex
|
|
106
|
+
microbit-fs add main.py --hex-file micropython.hex
|
|
107
|
+
|
|
108
|
+
# Add multiple files with a custom output file
|
|
109
|
+
microbit-fs add main.py helper.py --hex-file micropython.hex --output output.hex
|
|
110
|
+
|
|
111
|
+
# Use bundled micro:bit V1 MicroPython hex (latest version)
|
|
112
|
+
microbit-fs add main.py --v1=latest
|
|
113
|
+
|
|
114
|
+
# Use bundled micro:bit V2 MicroPython hex (latest version)
|
|
115
|
+
microbit-fs add main.py --v2=latest --output my_program.hex
|
|
116
|
+
|
|
117
|
+
# Use a specific MicroPython version
|
|
118
|
+
microbit-fs add main.py --v1=1.1.1 --output output.hex
|
|
119
|
+
microbit-fs add main.py --v2=2.1.2 --output output.hex
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
List available bundled MicroPython versions:
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
$ microbit-fs versions
|
|
126
|
+
Bundled MicroPython hex files:
|
|
127
|
+
|
|
128
|
+
micro:bit V1:
|
|
129
|
+
- 1.1.1
|
|
130
|
+
|
|
131
|
+
micro:bit V2:
|
|
132
|
+
- 2.1.2
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Extract files from a hex file:
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
# Extract all files to the current directory
|
|
139
|
+
microbit-fs get micropython_with_files.hex
|
|
140
|
+
|
|
141
|
+
# Extract all files to a specific directory
|
|
142
|
+
microbit-fs get micropython_with_files.hex --output-dir ./extracted
|
|
143
|
+
|
|
144
|
+
# Extract a specific file
|
|
145
|
+
microbit-fs get micropython_with_files.hex --filename main.py
|
|
146
|
+
|
|
147
|
+
# Overwrite existing files without prompting
|
|
148
|
+
microbit-fs get micropython_with_files.hex --force
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
## Library Quick Start
|
|
153
|
+
|
|
154
|
+
### Add files to a MicroPython hex
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
import micropython_microbit_fs as microbit_fs
|
|
158
|
+
|
|
159
|
+
# Read your MicroPython hex file
|
|
160
|
+
with open("micropython.hex") as f:
|
|
161
|
+
micropython_hex = f.read()
|
|
162
|
+
|
|
163
|
+
# Create files to add
|
|
164
|
+
files = [
|
|
165
|
+
microbit_fs.File.from_text("main.py", "from microbit import *\ndisplay.scroll('Hello!')"),
|
|
166
|
+
microbit_fs.File.from_text("helper.py", "def greet(name):\n return f'Hello {name}'"),
|
|
167
|
+
]
|
|
168
|
+
|
|
169
|
+
# Add files and get new hex string
|
|
170
|
+
new_hex = microbit_fs.add_files(micropython_hex, files)
|
|
171
|
+
|
|
172
|
+
with open("micropython_with_files.hex", "w") as f:
|
|
173
|
+
f.write(new_hex)
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Get files from a MicroPython hex
|
|
177
|
+
|
|
178
|
+
```python
|
|
179
|
+
import micropython_microbit_fs as microbit_fs
|
|
180
|
+
|
|
181
|
+
# Read hex file with embedded files
|
|
182
|
+
with open("micropython_with_files.hex") as f:
|
|
183
|
+
hex_data = f.read()
|
|
184
|
+
|
|
185
|
+
# Get all files
|
|
186
|
+
files = microbit_fs.get_files(hex_data)
|
|
187
|
+
|
|
188
|
+
for file in files:
|
|
189
|
+
print(f"{file.name}: {file.size} bytes")
|
|
190
|
+
print(file.get_text())
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Get device information
|
|
194
|
+
|
|
195
|
+
```python
|
|
196
|
+
import micropython_microbit_fs as microbit_fs
|
|
197
|
+
|
|
198
|
+
with open("micropython.hex") as f:
|
|
199
|
+
hex_data = f.read()
|
|
200
|
+
|
|
201
|
+
info = microbit_fs.get_device_info(hex_data)
|
|
202
|
+
print(f"Device: micro:bit {info.device_version.value}")
|
|
203
|
+
print(f"MicroPython: {info.micropython_version}")
|
|
204
|
+
print(f"Filesystem size: {info.fs_size} bytes")
|
|
205
|
+
print(f"Flash page size: {info.flash_page_size} bytes")
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### Use bundled MicroPython hex files
|
|
209
|
+
|
|
210
|
+
```python
|
|
211
|
+
import micropython_microbit_fs as microbit_fs
|
|
212
|
+
|
|
213
|
+
# List available bundled versions (dict keyed by device)
|
|
214
|
+
versions = microbit_fs.list_bundled_versions()
|
|
215
|
+
# {1: ['1.1.1'], 2: ['2.1.2']}
|
|
216
|
+
v1_versions = versions[1]
|
|
217
|
+
v2_versions = versions[2]
|
|
218
|
+
|
|
219
|
+
# Get the latest bundled hex for micro:bit V1
|
|
220
|
+
hex_data = microbit_fs.get_bundled_hex(1)
|
|
221
|
+
|
|
222
|
+
# Get a specific version
|
|
223
|
+
hex_data = microbit_fs.get_bundled_hex(2, "2.1.2")
|
|
224
|
+
|
|
225
|
+
# Add files to the bundled hex
|
|
226
|
+
files = [microbit_fs.File.from_text("main.py", "from microbit import *")]
|
|
227
|
+
new_hex = microbit_fs.add_files(hex_data, files)
|
|
228
|
+
|
|
229
|
+
with open("my_program.hex", "w") as f:
|
|
230
|
+
f.write(new_hex)
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
## Development
|
|
234
|
+
|
|
235
|
+
This project uses [uv](https://docs.astral.sh/uv/) for project management.
|
|
236
|
+
|
|
237
|
+
### Setup
|
|
238
|
+
|
|
239
|
+
```bash
|
|
240
|
+
git clone https://github.com/carlosperate/python-microbit-fs.git
|
|
241
|
+
cd python-microbit-fs
|
|
242
|
+
uv sync --all-extras --dev
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### Development Commands
|
|
246
|
+
|
|
247
|
+
This project includes a `make.py` script to automate common development tasks.
|
|
248
|
+
|
|
249
|
+
```bash
|
|
250
|
+
# Run all checks (lint, typecheck, format check, test with coverage)
|
|
251
|
+
uv run python make.py check
|
|
252
|
+
|
|
253
|
+
# Format code (ruff check --fix + ruff format)
|
|
254
|
+
uv run python make.py format
|
|
255
|
+
|
|
256
|
+
# Show all available commands
|
|
257
|
+
uv run python make.py help
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
## License
|
|
261
|
+
|
|
262
|
+
MIT License - see [LICENSE](LICENSE) for details.
|
|
263
|
+
|
|
264
|
+
## Related Projects
|
|
265
|
+
|
|
266
|
+
- This project has been ported (AI assisted) from the original
|
|
267
|
+
[microbit-fs](https://github.com/microbit-foundation/microbit-fs)
|
|
268
|
+
TypeScript library.
|
|
269
|
+
- This project packs the files inside a micro:bit MicroPython hex, which
|
|
270
|
+
can then be flashed to a micro:bit.
|
|
271
|
+
Alternatively, to read and write files from a running micro:bit device over
|
|
272
|
+
USB, the [microFs](https://github.com/ntoll/microfs) CLI tool can be used.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
micropython_microbit_fs/.DS_Store,sha256=sJCJ1a9O1Gae_hIXRjG1O_vwKIo7gz_2gRFe-0tvTmw,6148
|
|
2
|
+
micropython_microbit_fs/__init__.py,sha256=fr5PcF8Yv13scWSb737QXMUORxCv1sZISapi5rJNU1I,1528
|
|
3
|
+
micropython_microbit_fs/api.py,sha256=tnpPp5rRa4lXNCIig1q4254ejnHfwyodspZ7sgR7wMw,4026
|
|
4
|
+
micropython_microbit_fs/cli.py,sha256=WlkVV0BshX_9Ez6-gJcy8AI_rIyde5ixEogByk18c6Y,8305
|
|
5
|
+
micropython_microbit_fs/device_info.py,sha256=cSJ5VILQ0lE0d2Gh6gOPQhvqH80-pb9esYno4Y3UXNo,3438
|
|
6
|
+
micropython_microbit_fs/exceptions.py,sha256=ELDQb8zyQphL51-WPmBddPu9MKXsDVMLHxt9GPP5K-c,741
|
|
7
|
+
micropython_microbit_fs/file.py,sha256=ryNgCFf4eQ3vK2z7lZeWrRFmfTk3Pe4STxoiQcSr8H4,1907
|
|
8
|
+
micropython_microbit_fs/filesystem.py,sha256=VhstR88kNpIG9yqPIwuDTaOJPLD4soiFxpwMFLTEkJ4,15783
|
|
9
|
+
micropython_microbit_fs/flash_regions.py,sha256=fRtqjg-6cvlXPbLN87brMUSOOWDNiXrgy_FRFXY7lrQ,8772
|
|
10
|
+
micropython_microbit_fs/hex_utils.py,sha256=KyVavsO4ARGUbj9Ixh3uIu6BBbT1its4737-u7HRTYE,3888
|
|
11
|
+
micropython_microbit_fs/hexes/.DS_Store,sha256=dooJkRPNrvjXmcN3kxtEXnz7U6RBQ5dxWA_IPYpAMQs,6148
|
|
12
|
+
micropython_microbit_fs/hexes/microbitv1/.DS_Store,sha256=QI4ST-RRzHM2695mYD0wlbtVEM4CuRtKNovB7RB2mH0,6148
|
|
13
|
+
micropython_microbit_fs/hexes/microbitv1/v1.1.1/micropython-microbit-v1.1.1.hex,sha256=02huZpZ31Fbs6dIGpEuKy-c933PhoyEf-JuTlyuVMVI,635792
|
|
14
|
+
micropython_microbit_fs/hexes/microbitv2/.DS_Store,sha256=wlzvdw6KFHiXLk4kW0I_V6q_ODxODr601kFDiuh8Ecw,6148
|
|
15
|
+
micropython_microbit_fs/hexes/microbitv2/v2.1.2/micropython-microbit-v2.1.2.hex,sha256=Zvrge3F3fp46a1EionkwskzAvlACq5pn6SSUxUoWRe8,1239726
|
|
16
|
+
micropython_microbit_fs/hexes.py,sha256=SVE9_mlDHvrEVT3ueIGaKcX0PndeucK8DFYpFj4cOY0,5624
|
|
17
|
+
micropython_microbit_fs/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
18
|
+
micropython_microbit_fs/uicr.py,sha256=utcSo8MhaoUbIkBomBS759z2mxUP8N8VVrIkXq8MarY,2983
|
|
19
|
+
micropython_microbit_fs-0.1.0.dist-info/WHEEL,sha256=RRVLqVugUmFOqBedBFAmA4bsgFcROUBiSUKlERi0Hcg,79
|
|
20
|
+
micropython_microbit_fs-0.1.0.dist-info/entry_points.txt,sha256=ezUl-Ghp4t_RPLWhdQXToinaV3hA3dfd_NYnybFMm3w,66
|
|
21
|
+
micropython_microbit_fs-0.1.0.dist-info/METADATA,sha256=uqqy8tdUQnQdHxwcvKtLs0xWfbPMvSwIJ6G8rKKqBQU,8371
|
|
22
|
+
micropython_microbit_fs-0.1.0.dist-info/RECORD,,
|