3

I wanted to write an analogue watchface for my pebble, so I decided to get started by animating a second hand only to make sure I understand what I'm doing. I've done this, and strangely it works only between 0 and 22 seconds past the minute. As soon as it ticks over to 23 seconds, it crashes and won't start correctly again until the start of the next minute.

I have a single layer occupying the whole screen, and this is the callback function to update the layer:

void face_layer_update_callback(Layer *me, GContext* ctx) {

  //set colour
  graphics_context_set_stroke_color(ctx,  GColorBlack);

  time_t now = time(NULL);
  struct tm *t = localtime(&now);
  int seconds = t->tm_sec;

  int hand_length = 50;

  GPoint p0 = GPoint(72, 84);

  int x, y, z;

  z = seconds * 6;

  x = hand_length * sin( (PI/180) * z);
  y = hand_length * cos( (PI/180) * z);

  GPoint p1 = GPoint(72 + x, 84 - y);

  graphics_draw_line(ctx, p0, p1);

}

I've added logging to output the calculated values z, x and y and they're exactly what I expect them to be. Can anyone see what might be going wrong?

(This is on an original pebble by the way, not the Pebble Time.)

Update: following Thomas's comment, I've added more logging, and the code now looks like:

void face_layer_update_callback(Layer *me, GContext* ctx) {

  APP_LOG(APP_LOG_LEVEL_DEBUG, "Start of callback routine");

  //set colour
  graphics_context_set_stroke_color(ctx,  GColorBlack);

  time_t now = time(NULL);
  struct tm *t = localtime(&now);
  int seconds = t->tm_sec;

  APP_LOG(APP_LOG_LEVEL_DEBUG, "seconds: %d", seconds);

  int hand_length = 50;

  GPoint p0 = GPoint(72, 84);

  int x, y;
  int degrees;

  degrees = seconds * 6;
  float radians = (PI/180) * degrees;

  APP_LOG(APP_LOG_LEVEL_DEBUG, "About to do trigonometry: degrees = %d", degrees);

  x = hand_length * sin(radians);
  APP_LOG(APP_LOG_LEVEL_DEBUG, "Done sin, about to do cos");
  y = hand_length * cos(radians);

  APP_LOG(APP_LOG_LEVEL_DEBUG, "x = %d", x);
  APP_LOG(APP_LOG_LEVEL_DEBUG, "y = %d", y);
  APP_LOG(APP_LOG_LEVEL_DEBUG, "End of hand: %d, %d", 72 + x, 84 - y);

  GPoint p1 = GPoint(72 + x, 84 - y);

  APP_LOG(APP_LOG_LEVEL_DEBUG, "About to draw hand");

  graphics_draw_line(ctx, p0, p1);

The logging shows this:

[INFO    ] D analogueface.c:11 Start of callback routine
[INFO    ] D analogueface.c:20 seconds: 21
[INFO    ] D analogueface.c:32 About to do trigonometry: degrees = 126
[INFO    ] D analogueface.c:35 Done sin, about to do cos
[INFO    ] D analogueface.c:38 x = 40
[INFO    ] D analogueface.c:39 y = -29
[INFO    ] D analogueface.c:40 End of hand: 112, 113
[INFO    ] D analogueface.c:44 About to draw hand
[INFO    ] D analogueface.c:11 Start of callback routine
[INFO    ] D analogueface.c:20 seconds: 22
[INFO    ] D analogueface.c:32 About to do trigonometry: degrees = 132
[INFO    ] D analogueface.c:35 Done sin, about to do cos
[INFO    ] D analogueface.c:38 x = 37
[INFO    ] D analogueface.c:39 y = -33
[INFO    ] D analogueface.c:40 End of hand: 109, 117
[INFO    ] D analogueface.c:44 About to draw hand
[INFO    ] D analogueface.c:11 Start of callback routine
[INFO    ] D analogueface.c:20 seconds: 23
[INFO    ] D analogueface.c:32 About to do trigonometry: degrees = 138

I tried to log the radian value, but it wouldn't format it in the log.

(Apologies for trying to put things in comments which shouldn't be there).

Tim E
  • 45
  • 7
  • Do you know which line is the guilty one? What are the values at 22 sec and 23 sec? – Thomas Ayoub Sep 22 '15 at 11:56
  • The guilty line seems to be the sin calculating line. I've made a few changes and added more logging: – Tim E Sep 22 '15 at 17:51
  • @Thomas: analogueface.c:11 Start of callback routine
    analogueface.c:20 seconds: 22
    analogueface.c:32 About to do trigonometry: degrees = 132
    analogueface.c:35 Done sin, about to do cos
    analogueface.c:38 x = 37
    analogueface.c:39 y = -33
    analogueface.c:44 About to draw hand
    analogueface.c:11 Start of callback routine
    analogueface.c:20 seconds: 23
    analogueface.c:32 About to do trigonometry: degrees = 138
    Apologies for this formatting I need to find out how to do line breaks in comments...
    – Tim E Sep 22 '15 at 18:01
  • please, edit question :) – Thomas Ayoub Sep 22 '15 at 18:08
  • @Thomas I have done - apologies for newbie error. :) – Tim E Sep 22 '15 at 18:10
  • No problem. Which version of `sin` are you using? – Thomas Ayoub Sep 22 '15 at 19:29
  • I'm not sure - is sin in glibc? If so, 2.19. – Tim E Sep 22 '15 at 20:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90364/discussion-between-thomas-and-tim-e). – Thomas Ayoub Sep 22 '15 at 20:36
  • Probably not the best solution to suggest alternative method, but why instead of actual sin/cos use Pebble built-in lookups? Take a look how point for second hand is calculated in Simple Analog: https://github.com/pebble-examples/simple-analog/ – Yuriy Galanter Nov 09 '15 at 23:02

0 Answers0