Read GPI
read_gpiRead the general-purpose input pin state from a JS220 device, returning both raw 32-bit value and decoded pin states.
Instructions
Read JS220 general-purpose input pin state as a 32-bit value and decoded pins.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| device_path | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/joulescope_mcp/server.py:167-177 (registration)MCP tool registration for 'read_gpi' with title, description, readOnly annotations, and structured_output=True.
@mcp.tool( title="Read GPI", description="Read JS220 general-purpose input pin state as a 32-bit value and decoded pins.", annotations=read_only, structured_output=True, ) def read_gpi(device_path: str | None = None) -> dict[str, Any]: try: return service.read_gpi(device_path=device_path) except JoulescopeMcpError as exc: raise _tool_error(exc) from exc - src/joulescope_mcp/service.py:419-437 (handler)Core handler that opens a driver session, selects the device, publishes a request on 's/gpi/+/!req', waits for 's/gpi/+/!value' response, and returns the 32-bit GPI value plus decoded individual pin booleans.
def read_gpi(self, device_path: str | None = None) -> dict[str, Any]: with self._driver_session() as driver: device = self._select_device(driver, device_path=device_path) driver.open(device, mode="restore") try: value = driver.publish_and_wait( f"{device}/s/gpi/+/!req", 0, f"{device}/s/gpi/+/!value", timeout=1.0, ) finally: driver.close(device) return { "device_path": device, "gpi_value": int(value), "gpi_hex": f"0x{int(value):08x}", "pins": {str(i): bool(int(value) & (1 << i)) for i in range(32)}, } - tests/test_service.py:100-101 (helper)Fake driver's publish_and_wait used for testing read_gpi; returns 0b101 (binary) as the mock GPI value.
def publish_and_wait(self, *_args: Any, **_kwargs: Any) -> int: return 0b101 - tests/test_service.py:186-191 (helper)Unit test verifying read_gpi correctly decodes the pin mask from the fake driver's return value of 0b101.
def test_read_gpi_decodes_pin_mask() -> None: result = service_with(FakeDriver()).read_gpi() assert result["gpi_hex"] == "0x00000005" assert result["pins"]["0"] is True assert result["pins"]["1"] is False assert result["pins"]["2"] is True