Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ pip install platformio # required for automatic uploads

Place `target()` **at the very top of your script**, immediately after imports. This is the entry point that tells Reduino to parse your entire file, transpile it to Arduino C++, and (optionally) upload it.

| Parameter | Type | Default | Description |
| --------: | :----: | :-----: | ----------------------------------------------------------------------- |
| `port` | `str` | — | Serial port, e.g. `"COM3"` or `"/dev/ttyACM0"`. |
| `upload` | `bool` | `True` | If `True`, compile & upload via PlatformIO. If `False`, only transpile. |
| Parameter | Type | Default | Description |
| ---------: | :----: | :----------: | --------------------------------------------------------------------------- |
| `port` | `str` | — | Serial port, e.g. `"COM3"` or `"/dev/ttyACM0"`. |
| `upload` | `bool` | `True` | If `True`, compile & upload via PlatformIO. If `False`, only transpile. |
| `platform` | `str` | `"atmelavr"` | PlatformIO platform ID. Reduino currently supports `atmelavr` and `atmelmegaavr`. |
| `board` | `str` | `"uno"` | PlatformIO board ID. Must be compatible with `platform`. |

**Returns:** `str` of the generated Arduino C++ source.

Expand Down Expand Up @@ -145,6 +147,31 @@ print(cpp)
# Your Reduino code below...
```

### Targeting different platforms & boards

Reduino validates that the requested PlatformIO platform/board pair is supported.
At the moment two PlatformIO platforms are available:

* `atmelavr` – classic AVR-based boards (Uno, Nano, Leonardo, etc.).
* `atmelmegaavr` – newer megaAVR devices (Nano Every, Uno WiFi Rev2, Curiosity Nano kits, ...).

Every board listed in the [PlatformIO board registry for all platforms](https://docs.platformio.org/en/latest/boards/index.html) can be targeted. If you choose an unsupported board, or one that does not belong to the selected platform, `target()` raises a `ValueError` with a helpful message.

```python
from Reduino import target

# Build for an Arduino Nano Every without uploading automatically.
target(
"COM9",
upload=False,
platform="atmelmegaavr",
board="nano_every",
)

# Build for a classic Arduino Uno and upload immediately.
target("/dev/ttyUSB0", platform="atmelavr", board="uno")
```

> [!IMPORTANT]
> `target()` reads the whole file text and generates code for everything below it.
> If `upload=True`, it also builds and flashes using a temporary PlatformIO project.
Expand Down
38 changes: 34 additions & 4 deletions src/Reduino/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
import tempfile
from typing import List, Type

from Reduino.toolchain.pio import compile_upload, ensure_pio, write_project
from Reduino.toolchain.pio import (
compile_upload,
ensure_pio,
validate_platform_board,
write_project,
)
from Reduino.transpile.ast import LCDDecl, Program, ServoDecl
from Reduino.transpile.emitter import emit
from Reduino.transpile.parser import parse
Expand Down Expand Up @@ -83,7 +88,13 @@ def _visit(value: object) -> None:
return requirements


def target(port: str, *, upload: bool = True) -> None:
def target(
port: str,
*,
upload: bool = True,
platform: str = "atmelavr",
board: str = "uno",
) -> None:
"""Transpile the invoking script and prepare a PlatformIO project.

Parameters
Expand All @@ -95,8 +106,20 @@ def target(port: str, *, upload: bool = True) -> None:
after generating the temporary project directory. Uploading is
disabled by default so that unit tests can exercise the helper without
requiring an Arduino board to be connected.
platform:
PlatformIO platform identifier. Defaults to ``"atmelavr"`` for Arduino
AVR boards.
board:
PlatformIO board identifier. Defaults to ``"uno"``.

Raises
------
ValueError
If the requested platform or board is not supported or if the
combination is incompatible.
"""

validate_platform_board(platform, board)
ensure_pio()

main_file = pathlib.Path(sys.modules["__main__"].__file__)
Expand All @@ -112,8 +135,15 @@ def target(port: str, *, upload: bool = True) -> None:
cpp = emit(program)

tmp = pathlib.Path(tempfile.mkdtemp(prefix="reduino-pio-"))
write_project(tmp, cpp, port=port, lib_deps=required_libs)
write_project(
tmp,
cpp,
port=port,
platform=platform,
board=board,
lib_deps=required_libs,
)
if upload:
compile_upload(tmp)

return cpp
return cpp
Loading