Eventbus overview
The Eventbus 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.
Usage
The following steps are the basic steps for using the Eventbus directly with an event handler that you register on the eventbus.
-
Import the
system.eventbusandeventspackages: -
Import an event definition, or define your own event type.
To use button events, for example:
To define your own event type, subclass
Event: -
Define a synchronous or asynchronous method to be called when the event occurs:
-
Register your event handler, for example in the
__init__method of your app with the event and the event handler. Depending on whether the event handler is a synchronous or asynchronous method callon()oron_async(): -
Add code to emit the event, for example in your app's
update()method. Depending on whether you are emitting in a synchronous or asynchronous method, callemit()oremit_async(): -
Remove the event handler when the app is minimised or closed.
Warning
Make sure you remove the event handler when the app is minimised or closed!
You can see a more comprehensive example in dialog.py or pingpong_app.py.
Methods
You can use the following methods on the eventbus:
| Method | Description | Arguments | Returns |
|---|---|---|---|
on(event_type, event_handler, app) | Register an event for an app alongside the synchronous handler to be called when the event fires. |
| None |
on_async(event_type, event_handler, app) | Register an event for an app alongside the asynchronous handler to be called when the event fires. |
| None |
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 |
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 |
remove(event_type, event_handler, app) | Remove the event for an app from the eventbus. |
| None |
Common built-in events
There are a lot of event types built into the firmware. Here are a few that are useful to know about:
ButtonDownEventandButtonUpEvent
This is sent by the firmware when one of the buttons is pressed.
You can register to receive this event if you want to respond to button presses in your application.
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 emits a button event when a user presses the equivalent buttons on an attached megadrive controller so the controller can be used to navigate Tildagon apps.
PatternDisableandPatternEnable
When an app is supposed 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.
You probably don't need to listen for this event.
HexpansionMountedEventandHexpansionUnmountedEvent
The hexpansion manager emits these events after the firmware has finished handling the hexpansion being inserted or removed. For example, after the EEPROM filesystem has been mounted and a contained hexpansion app has been started.
In most cases you should listen for these events rather than HexpansionInsertionEvent and HexpansionRemovalEvent, because by the time HexpansionMountedEvent fires the hexpansion is fully ready to use.
HexpansionInsertionEventandHexpansionRemovalEvent
The hexpansion manager emits these events immediately when it detects a hexpansion being inserted or removed. They fire before the firmware has finished its own internal handling (such as mounting the EEPROM filesystem).
These events are only emitted for a PCB hexpansion with circuitry on it, because it is the electrical connection that triggers these events. Purely-decorative (cardboard or 3D printed) hexpansions do not cause these events to be emitted.
You probably shouldn't emit these events yourself.
EmotePositiveEventandEmoteNegativeEvent
Emote events are a lightweight way for a foreground app to signal to other components, such as hexpansion drivers or the back-of-board LED manager, that something good or bad just happened in the user interface. The components can react in their own way, for example by flashing the back LEDs green or red, or playing a short jingle on an audio hexpansion.
By default the firmware flashes the backboard LEDs green on a positive emote and red on a negative emote. This behaviour can be turned off in the badge settings.
Guidelines for use:
- Only emit emotes when your app is in the foreground.
- Send at most a handful of emotes per minute, as receivers may generate behaviours lasting several seconds (e.g. an audio jingle).
from events.emote import EmotePositiveEvent, EmoteNegativeEvent
# signal success
eventbus.emit(EmotePositiveEvent())
# signal failure
eventbus.emit(EmoteNegativeEvent())
If you are writing a hexpansion driver or app, you can listen for emote events: