Language
Channels
Pass values from one part of a program to another through a queue, safely.
A channel is a small queue. One part of the program sends values into it; another takes them out, in the order they went in.
channel events: int[8]
every 1s { events.send(1) }
task consumer {
every 100ms {
if events.has_data() {
let e: int = events.receive()
log("event", "got {e}")
}
}
}int[8] is the type of value and how many the channel can hold. Leave the size out for a default buffer.
Sending and receiving
send(value)adds a value. If the channel is full, the value is dropped, so a slow reader never blocks the sender.receive()takes the oldest value out. It never waits: when the channel is empty it returns0, so checkhas_data()first.count(),is_empty()andis_full()tell you how much is waiting;clear()drops it all.
Channels are safe between parts of the program that run separately, even between a strict timer block and an ordinary task: that’s what they’re for.
When to use one
Use a channel when one part of the program produces things for another to deal with in its own time: button presses for a menu, readings for an uploader, commands from MQTT for the motor control. For a value that’s only shared, like the latest temperature, a global var is enough.