Skip to content

Platform Devices (std.camera, std.bluetooth, std.serial, std.hid)

Flame includes native hardware drivers and hardware peripheral communication bindings directly within the standard library. Scripts interact with physical devices directly from top-level code without needing boilerplate main functions.


Discover connected camera imaging hardware and capture high-resolution snapshots directly to disk:

import std.camera
// List all connected video capture devices
let cameras = camera.devices()
println($"Detected {cameras.len()} camera device(s)")
for cam in cameras {
println($"[{cam.index}] {cam.name}: {cam.description}")
}
// Capture an image frame from device index 0 and save directly to disk
if cameras.len() > 0 {
let success = camera.capture(0, "snapshot.png")
if success {
println("Snapshot successfully captured and saved to snapshot.png")
}
}

Check local system BLE capability, enumerate host adapters, and scan for nearby advertising peripherals:

import std.bluetooth
// Verify host Bluetooth support
if bluetooth.supported() {
println("Bluetooth is supported on this system.")
// Enumerate local Bluetooth radio adapters
let adapters = bluetooth.adapters()
println($"Found {adapters.len()} host adapter(s)")
// Scan nearby BLE peripherals (performs an active 3-second scan)
println("Scanning for Bluetooth LE devices...")
let devices = bluetooth.scan()
for dev in devices {
println($"Device: {dev.name} | MAC Address: {dev.address} | Connected: {dev.connected}")
}
}

Query available RS-232 / COM communication port interfaces on the host system:

import std.serial
// Discover available hardware serial communication ports
let ports = serial.ports()
println($"Detected {ports.len()} serial interface(s):")
for port in ports {
println($"Port: {port.name}")
}

Enumerate Human Interface Devices (custom hardware, gamepads, interface controllers) and communicate via raw byte buffers:

import std.hid
// Enumerate all connected USB HID peripherals
let devices = hid.devices()
println($"Total HID peripherals detected: {devices.len()}")
for dev in devices {
println($"[{dev.vendor}:{dev.product}] {dev.manufacturer} - {dev.product_name} (Path: {dev.path})")
}
// Open device connection by Vendor ID and Product ID (e.g., VID: 0x046D, PID: 0xC077)
if devices.len() > 0 {
let handle = hid.open(1133, 49271)
if hid.isOpen(handle) {
println($"Successfully opened device handle: {handle}")
// Read up to 64 bytes with a 1000 millisecond timeout
let data = hid.readTimeout(handle, 64, 1000)
println($"Received payload bytes: {data}")
// Close HID device handle when finished
hid.close(handle)
}
}

Method Arguments Returns Description
camera.devices None Tuple<Formula> Lists available imaging hardware. Each formula contains { index, name, description }.
camera.capture index: Int, path: String Bool Opens camera index, captures a single high-resolution image, saves to path, and returns true.
Method Arguments Returns Description
bluetooth.supported None Bool Checks if the host operating system supports Bluetooth capabilities.
bluetooth.adapters None Tuple<Formula> Returns a list of discovered host Bluetooth radio adapters containing { index, name }.
bluetooth.scan None Tuple<Formula> Performs a 3-second scan and returns nearby peripherals containing { name, address, connected }.
Method Arguments Returns Description
serial.ports None Tuple<Formula> Lists available serial port interfaces on the host (COM ports / /dev/tty), returning { name }.
Method Arguments Returns Description
hid.devices None Tuple<Formula> Lists all connected HID peripherals with vendor, product, interface, and path details.
hid.open vendor: Int, product: Int Int Opens a connection to a device by Vendor ID and Product ID, returning a handle ID.
hid.openPath path: String Int Opens a connection to a device directly by system device path string, returning a handle ID.
hid.close handle: Int Nil Closes an open HID device handle and releases system hardware resources.
hid.isOpen handle: Int Bool Returns true if the specified HID handle ID currently refers to an open device.
hid.read handle: Int, len: Int Bytes Reads up to len bytes from an open HID device handle (blocks according to blocking mode).
hid.readTimeout handle: Int, len: Int, timeout_ms: Int Bytes Reads up to len bytes from an open HID device with a specified millisecond timeout.
hid.poll handle: Int, len: Int Bytes Performs an immediate, non-blocking read poll of up to len bytes (timeout 0).
hid.write handle: Int, payload: Bytes Int Writes a raw byte array payload to an open HID device and returns the bytes written count.
hid.getFeatureReport handle: Int, report_id: Int, len: Int Bytes Requests and reads a feature report packet of up to len bytes starting with report_id.
hid.sendOutputReport handle: Int, payload: Bytes Int Transmits an output report payload to the HID device and returns the bytes written count.
hid.setBlocking handle: Int, blocking: Bool Nil Sets whether subsequent reads on the device handle block indefinitely or return immediately.