Skip to content

Add Micropython Bindings#64

Open
Snooz82 wants to merge 2 commits intoTinkerforge:masterfrom
Snooz82:micropython
Open

Add Micropython Bindings#64
Snooz82 wants to merge 2 commits intoTinkerforge:masterfrom
Snooz82:micropython

Conversation

@Snooz82
Copy link

@Snooz82 Snooz82 commented Mar 20, 2026

Summary

Add MicroPython bindings generator for Tinkerforge devices. This enables using Bricks and Bricklets from MicroPython environments (ESP32, Raspberry Pi Pico, etc.) via TCP/IP.

Design Decisions

Synchronous/polling architecture (like PHP, unlike Python)
MicroPython doesn't support the threading module on most boards. Instead of Python's multi-threaded approach (background receive thread, callback queue thread, disconnect probe thread), the MicroPython bindings use explicit callback dispatching via ipcon.dispatch_callbacks(seconds), following the same pattern as the PHP bindings. The user controls when callbacks are processed — either in an infinite loop (dispatch_callbacks(-1)) or polled periodically.

Flat module structure (no package hierarchy)
MicroPython boards have limited filesystems and no pip/setuptools. All bindings are flat .py files meant to be copied directly to the board (via mpremote, Thonny, ampy, etc.). There is no __init__.py, no setup.py, and no namespace package. Imports use the flat form: from ip_connection import IPConnection rather than Python's from tinkerforge.ip_connection import IPConnection.

No auto-reconnect
Auto-reconnect in the Python bindings relies on a background thread to detect disconnections and re-establish the connection. Without threading, this isn't possible, so set_auto_reconnect() is not provided. Users must handle reconnection explicitly.

Simplified examples

  • No if __name__ == "__main__": guard — MicroPython scripts are executed directly
  • No input("Press key to exit") — embedded systems typically don't have interactive stdin
  • Callback examples end with ipcon.dispatch_callbacks(-1); non-callback examples end with time.sleep(1)
  • Shebang is #!/usr/bin/env micropython

No threading-related locks
The Python bindings use stream_lock for high-level streaming functions. Since MicroPython is single-threaded, these locks are omitted from generated bindings.

Minimal testing
The tester runs py_compile.compile() for syntax validation only (no pylint). MicroPython environments don't typically have linting tools available.

Device validity check in callback dispatch
Matches PHP's pattern: _dispatch_pending_callbacks() calls check_validity() before dispatching each callback, silently skipping callbacks for devices that have been replaced or don't match.

What's Included

  • ip_connection.py — full protocol implementation (connect, enumerate, authenticate, callback dispatch, streaming)
  • generate_micropython_bindings.py — generates device binding files
  • generate_micropython_doc.py — generates RST API documentation (EN/DE)
  • generate_micropython_examples.py — generates example scripts
  • generate_micropython_stubs.py — generate stubs for IDE support
  • generate_micropython_zip.py — packages bindings for distribution
  • micropython_common.py — language-specific Device/Packet/Element subclasses
  • test_micropython_bindings.py — syntax validation tester
  • example_enumerate.py, example_authenticate.py — IP Connection examples

Verification

  • All generators run without errors
  • 472/472 tests pass

Manual tests

I did test it with an ESP32-WROOM board via Wifi (connecting to a master with Wifi Extension).
I just made a small play application that can be started on the ESP32 by importing it.

import test_rotary_oled

Unfortunately i have not all the bricklets and bricks, that i can test it. 😉

config.py
test_rotary_oled.py

I also made a PR for the doc repo.

Best regards
René

Copilot AI review requested due to automatic review settings March 20, 2026 14:43

This comment was marked as spam.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@borg42
Copy link
Member

borg42 commented Mar 20, 2026

Very cool!

We will have to take a detailed look at it. Currently all of our devs are super busy with other projects, so it will unfortunately take a few days until we review this PR and can give you feedback.

But we will schedule someone!

Edit: Meta-Question: Did you schedule the Copilot review or is that something that automatically happened? I don't mind if you scheduled it, but if it is something that was done automatically i find it a bit rude and would rather turn it off by default.
Edit2: I think i found the setting, the automatic Copilot-Pull-Request-Review is now turned off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants