pebble-header We’re getting more and more excited now that the release of version 2.0 of the Pebble smartwatch software is drawing near. Truth be told, we’ve been hard at work for a good part of the Summer, Fall and Winter getting ready for this time (click here to see what it is we’ve been working on). As I was sitting down tonight and finalizing the details for getting the TimeLogger watch app ready for Pebble’s upcoming App Store launch, the ease of implementing one of the great new features inspired me to write about it. That feature is persistent storage.

One of the glaring omissions in the current version of the Pebble watch software is that it has no storage available for apps. Pebble has listened to the desires of its customers and has added that in version 2. Now those of us who want to make our apps do things such as store configuration options to make using our apps easier can do so.

But as I was researching exactly how to pull off a persistent storage feature, I found shockingly few examples. Well, I think the lack of examples may be largely due to the fact that it is amazingly simple. This is a quick guide for programmers who want to give it a shot.

What I wanted to do was to store the user’s preference of watch face colors. I’ll call the “standard” black with white text, and “inverted” is white with black text. Here’s all there is to it.

In my screen’s header file, I carve out some memory for a variable to store the user selection, another to hold the key that the persistent storage will use to lookup the stored value, and a third to keep the default value, which only has any bearing before a value is stored:

1
2
3
static uint8_t colors_inverted;
#define INVERT_FACE_PKEY 1
#define INVERT_FACE_DEFAULT 0

Then in my screen’s init function, I check to see if there is a value in the persistent store. If so, set the local variable’s value to what is stored there and if not, set the variable to the default value:

1
colors_inverted = persist_exists( INVERT_FACE_PKEY ) ? persist_read_int( INVERT_FACE_PKEY ) : INVERT_FACE_DEFAULT;

The only step remaining is to save the variable’s value to the persistent store when it changes, which was simple enough:

1
2
3
4
5
static void function invert_colors()
{
   colors_inverted = (colors_inverted == 0) ? 1 : 0;
   persist_write_int( INVERT_FACE_PKEY, colors_inverted );
}

That’s all there is to it. Of course, there are some more nifty tricks you can do, and they are documented here. Of particular interest are the read and write functions.

Let us know what you think of all the cool new Pebble goodness, and if you love TimeLogger (or anything else we do), be sure to leave an App Store review. Also, stay tuned for a detailed preview of TimeLogger for Pebble SDK 2. That day can’t come soon enough.

I can’t wait to get my hands on a Pebble Steel!