Language
Types and variables
Numbers, text, true and false, durations, your own enums and structs, and how values are declared and converted.
Types
| Type | Holds |
|---|---|
int | A whole number, 32-bit: 42, -7, 0x27 |
float | A number with a fraction: 3.14, 0.5 |
bool | true or false |
string | Text: "hello" |
byte | A whole number from 0 to 255 |
Duration | A length of time: 100us, 50ms, 2s, 10min, 1hour |
Your own types are enums and structs, below.
Declaring values
let declares a value that never changes; var declares one that can. The type comes after a colon:
enum Mode { idle, running, fault }
let limit: int = 100
var mode: Mode = Mode.idle
var ratio: float = 0.5
var armed: bool = false
setup {
let half: int = int(float(limit) * ratio)
log("boot", "half={half}")
if mode == Mode.idle && !armed {
mode = Mode.running
}
}Values declared outside any block are shared by the whole program, and always need a starting value. Values declared inside a block belong to that block.
Names can’t start with
let,varorelse:variancereads asvar iance. Pick another name, likespread.
Converting between numbers
Volt never converts between int and float behind your back. Use float(n) and int(x); int drops the fraction:
setup {
let reading: int = 2048
let volts: float = float(reading) * 3.3 / 4095.0
let whole: int = int(volts)
log("adc", "{volts}V, about {whole}V")
}Durations
A duration is a number with a unit, written together: 500ms, not 500 ms. A plain number is never a duration, so every 500 is an error, and the fix offers 500ms or 500s.
| Unit | Means |
|---|---|
us | microseconds |
ms | milliseconds |
s, sec, second, seconds | seconds |
min, minute, minutes | minutes |
hr, hour, hours | hours |
Fractions work: 1.5s is 1500 milliseconds.
Text
Strings can include values with {…}, and + joins a string with anything:
setup {
let line: string = "set 42"
if starts_with(line, "set ") {
let n: int = to_int("42")
log("cmd", "value " + n + " from " + len(line) + " chars")
log("cmd", "or, the same: value {n}")
}
}len, contains, starts_with, ends_with, to_int and to_float are built in; compare strings with == and !=. A joined string is cut at 127 characters.
Enums
An enum is a fixed set of names:
enum Light { red, amber, green }
var now: Light = Light.red
every 2s {
match(now) {
.red -> now = Light.green
.green -> now = Light.amber
.amber -> now = Light.red
}
}Structs
A struct groups values under one name:
struct Reading {
celsius: float
humidity: float
}
setup {
log("boot", "ready")
}Operators
| Arithmetic | + - * / % |
| Comparison | == != < > <= >= |
| Logic | && || ! |
| Assignment | = += -= *= /= |
/ between two ints is whole-number division: 7 / 2 is 3.