From fca92541f6f995192b3e9217a6e089c8c7ce348e Mon Sep 17 00:00:00 2001 From: Jean-Pierre Martineau Date: Sun, 22 Nov 2026 23:51:12 -0500 Subject: [PATCH] The device screen names the Cap's slot, and the node names the Cap Until MeshTerm connects, nothing can say which LoRa Cap is on a Cardputer Zero. A radio on the SPI bus answers nothing until the node runs on it, and the list probes nothing on purpose: a radio is in it exactly when there is a device file to open. A probe would have to power the header on every pass of the device screen, and it still could not tell the older Cap from an empty header, because that answer comes from the radio itself. So the row names the slot. It reads "Cap LoRa", the family of the two boards, in place of a claim that the newer one is attached. The node names the board. It is the one part that sees which revision answered, so the parent sends it the model of each revision and the node reports the one that it found. The rest of the path was already there: the frame server gives that model to the client, the store keeps it beside the node name, and the hardware column of the device screen shows it. A Cap is exact from its first connection onward, with no probing, no power to the header for a listing, and no new permissions. board_name is gone. Its one caller wanted the model, which board_models answers now, and the row takes its name from the table as it always did. Co-Authored-By: Claude Opus 5 --- docs/devices/cardputer-zero.md | 7 +++-- docs/guide/configuration.md | 8 ++++-- meshterm/core/radionode.py | 31 ++++++++++++++++++--- meshterm/core/spiradio.py | 50 +++++++++++++++++++++++++++------- tests/test_radionode.py | 17 ++++++++++++ tests/test_spiradio.py | 38 ++++++++++++++++++-------- 6 files changed, 121 insertions(+), 30 deletions(-) diff --git a/docs/devices/cardputer-zero.md b/docs/devices/cardputer-zero.md index ce8e21a..bc22c3d 100644 --- a/docs/devices/cardputer-zero.md +++ b/docs/devices/cardputer-zero.md @@ -199,8 +199,11 @@ says that power without the antenna can permanently damage the radio. 3. Push the Cap onto the expansion header at the back. 4. Turn on the Cardputer Zero, and start MeshTerm. -The device screen lists the Cap as **Cap LoRa-1262**. `meshterm --spi` also reaches it from the -command line. +The device screen lists the Cap as **Cap LoRa**. It cannot say more before it connects, +because a radio on the SPI bus answers nothing until MeshTerm starts the node on it. After +the first connection, the hardware column of the device screen names the Cap that +answered: **Cap LoRa-1262**, or **Cap LoRa868** for the older one. `meshterm --spi` also +reaches the Cap from the command line. The Cap has no MeshCore firmware of its own. It is only a radio chip. Thus, while MeshTerm is open, MeshTerm is the node, as it is on the AIO board of a uConsole. MeshTerm needs the diff --git a/docs/guide/configuration.md b/docs/guide/configuration.md index c059173..4875d90 100644 --- a/docs/guide/configuration.md +++ b/docs/guide/configuration.md @@ -93,9 +93,11 @@ to install the library. **Two boards need no configuration.** The wiring of an AIO v1 is the MeshTerm default. Thus, when `/dev/spidev1.0` exists, the device screen lists it. The name is **uConsole AIO** -on a uConsole, and **SPI radio** on any other machine. MeshTerm also includes the Cap -LoRa-1262 of M5Stack on a Cardputer Zero. The device screen lists it as **Cap LoRa-1262** -on a Cardputer Zero (refer to [the Cardputer Zero page](../devices/cardputer-zero.md#the-cap-lora-1262)). +on a uConsole, and **SPI radio** on any other machine. MeshTerm also includes the LoRa +Cap of M5Stack on a Cardputer Zero. The device screen lists it as **Cap LoRa**, because +until MeshTerm connects, nothing can say which of the two Caps is attached. After it +connects, the hardware column names the one that answered (refer to [the Cardputer Zero +page](../devices/cardputer-zero.md#the-cap-lora-1262)). For either board, `meshterm --spi` reaches the radio. To add any other board, write a profile that states how the board is wired. A profile on the same `/dev/spidev*` replaces the wiring that MeshTerm includes. diff --git a/meshterm/core/radionode.py b/meshterm/core/radionode.py index 51d6014..a4c4d8f 100644 --- a/meshterm/core/radionode.py +++ b/meshterm/core/radionode.py @@ -85,7 +85,7 @@ BEGIN_BACKOFF_S = 1.5 #: The model string that the frame server reports for a radio on a board that MeshTerm #: does not know by name. MeshTerm shows it as the device model (the Cap reports -#: "Cap LoRa-1262"). +#: "Cap LoRa-1262", or "Cap LoRa868" where it found no expander). DEVICE_MODEL = "MeshTerm SPI node" log = logging.getLogger("radionode") @@ -513,6 +513,29 @@ def confirm_sx126x(lora: Any) -> None: log.info("the chip reports itself as %s", name) +def board_model(config: dict, expander: bool | None) -> str: + """The model that the node reports for the board it is on. + + The parent cannot know which revision of a board is attached, because nothing answers + on an SPI radio until the node runs. For this reason it sends the model of each + revision, and the node names the one that it found. A board with one revision has one + model, and the question does not come up. + + Args: + config: The configuration from the parent (``model``, and + ``model_without_expander`` for a board that has a revision with no expander). + expander: What :func:`prepare_board` found (``False`` for an absent one). + + Returns: + The model to report, or :data:`DEVICE_MODEL` for a board MeshTerm does not know. + """ + if expander is False: + without = str(config.get("model_without_expander") or "") + if without: + return without + return str(config.get("model") or DEVICE_MODEL) + + def begin_failure(kind: str, detail: str, expander: bool | None) -> NodeError: """The error that a failed ``radio.begin()`` gives the user. @@ -836,8 +859,8 @@ async def run_node(config: dict, report) -> int: # noqa: ANN001 - (dict) -> Non Args: config: ``state_dir``, ``wiring`` (the pins and switches of the board), ``seed`` (name and radio settings for a node that has no ``prefs.json`` yet), and - ``model`` (the name of the board, reported as the device model. Without it, - :data:`DEVICE_MODEL`). + ``model`` and ``model_without_expander`` (the name of the board, reported as + the device model. Refer to :func:`board_model`). report: Sends the one status line to the parent. Returns: @@ -878,7 +901,7 @@ async def run_node(config: dict, report) -> int: # noqa: ANN001 - (dict) -> Non seed, report, expander=board.expander, - model=str(config.get("model") or DEVICE_MODEL), + model=board_model(config, board.expander), models=models, sx1262=sx1262, companion_mod=companion_mod, diff --git a/meshterm/core/spiradio.py b/meshterm/core/spiradio.py index 90e0f6c..4620af6 100644 --- a/meshterm/core/spiradio.py +++ b/meshterm/core/spiradio.py @@ -345,12 +345,19 @@ class BuiltinRadio: marker. board: A test that only its machine passes, for a machine that has no single path of its own (:func:`is_uconsole`). ``None`` for no test. + with_expander: The model of the revision whose RF expander answers, for a board + that has two revisions. Empty where the board has one revision, and then + ``name`` is its model. + without_expander: The model of the revision that has no expander. Empty for a + board that must have one. """ name: str wiring: SpiWiring marker: str = "" board: Callable[[], bool] | None = None + with_expander: str = "" + without_expander: str = "" def present(self) -> bool: """Whether this machine is its board: the SPI device file and all its signs are there.""" @@ -369,22 +376,40 @@ class BuiltinRadio: BUILTIN_RADIOS = ( BuiltinRadio("uConsole AIO", SpiWiring(), board=is_uconsole), BuiltinRadio("", SpiWiring()), - BuiltinRadio("Cap LoRa-1262", CARDPUTER_ZERO_CAP, marker="/sys/class/leds/ext_5v_out"), + BuiltinRadio( + "Cap LoRa", + CARDPUTER_ZERO_CAP, + marker="/sys/class/leds/ext_5v_out", + with_expander="Cap LoRa-1262", + without_expander="Cap LoRa868", + ), ) -def board_name(wiring: SpiWiring) -> str: - """The name of the shipped board on which the radio of ``wiring`` is, or ``""`` for none. +def board_models(wiring: SpiWiring) -> tuple[str, str]: + """The model of the board of ``wiring`` with its RF expander, and the one without it. - The node reports this name as its model. Thus the Cap shows as "Cap LoRa-1262", and the - AIO as "uConsole AIO", in all places that show the model of a device. The match uses - the SPI device file on the board of this machine, not the full wiring. Thus a profile - that changes the pins of the Cap is still the Cap. + The device screen names the **slot**: the name in :data:`BUILTIN_RADIOS`, which is the + family where a board has two revisions ("Cap LoRa"), because nothing answers until the + node runs. The **model** is what the node found there, and a board with two revisions + has two of them. The match uses the SPI device file on the board of this machine, not + the full wiring. Thus a profile that changes the pins of the Cap is still the Cap. + + The node is given both models and names the one that it found (refer to + ``meshterm.core.radionode.board_model``). The Cap LoRa-1262 has the expander, and the + older Cap LoRa868 has none. + + Args: + wiring: The wiring of the radio, of a profile or of a board. + + Returns: + The model with the expander and the model without it. The second is empty for a + board that must have one, and both are empty where this machine is not its board. """ for radio in BUILTIN_RADIOS: if radio.name and radio.wiring.spidev == wiring.spidev and radio.present(): - return radio.name - return "" + return (radio.with_expander or radio.name, radio.without_expander) + return ("", "") def builtin_wiring(spidev: str) -> SpiWiring | None: @@ -806,6 +831,10 @@ async def start_node(wiring: SpiWiring, state: Path, *, node_name: str) -> NodeP state.mkdir(parents=True, exist_ok=True) for line in await asyncio.to_thread(import_bridge_state, state): _log.info("spi radio: carried over %s", line) + # Both models go to the node, because only the node can see which revision of the board + # is attached. It answers with the one it found, and that model reaches the device + # screen through the store of remembered devices. + model, model_without_expander = board_models(wiring) config = { "state_dir": str(state), "wiring": { @@ -832,7 +861,8 @@ async def start_node(wiring: SpiWiring, state: Path, *, node_name: str) -> NodeP "gps_baud": wiring.gps_baud, }, "seed": {"node_name": node_name, **DEFAULT_SEED}, - "model": board_name(wiring), + "model": model, + "model_without_expander": model_without_expander, } log_path = state / "node.log" _rotate(log_path) diff --git a/tests/test_radionode.py b/tests/test_radionode.py index adf392a..b4316c9 100644 --- a/tests/test_radionode.py +++ b/tests/test_radionode.py @@ -267,6 +267,23 @@ def test_a_radio_that_does_not_start_says_the_useful_thing( assert str(error) == radionode.NO_RADIO, "one sentence for a radio that is not there" +@pytest.mark.parametrize( + ("expander", "expected"), + [(True, "Cap LoRa-1262"), (False, "Cap LoRa868"), (None, "Cap LoRa-1262")], + ids=["found", "absent", "no-expander-in-wiring"], +) +def test_the_node_reports_the_revision_that_it_found(expander: bool | None, expected: str) -> None: + """The parent sends both names, because only the node can see which board answered.""" + config = {"model": "Cap LoRa-1262", "model_without_expander": "Cap LoRa868"} + assert radionode.board_model(config, expander) == expected + + +def test_a_board_with_one_revision_keeps_its_one_model() -> None: + """The AIO has no second name, and a board MeshTerm does not know has none at all.""" + assert radionode.board_model({"model": "uConsole AIO"}, False) == "uConsole AIO" + assert radionode.board_model({}, False) == radionode.DEVICE_MODEL + + def test_a_chip_that_cannot_be_read_is_not_a_board() -> None: """A read that fails is the same answer as a read of nothing: no board is attached.""" diff --git a/tests/test_spiradio.py b/tests/test_spiradio.py index a86018b..e84adad 100644 --- a/tests/test_spiradio.py +++ b/tests/test_spiradio.py @@ -263,7 +263,13 @@ def cardputer(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: "BUILTIN_RADIOS", ( spiradio.BuiltinRadio("", SpiWiring()), - spiradio.BuiltinRadio("Cap LoRa-1262", spiradio.CARDPUTER_ZERO_CAP, marker=str(marker)), + spiradio.BuiltinRadio( + "Cap LoRa", + spiradio.CARDPUTER_ZERO_CAP, + marker=str(marker), + with_expander="Cap LoRa-1262", + without_expander="Cap LoRa868", + ), ), ) return marker @@ -272,7 +278,7 @@ def cardputer(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: def test_the_cardputer_cap_is_listed_with_no_profile(cardputer: Path) -> None: """On a Cardputer Zero, the Cap is a radio like the AIO: it is present, named, and wired.""" radios = spiradio.spi_radios({}) - assert [(d.port, d.name) for d in radios] == [("/dev/spidev0.1", "Cap LoRa-1262")] + assert [(d.port, d.name) for d in radios] == [("/dev/spidev0.1", "Cap LoRa")] assert radios[0].spi == spiradio.CARDPUTER_ZERO_CAP @@ -283,14 +289,24 @@ def test_a_bare_spidev_node_is_not_a_cardputer(cardputer: Path) -> None: assert spiradio.builtin_wiring("/dev/spidev0.1") is None -def test_the_cap_reports_itself_by_name(cardputer: Path) -> None: - """The model of the node is the name of the board: the Cap, also under a changed profile.""" - assert spiradio.board_name(spiradio.CARDPUTER_ZERO_CAP) == "Cap LoRa-1262" +def test_the_node_is_given_the_model_of_each_revision(cardputer: Path) -> None: + """The node sees which Cap is attached. Thus the parent sends it both names.""" + assert spiradio.board_models(spiradio.CARDPUTER_ZERO_CAP) == ("Cap LoRa-1262", "Cap LoRa868") tweaked = replace(spiradio.CARDPUTER_ZERO_CAP, use_dio3_tcxo=False) - assert spiradio.board_name(tweaked) == "Cap LoRa-1262" - assert spiradio.board_name(SpiWiring()) == "" # the node of the AIO: no board name to give + assert spiradio.board_models(tweaked) == ("Cap LoRa-1262", "Cap LoRa868"), "a profile of it" + assert spiradio.board_models(SpiWiring()) == ("", "") # the AIO: no name to give cardputer.rmdir() - assert spiradio.board_name(spiradio.CARDPUTER_ZERO_CAP) == "" # another Pi + assert spiradio.board_models(spiradio.CARDPUTER_ZERO_CAP) == ("", "") # another Pi + + +def test_the_shipped_table_knows_both_revisions_of_the_cap() -> None: + """The fixture above copies the table. This keeps the table itself from drifting.""" + cap = next(r for r in spiradio.BUILTIN_RADIOS if r.wiring is spiradio.CARDPUTER_ZERO_CAP) + assert (cap.name, cap.with_expander, cap.without_expander) == ( + "Cap LoRa", + "Cap LoRa-1262", + "Cap LoRa868", + ) #: Each mark of a uConsole, as the CM5 uConsole of JP showed them. @@ -324,7 +340,7 @@ def test_the_aio_is_named_on_a_uconsole(uconsole: set) -> None: """When each mark of a uConsole is present, the AIO is the uConsole AIO.""" radios = spiradio.spi_radios({}) assert [(d.port, d.name) for d in radios] == [("/dev/spidev1.0", "uConsole AIO")] - assert spiradio.board_name(SpiWiring()) == "uConsole AIO" + assert spiradio.board_models(SpiWiring()) == ("uConsole AIO", "") @pytest.mark.parametrize("missing", _UCONSOLE_MARKS, ids=str) @@ -333,14 +349,14 @@ def test_one_missing_mark_is_enough_to_doubt_it(uconsole: set, missing: object) uconsole.discard(missing) radios = spiradio.spi_radios({}) assert [(d.port, d.name) for d in radios] == [("/dev/spidev1.0", None)] - assert spiradio.board_name(SpiWiring()) == "" + assert spiradio.board_models(SpiWiring()) == ("", "") def test_the_keyboard_s_names_are_part_of_its_mark(uconsole: set) -> None: """1eaf:0024 is any LeafLabs Maple. Only the uConsole keyboard of ClockworkPi counts.""" uconsole.discard(spiradio.UCONSOLE_KEYBOARD) uconsole.add(("1eaf", "0024", "LeafLabs", "Maple")) - assert spiradio.board_name(SpiWiring()) == "" + assert spiradio.board_models(SpiWiring()) == ("", "") def test_usb_devices_are_read_from_sysfs(tmp_path: Path) -> None: