There are two ways to learn to use Slate to create a configuration screen to change the background of your watchface. The right way involves reading the documentation for AppMessage (which will push you down the rabbit role of data structures); watching the video tutorial, that suggests you install the SDK, an http server through npm, and Slate through Bower; and possibly many other tasks which I'm not aware of because I followed a different path.
Then there is a quick way, which I'll describe in this easy tutorial. Here is what you'll need:
- a CloudPebble account
- a watchface app, written in C, with a bitmap background that you want to be configurable. The one from the Create a C Watchface tutorial is a good start.
- a web server to host static files. GitHub Pages works very well for that.
And here are the step-by-step instructions:
Publish a configuration screen
Next, you want to edit the index.html file, to show your list of background options, using a HTML select tag. In my example, I reused the id background_color_picker, but made a few changes to the function getConfigData in order to pass the value with the name gem_color. If you copy & paste my file, these are the only two strings that you may consider adapting to your watchface context.
Once ready, publish your files, for example by pushing them to a gh-pages branch in your GitHub project. Mine is available at http://bani.github.io/pebble-diamond/config.
Update Application Settings
Add files to your project
- the URL where your configuration page is available;
- the name of the key in configData must match what you passed from the options in getConfigData();
- the key in the dict you'll pass as AppMessage must match the MESSAGE KEY entered above.
Update your code
Now is time for the fun part of making your background actually change when the user submits a new configuration.
Define your message key:
The same way you created a MESSAGE KEY in settings you want to define it in your C file just below the include header:
#define KEY_GEM_COLOR 0
Register the handler to receive messages inside your handle_init():
There is some explanation on how to calculate the actual size that you need in the dict_calc_buffer_size documentation, but let's leave that for people following "The right way"™ path. 64 works ¯\_(ツ)_/¯.
Implement the inbox_received_handler function:
Another copy & paste comes in handy here:
Again, update the references to gem_color with your variable name.
Implement the set_background function:
I've extracted some of the lines that were in the "Background Image" section of main_window_load in the example watchface to a separate function set_background_gem that I could call whenever the image needed to be updated. The lines in question were:
s_bitmap = gbitmap_create_with_resource(RESOURCE_ID_BGIMAGE);
You just need to create a switch statement to decide which bitmap to load and then add it to the layer in the end, like in this example:
Note that before reassigning the pointer you should destroy the old one, in order to avoid leaking memory. Also in my code I decided to mark the layer dirty inside the inbox_received_handler, but here could be a good location for this code as well.
That's it! If everything went according to the plan, you should have a working configurable background now. I'd like to end this tutorial with a big thanks to the people in the Pebble dev Slack group who supplemented my lack of RTFM answering all my questions while I was trying to add a configuration screen to my watchface ♥. My full code is available in GitHub.