micropython-microbit-fs 0.1.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.
- micropython_microbit_fs-0.1.0/PKG-INFO +272 -0
- micropython_microbit_fs-0.1.0/README.md +235 -0
- micropython_microbit_fs-0.1.0/pyproject.toml +107 -0
- micropython_microbit_fs-0.1.0/src/micropython_microbit_fs/.DS_Store +0 -0
- micropython_microbit_fs-0.1.0/src/micropython_microbit_fs/__init__.py +59 -0
- micropython_microbit_fs-0.1.0/src/micropython_microbit_fs/api.py +114 -0
- micropython_microbit_fs-0.1.0/src/micropython_microbit_fs/cli.py +246 -0
- micropython_microbit_fs-0.1.0/src/micropython_microbit_fs/device_info.py +121 -0
- micropython_microbit_fs-0.1.0/src/micropython_microbit_fs/exceptions.py +37 -0
- micropython_microbit_fs-0.1.0/src/micropython_microbit_fs/file.py +62 -0
- micropython_microbit_fs-0.1.0/src/micropython_microbit_fs/filesystem.py +464 -0
- micropython_microbit_fs-0.1.0/src/micropython_microbit_fs/flash_regions.py +276 -0
- micropython_microbit_fs-0.1.0/src/micropython_microbit_fs/hex_utils.py +131 -0
- micropython_microbit_fs-0.1.0/src/micropython_microbit_fs/hexes/.DS_Store +0 -0
- micropython_microbit_fs-0.1.0/src/micropython_microbit_fs/hexes/microbitv1/.DS_Store +0 -0
- micropython_microbit_fs-0.1.0/src/micropython_microbit_fs/hexes/microbitv1/v1.1.1/micropython-microbit-v1.1.1.hex +14455 -0
- micropython_microbit_fs-0.1.0/src/micropython_microbit_fs/hexes/microbitv2/.DS_Store +0 -0
- micropython_microbit_fs-0.1.0/src/micropython_microbit_fs/hexes/microbitv2/v2.1.2/micropython-microbit-v2.1.2.hex +28186 -0
- micropython_microbit_fs-0.1.0/src/micropython_microbit_fs/hexes.py +166 -0
- micropython_microbit_fs-0.1.0/src/micropython_microbit_fs/py.typed +0 -0
- micropython_microbit_fs-0.1.0/src/micropython_microbit_fs/uicr.py +89 -0
|
@@ -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,235 @@
|
|
|
1
|
+
# micropython-microbit-fs
|
|
2
|
+
|
|
3
|
+
[](https://github.com/carlosperate/python-microbit-fs/actions/workflows/test.yml)
|
|
4
|
+
[](https://pypi.org/project/micropython-microbit-fs/)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
A Python library and command line tool to inject and extract files from
|
|
8
|
+
[MicroPython](https://microbit-micropython.readthedocs.io)
|
|
9
|
+
Intel Hex file for the [BBC micro:bit](https://microbit.org).
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **Inject files** into a MicroPython hex file for flashing to micro:bit.
|
|
14
|
+
- **Extract files** from an existing micro:bit MicroPython hex file.
|
|
15
|
+
- **Get device info** from hex, including filesystem size and MicroPython version.
|
|
16
|
+
- **Command-line interface** for easy scripting and automation.
|
|
17
|
+
- Includes the latest MicroPython releases for micro:bit V1 and V2 boards.
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
To install this terminal tool we recommend using [uv](https://docs.astral.sh/uv/):
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
uv tool install micropython-microbit-fs
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
It can also be installed via pip as a normal Python package:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pip install micropython-microbit-fs
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Command Line Interface
|
|
34
|
+
|
|
35
|
+
The package includes a `microbit-fs` command for working with hex files from
|
|
36
|
+
the terminal.
|
|
37
|
+
|
|
38
|
+
### Usage
|
|
39
|
+
|
|
40
|
+
Display device information:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
$ microbit-fs info micropython.hex
|
|
44
|
+
|
|
45
|
+
Device: micro:bit V2
|
|
46
|
+
MicroPython version: micro:bit v2.1.2+0697c6d on 2023-10-30; MicroPython v1.18 on 2023-10-30
|
|
47
|
+
Flash page size: 4096 bytes
|
|
48
|
+
Filesystem size: 20480 bytes
|
|
49
|
+
Filesystem start: 0x0006D000
|
|
50
|
+
Filesystem end: 0x00073000
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
List files in a hex file:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
$ microbit-fs list micropython_with_files.hex
|
|
57
|
+
|
|
58
|
+
File Size
|
|
59
|
+
──────────────────────────────────────── ────────────
|
|
60
|
+
main.py 183 bytes
|
|
61
|
+
──────────────────────────────────────── ────────────
|
|
62
|
+
Total (1 files) 183 bytes
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Add files to a hex file:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
# Add a single file using an existing MicroPython hex
|
|
69
|
+
microbit-fs add main.py --hex-file micropython.hex
|
|
70
|
+
|
|
71
|
+
# Add multiple files with a custom output file
|
|
72
|
+
microbit-fs add main.py helper.py --hex-file micropython.hex --output output.hex
|
|
73
|
+
|
|
74
|
+
# Use bundled micro:bit V1 MicroPython hex (latest version)
|
|
75
|
+
microbit-fs add main.py --v1=latest
|
|
76
|
+
|
|
77
|
+
# Use bundled micro:bit V2 MicroPython hex (latest version)
|
|
78
|
+
microbit-fs add main.py --v2=latest --output my_program.hex
|
|
79
|
+
|
|
80
|
+
# Use a specific MicroPython version
|
|
81
|
+
microbit-fs add main.py --v1=1.1.1 --output output.hex
|
|
82
|
+
microbit-fs add main.py --v2=2.1.2 --output output.hex
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
List available bundled MicroPython versions:
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
$ microbit-fs versions
|
|
89
|
+
Bundled MicroPython hex files:
|
|
90
|
+
|
|
91
|
+
micro:bit V1:
|
|
92
|
+
- 1.1.1
|
|
93
|
+
|
|
94
|
+
micro:bit V2:
|
|
95
|
+
- 2.1.2
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Extract files from a hex file:
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
# Extract all files to the current directory
|
|
102
|
+
microbit-fs get micropython_with_files.hex
|
|
103
|
+
|
|
104
|
+
# Extract all files to a specific directory
|
|
105
|
+
microbit-fs get micropython_with_files.hex --output-dir ./extracted
|
|
106
|
+
|
|
107
|
+
# Extract a specific file
|
|
108
|
+
microbit-fs get micropython_with_files.hex --filename main.py
|
|
109
|
+
|
|
110
|
+
# Overwrite existing files without prompting
|
|
111
|
+
microbit-fs get micropython_with_files.hex --force
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
## Library Quick Start
|
|
116
|
+
|
|
117
|
+
### Add files to a MicroPython hex
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
import micropython_microbit_fs as microbit_fs
|
|
121
|
+
|
|
122
|
+
# Read your MicroPython hex file
|
|
123
|
+
with open("micropython.hex") as f:
|
|
124
|
+
micropython_hex = f.read()
|
|
125
|
+
|
|
126
|
+
# Create files to add
|
|
127
|
+
files = [
|
|
128
|
+
microbit_fs.File.from_text("main.py", "from microbit import *\ndisplay.scroll('Hello!')"),
|
|
129
|
+
microbit_fs.File.from_text("helper.py", "def greet(name):\n return f'Hello {name}'"),
|
|
130
|
+
]
|
|
131
|
+
|
|
132
|
+
# Add files and get new hex string
|
|
133
|
+
new_hex = microbit_fs.add_files(micropython_hex, files)
|
|
134
|
+
|
|
135
|
+
with open("micropython_with_files.hex", "w") as f:
|
|
136
|
+
f.write(new_hex)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Get files from a MicroPython hex
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
import micropython_microbit_fs as microbit_fs
|
|
143
|
+
|
|
144
|
+
# Read hex file with embedded files
|
|
145
|
+
with open("micropython_with_files.hex") as f:
|
|
146
|
+
hex_data = f.read()
|
|
147
|
+
|
|
148
|
+
# Get all files
|
|
149
|
+
files = microbit_fs.get_files(hex_data)
|
|
150
|
+
|
|
151
|
+
for file in files:
|
|
152
|
+
print(f"{file.name}: {file.size} bytes")
|
|
153
|
+
print(file.get_text())
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Get device information
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
import micropython_microbit_fs as microbit_fs
|
|
160
|
+
|
|
161
|
+
with open("micropython.hex") as f:
|
|
162
|
+
hex_data = f.read()
|
|
163
|
+
|
|
164
|
+
info = microbit_fs.get_device_info(hex_data)
|
|
165
|
+
print(f"Device: micro:bit {info.device_version.value}")
|
|
166
|
+
print(f"MicroPython: {info.micropython_version}")
|
|
167
|
+
print(f"Filesystem size: {info.fs_size} bytes")
|
|
168
|
+
print(f"Flash page size: {info.flash_page_size} bytes")
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Use bundled MicroPython hex files
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
import micropython_microbit_fs as microbit_fs
|
|
175
|
+
|
|
176
|
+
# List available bundled versions (dict keyed by device)
|
|
177
|
+
versions = microbit_fs.list_bundled_versions()
|
|
178
|
+
# {1: ['1.1.1'], 2: ['2.1.2']}
|
|
179
|
+
v1_versions = versions[1]
|
|
180
|
+
v2_versions = versions[2]
|
|
181
|
+
|
|
182
|
+
# Get the latest bundled hex for micro:bit V1
|
|
183
|
+
hex_data = microbit_fs.get_bundled_hex(1)
|
|
184
|
+
|
|
185
|
+
# Get a specific version
|
|
186
|
+
hex_data = microbit_fs.get_bundled_hex(2, "2.1.2")
|
|
187
|
+
|
|
188
|
+
# Add files to the bundled hex
|
|
189
|
+
files = [microbit_fs.File.from_text("main.py", "from microbit import *")]
|
|
190
|
+
new_hex = microbit_fs.add_files(hex_data, files)
|
|
191
|
+
|
|
192
|
+
with open("my_program.hex", "w") as f:
|
|
193
|
+
f.write(new_hex)
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## Development
|
|
197
|
+
|
|
198
|
+
This project uses [uv](https://docs.astral.sh/uv/) for project management.
|
|
199
|
+
|
|
200
|
+
### Setup
|
|
201
|
+
|
|
202
|
+
```bash
|
|
203
|
+
git clone https://github.com/carlosperate/python-microbit-fs.git
|
|
204
|
+
cd python-microbit-fs
|
|
205
|
+
uv sync --all-extras --dev
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### Development Commands
|
|
209
|
+
|
|
210
|
+
This project includes a `make.py` script to automate common development tasks.
|
|
211
|
+
|
|
212
|
+
```bash
|
|
213
|
+
# Run all checks (lint, typecheck, format check, test with coverage)
|
|
214
|
+
uv run python make.py check
|
|
215
|
+
|
|
216
|
+
# Format code (ruff check --fix + ruff format)
|
|
217
|
+
uv run python make.py format
|
|
218
|
+
|
|
219
|
+
# Show all available commands
|
|
220
|
+
uv run python make.py help
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
## License
|
|
224
|
+
|
|
225
|
+
MIT License - see [LICENSE](LICENSE) for details.
|
|
226
|
+
|
|
227
|
+
## Related Projects
|
|
228
|
+
|
|
229
|
+
- This project has been ported (AI assisted) from the original
|
|
230
|
+
[microbit-fs](https://github.com/microbit-foundation/microbit-fs)
|
|
231
|
+
TypeScript library.
|
|
232
|
+
- This project packs the files inside a micro:bit MicroPython hex, which
|
|
233
|
+
can then be flashed to a micro:bit.
|
|
234
|
+
Alternatively, to read and write files from a running micro:bit device over
|
|
235
|
+
USB, the [microFs](https://github.com/ntoll/microfs) CLI tool can be used.
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "micropython-microbit-fs"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Library and cli app to add and extract files from MicroPython Hex files for micro:bit"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
requires-python = ">=3.9"
|
|
8
|
+
authors = [{ name = "Carlos Pereira Atencio", email = "carlosperate@embeddedlog.com" },]
|
|
9
|
+
keywords = ["microbit", "micropython", "filesystem", "intel-hex", "embedded"]
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Development Status :: 4 - Beta",
|
|
12
|
+
"Intended Audience :: Developers",
|
|
13
|
+
"License :: OSI Approved :: MIT License",
|
|
14
|
+
"Programming Language :: Python :: 3",
|
|
15
|
+
"Programming Language :: Python :: 3.9",
|
|
16
|
+
"Programming Language :: Python :: 3.10",
|
|
17
|
+
"Programming Language :: Python :: 3.11",
|
|
18
|
+
"Programming Language :: Python :: 3.12",
|
|
19
|
+
"Programming Language :: Python :: 3.13",
|
|
20
|
+
"Programming Language :: Python :: 3.14",
|
|
21
|
+
"Topic :: Software Development :: Embedded Systems",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
dependencies = [
|
|
25
|
+
"cyclopts>=3.24.0",
|
|
26
|
+
"intelhex>=2.3.0",
|
|
27
|
+
"packaging>=21.0",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.optional-dependencies]
|
|
31
|
+
dev = [
|
|
32
|
+
"ruff>=0.8.0",
|
|
33
|
+
"pytest>=8.0.0",
|
|
34
|
+
"pytest-cov>=4.0.0",
|
|
35
|
+
"pytest-xdist>=3.0.0",
|
|
36
|
+
"pytest-sugar>=1.0.0",
|
|
37
|
+
"mypy>=1.0.0",
|
|
38
|
+
"mini-racer>=0.12.4",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[project.scripts]
|
|
42
|
+
microbit-fs = "micropython_microbit_fs.cli:main"
|
|
43
|
+
|
|
44
|
+
[project.urls]
|
|
45
|
+
Homepage = "https://github.com/carlosperate/python-microbit-fs"
|
|
46
|
+
Repository = "https://github.com/carlosperate/python-microbit-fs.git"
|
|
47
|
+
Issues = "https://github.com/carlosperate/python-microbit-fs/issues"
|
|
48
|
+
Changelog = "https://github.com/carlosperate/python-microbit-fs/blob/main/CHANGELOG.md"
|
|
49
|
+
|
|
50
|
+
[build-system]
|
|
51
|
+
requires = ["uv_build>=0.9.20,<0.10.0"]
|
|
52
|
+
build-backend = "uv_build"
|
|
53
|
+
|
|
54
|
+
[tool.ruff]
|
|
55
|
+
line-length = 88
|
|
56
|
+
target-version = "py39"
|
|
57
|
+
|
|
58
|
+
[tool.ruff.lint]
|
|
59
|
+
select = [
|
|
60
|
+
"E", # pycodestyle errors
|
|
61
|
+
"W", # pycodestyle warnings
|
|
62
|
+
"F", # Pyflakes
|
|
63
|
+
"I", # isort
|
|
64
|
+
"B", # flake8-bugbear
|
|
65
|
+
"C4", # flake8-comprehensions
|
|
66
|
+
"UP", # pyupgrade
|
|
67
|
+
"ARG", # flake8-unused-arguments
|
|
68
|
+
"SIM", # flake8-simplify
|
|
69
|
+
]
|
|
70
|
+
ignore = [
|
|
71
|
+
"E501", # line too long (handled by ruff format)
|
|
72
|
+
"B008", # do not perform function calls in argument defaults
|
|
73
|
+
"UP045", # Use `X | None` - conflicts with py39 support using Optional
|
|
74
|
+
]
|
|
75
|
+
|
|
76
|
+
[tool.ruff.lint.isort]
|
|
77
|
+
known-first-party = ["micropython_microbit_fs"]
|
|
78
|
+
|
|
79
|
+
[tool.pytest.ini_options]
|
|
80
|
+
testpaths = ["tests"]
|
|
81
|
+
python_files = ["test_*.py"]
|
|
82
|
+
python_functions = ["test_*"]
|
|
83
|
+
addopts = "--tb=short"
|
|
84
|
+
|
|
85
|
+
[tool.mypy]
|
|
86
|
+
python_version = "3.10"
|
|
87
|
+
warn_return_any = true
|
|
88
|
+
warn_unused_configs = true
|
|
89
|
+
disallow_untyped_defs = true
|
|
90
|
+
disallow_incomplete_defs = true
|
|
91
|
+
check_untyped_defs = true
|
|
92
|
+
strict_optional = true
|
|
93
|
+
warn_redundant_casts = true
|
|
94
|
+
warn_unused_ignores = false
|
|
95
|
+
ignore_missing_imports = true
|
|
96
|
+
|
|
97
|
+
[tool.coverage.run]
|
|
98
|
+
source = ["src/micropython_microbit_fs"]
|
|
99
|
+
branch = true
|
|
100
|
+
|
|
101
|
+
[tool.coverage.report]
|
|
102
|
+
exclude_lines = [
|
|
103
|
+
"pragma: no cover",
|
|
104
|
+
"def __repr__",
|
|
105
|
+
"raise NotImplementedError",
|
|
106
|
+
"if TYPE_CHECKING:",
|
|
107
|
+
]
|
|
Binary file
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
micropython-microbit-fs: Inject and extract files from MicroPython Intel Hex files.
|
|
4
|
+
|
|
5
|
+
This library provides a simple API for working with MicroPython filesystem
|
|
6
|
+
embedded in Intel Hex files for the BBC micro:bit.
|
|
7
|
+
|
|
8
|
+
Main functions:
|
|
9
|
+
- add_files: Add files to a MicroPython hex file
|
|
10
|
+
- get_files: Read files from a MicroPython hex file
|
|
11
|
+
- get_device_info: Get device memory information from a hex file
|
|
12
|
+
- get_bundled_hex: Get a bundled MicroPython hex file
|
|
13
|
+
- list_bundled_versions: List available bundled hex versions
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from micropython_microbit_fs.api import (
|
|
17
|
+
add_files,
|
|
18
|
+
get_device_info,
|
|
19
|
+
get_files,
|
|
20
|
+
)
|
|
21
|
+
from micropython_microbit_fs.device_info import DeviceInfo, DeviceVersion
|
|
22
|
+
from micropython_microbit_fs.exceptions import (
|
|
23
|
+
FilesystemError,
|
|
24
|
+
HexNotFoundError,
|
|
25
|
+
InvalidFileError,
|
|
26
|
+
InvalidHexError,
|
|
27
|
+
NotMicroPythonError,
|
|
28
|
+
StorageFullError,
|
|
29
|
+
)
|
|
30
|
+
from micropython_microbit_fs.file import File
|
|
31
|
+
from micropython_microbit_fs.hexes import (
|
|
32
|
+
MicroPythonHex,
|
|
33
|
+
get_bundled_hex,
|
|
34
|
+
list_bundled_versions,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
__version__ = "0.1.0"
|
|
38
|
+
|
|
39
|
+
__all__ = [
|
|
40
|
+
# Main API functions
|
|
41
|
+
"add_files",
|
|
42
|
+
"get_files",
|
|
43
|
+
"get_device_info",
|
|
44
|
+
# Bundled hex functions
|
|
45
|
+
"MicroPythonHex",
|
|
46
|
+
"get_bundled_hex",
|
|
47
|
+
"list_bundled_versions",
|
|
48
|
+
# Data classes
|
|
49
|
+
"File",
|
|
50
|
+
"DeviceInfo",
|
|
51
|
+
"DeviceVersion",
|
|
52
|
+
# Exceptions
|
|
53
|
+
"FilesystemError",
|
|
54
|
+
"InvalidHexError",
|
|
55
|
+
"NotMicroPythonError",
|
|
56
|
+
"InvalidFileError",
|
|
57
|
+
"StorageFullError",
|
|
58
|
+
"HexNotFoundError",
|
|
59
|
+
]
|