Search code examples
canimationpebble-watchpebble-sdk

Pebble: How to create a custom animation?


I am having trouble implementing a custom animation on Pebble. There's not really any tutorial online to follow, the only thing I can find is the official Pebble one: https://developer.pebble.com/guides/pebble-apps/display-and-animations/property-animations/#writing-custom-animation-types

This is part of my project's code:

static Animation *large_pin_animation;

static void anim_update_handler(Animation *animation, const AnimationProgress progress) {
  APP_LOG(APP_LOG_LEVEL_INFO, "%d", (int)progress);
}

static void window_load(Window *window) {
  large_pin_animation = animation_create();
  animation_set_duration(large_pin_animation, 1000);
  animation_set_delay(large_pin_animation, 0);
  AnimationImplementation anim_implementation = (AnimationImplementation) {
    .update = anim_update_handler
  };
  animation_set_implementation(large_pin_animation, &anim_implementation);
}

When I call animation_schedule(large_pin_animation); the app crashes, and the Pebble logs aren't helpful (it says app fault, so some kind of segfault). Is there something I'm missing?


Solution

  • The issue is the scope of the anim_implementation variable. It should be declared where the Animation is declared, otherwise after window_load it goes out of scope and is deallocated, so in the other function where I'm trying to run the animation, it doesn't know what anim_implementation is anymore.

    Another issue that I had was that in order to run the animation multiple times, I had to re-create the animation. So, in the end I put all the animation stuff in a separate function:

    static Animation *large_pin_animation;
    static AnimationImplementation anim_implementation;
    
    static void anim_update_handler(Animation *animation, const AnimationProgress progress) {
      APP_LOG(APP_LOG_LEVEL_INFO, "%d", (int)progress);
    }
    
    static void animate_large_pin() {
      large_pin_animation = animation_create();
      animation_set_duration(large_pin_animation, 1000);
      animation_set_delay(large_pin_animation, 0);
      anim_implementation = (AnimationImplementation) {
        .update = anim_update_handler
      };
      animation_set_implementation(large_pin_animation, &anim_implementation);
      animation_schedule(large_pin_animation);
    }