pebble-events
pebble-events fixes the Pebble SDK's event system, supporting multiple independent subscriptions to a single event, and providing a context parameter to those that didn't already have one.
Everyone should use it!
Note: This library depends on Pebble SDK 3.14 or greater for the PBL_API_EXISTS
macro.
Installation
pebble package install pebble-events
You must be using a new-style project; install the latest pebble tool and SDK
and run pebble convert-project
on your app if you aren't.
Usage
Usage mirrors the Pebble SDK, but with events_
prefixing all the function
names. Subscription functions have the same signature as the SDK functions of
the same name, except that they return an EventHandle
that can be passed to
the corresponding unsubscribe function.
To support usage where some data needs to be passed to the callback (for
instance, a layer component that needs to subscribe to the tick service might
need to pass a pointer to itself to that callback), you can use the _context
variants. These have a signature similar to the non-context versions, but with
a final void *context
argument at the end.
App Focus Service
Wrapping the SDK AppFocusService, with the same behaviour.
EventHandleEventHandleEventHandleEventHandlevoid
Battery State Service
Wrapping the SDK BatteryStateService, with the same behaviour.
EventHandleEventHandlevoid
Connection Service
Wrapping the SDK ConnectionService, with the same behaviour. Note that the deprecated BluetoothConnectionService is not implemented.
EventHandleEventHandlevoid
Health Service
Wrapping the SDK HealthService,
with the same behaviour. Note that no _context
variant is provided because the
native version already has a context
parameter.
EventHandlebool
Tick Timer Service
Wrapping the SDK TickTimerService,
with similar behaviour. Unlike the TickTimerService
, however, we will not
necessarily call your handler immediately after subscription. We also may
spuriously call your handler as a result of internal subscription management.
However, in most cases we will call your handlers only on the requested time
units. We will subscribe to the lowest-resolution time unit that permits this.
EventHandleEventHandlevoid
Appmessage Service
The appmessage service is more complex than other services because once we open
appmessage, we cannot change the inbox size. Therefore, to use the appmessage
service, you must call events_app_message_request_inbox_size
with your
desired inbox size and events_app_message_request_outbox_size
with your
desired outbox size (if you don't send messages in one direction you don't need)
to call it in that direction. The consumer app, and only the app, should call
events_app_message_open
once all libraries have been initialised. After this
point, the two request
functions will no longer work. If you are building a
library, this may require you to add a library_prepare()
method or similar to
be called at app initialisation.
To subscribe to more than one appmessage callback at once, you can use
events_app_message_subscribe_handlers
. Otherwise, analogues to the SDK's
standard appmessage functions are also available.
Keep in mind that you will receive all appmessages, even if you did not expect them. Check for a key that your code sends to ensure it is intended for you. If you could have multiple instances that should not all process a given message, include some means of determining which instance should handle it (e.g. if it's a request/response, a pointer cast to a uint32 could work).
typedef struct EventAppMessageHandlers EventAppMessageHandlers;voidvoidAppMessageResultEventHandlevoid // For consistency with the SDK. EventHandleEventHandleEventHandleEventHandle
Accelerometer Tap Service
Wrapping the Tap part of the SDK AccelerometerService, with the same behaviour.
EventHandleEventHandlevoid
Unobstructed Area Service
Wrapping the SDK UnobstructedAreaService,
with the same behaviour. Note that no _context
variant is provided because the
native version already has a context
parameter.
EventHandlevoid