Tag Archives: c

Pebbles, rectangles and stack overflow

UntitledWhile working on my Pebble watchface “Future Time” I have encountered persistent annoying problem – watchface would run for a while and then crash – and not only crash, but actually restart the watch. What made this even more frustrating – after 3 restarts in a row Pebble would revert to recovery mode and complete firmware reinstall was required (did I accidentally write a Pebble virus?).
When I dug into device logs I found out that every restart was given reason: “Dangerous Stack Overflow”. Which is kinda strange – I don’t have any recursions nor deeply nested function calls nor large local variables. I tried lots of things – including extreme ones like declaring all local function variables as global or static – nothing helped.
Finally Christian form Pebble developers forum shed a light. He pointed out that I use a lot of GRect constructs which is basic rectangle building block for pretty much anything from defining layers to graphics functions. I used GRect inline directly inside function calls, which I thought wasn’t a big deal, after all “everybody does that” – including Pebble in its examples. Well, as Christian pointed out those GRects are kind of local variables, and either memory is not reclaimed fast enough when they go out of scope or they spring memory leak.
Keeping this in mind I created a global GRect variable and when needed to use a GRect in local function, first I’d assign it’s value to the variable and then use variable in the function.
So something like this:

static void layer_update(Layer *layer, GContext *ctx) {
   //...
   graphics_draw_rect(ctx, GRect(10, 10, 30, 30));
   //...
   graphics_draw_bitmap_in_rect(ctx, bitmap, GRect(20, 20, 40, 40));
}

Becomes thus:

GRect temp_rect;

static void layer_update(Layer *layer, GContext *ctx) {
   //...
   temp_rect = GRect(10, 10, 30, 30);
   graphics_draw_rect(ctx, temp_rect);
   //...
   temp_rect = GRect(20, 20, 40, 40);
   graphics_draw_bitmap_in_rect(ctx, bitmap, temp_rect);
}

That’s it. This simple change cured the plague, no more crashes, restarts or recovery – just smooth sailing.

Fire on High or Framebuffer in Rocky.js

First things first. DISCLAMER: Everything described here is a hack upon a crude hack and most likely, barring a divine intervention, won’t work in final product. And I apologize in advance to Pebble dev team if my attempts at “hacking” seem silly. Now to business. Pebble SDK offers very cool framebuffer API that allows developers to address display memory of the watch directly. This makes possible creation of many cool special effects (matter of fact EffectLayer library uses framebuffer extensively).
Rocky.js is JavaScript incarnation of Pebble SDK and it made me wonder whether it offers framebuffer access. Turned out it is hidden, but it’s there. At least at the latest commit at the time of this article it is. If you take a look at source file html-bindings.js you will see that binding function looks something like this:

Rocky.bindCanvas = function(canvas, options) {
  
  //...
   
  var framebufferPixels = new Uint8Array(module.HEAPU8.buffer,
                                         framebufferPixelPTR,
                                         canvasW * canvasH);

  //...

  var binding = {
  
  //...

  }

  //...

  return binding;
};

Continue reading →

Rocky.js – Pebble watch coding in JavaScript

Pebble never ceases to amaze. And every time you think – this is it, they reached the pinnacle of awesomeness – they surprise you again. This time they did pretty much the impossible – ported their C SDK to JavaScript, by creating Rocky.JS project. Ultimate goal is to run JS directly on the watch hardware – this will open way to huge number of new developers who hesitate to dive into depth of C. Meanwhile it provides ability to run Pebble code directly in a browser! It’s a lot of fun and as a bonus you can insert Pebble watchfaces directly into your website as evident by living watchface you see here.
Watchface you see running above is called Meyer Object it’s been available for Pebble watch for a while and I decided to port it to Rocky.JS Continue reading →

Solved: Issue with Pebble framebuffer after notification is dismissed

Effect Layer Issue I’ve encountered a weird issue while working with EffectLayer Library (a visual effect library for Pebble smartwatch). In this particular watchface called Clean & Smart I used “invert” effect which inverts colors of the watchface should the user choose that option in settings. It was working fine when option changed when watchface was loaded/unload and behaved weirdly only in one particular scenario: when you would receive a notification (email, text etc.) and then dismiss it. Upon coming back from notification to watchface invert effect would only partially cover the watchface (as seen on the screenshot).
I don’t know exactly what was happening, but had a theory. Continue reading →

How to make your Pebble smartwatch really tick

“Tick tock, goes the clock, And Now what shall we play?”.

Pebble smartwatch is an amazing piece of hardware with no less amazing software to support it. Pebble appstore boasts huge variety of watchfaces from intricately carved art pieces to simplicity personified. And the apps, my gods the apps! You want to track your sleep, count swimming stokes, automate your home – Pebble can do all those things and more. But something was missing. Something that ordinary mechanical clocks could do since the dawn of time.

Tick tock, goes the clock, And then what shall we see?
Continue reading →

Sprite animation on original classic Pebble smartwatch

SDK 3.x for Pebble Time smartwatch offers cool and very convenient set of functions to create animation from your existing GIF or MP4 via APNG support. APNG is an obscure “Animated PNG” format (at the time of the post only Mozilla Firefox supports it) but it’s very powerful and can store animation in much more compressed format than traditional animated GIF, so Pebble chose it for a reason. So if you have a GIF, convert it to APNG with Gif2Apng (or if you have a video, convert MP4 to GIF first and then to APNG) and you’re ready to use it on Pebble Time. Just keep the size in check, since Pebble has to load entire APNG sequence in memory, try not to go overboard. The first video is showing animation from my “Vortex” watchface using this approach on Pebble Time.

But what about original classic Pebbles? Eventually they will get firmware 3.x and SDK 3.x support and with that APNG functions among other advantages, but at the time of this writing it is still hazy when this is going to happen. But where there’s a will there’s a way – you can still use your MP4/GIF source for animation it’s just a bit more tricky. Instead of dealing with a single APNG file as your resource and relying on Pebble firmware to draw the frames you will need to help it a little.

First you will need to split your source into individual frames, for example using this service. Yes, you will be dealing with individual frames, so don’t go creating a Hollywood blockbuster. But don’t fret, it’s a bit more manual work, but you won’t have to hand-crank the moving pictures all the way.
Continue reading →

EffectLayers gets (long overdue) remove function

EffectLayer for Pebble Smartwatch is a library that allows you to easily add special effects to your watchfaces or watch apps. You can even add multiple effects (up to 4 by default) to a single layer. But up until now you couldn’t easily remove added effect.

This feature could be useful when you need to add/remove an effect on the fly. For example user can choose to turn off or on color inversion from watchface config, so instead of creating/showing/destroying/hiding entire layer you can simple add/remove inversion effect.

Another use case is where you need to swap effects, for example remove 90-degree rotation clockwise and add 90-degree rotation counter-clockwise.

Well now you can, the library now has effect_layer_remove_effect function. What it does is simple removes last added effect. The effect showing in the demo above is achieved by this block of code:

switch (anim_count) {
   case 0:
      effect_layer_add_effect(effect_layer, effect_invert, NULL);
      break;
   case 1:
      effect_layer_remove_effect(effect_layer);
      effect_layer_add_effect(effect_layer, effect_rotate_90_degrees, (void *)true);
      break;
   case 2:
      effect_layer_remove_effect(effect_layer);
      effect_layer_add_effect(effect_layer, effect_mirror_vertical, NULL);
      break;
   case 3:
      effect_layer_remove_effect(effect_layer);
      break;
}
  
anim_count++;
if (anim_count == 4)  anim_count = 0;

It is called every time animation movement is initiated for the layer. Layer is moved 4 times in this demo:

  • On 1st call – inversion effect is added to the layer
  • On 2nd call – last added effect (inversion) is removed and 90-degree rotation added
  • On 3rd call – 90-degree rotation removed and vertical mirror effect is added
  • On 4th call – effect is removed so now layer has no effects.

Combined the chain produced the effect shown in the animation above.

Ideally library should have “insert” and “remove_at” function to be able to insert and remove effects from arbitrary index (and not only the end of effect chain). Stay tuned.

Useful Links

“Background” vibes on Pebble smartwatch

Matt Thompson from Pebble G+ community asked a question that got me curious: Is there a way to buzz Pebble vibe at regular intervals in the background, while a regular watchface is displayed in foreground?

Besides running a normal app, Pebble has 2 ways to run code in the background: background worker and Wakeup API.

Background worker can truly run in the background, but has no access to UI (and vibes are considered UI) as well as other limitations. Besides you can have only one background worker, so for example if you’re running MisFit app and want to run another background app – you’re out of luck.

WakeUp API has the ability to act as a timer in the background and launch your app when timer countdown finished. Interesting thing is – if your app doesn’t have any UI (windows) – it exits right away, so from the user’s point of view – it didn’t even ran – then point in the watch interface remains the same (if you’re looking at a watchface, or at settings etc. – you remain at the same spot).

We can use this to wake the app, buzz the vibe, reschedule wakeup time and exit. User will just hear a buzz with no visual indication that something was launched. Here’s a basic code to achieve this:

static uint32_t const segments[] = {1000, 500, 1000, 500, 1000};  

static void init(void) {

  wakeup_service_subscribe(NULL);
  wakeup_schedule(time(NULL) + 60, 0, false);
  
  vibes_cancel();
  
  VibePattern pat = {
    .durations = segments,
    .num_segments = ARRAY_LENGTH(segments),
  };
  vibes_enqueue_custom_pattern(pat);
  
}

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

This is pretty straightforward. Line 01 declares an array for custom vibe pattern (3 one-second buzzes separated by half-a-second silence) Line 05 subscribes to WakeUp event. Ordinary you need to specify a callback function as a parameter, but our entire code runs in the Init, so we don’t use it here. Line 06 schedules app wake-up in 60 seconds. Line 08 cancels any current vibes in case any are still running. Lines 10-13 prepare structure for custom vibe sequence and Line 14 runs the vibes.

That’s it. When you launch the app – it schedules its own wakeup, buzzes the vibe and exits immediately. You’re free to do what you want – set a watchface, run another app etc. When time comes – the app wakes up, buzzes the vibe, schedules next wakeup and exits without interfering with whatever user is doing. Etc. Etc. Etc.

Just remember that the only way to stop it is delete the app from the watch and wait for the current buzz sequence to finish.

Useful Links

Pushing pins to Pebble Time timeline from .NET code

Timeline on Pebble Time Pebble Time timeline is a very cool user interface allowing you to see future and past events and act upon them right on your watch. Right out of the box Pebble Time supports calendar pins that shows your future and past appointments in the timeline as well as weather alerts. But the real power comes from 3rd party apps using timeline – they can add anything from sports scores to latest news to TV showtimes – limit is just your imagination.
Pebble has always had open SDK – this is one of its major strengths, and Timeline is not an exception. Timeline API is a very straightforward way to push your own pins to users of your app. There’re various examples and libraries including PHP and node.js on how to deal with the timeline, but I, being mostly a Microsoft developer by trade, decided to bring Timeline into .NET. This particular example is in ASP.NET – pin is pushed from Webpage when user clicks a button, but it’s just one of the possible scenarios.

In order to push timeline pins successfully you will need 2 pieces:

  1. A watchapp that runs on Pebble. In fact after first run, that subscribes user to timeline, the app doesn’t have to be running on the watch anymore. It doesn’t even have to be on the watch. As long as it simple remains in your locker on the phone – you will continue to receive its pins
  2. Your own server that sends calls to Pebble public Timeline API to control pins

Continue reading →

Mask Effect for EffectLayer for Pebble

I’ve written before about EffectLayer library for Pebble smartwatch I’ve been working on. The idea is – user places the layer over screen and that layer applies an effect to screen content.

I’ve started with a few basic effects (invert, mirror) but since then several more developers joined the project adding more cool features. Ron added 90° rotation, zoom and lens effect. Gregoire Sage added cool blur effect. LeFauve not only added FPS effect, but also optimized the library to run the effects in a very efficient way: now effect can be defined as a function (even user defined function!) and that function passed as a parameter to effect_layer_add_effect method along with parameters for that effect.

I, for my part, contributed “mask” effect. What it does is essentially lets you show parts of background image thru user defined mask, creating a feel of transparency. Continue reading →