アットウィキロゴ
#blognavi
battery_state_service

Pebble timeの時計を開発メモ

以下、バッテリー表示用のプログラムの記述セット

そのままコピーして、貼り付ければ、動くようにしています。

追記:battery_state_service_subscribe(handle_battery);だけだと、バッテリー情報に変化がないと表示が切り替わらない。window_load時にバッテリーの初期値をゲットして表示さるとキビキビ表示されるようになる。(2016/09/18)



  1. #include <pebble.h>
  2.  
  3. static Window *s_main_window;
  4. static TextLayer *s_battery_layer;
  5. static GFont s_time_fontss;
  6.  
  7.  
  8. static void batteryhyouji(BatteryChargeState charge_state){
  9. //バッテリーの情報表示用、文字列の容器と初期値
  10. // static char battery_text[] = "---% charged";
  11. static char battery_text[] = "-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n";
  12.  
  13. if (charge_state.is_charging) {
  14. //バッテリーの充電中の表示
  15. snprintf(battery_text, sizeof(battery_text), "+");
  16. } else {
  17. //バッテリーの残量を得る。そして残量を表示
  18. // snprintf(battery_text, sizeof(battery_text), "%d%% charged", charge_state.charge_percent);
  19. if(charge_state.charge_percent==100){
  20. snprintf(battery_text, sizeof(battery_text), "-\n-\n-\n-\n-\n-\n-\n-\n-\n-\n");
  21. }else if(charge_state.charge_percent==90){
  22. snprintf(battery_text, sizeof(battery_text), "-\n-\n-\n-\n-\n-\n-\n-\n-\n");
  23. }else if(charge_state.charge_percent==80){
  24. snprintf(battery_text, sizeof(battery_text), "-\n-\n-\n-\n-\n-\n-\n-\n");
  25. }else if(charge_state.charge_percent==70){
  26. snprintf(battery_text, sizeof(battery_text), "-\n-\n-\n-\n-\n-\n-\n");
  27. }else if(charge_state.charge_percent==60){
  28. snprintf(battery_text, sizeof(battery_text), "-\n-\n-\n-\n-\n-\n");
  29. }else if(charge_state.charge_percent==50){
  30. snprintf(battery_text, sizeof(battery_text), "-\n-\n-\n-\n-\n");
  31. }else if(charge_state.charge_percent==40){
  32. snprintf(battery_text, sizeof(battery_text), "-\n-\n-\n-\n");
  33. }else if(charge_state.charge_percent==30){
  34. snprintf(battery_text, sizeof(battery_text), "-\n-\n-\n");
  35. }else if(charge_state.charge_percent==20){
  36. snprintf(battery_text, sizeof(battery_text), "-\n-\n");
  37. }else if(charge_state.charge_percent==10){
  38. snprintf(battery_text, sizeof(battery_text), "-\n");
  39. }else if(charge_state.charge_percent==0){
  40. snprintf(battery_text, sizeof(battery_text), " ");
  41. }
  42. }
  43. text_layer_set_text(s_battery_layer, battery_text);
  44. }
  45.  
  46.  
  47. static void handle_battery(BatteryChargeState charge_state) {
  48. batteryhyouji(charge_state);
  49. }
  50.  
  51. static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
  52. //ここに時計の表示などを記載する。
  53. }
  54.  
  55. static void main_window_load(Window *window) {
  56. Layer *window_layer = window_get_root_layer(window);
  57. GRect bounds = layer_get_frame(window_layer);
  58. //フォントを設定
  59. s_time_fontss = fonts_get_system_font(FONT_KEY_GOTHIC_18);
  60. //バッテリーの情報を表示する場所を作成(作るだけ。表示はまだ)。その位置、サイズを設定。
  61. s_battery_layer = text_layer_create(GRect(bounds.size.w*2/4-33, bounds.size.h/2-74, 20, bounds.size.h));
  62.  
  63. //Pebble timeなどのカラーの場合とPebbleなどの白黒の場合を分けて設定。
  64. #ifdef PBL_COLOR
  65. text_layer_set_text_color(s_battery_layer, GColorRed);
  66. text_layer_set_background_color(s_battery_layer, GColorClear);
  67. #else
  68. text_layer_set_text_color(s_battery_layer, GColorWhite);
  69. text_layer_set_background_color(s_battery_layer, GColorClear);
  70. #endif
  71.  
  72. //フォントの設定を関連付ける。
  73. text_layer_set_font(s_battery_layer, s_time_fontss);
  74. text_layer_set_text_alignment(s_battery_layer, GTextAlignmentCenter);
  75.  
  76. //バッテリー情報の文字列表示、初期値
  77. text_layer_set_text(s_battery_layer, "X");
  78.  
  79. //バッテリーの情報に変化があったときhandle_batteryが呼び出される
  80. battery_state_service_subscribe(handle_battery);
  81. //バッテリー初期値
  82. BatteryChargeState charge_state = battery_state_service_peek();
  83. //バッテリーの初期値表示
  84. batteryhyouji(charge_state);
  85.  
  86. //バッテリー情報を表示する場所を、時計のウインドウへセットし、表示する。
  87. layer_add_child(window_layer, text_layer_get_layer(s_battery_layer));
  88. }
  89.  
  90. static void main_window_unload(Window *window) {
  91. // Destroy TextLayer
  92. text_layer_destroy(s_battery_layer);
  93.  
  94. //バッテリーサービス解放
  95. battery_state_service_unsubscribe();
  96. }
  97.  
  98. static void init() {
  99. // Create main Window element and assign to pointer
  100. s_main_window = window_create();
  101. // Set handlers to manage the elements inside the Window
  102. window_set_window_handlers(s_main_window, (WindowHandlers) {
  103. .load = main_window_load,
  104. .unload = main_window_unload
  105. });
  106. // Show the Window on the watch, with animated=true
  107. window_stack_push(s_main_window, true);
  108.  
  109. // Register with TickTimerService
  110. //SECOND_UNITだと秒の間隔で更新する。
  111. tick_timer_service_subscribe(SECOND_UNIT, tick_handler);
  112. }
  113.  
  114. static void deinit() {
  115. // Destroy Window
  116. window_destroy(s_main_window);
  117. }
  118.  
  119. int main(void) {
  120. init();
  121. app_event_loop();
  122. deinit();
  123. }
  124.  
  125.  

カテゴリ: [開発] - &trackback() - 2016年04月15日 12:46:04
名前: コメント:



#blognavi
最終更新:2016年11月23日 23:59
添付ファイル