|
| 1 | +--- |
| 2 | +title: Using events in C/C++ and WebAssembly with twr-wasm |
| 3 | +description: Certain twr-wasm APIs support events. This section describes how they function. |
| 4 | +--- |
| 5 | + |
| 6 | +# Overview of Events |
| 7 | +This section describes how to use twr-wasm to: |
| 8 | + |
| 9 | +- register event callbacks in C/C++ |
| 10 | +- use events in C/C++ |
| 11 | + |
| 12 | +## Quick Example |
| 13 | +~~~c title="timer events" |
| 14 | +#include <stdio.h> |
| 15 | +#include "twr-crt.h" |
| 16 | + |
| 17 | +int t2_count=0; |
| 18 | +int t2_id; |
| 19 | + |
| 20 | +// timer2 event callback (called multiple times) |
| 21 | +__attribute__((export_name("on_timer2"))) |
| 22 | +void on_timer2(int event_id) { |
| 23 | + t2_count++; |
| 24 | + printf("timer callback 2 entered (event id=%d, count=%d)\n", event_id, t2_count); |
| 25 | + |
| 26 | + if (t2_count==5) { |
| 27 | + twr_timer_cancel(t2_id); |
| 28 | + printf("timer example complete\n") |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// C entry point to call from JavaScript |
| 33 | +int timer_main() { |
| 34 | + printf("the timer will trigger 5 times...\n"); |
| 35 | + |
| 36 | + int t2_eventid=twr_register_callback("on_timer2"); |
| 37 | + t2_id=twr_timer_repeat(500, t2_eventid); |
| 38 | +} |
| 39 | +~~~ |
| 40 | +
|
| 41 | +## Examples |
| 42 | +| Name | View Live Link | Source Link | |
| 43 | +| --------- | ------------ | ----------- | |
| 44 | +| timer example | [View timer test](/examples/dist/tests-timer/index.html) | [Source](https://github.com/twiddlingbits/twr-wasm/tree/main/examples/test-timer) | |
| 45 | +| library example | [View library example](/examples/dist/lib/index.html) | [Source](https://github.com/twiddlingbits/twr-wasm/tree/main/examples/lib) | |
| 46 | +
|
| 47 | +# Events |
| 48 | +In twr-wasm, certain APIs can trigger events. For example a timer can trigger a "timer complete" event, or an audio api my trigger a "file has finished playing" event. An event had an `id` and an associated callback. The `id` is an integer that identifies the event (`int event_id`). In order to receive an event call back: |
| 49 | +
|
| 50 | +1. Write your callback in C/C++. It must be C linkage, and you should be exported from your C code to JavaScript/TypeScript using the `export_name` clang `__attribute__` like this: `__attribute__((export_name("on_timer2")))` |
| 51 | +2. Register your callback. This will also allocate the event ID paired to this callback. For example: `int t2_eventid=twr_register_callback("on_timer2");` |
| 52 | +3. Call an API that takes an event `id`. For example: `twr_timer_repeat(500, t2_eventid);`. This will call the `t2_event` callback every 500ms. |
| 53 | +
|
| 54 | +You can use the same event/callback with multiple APIs if you wish. When the event callback is called, the first argument will be the event `id` triggering the callback. There may then optional parameters. These are event specific. |
| 55 | +
|
| 56 | +# When using twrWasmModuleAsync |
| 57 | +
|
| 58 | +With a `twrWasmModuleAsync` module, various blocking APIs are available. For example: `twr_sleep`. When these functions are blocking (waiting), event callbacks are still processed. For example, in the `tests-timer` example, you can see the repeating timer callback happen while the function sleeps. |
| 59 | +
|
| 60 | +With a `twrWasmModuleAsync` module, events are sent from the JavaScript main thread to the worker thread that is running the C/C++ code. |
| 61 | +
|
| 62 | +# twr_register_callback |
| 63 | +See [twr_register_callback](../api/api-c-general.md#twr_register_callback) |
0 commit comments