Files

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

  1. Install cross:
    cargo install cross --git https://github.com/cross-rs/cross
    
  2. Compile your binary for Minux:
    cross build --release
    
  3. Copy target/<triple>/release/your-app onto 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::flush drains stale responses before each call.
  • Dynamic calls: Use Device::call or DeviceBus::call. Anything that implements IntoJsonArgs (tuples, slices, Vec<T>) is accepted and converted into JSON automatically.
  • Subscriptions: Device::subscribe / Device::unsubscribe send 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, use EventLoop which mirrors event.pull and friends.

Development

The crate targets rust nightly for size optimisations and relies on miniserde and libc.