Language guide
Wrench for XOBIT
Wrench is a compact, C-like scripting language embedded in the XOBIT firmware. It compiles source into bytecode, then the board runs your `setup()` and `loop()` functions with XOBIT modules.
This page summarizes the upstream Wrench reference at home.workshopfriends.com/wrench/www and the XOBIT-specific modules in the local firmware.
Mental Model
- C-like and weakly typed. Wrench syntax looks familiar if you know JavaScript, Arduino C, or Processing, but values can convert between ints, floats, strings, and containers.
- Small enough for firmware. Upstream Wrench is designed for embedded use: compact bytecode, low overhead integration, and native callbacks into host C/C++ code.
- XOBIT adds the sketch loop. In XOBIT, write a `setup()` function for one-time configuration and a `loop()` function for repeated work.
- Module calls are native calls. Functions such as `ledConfig()`, `uiSlider()`, `haSwitch()`, and `httpGet()` are XOBIT firmware services exposed to Wrench.
Variables And Values
Declare variables with `var`. Wrench natively handles 32-bit integers, floats, and 8-bit character strings. Variable names follow C-style rules: start with a letter or `_`, then use letters, numbers, or `_`.
var count = 0; var brightness = 120; var temperature = 21.5; var label = "hourglass";
Comments
// Single-line comments work. var stripPin = 16; // xobit-circuit: IO16 ledStrip /* Block comments work too. Use them for longer notes. */
Operators
Wrench supports familiar C-style arithmetic, comparisons, logical operators, bitwise operators, and compound assignment.
count = count + 1; brightness += 10; if (brightness > 255) brightness = 255; var isBright = brightness >= 200; var masked = count & 15; var wrapped = count % 60;
Functions
Functions are called like C or JavaScript functions. XOBIT looks for `setup()` and `loop()` by name.
function scaleBrightness(value) {
return constrain(value, 0, 255);
}
function setup() {
println("ready");
}
function loop() {
var b = scaleBrightness(120);
delay(20);
}
Conditionals And Loops
Use `if`, `else`, `while`, `for`, and `switch` for control flow. In XOBIT, prefer short loops that return control to the firmware often.
var i = 0;
while (i < 10) {
println(i);
i = i + 1;
}
for (var pixel = 0; pixel < ledCount(0); pixel++) {
ledSetHsv(0, pixel, pixel * 8, 255, 80);
}
Arrays
Arrays are zero-based and can contain mixed values. For hot LED loops, reuse small scratch arrays instead of allocating new ones every frame.
var rgb[] = { 0, 0, 0 };
var values[] = { "zero", 1, 3.55 };
println(values[1]);
ledGetRgb(0, 3, rgb);
rgbToHsv(rgb, rgb);
XOBIT Sketch Pattern
// One sentence describing the sketch.
var lastFrameAt = 0;
function setup() {
ledConfig(0, 16, 30, 80);
}
function loop() {
if ((millis() - lastFrameAt) < 30) {
delay(1);
return;
}
lastFrameAt = millis();
// Draw one frame here.
ledShow();
}
XOBIT Differences To Remember
- Use constants such as `OUTPUT`, `HIGH`, and `LOW`, for example `pinMode(16, OUTPUT)` and `digitalWrite(16, HIGH)`.
- Use top-level math helpers such as `sin()`, `cos()`, `map()`, `constrain()`, `radians()`, and `degrees()` for generated sketches.
- Float arithmetic and direct `println(floatValue)` work, but avoid building strings with floats in hot paths.
- Use `timeLocal(out)`, `sunLocal(..., out)`, `ledGetRgbInto(...)`, and color conversion `*Into` helpers with reusable output arrays.
- For online data, prefer `fetchJson()` and `getJsonValue()` when you only need a few fields.
Modules
Print, timing, random, heap, and error helpers.
memoryPins and actuatorsPins, analog input, PWM, servo, and fan helpers.
wavesMath and motionScaling helpers, trigonometry, and noise.
scheduleLocal timeSynced clock values and local date parts.
wb_sunnySun locationSun position, daylight brightness, and color temperature.
flareLED stripsNeoPixel-style setup, RGB/HSV pixels, brightness, and frames.
tuneBrowser UIControls and events exposed by a running sketch.
home_iot_deviceHome AssistantExpose entities and consume HA events.
data_objectOnline dataFetch web values without excessive heap pressure.