#blognavi
accel_data_service

Pebble timeの時計を開発メモ

以下、accel(加速度センサ)表示用のプログラムの記述セット

そのままコピーして、貼り付ければ、動くようにしています。
  1. #include <pebble.h>
  2.  
  3. static Window *s_main_window;
  4. static TextLayer *s_time_layer;
  5. static GFont s_time_font;
  6.  
  7. static void accel_tap_handler(AccelAxisType axis, int32_t direction) {
  8. // A tap event occured
  9. }
  10.  
  11. static void accel_data_handler(AccelData *data, uint32_t num_samples) {
  12. // Read sample 0's x, y, and z values
  13. int16_t x = data[0].x;
  14. int16_t y = data[0].y;
  15. int16_t z = data[0].z;
  16.  
  17. // Determine if the sample occured during vibration, and when it occured
  18. bool did_vibrate = data[0].did_vibrate;
  19. //uint64_t timestamp = data[0].timestamp;
  20.  
  21. if(!did_vibrate) {
  22. // Print it out
  23. // APP_LOG(APP_LOG_LEVEL_INFO, "t: %llu, x: %d, y: %d, z: %d", timestamp, x, y, z);
  24.  
  25. if(abs(x) + abs(y) + abs(z) > 4000) {
  26. #ifdef PBL_COLOR
  27. // text_layer_set_background_color(s_time_layer, GColorFromRGB(rand() % 255, rand() % 255, rand() % 255));
  28. text_layer_set_background_color(s_time_layer, GColorFromRGB(rand() % 170, rand() % 85, rand() % 170));
  29. #endif
  30. }
  31.  
  32. } else {
  33. // Discard with a warning
  34. APP_LOG(APP_LOG_LEVEL_WARNING, "Vibration occured during collection");
  35. }
  36. }
  37.  
  38. static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
  39.  
  40. }
  41.  
  42. static void main_window_load(Window *window) {
  43. Layer *window_layer = window_get_root_layer(window);
  44. GRect bounds = layer_get_frame(window_layer);
  45.  
  46. s_time_layer = text_layer_create(GRect(0,0, bounds.size.w, bounds.size.h));
  47.  
  48. text_layer_set_background_color(s_time_layer, GColorBlack);
  49. text_layer_set_text_color(s_time_layer, GColorWhite);
  50. text_layer_set_text(s_time_layer, "");
  51. // Improve the layout to be more like a watchface
  52. //text_layer_set_font(s_time_layer, fonts_get_system_font(FONT_KEY_BITHAM_42_BOLD));
  53. // Create GFont
  54. s_time_font = fonts_get_system_font(FONT_KEY_BITHAM_42_BOLD);
  55.  
  56. // Apply to TextLayer
  57. text_layer_set_font(s_time_layer, s_time_font);
  58. text_layer_set_text_alignment(s_time_layer, GTextAlignmentCenter);
  59.  
  60. // Add it as a child layer to the Window's root layer
  61. layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_time_layer));
  62.  
  63. uint32_t num_samples = 1; // Number of samples per batch/callback
  64. // Subscribe to batched data events
  65. accel_data_service_subscribe(num_samples, accel_data_handler);
  66. //accel_service_set_samples_per_update(3);
  67. accel_service_set_sampling_rate(ACCEL_SAMPLING_10HZ);
  68. }
  69.  
  70. static void main_window_unload(Window *window) {
  71. // Destroy TextLayer
  72. text_layer_destroy(s_time_layer);
  73.  
  74. accel_tap_service_unsubscribe();
  75. }
  76.  
  77. static void init() {
  78. // Create main Window element and assign to pointer
  79. s_main_window = window_create();
  80. // Set handlers to manage the elements inside the Window
  81. window_set_window_handlers(s_main_window, (WindowHandlers) {
  82. .load = main_window_load,
  83. .unload = main_window_unload
  84. });
  85. // Show the Window on the watch, with animated=true
  86. window_stack_push(s_main_window, true);
  87.  
  88. // Register with TickTimerService
  89. tick_timer_service_subscribe(SECOND_UNIT, tick_handler);
  90. accel_tap_service_subscribe(accel_tap_handler);
  91. }
  92.  
  93. static void deinit() {
  94. // Destroy Window
  95. window_destroy(s_main_window);
  96. }
  97.  
  98. int main(void) {
  99. init();
  100. app_event_loop();
  101. deinit();
  102. }
  103.  



カテゴリ: [開発] - &trackback() - 2016年04月18日 22:31:03
名前: コメント:
#blognavi
最終更新:2016年11月23日 23:57