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

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

Modules