This uses the protothreads approach to enable imperative synchronous programming (as promoted by Blech) in pure C.
/// This allows to delay for a multiple of the base tick.
pa_activity (Delay, pa_ctx(unsigned i), unsigned ticks) {
pa_self.i = ticks;
while (pa_self.i > 0) {
--pa_self.i;
pa_pause;
}
} pa_activity_end;
/// This blinks an LED with 2 ticks red and 1 tick off(black).
pa_activity (Blink, pa_ctx(pa_use(Delay))) {
while (true) {
setLED(RED);
pa_run (Delay, 2);
setLED(BLACK);
pa_run (Delay, 1);
}
} pa_activity_end;
/// This drives a blinking LED and preempts it after 10 ticks.
pa_activity (Main, pa_ctx(pa_co_res(2); pa_use(Blink); pa_use(Delay))) {
pa_co(2) {
pa_with (Delay, 10);
pa_with_weak (Blink);
} pa_co_end;
} pa_activity_end;
This will blink an LED with a period of 2 ticks red and 1 tick black for a total of 10 ticks.
For a description of the statements, currently refer to the Blech documentation.
- A medium article about proto_activities can be found here.
- Here is a little robot with
proto_activities
running on three ESP32 nodes. - See running proto_activities code in this online Wokwi simulator.
- Pappe is a sibling project which uses an embedded DSL to allow Blech-style imperative synchronous programming in Swift.