Skip to content

Latest commit

 

History

History
56 lines (36 loc) · 6.49 KB

events.md

File metadata and controls

56 lines (36 loc) · 6.49 KB

Explicit condition events

Public Events blocks define independent synchronous or explicitly asynchronous handlers and explicit check blocks. Each check evaluates each Boolean condition once, in definition order; every true result invokes its body. No hidden edge detection, debounce, scheduler, hardware update, or runtime registration table.

  • When condition … is met is a top-level definition, not a statement in a loop.
  • Check all events calls the generated synchronous Python update_events function once. With no events it does nothing.
  • Asynchronous checkbox on an event allows explicit Python await inside its body. The condition remains synchronous. If any enabled event is asynchronous, use await (check all events asynchronously) in your own async loop or the asyncio/SPIKE start shortcut; ordinary checks and micro:bit start reject that configuration at generation time.
  • Value changed / became true / became false returns Boolean, remembers a separate previous sample per block, and initially returns false. Use it anywhere a Boolean fits. It does not require a clock.
  • Debounced rising edge is optional and explicitly uses firmware utime.ticks_ms/ticks_diff. The first sample records the initial state, then a stable false-to-true transition triggers once.
  • Consuming Boolean sources (micro:bit was_pressed, rotary encoder left/right) go directly into the condition socket; consecutive true samples are separate events.

micro:bit

Define one event per button using the native was_pressed reader, then put one check-all-events statement inside the micro:bit every N ms block, which directly calls the native run_every(callback, ms=N) API. This requires compatible V2 firmware. For other firmware, put check-all-events inside an ordinary main loop instead.

import microbit as _microbit

def on_a():
    if _microbit.button_a.was_pressed():
        print("A pressed")

def on_b():
    if _microbit.button_b.was_pressed():
        print("B pressed")

def update_events():
    on_a()
    on_b()

_microbit.run_every(update_events, ms=20)
while True:
    _microbit.sleep(100)

The timer only schedules the callback; it is independent of events. Keep the program active and callbacks short. An uncaught callback exception cancels scheduling. This is not a thread or a hard real-time guarantee. See native API.

Other boards

The same definitions and check statement may be used in an ordinary loop or an explicit yielding asyncio loop. M5Stack updates remain explicit. No asyncio loop starts automatically. Public handlers are synchronous by default; enable the asynchronous checkbox to use await. The generated async dispatcher awaits handlers sequentially in definition order; other tasks can run while a handler awaits, but the next event is not checked until it completes. The SPIKE start shortcut uses independent periodic tasks when any event is asynchronous; manual asynchronous checks remain sequential. Choose one scheduling location per program to avoid concurrent consumption. Slow handlers delay later checks, and brief unsampled changes can be missed. GPIO IRQ latching and encoder capture remain separate hardware capabilities.

Explicit start shortcuts

Each platform's start checking events every N ms uses the same update_events function. The micro:bit V2 shortcut calls native run_every(update_events, ms=N), registers and returns. The asyncio shortcut at top level runs an infinite coroutine with asyncio.run; inside an existing async program it schedules that coroutine with create_task. Keep the main coroutine alive. The SPIKE shortcut at top level runs a periodic coroutine through runloop.run; directly inside SPIKE run tasks it adds event tasks to that same runloop.run call. With synchronous events only, one task calls the common dispatcher. With any asynchronous event, each event has a separate cooperative periodic task, with no overlapping execution of the same async handler. Do not nest event loops. A program may contain only one dedicated start-event-checks block across all runtimes. The generator rejects duplicates; the generated one-shot lifetime claim rejects a second execution of the same start block. It is intentionally not a restart/stop API. Arbitrary user-written loops or periodic callbacks calling check-all-events remain explicit and are not counted as shortcut starts.

The general asyncio run asynchronously every N ms block uses the same scheduling pattern with a user-defined async body. Each iteration runs immediately, awaits completion, then sleeps; it is a fixed delay rather than a hard real-time period. The shortcuts clamp asyncio/SPIKE sleep intervals to at least 1 ms. No public event implicitly starts a scheduler.

See the micro:bit, Matrix:bit, M5 and rotary encoder examples under examples/. Old inline/automatic async condition event projects are not migrated; replace their events with independent definitions, explicit checks, and an explicit edge detector if required.

Automated generation and simulated runtime tests cover plain Boolean events, change detection, debounce, explicit scheduling and micro:bit native API calls. Physical timing and callback behavior require board testing.

Reentrancy and asynchronous handlers

The single dispatcher has a busy flag protected by try/finally. Recursive checks or overlapping checks while an async handler is awaiting return immediately without evaluating conditions or consuming pending events. The flag is released on normal completion, exceptions or cancellation; exceptions still propagate. This is a cooperative/runtime callback guard, not a hardware IRQ/thread lock. Never run arbitrary user handlers from a hard GPIO interrupt. Independent manual schedules that run sequentially are not deduplicated.

For SPIKE, define an event with Asynchronous checked, and put an explicit await around a motor operation in its body. Start event checks in SPIKE Program, or manually await the async checker inside an existing runloop task. There is no automatic scheduling or automatic insertion of await into motor blocks. With the SPIKE start shortcut, a long awaited handler does not delay other cooperative event tasks, but it delays its own next condition check. Explicit manual async checks remain sequential. Each async handler has a shared busy guard; do not mix manual and automatic checks, because sequential extra checks can still consume events or repeat actions. Blocking non-await code still delays all runloop tasks.