Skip to main content

Library and cli app to add and extract files from MicroPython Hex files for micro:bit

Project description

micropython-microbit-fs

Test PyPI versions PyPI - License

A Python library and command line tool to inject and extract files from MicroPython Intel Hex file for the BBC micro:bit.

Features

  • Inject files into a MicroPython hex file for flashing to micro:bit.
  • Extract files from an existing micro:bit MicroPython hex file.
  • Get device info from hex, including filesystem size and MicroPython version.
  • Command-line interface for easy scripting and automation.
  • Includes the latest MicroPython releases for micro:bit V1 and V2 boards.

Installation

To install this terminal tool we recommend using uv:

uv tool install micropython-microbit-fs

It can also be installed via pip as a normal Python package:

pip install micropython-microbit-fs

Command Line Interface

The package includes a microbit-fs command for working with hex files from the terminal.

Usage

Display device information:

$ microbit-fs info micropython.hex

Device: micro:bit V2
MicroPython version: micro:bit v2.1.2+0697c6d on 2023-10-30; MicroPython v1.18 on 2023-10-30
Flash page size: 4096 bytes
Filesystem size: 20480 bytes
Filesystem start: 0x0006D000
Filesystem end: 0x00073000

List files in a hex file:

$ microbit-fs list micropython_with_files.hex

File                                             Size
──────────────────────────────────────── ────────────
main.py                                     183 bytes
──────────────────────────────────────── ────────────
Total (1 files)                             183 bytes

Add files to a hex file:

# Add a single file using an existing MicroPython hex
microbit-fs add main.py --hex-file micropython.hex

# Add multiple files with a custom output file
microbit-fs add main.py helper.py --hex-file micropython.hex --output output.hex

# Use bundled micro:bit V1 MicroPython hex (latest version)
microbit-fs add main.py --v1=latest

# Use bundled micro:bit V2 MicroPython hex (latest version)
microbit-fs add main.py --v2=latest --output my_program.hex

# Use a specific MicroPython version
microbit-fs add main.py --v1=1.1.1 --output output.hex
microbit-fs add main.py --v2=2.1.2 --output output.hex

List available bundled MicroPython versions:

$ microbit-fs versions
Bundled MicroPython hex files:

micro:bit V1:
  - 1.1.1

micro:bit V2:
  - 2.1.2

Extract files from a hex file:

# Extract all files to the current directory
microbit-fs get micropython_with_files.hex

# Extract all files to a specific directory
microbit-fs get micropython_with_files.hex --output-dir ./extracted

# Extract a specific file
microbit-fs get micropython_with_files.hex --filename main.py

# Overwrite existing files without prompting
microbit-fs get micropython_with_files.hex --force

Library Quick Start

Add files to a MicroPython hex

import micropython_microbit_fs as microbit_fs

# Read your MicroPython hex file
with open("micropython.hex") as f:
    micropython_hex = f.read()

# Create files to add
files = [
    microbit_fs.File.from_text("main.py", "from microbit import *\ndisplay.scroll('Hello!')"),
    microbit_fs.File.from_text("helper.py", "def greet(name):\n    return f'Hello {name}'"),
]

# Add files and get new hex string
new_hex = microbit_fs.add_files(micropython_hex, files)

with open("micropython_with_files.hex", "w") as f:
    f.write(new_hex)

Get files from a MicroPython hex

import micropython_microbit_fs as microbit_fs

# Read hex file with embedded files
with open("micropython_with_files.hex") as f:
    hex_data = f.read()

# Get all files
files = microbit_fs.get_files(hex_data)

for file in files:
    print(f"{file.name}: {file.size} bytes")
    print(file.get_text())

Get device information

import micropython_microbit_fs as microbit_fs

with open("micropython.hex") as f:
    hex_data = f.read()

info = microbit_fs.get_device_info(hex_data)
print(f"Device: micro:bit {info.device_version.value}")
print(f"MicroPython: {info.micropython_version}")
print(f"Filesystem size: {info.fs_size} bytes")
print(f"Flash page size: {info.flash_page_size} bytes")

Use bundled MicroPython hex files

import micropython_microbit_fs as microbit_fs

# List available bundled versions (dict keyed by device)
versions = microbit_fs.list_bundled_versions()
# {1: ['1.1.1'], 2: ['2.1.2']}
v1_versions = versions[1]
v2_versions = versions[2]

# Get the latest bundled hex for micro:bit V1
hex_data = microbit_fs.get_bundled_hex(1)

# Get a specific version
hex_data = microbit_fs.get_bundled_hex(2, "2.1.2")

# Add files to the bundled hex
files = [microbit_fs.File.from_text("main.py", "from microbit import *")]
new_hex = microbit_fs.add_files(hex_data, files)

with open("my_program.hex", "w") as f:
    f.write(new_hex)

Development

This project uses uv for project management.

Setup

git clone https://github.com/carlosperate/python-microbit-fs.git
cd python-microbit-fs
uv sync --all-extras --dev

Development Commands

This project includes a make.py script to automate common development tasks.

# Run all checks (lint, typecheck, format check, test with coverage)
uv run python make.py check

# Format code (ruff check --fix + ruff format)
uv run python make.py format

# Show all available commands
uv run python make.py help

License

MIT License - see LICENSE for details.

Related Projects

  • This project has been ported (AI assisted) from the original microbit-fs TypeScript library.
  • This project packs the files inside a micro:bit MicroPython hex, which can then be flashed to a micro:bit. Alternatively, to read and write files from a running micro:bit device over USB, the microFs CLI tool can be used.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

micropython_microbit_fs-0.1.0.tar.gz (785.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

micropython_microbit_fs-0.1.0-py3-none-any.whl (790.6 kB view details)

Uploaded Python 3

File details

Details for the file micropython_microbit_fs-0.1.0.tar.gz.

File metadata

  • Download URL: micropython_microbit_fs-0.1.0.tar.gz
  • Upload date:
  • Size: 785.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for micropython_microbit_fs-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c2ff88fd0637e663c94a42ad21a145da0f011a0a3b92027fa74a768d9104ce6c
MD5 c96799fca804bd2eba2292cfd14aeacc
BLAKE2b-256 68b808fada61bb3940670e9e108ea5650d13fd246479e957ef2409ee361e7780

See more details on using hashes here.

File details

Details for the file micropython_microbit_fs-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: micropython_microbit_fs-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 790.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for micropython_microbit_fs-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 379d38d1b61eec684ac5b65de59a565754c24cf7b6f29bda15e1865a58a5d157
MD5 f7e502ebe077bb640d914d8bd86edc79
BLAKE2b-256 e32a2c317ad6546204ef0e0e85c1f36fdc0ac42b53d215183ecd58cd7c915c90

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page