Pebbleアプリ開発メモ

Pebbleアプリ、Watchface開発のメモを残していきます。

【Pebble】【Watchface】時間帯ごとにアナログ/デジタルを切り替える

f:id:shotsep:20151014113201p:plain

Pebbleをしはじめてしばらく経つのですが、相変わらずデジタル時計には慣れません。

例えばですが、11時から会議で時計見たら10時58分だったとき、60-58であと2分ではなく、12度だからあと2分みたいな感じで認識するクセがついているのかと。

少し前にデジタル時計とアナログ時計の電池持ちを比べてみたところ、相当な差が出ました。AppMessage呼んでいるのも原因の一つだと思いますが、描画もかなり電池食うってのをどっかで見ました。

というわけで、時間帯でFaceを切り替える時計を作ってみました。これでしばらく実験してみたいと思います。

以下は適当ですが5分毎にアナログ/デジタル表記を切替えるサンプルです。

これで動かしてみたところ、9日くらいは持ちそうな感じですね。確実に電池持ちは良くなります。

実験結果

切り替えずに常時アナログでもあまり変わらない結果に。。

電池食うのはAppMessageみたいですね。

#include "pebble.h"
#include "line_draw_with_width.c"

#include <math.h>

#define PI 3.141592653589793

static const GPathInfo MINUTE_HAND_POINTS = {
  10, (GPoint []){
    {-1, 6},
    {1, 6},
    {1, -8},
    {3, -10},
    {3, -68},
    {1, -71},
    {-1, -71},
    {-3, -68},
    {-3, -10},
    {-1, -8}
  }
};

static const GPathInfo HOUR_HAND_POINTS = {
  10, (GPoint []){
    {-1, 6},
    {1, 6},
    {1, -8},
    {3, -10},
    {3, -40},
    {1, -43},
    {-1, -43},
    {-3, -40},
    {-3, -10},
    {-1, -8}
  }
};

static Window *window;
static Layer *s_simple_bg_layer, *s_date_layer, *s_hands_layer;
static TextLayer *s_num_label, *s_battery_layer;

static GPath *s_minute_arrow, *s_hour_arrow;
static char s_num_buffer[4];

static BitmapLayer *s_bt_layer;
static GBitmap *s_bt_bitmap;

static BitmapLayer *s_charge_layer;
static GBitmap *s_charge_bitmap;

static TextLayer *s_digit_layer;

static bool isShowingAnalog;
static void toggleWatch();

static void bg_update_proc(Layer *layer, GContext *ctx) {
  graphics_context_set_fill_color(ctx, GColorBlack);
  graphics_fill_rect(ctx, layer_get_bounds(layer), 0, GCornerNone);
}

static void handle_battery(BatteryChargeState charge_state) {
  static char battery_text[] = "100%";

  layer_set_hidden(bitmap_layer_get_layer(s_charge_layer), !charge_state.is_charging);

  snprintf(battery_text, sizeof(battery_text), "%d%%", charge_state.charge_percent);

  text_layer_set_text(s_battery_layer, battery_text);
}

static void handle_bluetooth(bool connected) {
  layer_set_hidden(bitmap_layer_get_layer(s_bt_layer), !connected);
}

static void hands_update_proc(Layer *layer, GContext *ctx) {

  APP_LOG(APP_LOG_LEVEL_DEBUG, "hands_update_proc");
  if(isShowingAnalog) {
    GRect bounds = layer_get_bounds(layer);
    GPoint center = grect_center_point(&bounds);
    int16_t second_hand_length = bounds.size.w / 2 - 10;

    time_t now = time(NULL);
    struct tm *t = localtime(&now);
    int32_t second_angle = TRIG_MAX_ANGLE * t->tm_sec / 60;
    GPoint second_hand = {
      .x = (int16_t)(sin_lookup(second_angle) * (int32_t)second_hand_length / TRIG_MAX_RATIO) + center.x,
      .y = (int16_t)(-cos_lookup(second_angle) * (int32_t)second_hand_length / TRIG_MAX_RATIO) + center.y,
    };

    graphics_context_set_stroke_color(ctx, GColorWhite);
    graphics_draw_line2(ctx, second_hand, center, 1);

    graphics_context_set_fill_color(ctx, GColorWhite);
    graphics_context_set_stroke_color(ctx, GColorBlack);

    gpath_rotate_to(s_minute_arrow, TRIG_MAX_ANGLE * t->tm_min / 60);
    gpath_draw_filled(ctx, s_minute_arrow);
    gpath_draw_outline(ctx, s_minute_arrow);

    gpath_rotate_to(s_hour_arrow, (TRIG_MAX_ANGLE * (((t->tm_hour % 12) * 6) + (t->tm_min / 10))) / (12 * 6));
    gpath_draw_filled(ctx, s_hour_arrow);
    gpath_draw_outline(ctx, s_hour_arrow);

    handle_battery(battery_state_service_peek());
  } else {
    time_t now = time(NULL);
    struct tm *current_time = localtime(&now);
    static char s_time_text[] = "00:00";

    strftime(s_time_text, sizeof(s_time_text), "%X", current_time);
    text_layer_set_text(s_digit_layer, s_time_text);
  }
  
}

static void date_update_proc(Layer *layer, GContext *ctx) {
  time_t now = time(NULL);
  struct tm *t = localtime(&now);

  strftime(s_num_buffer, sizeof(s_num_buffer), "%d", t);
  text_layer_set_text(s_num_label, s_num_buffer);
}

static void handle_second_tick(struct tm *tick_time, TimeUnits units_changed) {

  if(isShowingAnalog) {
    layer_mark_dirty(window_get_root_layer(window));
  } else {
    if(tick_time->tm_sec == 0) {
      layer_mark_dirty(window_get_root_layer(window));
    }
  }

  //if(tick_time->tm_sec % 30 == 0) {
  if(tick_time->tm_sec == 0 && tick_time->tm_min % 5 == 0) {
  /*if((tick_time->tm_hour == 1 &&
      tick_time->tm_min == 0 &&
      tick_time->tm_sec == 0) ||
     (tick_time->tm_hour == 8 &&
      tick_time->tm_min == 0 &&
      tick_time->tm_sec == 0)
    ) {*/
    toggleWatch();
  }
}

static void addElementsToRootLayer();

static void window_load(Window *window) {

  addElementsToRootLayer();

  battery_state_service_subscribe(handle_battery);
  bluetooth_connection_service_subscribe(handle_bluetooth);
}

static void window_unload(Window *window) {
  
  layer_destroy(s_simple_bg_layer);
  if(isShowingAnalog) {
    layer_destroy(s_date_layer);
    layer_destroy(s_hands_layer);

    text_layer_destroy(s_num_label);
    text_layer_destroy(s_battery_layer);
  } else {
    text_layer_destroy(s_digit_layer);
  }
  
  battery_state_service_unsubscribe();
  bluetooth_connection_service_unsubscribe();
}

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

  s_num_buffer[0] = '\0';

  s_minute_arrow = gpath_create(&MINUTE_HAND_POINTS);
  s_hour_arrow = gpath_create(&HOUR_HAND_POINTS);

  Layer *window_layer = window_get_root_layer(window);
  GRect bounds = layer_get_bounds(window_layer);
  GPoint center = grect_center_point(&bounds);
  gpath_move_to(s_minute_arrow, center);
  gpath_move_to(s_hour_arrow, center);

  isShowingAnalog = true;
  tick_timer_service_subscribe(SECOND_UNIT, handle_second_tick);
}

static void removeElementsToRootLayer();

static void toggleWatch() {
  if(isShowingAnalog) {
    removeElementsToRootLayer();
  } else {
    addElementsToRootLayer();
  }

  isShowingAnalog = !isShowingAnalog;
}

static void addElementsToRootLayer() {
  Layer *window_layer = window_get_root_layer(window);
  
  layer_remove_child_layers(window_layer);
  
  GRect bounds = layer_get_bounds(window_layer);

  s_simple_bg_layer = layer_create(bounds);
  layer_set_update_proc(s_simple_bg_layer, bg_update_proc);
  layer_add_child(window_layer, s_simple_bg_layer);

  s_bt_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_ICON_BT);
  s_bt_layer = bitmap_layer_create(GRect(0, 1, 15, 18));
  bitmap_layer_set_bitmap(s_bt_layer, s_bt_bitmap);
  layer_add_child(window_get_root_layer(window), bitmap_layer_get_layer(s_bt_layer));

  s_charge_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_ICON_CHARGE);
  s_charge_layer = bitmap_layer_create(GRect(bounds.size.w - 15, 16, 15, 18));
  bitmap_layer_set_bitmap(s_charge_layer, s_charge_bitmap);
  layer_add_child(window_get_root_layer(window), bitmap_layer_get_layer(s_charge_layer));
  layer_set_hidden(bitmap_layer_get_layer(s_charge_layer), true);

  s_date_layer = layer_create(bounds);
  layer_set_update_proc(s_date_layer, date_update_proc);
  layer_add_child(window_layer, s_date_layer);

  s_num_label = text_layer_create(GRect(bounds.size.w - 20, bounds.size.h / 2 - 13 - 10, 27, 20));
  text_layer_set_text(s_num_label, s_num_buffer);
  text_layer_set_background_color(s_num_label, GColorClear);
  text_layer_set_text_color(s_num_label, GColorWhite);
  text_layer_set_font(s_num_label, fonts_get_system_font(FONT_KEY_GOTHIC_18));

  layer_add_child(s_date_layer, text_layer_get_layer(s_num_label));

  s_hands_layer = layer_create(GRect(0, -12, bounds.size.w, bounds.size.h));
  layer_set_update_proc(s_hands_layer, hands_update_proc);
  layer_add_child(window_layer, s_hands_layer);

  s_battery_layer = text_layer_create(GRect(bounds.size.w - 50, 0, 50, 30));
  text_layer_set_text_color(s_battery_layer, GColorWhite);
  text_layer_set_background_color(s_battery_layer, GColorClear);
  text_layer_set_font(s_battery_layer, fonts_get_system_font(FONT_KEY_GOTHIC_14));
  text_layer_set_text_alignment(s_battery_layer, GTextAlignmentRight);
  text_layer_set_text(s_battery_layer, "100%");

  layer_add_child(window_layer, text_layer_get_layer(s_battery_layer));
}

static void removeElementsToRootLayer() {
  Layer *window_layer = window_get_root_layer(window);
  layer_remove_child_layers(window_layer);

  GRect bounds = layer_get_bounds(window_layer);
  
  bitmap_layer_destroy(s_charge_layer);
  bitmap_layer_destroy(s_bt_layer);

  layer_destroy(s_date_layer);
  text_layer_destroy(s_num_label);

  layer_destroy(s_hands_layer);
  text_layer_destroy(s_battery_layer);

  gbitmap_destroy(s_bt_bitmap);
  gbitmap_destroy(s_charge_bitmap);
  
  s_simple_bg_layer = layer_create(bounds);
  layer_set_update_proc(s_simple_bg_layer, hands_update_proc);
  layer_add_child(window_layer, s_simple_bg_layer);
  
  s_digit_layer = text_layer_create(GRect(0, 67, 144, 34));
  text_layer_set_text_color(s_digit_layer, GColorBlack);
  text_layer_set_background_color(s_digit_layer, GColorClear);
  text_layer_set_font(s_digit_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD));
  text_layer_set_text_alignment(s_digit_layer, GTextAlignmentCenter);
  
  time_t now = time(NULL);
  struct tm *current_time = localtime(&now);
  static char s_time_text[] = "00:00";

  strftime(s_time_text, sizeof(s_time_text), "%X", current_time);
  text_layer_set_text(s_digit_layer, s_time_text);
  layer_add_child(window_layer, text_layer_get_layer(s_digit_layer));
}

static void deinit() {
  gpath_destroy(s_minute_arrow);
  gpath_destroy(s_hour_arrow);

  tick_timer_service_unsubscribe();
  window_destroy(window);
}

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