How-to guides
Hardware (I2C, camera)
Hardware is given by Context.Hardware (IHardwareAccess). Every device is a resource (i2c-1:0x40, video:/dev/video0, gpio:18) that only one instance can use at a time. The second instance stops with an error (Faulted, "busy: used by instance 'X'").
protected override Task InitializeAsync(CancellationToken ct)
{
_device = Context.Hardware.OpenI2cDevice(Settings.BusId, Settings.Address); // reserves and opens
WriteNeutral();
Context.Hardware.RegisterFailsafe(() => { lock (_lock) WriteNeutral(); }); // watchdog
return Task.CompletedTask;
}
Rules
- The opened devices belong to the instance: on stop and on a fault KOFA first runs the registered failsafes, then closes the devices and releases the reservation. You do not need to (and should not) close them in
DisposeAsync. - If the settings reveal which resource is needed, the settings record should implement
IHardwareResourceSettings: the graph validation then reports before applying if two nodes would ask for the same one. - For an actuator (motor, servo, relay) the input
Deadline+OnInputTimeoutis mandatory, and anestopinput is recommended; see Lifecycle and safety.
Camera
OpenVideoCapture(new VideoCaptureOptions(device, width, height, fps)), then ReadFramesAsync gives JPEG frames (MJPEG without decoding). The output type is CameraFrame. A frame is large data: for remote use a relay priority of Low suits it.
Kofa.Abstractions.Imaging provides a baseline JpegDecoder and an ImageResampler.ResizeBox resizer. Progressive JPEG is not decoded; use one JpegDecoder instance from one thread.
Simulated hardware
On a development machine (Windows, macOS) the simulated backend runs: every I2C address answers (it stores the registers), the camera gives a test image, and Context.Hardware.IsSimulated is true. So your Komod can be tried without a real device.
Other devices (GPIO, serial port)
Acquire("gpio:18") reserves a resource without opening a device: you drive the pin or port yourself (for example with your own NuGet package such as System.Device.Gpio), and the reservation prevents another instance from using the same one. The returned IHardwareLease is released on stop. Native libraries: Native dependencies.