|
1 | 1 | # Eventbus overview |
2 | 2 |
|
3 | | -You can register your events and event handlers with the [`Eventbus`](https://github.com/emfcamp/badge-2024-software/blob/main/modules/system/eventbus.py) package: |
| 3 | +The [`Eventbus`](https://github.com/emfcamp/badge-2024-software/blob/main/modules/system/eventbus.py) lets a Tildagon app broadcast an event, and other apps register to receive that event if they are interested. Some examples are: a button press (a ``ButtonDownEvent``) or an app wants to take over the pattern LEDs (a ``PatternDisable`` event). You can also define your own types of event, if you have something interesting to broadcast to other apps. |
4 | 4 |
|
5 | 5 | ## Usage |
6 | 6 |
|
7 | | -You can use your own events directly with an event handler that you register on the [`eventbus`](https://github.com/emfcamp/badge-2024-software/blob/main/modules/system/eventbus.py). |
| 7 | +Here are the basic steps for using the Eventbus. Often you won't need to do all of the steps, because some other piece of the software has already done it. |
8 | 8 |
|
9 | 9 | 1. Import the `system.eventbus` package: |
10 | 10 |
|
11 | 11 | ```python |
12 | 12 | from system.eventbus import eventbus |
13 | 13 | ``` |
14 | 14 |
|
15 | | -2. Define an event: |
| 15 | +2. Import an event definition, or define your own event type. |
| 16 | + |
| 17 | + To use button events, for example: |
| 18 | + |
| 19 | + ```python |
| 20 | + from events.input import ButtonDownEvent |
| 21 | + ``` |
| 22 | + |
| 23 | + To define your own event type, subclass ``Event``: |
16 | 24 |
|
17 | 25 | ```python |
18 | | - class SpecialEvent: |
| 26 | + from system.eventbus import Event |
| 27 | + |
| 28 | + class SpecialEvent(Event): |
19 | 29 | def __init__(self): |
20 | 30 | pass |
21 | 31 |
|
@@ -85,3 +95,43 @@ You can use the following methods on the `eventbus`: |
85 | 95 | | `emit(event)` | Emit an event to the eventbus. The handler for the event must be synchronous. | `event` : The event, for example `ButtonDownEvent`. An `event` object must have the methods `__init__()` and `__str__()`. | None | |
86 | 96 | | `emit_async(event)` | Emit an event to the eventbus. The handler for the event must be asynchronous. | `event` : The event, for example `ButtonDownEvent`. An `event` object must have the methods `__init__()` and `__str__()`. | None | |
87 | 97 | | `remove(event_type, event_handler, app)` | Remove the event for an app from the eventbus. | <ul><li><code>event_type</code>: The event to be removed.</li><li><code>event_handler</code>: The asynchronous function to be called when the event fires to handle the event.</li><li><code>app</code>: The app this event is being removed for.</li></ul> | None | |
| 98 | + |
| 99 | +## Common built-in events |
| 100 | + |
| 101 | +There are a lot of event types built into the firmware. Here are a few that are useful to know about: |
| 102 | + |
| 103 | +* ``ButtonDownEvent`` and ``ButtonUpEvent`` |
| 104 | + |
| 105 | + This is sent by the firmware when one of the buttons is pressed. |
| 106 | + |
| 107 | + You can register to receive this event if you want to respond to button presses in your application. |
| 108 | + |
| 109 | + If you want to make the Tildagon think a button has been pressed, you can emit this event. For example, the megadrive controller hexpansion [firmware](https://github.com/MatthewWilkes/md-updater/blob/main/sega.py) emits a button event when a user presses the equivalent buttons on an attached megadrive controller - so controller can be used to nagivate Tildagon apps. |
| 110 | + |
| 111 | + ```python |
| 112 | + from events.input import Button |
| 113 | + ``` |
| 114 | + |
| 115 | +* ``PatternDisable`` and ``PatternEnable`` |
| 116 | + |
| 117 | + When an app wants to take over the front LEDs that usually show a pattern, it can emit a ``PatternDisable`` event. The pattern manager will receive that event and stop updating the LEDs, leaving them free for the app. Then the app can set the LEDs however it wants. When the app is finished with the LEDs (for example, when it is put in the background) it can emit a ``PatternEnable`` event to start the pattern again. |
| 118 | + |
| 119 | + You probably don't need to listen for this event. |
| 120 | + |
| 121 | + ```python |
| 122 | + from system.patterndisplay.events import PatternDisable, PatternEnable |
| 123 | + ``` |
| 124 | + |
| 125 | +* ``HexpansionInsertionEvent`` and ``HexpansionRemovalEvent`` |
| 126 | + |
| 127 | + The hexpansion manager emits these events when it detects a hexpansion has been inserted or removed. |
| 128 | + |
| 129 | + You can listen for these events if you want to do something interesting when a hexpansion is inserted or removed. |
| 130 | + |
| 131 | + These events are only emitted for a PCB hexpansion with circuity on it, because it is the electrical connection that triggers these events. Purely-decorative (cardboard or 3-d printed) hexpansions won't cause these events to be emitted. |
| 132 | + |
| 133 | + You probably shouldn't emit these events yourself. |
| 134 | + |
| 135 | + ```python |
| 136 | + from system.hexpansion.events import HexpansionInsertionEvent, HexpansionRemovalEvent |
| 137 | + ``` |
0 commit comments