Event Handlers
Controllers can use Final’s EventLoop: you register one of your methods on an Event object, and then Final raises an Event, and your TypedObject method runs. Background on the model: Events and the EventLoop.
For Pi work you mainly use:
| Source | Handler pattern | Covered in |
|---|---|---|
| GPIO level | #handleGpioLow / #handleGpioHigh | Level Change Events (2) |
| Periodic tick | #handleTicker | Timer Events |
| One-shot delay | #handleTimer (name as you like) | Timer Events |
| Tight re-arm loop | #handleFrame (name as you like) + EventAgain | Events |
| Esc / Fn | #handleEscFn | Esc/Fn key Events |
Rules of thumb:
- Form a
MethodRefwith~, pass it when you create the source or callsetOnLow/setOnHigh. - Handlers receive
( ev:Event -- )— dropevor read.index/.paramsthen drop. - Do not block the EventLoop with
forever/ longdelayMsloops; use ticker/timer, orEventAgainfor no-delay re-arm. - On stop,
.close^EventAgain/ tickers / timers andEventEscFn, thenGpioCtrl.inst.reset^.
Worked examples: examples/pi/led-btn-ctrl.fnl (ticker + Esc); examples/pi/i2c-ssd1306-life.fnl (EventAgain + Esc). General ticker/timer demo: examples/events/ticker-timer-demo.fnl.