Edit: 100th post!

Quick post to share  a ‘new’ Layer type I created for an upcoming project: ColorLayer. It’s supposed to be a convenience for adding a simple layer of colour. Problem is, unless I’m missing something very obvious, the two options are to declare a standard Layer and assign it a basic _fill_rect() LayerUpdateProc, or use a TextLayer and modifying the background colours.

I normally choose the latter, so for the umpteenth time of doing so I decided to wrap it up to make it a bit simpler to use. Here’s the result!

ColorLayer.h

/**
 * Layer on top of TextLayer used just for coloring areas without using LayerUpdateProc
 * Author: Chris Lewis (@Chris_DL)
 * Version 1.0.0
 */
#include <pebble.h>

#ifndef COLOR_LAYER_H
#define COLOR_LAYER_H

typedef struct {
	TextLayer *layer;
} ColorLayer;

ColorLayer* color_layer_create(GRect bounds, GColor fill_color);
void color_layer_destroy(ColorLayer *this);
void color_layer_set_color(ColorLayer *this, GColor fill_color);
void color_layer_set_frame(ColorLayer *this, GRect bounds);
Layer* color_layer_get_layer(ColorLayer *this);

#endif

ColorLayer.c

#include "color_layer.h"

ColorLayer* color_layer_create(GRect bounds, GColor fill_color)
{
	ColorLayer *this = malloc(sizeof(ColorLayer));
	this->layer = text_layer_create(bounds);
	text_layer_set_background_color(this->layer, fill_color);

	return this;
}

void color_layer_destroy(ColorLayer *this)
{
	text_layer_destroy(this->layer);
	free(this);
}

void color_layer_set_color(ColorLayer *this, GColor fill_color)
{
	text_layer_set_background_color(this->layer, fill_color);
}

void color_layer_set_frame(ColorLayer *this, GRect bounds)
{
	layer_set_frame(text_layer_get_layer(this->layer), bounds);
}

Layer* color_layer_get_layer(ColorLayer *this)
{
	return text_layer_get_layer(this->layer);
}

It could be argued that it’s such a thin layer you may as well not bother, but I find it to be sufficiently easier to setup and read (as well as avoiding confusion with TextLayers that actually show text), so once again I’m glad coding allows a degree of personal preference and style!

CL Pebble Apps (formerly Watch App Selector) now has a third tab; SDK Tutorial. It’s been an idea I’ve had for a while, but only just got around to implementing.

This means that as well as being able to install all my published watch apps and watchfaces, users can now also see a list and be linked to my Pebble SDK tutorials.

Screenshot_2014-07-26-15-10-48

Neat! If I write more parts to the tutorial, this app will be updated as it will for new versions of the other watch apps and watchfaces.

Get it on Google Play

Required Reading

Pebble SDK 2.0 Tutorial #1: Your First Watchapp

Pebble SDK 2.0 Tutorial #2: Telling the Time

Pebble SDK 2.0 Tutorial #3: Images and Fonts

Pebble SDK 2.0 Tutorial #4: Animations and Timers

Pebble SDK 2.0 Tutorial #5: Buttons and Vibrations

Pebble SDK 2.0 Tutorial #6: AppMessage for PebbleKit JS

Pebble SDK 2.0 Tutorial #7: MenuLayers

Pebble SDK 2.0 Tutorial #8: Android App Integration

Pebble SDK 2.0 Tutorial #9: App Configuration

Introduction

In this section of the tutorial series, I will be covering some of the Event Services introduced in the 2.0 version of the Pebble SDK which have been oft requested due to their popularity in newer, interactive watchfaces. Namely:

  • Bluetooth Connection Service
  • Battery State Service
  • Accelerometer Service (tap and raw)
  • App Focus Service (not covered, but works identically to the Bluetooth Connection Service)

Setup

To begin with, we will be using the blank template from before, shown below for convenience. Create a new CloudPebble project and start a new C file with the template as its contents:

#include <pebble.h>

static Window* window;

static void window_load(Window *window)
{

}

static void window_unload(Window *window)
{

}

static void init()
{
  window = window_create();
  WindowHandlers handlers = {
    .load = window_load,
    .unload = window_unload
  };
  window_set_window_handlers(window, (WindowHandlers) handlers);
  window_stack_push(window, true);
}

static void deinit()
{
  window_destroy(window);
}

int main(void)
{
  init();
  app_event_loop();
  deinit();
}

Bluetooth Connection Service
The first Event Service we will be using is the Bluetooth Connection Service, which allows us to see the current connection status as well as subscribe to updates (only while Bluetooth is actually connected, so be careful with debug logs), much in the same way as with the TickTimerService. Firstly, we will create a TextLayer in window_load() to use for showing the events happening. First is the global pointer:

static TextLayer *bt_layer;

then creation proper in window_load(). Note the use of bluetooth_connection_service_peek() to show the state of the connection at the time of creation. As always we also add the corresponding destruction function call to free memory:

static void window_load(Window *window)
{
  //Setup BT Layer
  bt_layer = text_layer_create(GRect(5, 5, 144, 30));
  text_layer_set_font(bt_layer, fonts_get_system_font(FONT_KEY_GOTHIC_18));
  if(bluetooth_connection_service_peek() == true)
  {
    text_layer_set_text(bt_layer, "BT: CONNECTED");
  }
  else
  {
    text_layer_set_text(bt_layer, "BT: DISCONNECTED");
  }
  layer_add_child(window_get_root_layer(window), text_layer_get_layer(bt_layer));
}

static void window_unload(Window *window)
{
  text_layer_destroy(bt_layer);
}

Next we will subscribe to the BluetoothConnectionService to update this TextLayer whenever the status of the Bluetooth connection to the phone changes. Like a TickHandler, we start by creating a function to use as a handler with the correct signature, and fill it with logic to change the text displayed. This should be placed before init(), where it will be registered:

static void bt_handler(bool connected)
{
  if(connected == true)
  {
    text_layer_set_text(bt_layer, "BT: CONNECTED");
  }
  else
  {
    text_layer_set_text(bt_layer, "BT: DISCONNECTED");
  }
}

The final step is to perform the actual subscription, which is very easy to do, and happens in init():

//Subscribe to BluetoothConnectionService
bluetooth_connection_service_subscribe(bt_handler);

After compiling and installing the project, try disconnecting and re-connecting your phone’s Bluetooth radio a few times and observe the result.

Battery State Service
The next Event Service we will be adding will be the Battery State Service, which provides information on the Pebble’s battery. It provides more detail than a simple bool, including charging status and whether the cable is plugged in or not. As before, we will create a new TextLayer to show the output. Add the pointer to the last one in the declaration:

static TextLayer *bt_layer, *batt_layer;

Then, perform the proper creation in window_load(). This time, the information provided by the Battery State Service comes in the form on the BatteryChargeState data structure, with fields as shown in the documentation. It is worth noting that the Service only returns the battery charge in increments of 10. The setup of the new TextLayer is shown below:

//Setup Battery Layer
batt_layer = text_layer_create(GRect(5, 25, 144, 30));
text_layer_set_font(batt_layer, fonts_get_system_font(FONT_KEY_GOTHIC_18));
layer_add_child(window_get_root_layer(window), text_layer_get_layer(batt_layer));

//Get info, copy to long-lived buffer and display
BatteryChargeState state = battery_state_service_peek();
static char buffer[] = "Battery: 100/100";
snprintf(buffer, sizeof("Battery: 100/100"), "Battery: %d/100", state.charge_percent);
text_layer_set_text(batt_layer, buffer);

After re-compiling, the battery charge percentage should be shown below the Bluetooth status.

Accelerometer Service (tap)
The Accelerometer Service operates in a very similar manner to the previous two Event Services, but can operate in two modes: tap and raw data. The tap mode will call a handler that we subscribe when the Pebble is tapped (or wrist is shaken), whereas the raw data mode will supply X, Y and Z values at an rate we select. I’ll show both of these for the sake of completeness. An application of the latter mode can be seen here.

First, we create a further TextLayer to show the output data:

static TextLayer *bt_layer, *batt_layer, *accel_layer;

The first mode we will use is the tap mode. Let’s create the TextLayer proper in window_load():

//Setup Accel Layer
accel_layer = text_layer_create(GRect(5, 45, 144, 30));
text_layer_set_font(accel_layer, fonts_get_system_font(FONT_KEY_GOTHIC_18));
text_layer_set_text(accel_layer, "Accel tap: N/A");
layer_add_child(window_get_root_layer(window), text_layer_get_layer(accel_layer));

Next, we will create the handler function to be called whenever a tap is detected, and furnish it with logic to show what kind of tap was detected:

static void accel_tap_handler(AccelAxisType axis, int32_t direction)
{
  switch(axis)
  {
  case ACCEL_AXIS_X:
    if(direction > 0)
    {
      text_layer_set_text(accel_layer, "Accel tap: X (+)");
    }
    else
    {
      text_layer_set_text(accel_layer, "Accel tap: X (-)");
    }
    break;
  case ACCEL_AXIS_Y:
    if(direction > 0)
    {
      text_layer_set_text(accel_layer, "Accel tap: Y (+)");
    }
    else
    {
      text_layer_set_text(accel_layer, "Accel tap: Y (-)");
    }
    break;
  case ACCEL_AXIS_Z:
    if(direction > 0)
    {
      text_layer_set_text(accel_layer, "Accel tap: Z (+)");
    }
    else
    {
      text_layer_set_text(accel_layer, "Accel tap: Z (-)");
    }
    break;
  }
}

Finally, we subscribe our handler function to the Accelerometer Event Service in init():

//Subscribe to AccelerometerService
accel_tap_service_subscribe(accel_tap_handler);

You should now be able to see the result of tapping the watch. Personally I’ve found that shaking the wrist is a more reliable way of triggering events (such as showing more information on a watchface), but taps can still be used as an option.

Accelerometer Service (raw data)
Finally, we will use the raw data mode of the Accelerometer Service. To do this, we will first remove the existing Accelerometer Service subscription (but still keep the handler for reference).

In the raw data mode, the data values arrive at a specific interval chosen with a call to accel_service_set_sampling_rate(), and the number of samples in a batch can be chosen using accel_service_set_samples_per_update(). We will stick with the default rate and update size for simplicity. Be aware that this mode will drain the battery significantly faster than the tap mode.

Next, we will create a new handler function to let us access the data that arrives from the Event Service. Accessing the data is as simple as reading the fields in the data parameter in the handler, as shown below:

static void accel_raw_handler(AccelData *data, uint32_t num_samples)
{
  static char buffer[] = "XYZ: 9999 / 9999 / 9999";
  snprintf(buffer, sizeof("XYZ: 9999 / 9999 / 9999"), "XYZ: %d / %d / %d", data[0].x, data[0].y, data[0].z);
  text_layer_set_text(accel_layer, buffer);
}

Finally, we add the new subscription, making sure we have disabled the one one in init():

//Subscribe to AccelerometerService (uncomment one to choose)
//accel_tap_service_subscribe(accel_tap_handler);
accel_data_service_subscribe(1, accel_raw_handler);

Now this is all in place, re-compile and re-install the watch app to see the live values. Try tilting the watch in each axis to see the constant g acceleration act on each in turn.

The final result should look like this:
final

Conclusion
So, that’s the new Event Services. As I mentioned, there is another called the App Focus Service which tells you when your app is covered by a notification, but it works in a very similar way to the Bluetooth Connection Service, so you should be able to figure it out!

The full source code can be found here on GitHub.

Later today I will be (finally!) graduating from university, and to mark the occasion I will be wearing a special watchface bearing the university’s coat of arms, which will be a cool example to show anyone who asks about my Pebble. Exciting!

image

smsrepeater

My mum recently got a new phone, a Moto G, after my recommendation. It’s a big step up from Samsung S8000 Jet she had before (one of Samsung’s last phones before adopting Android, and which had a beta version of Android 2.1 a.k.a JetDroid installed, which I had a small hand in).

One of the features she liked most about it was the option to have the SMS tone repeat every minute, which was useful if it went off when she was out of hearing range (this handset was before notification LEDs or smartwatches!). This feature was markedly missing from Android 4.4.2 KitKat which is currently on here phone, so I offered to create an app to emulate this feature, because I could and wanted to see if I could.

After two hours, a brief foray into Timers, AlarmManagers, AsyncTasks and Handlers, I arrived at a solution that uses a BroadcastReceiver to detect an incoming SMS, start a Service once every 1, 2, 5 or 10 minutes using an AlarmManager Alarm and re-emit the default notification tone. This cycle is broken when the keyguard is removed when the phone is unlocked to answer the SMS. She was very pleased to have the feature back, which makes up for the predictive keyboard I forced on her!

repeater-screeny

If anyone is interested in using this or taking it further (I’m sure there are already many apps like this!), you can find the source code here.

Updates:
15/07/14 – Added option to change story detail view font size.

31/5/14 – The news category and number of items is now exposed as a configuration page

18/06/14 – v1.2.0 streams stories instead of pre-loading them for extra speed. Configuration page now shows version numbers and update news.

Most of my Pebble apps so far have either been watchfaces or control watchapps – those that control a camera (Watch Trigger), SMS sending (Wristponder) or radios (Data Toggle). Another (and some would say the primary use of a Pebble smartwatch) is to be a data display device, rather than data input. With this in mind I decided to make a watchapp that I would myself use on a daily basis that involved data fetching and formatting for display. News is the obvious application that came to me, so after studying the BBC’s public news RSS feeds I came up with this:

bbc newsWhen the user opens the watchapp, PebbleKit JS fetches the latest data from the RSS feed and creates 15 Story objects that contain the headline and the short description of the news story. These are streamed (using ACK callbacks for maximum speed) to the watch and displayed in a MenuLayer to the user. When the user clicks SELECT on a news item, the full summary is shown. The splash screen also uses my recently developed ProgressBarLayer object to show download progress.

Using the configuration skill recently gained from finally experimenting it is possible (and I’d like to do it) to use the configuration process to allow the user to choose their news category (such as Science and Technology or Sport), but for now the main headlines seem enough.

You can get this app from the Pebble Appstore. Enjoy!

 

Required Reading

Pebble SDK 2.0 Tutorial #1: Your First Watchapp

Pebble SDK 2.0 Tutorial #2: Telling the Time

Pebble SDK 2.0 Tutorial #3: Images and Fonts

Pebble SDK 2.0 Tutorial #4: Animations and Timers

Pebble SDK 2.0 Tutorial #5: Buttons and Vibrations

Pebble SDK 2.0 Tutorial #6: AppMessage for PebbleKit JS

Pebble SDK 2.0 Tutorial #7: MenuLayers

Pebble SDK 2.0 Tutorial #8: Android App Integration

Introduction

In this section of the tutorial series we will create a basic app that can be configured from the Pebble app. Lots of watchfaces and watchapps use this functionality to let the user tweak various aspects to their own liking. Watchfaces I’ve created before SDK 2.0 get around this by having each tweak in a separate watchface package, which lead to having five or six of the same watchface.

I’ve not yet gotten around to adding configuration to any of my watchfaces (although I plan to in the future) due to the fact that the configuration pages loaded from the Pebble app are not included in the watchapp package itself but are loaded from a remote website, and I have no web hosting to speak of. However, I have since discovered (although I’m sure I’m not the first) that such a page can be hosted on Dropbox. It must be in the Public folder, otherwise it is offered as a download and not as a webpage to view.

Let’s get started!

Watchapp Setup
The watchapp we will be creating will have a single option to keep things simple – the option to invert the colours. To begin with, create a new project and use the following code as a starting point:

#include <pebble.h>

static Window *window;
static TextLayer *text_layer;

static void window_load(Window *window) 
{
  //Create TextLayer
  text_layer = text_layer_create(GRect(0, 0, 144, 168));
  text_layer_set_font(text_layer, fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD));
  text_layer_set_text_color(text_layer, GColorBlack);
  text_layer_set_background_color(text_layer, GColorWhite);
  text_layer_set_text(text_layer, "Not inverted!");

  layer_add_child(window_get_root_layer(window), text_layer_get_layer(text_layer));
}

static void window_unload(Window *window) 
{
  text_layer_destroy(text_layer);
}

static void init(void) 
{
  window = window_create();
  window_set_window_handlers(window, (WindowHandlers) {
    .load = window_load,
    .unload = window_unload,
  });

  window_stack_push(window, true);
}

static void deinit(void) 
{
  window_destroy(window);
}

int main(void) 
{
  init();
  app_event_loop();
  deinit();
}

This should be familiar: a basic app that has a single TextLayer stating that the app is not inverted. The process I’ve adopted to setup app configuration has the following steps:

1. Setup AppMessage to enable messages containing option data to be sent from PebbleKit JS.
2. Setup the app to be configurable in appinfo.json, or the Settings page in CloudPebble.
3. Setup PebbleKit JS code to load the configuration page and send the result to the watch.
4. Write the HTML page that presents an interface to the user to allow them to choose their options.

Setting Up AppMessage
We will start by declaring the key we will be using to receive the option to invert the watchapp. Don’t forget to declare this in Settings on CloudPebble or in appinfo.json if you are working with the native SDK:

#define KEY_INVERT 0

Next, we create the AppMessageInboxReceived handler that will process any received messages. If they contain our key, we will compare the payload value cstring to set the colours of the app to be inverted or not, depending on the value received. We then use the Persistent Storage API to save the result for the next time the watchapp is opened. This should be placed above init() as it will be called there in a moment:

static void in_recv_handler(DictionaryIterator *iterator, void *context)
{
  //Get Tuple
  Tuple *t = dict_read_first(iterator);
  if(t)
  {
    switch(t->key)
    {
    case KEY_INVERT:
      //It's the KEY_INVERT key
      if(strcmp(t->value->cstring, "on") == 0)
      {
        //Set and save as inverted
        text_layer_set_text_color(text_layer, GColorWhite);
        text_layer_set_background_color(text_layer, GColorBlack);
        text_layer_set_text(text_layer, "Inverted!");

        persist_write_bool(KEY_INVERT, true);
      }
      else if(strcmp(t->value->cstring, "off") == 0)
      {
        //Set and save as not inverted
        text_layer_set_text_color(text_layer, GColorBlack);
        text_layer_set_background_color(text_layer, GColorWhite);
        text_layer_set_text(text_layer, "Not inverted!");

        persist_write_bool(KEY_INVERT, false);
      }
      break;
    }
  }
}

The final step is to actually open AppMessage to enable communication with the phone. Do this in init():

app_message_register_inbox_received((AppMessageInboxReceived) in_recv_handler);
app_message_open(app_message_inbox_size_maximum(), app_message_outbox_size_maximum());

Note we used the app_message_inbox_size_maximum() and app_message_outbox_size_maximum() functions to get the maximum buffer sizes available. While not strictly required here, it is a good best practice. I’ve wasted a lot of time in past projects not realising the buffer sizes I’d chosen were too small!

The final step is to set up the app to load the last stored configuration when the app is restarted, and takes for form of a similar if, else as the AppMessageInboxReceived handler above. Again, we use the Persistent Storage API to get our last saved configuration value. The window_load()function becomes thus:

static void window_load(Window *window) 
{
  //Check for saved option
  bool inverted = persist_read_bool(KEY_INVERT);

  //Create TextLayer
  text_layer = text_layer_create(GRect(0, 0, 144, 168));
  text_layer_set_font(text_layer, fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD));

  //Option-specific setup
  if(inverted == true)
  {
    text_layer_set_text_color(text_layer, GColorWhite);
    text_layer_set_background_color(text_layer, GColorBlack);
    text_layer_set_text(text_layer, "Inverted!");
  }
  else
  {
    text_layer_set_text_color(text_layer, GColorBlack);
    text_layer_set_background_color(text_layer, GColorWhite);
    text_layer_set_text(text_layer, "Not inverted!");
  }

  layer_add_child(window_get_root_layer(window), text_layer_get_layer(text_layer));
}

Now the C code is complete!

PebbleKit JS Setup
The PebbleKit JS component of the app is the part responsible for loading the configuration page and sends the results of the user interaction to the watch to be processed as we just set up. This is done through the “showConfiguration” and “webviewclosed” events. Here is our initial JS code. Add a new JS file in CloudPebble or to the src/js/pebble-js-app.js if coding natively:

Pebble.addEventListener("ready",
  function(e) {
    console.log("PebbleKit JS ready!");
  }
);

So far, so simple. Next, we add an event listener for the “showConfiguration” event, triggered when a user chooses the Settings button in the Pebble app, like that shown below:

Screenshot_2014-05-24-15-04-23

The job of this event listener is to call Pebble.openURL(), a requirement of the system. This is when the configuration page is loaded (we will design this later). As stated in the introduction a good place to store this file is in your Public Dropbox folder. This way it is shown as a webpage and not as a download. Use mine for the moment, but if you want to make any changes you will need to change this to point to your own file:

Pebble.addEventListener("showConfiguration",
  function(e) {
    //Load the remote config page
    Pebble.openURL("https://dl.dropboxusercontent.com/u/10824180/pebble%20config%20pages/sdktut9-config.html");
  }
);

When the user has chosen their options and closed the page, the “webviewclosed” event is fired. We will register another event listener to handle this. The data returned will be encoded in the URL as a JSON dictionary containing one element: “invert” which will have a value of either “on” or “off” depending on what the user chose. This is then assembled into an AppMessage and sent to the watch, which then sets and saves as appropriate:

Pebble.addEventListener("webviewclosed",
  function(e) {
    //Get JSON dictionary
    var configuration = JSON.parse(decodeURIComponent(e.response));
    console.log("Configuration window returned: " + JSON.stringify(configuration));

    //Send to Pebble, persist there
    Pebble.sendAppMessage(
      {"KEY_INVERT": configuration.invert},
      function(e) {
        console.log("Sending settings data...");
      },
      function(e) {
        console.log("Settings feedback failed!");
      }
    );
  }
);

That concludes the PebbleKit JS setup. Now for the last part – HTML!

Configuration HTML Page Setup
The final piece of the puzzle is the part the user will actually see and takes the form of a HTML page consisting of form elements such as checkboxes, selectors and buttons. We will just use one selector and one button to let the user choose if they want the watchapp to be inverted or not. Here’s the layout code:

<!DOCTYPE html>
<html>
  <head>
    <title>SDKTut9 Configuration</title>
  </head>
  <body>
    <h1>Pebble Config Tutorial</h1>
    <p>Choose watchapp settings</p>

    <p>Invert watchapp:
    <select id="invert_select">
      <option value="off">Off</option>
      <option value="on">On</option>
    </select>
    </p>

    <p>
    <button id="save_button">Save</button>
    </p>
  </body>
</html>

With this done we add a script to add a click listener to the button and a function to assemble the JSON option dictionary. This dictionary is then encoded into the URL and handed to the PebbleKit JS code to be sent to the watch in the “webviewclosed” event. Insert this into the HTML page:

<script>
  //Setup to allow easy adding more options later
  function saveOptions() {
    var invertSelect = document.getElementById("invert_select");

    var options = {
      "invert": invertSelect.options[invertSelect.selectedIndex].value
    }
    
    return options;
  };

  var submitButton = document.getElementById("save_button");
  submitButton.addEventListener("click", 
    function() {
      console.log("Submit");

      var options = saveOptions();
      var location = "pebblejs://close#" + encodeURIComponent(JSON.stringify(options));
      
      document.location = location;
    }, 
  false);
</script>

That completes the page that will get the user’s option choices and also the app itself! Compile the app and install on your watch. By choosing either ‘On’ or ‘Off’ on the configuration page you should be able to toggle the colour used in the watchapp. This should look like that shown below:

invert-notinvert

Conclusion

So, that’s the process I’ve adopted to set up app configuration. You can expand it by adding more AppMessage keys and more elements in the HTML page. Make sure to add the fields to the JSON object constructed in saveOptions() though.

As usual, the full code is available on GitHub.

Follow

Get every new post delivered to your Inbox.

Join 143 other followers