Archive

Monthly Archives: April 2015

According to the Pebble dev portal, the last version of Beam Up to be released was December 14th, 2014. If I remember correctly, that version was the much required update that rolled all the different versions (inverted, with date, with battery etc.) into one version. This was a move away from the SDK 1.x hangover where app config pages did not exist, so multiple apps were required.

The SDK has come a long way since then, with the latest release (3.0-dp9) released today, after much testing. The biggest change this time around is deprecating the InverterLayer element, popularly used to add a quick black-on-white effect to many watchfaces, as it no longer makes sense in a world of 64 colors.

How do you invert a color? As I found out during my degree course, techniques applied to colors (and even grayscale) cannot be easily applied to color in the same way. A prime example from that work was the techniques involved in Mathematical Morphology and utilized ordering of pixels heavily. A white pixel can be defined as ‘greater’ than a black pixel, a grayscale pixel with value 128 can be ‘lesser’ than one with a value of 238, and so on. This enabled the implementation of various filters that went on to enable the automatic counting of tree canopies. But the last part of the work was adapting these techniques for color images. The crucial question was this: how can you decide whether one color is ‘greater’ than another? There were several options, with no clear leader in terms of logic. Was the ‘greater’ color:

  • The one with the largest single component?
  • The one with the largest averaged value?
  • The one with the largest total component value?

The answer I chose was borne out of a piece I read arguing that the human eye is more sensitive to green light (perhaps a hangover from living in the verdant wilderness?), and so proposed that the colors be ordered with a preference for the green component, and this gave good results.

The same problem exists when placing a Pebble InverterLayer over another color in a Layer beneath it: what’s the inverse of yellow? Is it:

  • The inverted color according to classic opposites?
  • The inverted bits in the byte representation?
  • The inverted values of the RGB channels?

So it was removed, and APIs added to MenuLayer (the chief user of the InverterLayer in the system) to allows developers to specify their own choice of colors for menu items when each is selected. A more general approach I adopted to enable me to continue to develop Beam Up (a handy coincidence when I wanted to add color themes) is to have the developer specify two colors, and use the frame buffer API to invert then to each other wherever they appear. This approach worked really well, and enables any color combination desired as an artificial ‘inversion’; especially useful for adding themes to Beam Up. These take the form of pairs of colors, selectable in presets (for now!) in the app config page.

The image below shows three of the new themes in action: blue, green and midnight. Classic, Inverted Classic, Red, and Yellow are also initially available.

themes

The new version is available on the Pebble appstore. On Aplite (Pebble, Pebble Steel) it behaves as it ever did, except now the config page remembers your choices from last time you saved. On Basalt, the new themes are available, using the new pseudo-InverterLayer, called InverterLayerCompat in the code. This is still available in full on GitHub!

A few weeks ago I wrote about transmitting images to Pebble Time from Android, as a quick and convenient way to open any image via an Intent and display it on the watch. At that time, I had problems sending some types of images, where only the blue channel appeared to make it across correctly. The only exception appeared to be the image bundled in the Android app’s resources, which always displayed correctly.

After a getting and re-write of the flaky transmission system (and a bonus first-time working solution!), I now have the app in a state where any image opened with the Android app from a file manager will be resized to fit the Pebble screen and transmitted for display.

Now, the focus will be to enable cropping and scaling of the image in the Android app to let the user choose which part of the image to display (as a photo shown on the watch loses detail somewhat!). In the meantime, here are some images!

Original Image (credit to gmiezis!)

11034411_954803447872213_3799211289901104533_o

Imported to the Android app

Screenshot_2015-04-19-17-45-46

Image on Pebble Time

IMG_20150419_174608

Introduction

As a side-effect of being involved with the development and testing of the new Pebble timeline experience, I’ve been eager to try my hand at exploiting this new mechanism to bring timely updates to users of BBC News (which as with most of my apps, began as an app for personal use) with timeline pins.

The Concept

As you may know from reading the Pebble Developers timeline guides, Pebble watchapps can now incorporate a purely web-based element to display data from web data sources (such as the BBC News feeds) on the timeline. This is ideal for chronological events such as news stories, as each pin is shown on the timeline according to date.

Pins can also include notifications that appear when they are first created or updated, keeping the user informed as details change. This functionality is used to show a notification each time a new news story is available. The pin itself includes only the icon (standard timeline pin icon) and the title, which is the main title of the story. In addition, timeline pins can have actions associated with them, enabling the user to launch the associated watchapp (whose API key is used to push the pins) and pass a single integer as a context argument. My idea was to use this action ability to enable the pin to open the BBC News watchapp to show the full story details, since a very long timeline pin body was a poor scrolling experience.

The Problem

The fundamental question was this; How does one tell the watchapp which story to show from the pin using a single integer? The first guess was to specify the launchCode (the argument) as the position in the array of downloaded news stories. This would work for a very recent pin only, as the order of news stories in the feed changes frequently as new stories are added and removed from the headlines. After an hour or two, the order would change and an existing pin would possess a launchCode pointing to the same array location, but no longer guaranteed to be the same story.

Implementation

The solution I ended up choosing came from Wristponder, which uses a checksum of the persisted list of responses on the Android and Pebble sides, prompting a re-download to the watch if the two do not match (i.e.: The user has modified their list of responses). Thus the procedure to open a news story from its pin is this:

1. Backend server running on DigitalOcean downloads the RSS feed from the BBC every half an hour, using a function to create a list of story objects from the XML;

var parseFeed = function(responseText) {
  var items = [];
  var longestTitle = 0;
  var longestDesc = 0;
  while(responseText.indexOf('<title>') > 0 && items.length < MAX_ITEMS) {
    //Title
    var title = responseText.substring(responseText.indexOf('<title>') + '<title>'.length);
    title = title.substring(0, title.indexOf('</title>'));
    responseText = responseText.substring(responseText.indexOf('</title>') + '</title>'.length);

    //Desc
    var desc = responseText.substring(responseText.indexOf('<description>') + '<description>'.length);
    desc = desc.substring(0, desc.indexOf('</description>'));

    // Date
    var date = responseText.substring(responseText.indexOf('<pubDate>') + '<pubDate>'.length);
    date = date.substring(0, date.indexOf('</pubDate>'));

    //Add
    var s = { 'title': title, 'description': desc, 'date': date };
    items.push(s);

    // Metrics
    Log('Story '+ items.length + ': ' + s.title + ' // ' + s.description);
    Log('(' + s.title.length + 'x' + s.description.length + ')');
    if(s.title.length > longestTitle) {
      longestTitle = s.title.length;
    }
    if(s.description.length > longestDesc) {
      longestDesc = s.description.length;
    }

    // Next
    responseText = responseText.substring(responseText.indexOf('</description>') + '</description>'.length);
  }

  Log('parseFeed(): Extracted ' + items.length + ' items.');
  Log('parseFeed(): Longest title/description: ' + longestTitle + '/' + longestDesc);
  return items;
};

2. Backend uses story titles, publish dates and checksums of the titles to push a pin for each story. The id field of each pin is a prefix followed by the Unix timestamp of the pubDate RSS field (as an easy solution of a fairly unique number from each story). The checksum is generated by simply adding all the character codes in each title, then specifying this number as the ‘Open Story’ pin action’s lanchCode;

var pin = {
  'id': 'bbcnews-story-' + pubDate.unix(),
  'time': pubDate.toDate(),
  'layout': {
    'type': 'genericPin',
    'tinyIcon': 'system://images/TIMELINE_PIN_TINY',
    'title': gStories[i].title,
    'subtitle': 'BBC News Headline'
  },
  'createNotification': {
    'layout': {
    'type': 'genericPin',
    'tinyIcon': 'system://images/TIMELINE_PIN_TINY',
    'title': gStories[i].title,
    'subtitle': 'BBC News Headline'
    }
  },
  'actions': [
    {
      'title': 'Open Story',
      'type': 'openWatchApp',
      'launchCode': checksum(gStories[i].title)
    }
  ]
};

3. These pins are filtered for duplicates and staleness by the Pebble timeline public API, then pushed through the Pebble mobile app to the user’s watch. This is demanded by a subscription to the ‘headlines’ topic that all users are subscribed to when they first launch the BBC News watchapp (this will be optional in the first release).

4. The user selects a BBC News story from their timeline and chooses the ‘Open Story’ pin action. This opens the watchapp with the launch_get_args() value set as the checksum stored in the pin when it was originally generated. If the launchCode is not zero, a pin was specified;

#ifdef PBL_PLATFORM_APLITE

// Timeline not available, give me ALL the stories!
comm_request(COMM_MODE_LIST, 0);

#elif PBL_PLATFORM_BASALT

// Is launchCode specified from a pin?
int launch_code = launch_get_args();

if(launch_code == 0) {
  // Get all stories
  comm_request(COMM_MODE_LIST, 0);

  stories_window_set_desc_text("Updating...");
} else if(launch_reason() == APP_LAUNCH_TIMELINE_ACTION) {
  // Check this checksum, JS!
  comm_request(COMM_MODE_PIN, launch_code);

  stories_window_set_desc_text("Getting pin story...");
}

#endif

5. The watchapp sends the checksum for the pin in question to PebbleKit JS, which downloads the latest feed from the BBC News site and checks the query checksum against checksums of all the stories downloaded. First, the checksum is obtained from the watch’s AppMessage;

if(hasKey(dict, 'KEY_ACTION')) {
  gLaunchCode = getValue(dict, 'KEY_ACTION');
  Log('TIMELINE PIN LAUNCH CODE: ' + gLaunchCode + '\n\n\n');

  // Download stories, and match the titles to the pin
  download(persistRead('category', 'headlines'), findPinWithHash);
}

Next, the matching story is found;

function findPinWithHash(responseText) {
  //Strip metadata
  var spool = responseText.substring(responseText.indexOf('<item>') + '<item>'.length);
  gQuantity = 30; // Get all

  var stories = parseFeed(spool);
  Log('Finding title with launchCode=' + gLaunchCode + ' in list of ' + stories.length + ' stories');

  var found = false;
  for(var i = 0; i < stories.length; i += 1) {
    var check = checksum(stories[i].title);
    if('' + check == '' + gLaunchCode) {
      Log('Found! check=' + check + ', gLaunchCode=' + gLaunchCode);

      // Send to phone
      var dict = {
        'KEY_ACTION': 0,
        'KEY_TITLE': stories[i].title,
        'KEY_DESCRIPTION': stories[i].description
      };
      Pebble.sendAppMessage(dict, function() {
        Log('Sent pin data to watch!');
        found = true;
      });
    }
  }

  // Not found?
  if(found == false) {
    var dict = {
      'KEY_FAILED': 1
    };
    Pebble.sendAppMessage(dict, function() {
      Log('Informed Pebble of failure to find story.');
    },
    function(err) {
      Log('Failed to inform of failure!');
    });
  }
}

6. If the story is still relatively recent (this can vary) and a checksum match is found, the story’s full title and body are sent to the watchapp for display to the user. The RSS feed also contains links to thumbnails already formatted to 144 pixels in width, which is ideally placed to be a possible future feature.

Results

The resulting flow looks something like the image below, which is still a work in progress:

pin

This new layout will be available soon (once bugs are worked out) for Aplite users of the existing BBC News Headlines watchapp, in color on Basalt for those with Pebble Time watches (admittedly few right now!). Stay tuned!