OC2R-Core
Minimal Rust client for the OC2R high-level API. It mirrors the bundled Lua
helper (devices.lua) so you can list devices, invoke methods,
and subscribe to events. For wrappers on top of these primitives, add
the opencomputers crate.
Getting Started
- Install cross:
cargo install cross --git https://github.com/cross-rs/cross - Compile your binary for Minux:
cross build --release - Copy
target/<triple>/release/your-apponto the Import/Export card and drop it inside the OC2R VM (for example/usr/bin/your-app).
Quick Start
use oc2r_core::{DeviceBus, DeviceEvent, Result, DEFAULT_DEVICE_PATH};
fn main() -> Result<()> {
let mut bus = DeviceBus::connect(DEFAULT_DEVICE_PATH)?;
// Enumerate devices.
let devices = bus.list()?;
println!("Found {} device(s)", devices.len());
// Call a method dynamically.
let first = &devices[0].device_id;
let mut device = bus.device(first)?.expect("device disappeared");
let response = device.call("help", ())?;
println!("help(): {response}");
// Subscribe to events and drain one.
device.subscribe()?;
let event = bus.next_event_for(&device_id)?;
println!("{device_id} -> {:#?}", event.payload);
Ok(())
}
Transport & API Surface
- Framing: Messages are JSON framed with a leading/trailing NUL, exactly
like the Lua RPC client.
DeviceBus::flushdrains stale responses before each call. - Dynamic calls: Use
Device::callorDeviceBus::call. Anything that implementsIntoJsonArgs(tuples, slices,Vec<T>) is accepted and converted into JSON automatically. - Subscriptions:
Device::subscribe/Device::unsubscribesend the corresponding RPC requests.DeviceBus::next_event_for(device_id)waits for the next matching event;Device::next_event()provides a convenience wrapper.try_event*variants check queues without blocking. For a Lua-style dispatcher, useEventLoopwhich mirrorsevent.pulland friends.
Development
The crate targets rust nightly for size optimisations and relies on miniserde
and libc.