Browse code

fix vibration every minutes on low battery

Louis authored on12/12/2022 18:41:53
Showing1 changed files
... ...
@@ -9,6 +9,7 @@
9 9
 static Window *window;
10 10
 static TextLayer *time_text_layer;
11 11
 static TextLayer *date_text_layer;
12
+static TextLayer *s_battery_layer;
12 13
 
13 14
 static GBitmap *world_bitmap;
14 15
 
... ...
@@ -16,10 +17,10 @@ static Layer *canvas;
16 17
 static GBitmap *image;
17 18
 static int redraw_counter;
18 19
 // BT connection & Battery icon declare
19
-static BitmapLayer *s_battery_icon_layer, *s_bt_icon_layer;
20
-static GBitmap *s_battery_icon_bitmap, *s_bt_icon_bitmap;
20
+static BitmapLayer *s_bt_icon_layer; //*s_battery_icon_layer
21
+static GBitmap *s_bt_icon_bitmap; //*s_battery_icon_bitmap
21 22
 static bool low_battery;
22
-static int s_battery_level;
23
+static int i_battery_level;
23 24
 // s is set to memory of size STR_SIZE, and temporarily stores strings
24 25
 char *s;
25 26
 
... ...
@@ -104,21 +105,38 @@ static void bluetooth_callback(bool connected) {
104 105
   if(!connected && !quiet_time_is_active ()) {
105 106
     // Issue a vibrating alert
106 107
     vibes_double_pulse();
107
-  }
108
+  } 
108 109
 }
109 110
 
110 111
 static void battery_callback(BatteryChargeState state) {
112
+  static char buff[4] = {};
113
+  int i = 0,temp_num = state.charge_percent, length = 0;
114
+
111 115
   // Record the new battery level
112
-  s_battery_level = state.charge_percent;
116
+  i_battery_level=state.charge_percent;
117
+  // count how many characters in the number
118
+    while(temp_num) {
119
+      temp_num /= 10;
120
+      length++;
121
+    }
122
+  for(i = 0; i < length; i++) {
123
+    buff[(length-1)-i] = '0' + (i_battery_level % 10);
124
+    i_battery_level /= 10;
125
+  }
126
+  buff[i] = '\0'; // can't forget the null byte to properly end our string
113 127
   
114
- // display icon and vibrate if low battery
115
-  if( s_battery_level < 11 && !low_battery && !state.is_charging ) {
128
+  text_layer_set_text(s_battery_layer, buff);
129
+  
130
+  // display --icon and-- vibrate if low battery and not charging
131
+  
132
+  if( state.charge_percent < 11 && !low_battery && !state.is_charging ) {
116 133
     vibes_double_pulse();
117 134
     low_battery = true;
118
-  }else if (state.is_charging) {
135
+  }else if (state.is_charging){
119 136
     low_battery = false;
120 137
   }
121
-  layer_set_hidden(bitmap_layer_get_layer(s_battery_icon_layer), !low_battery);
138
+  // Batterie is now displayed as integer, no need to display icon below
139
+  // layer_set_hidden(bitmap_layer_get_layer(s_battery_icon_layer), !low_battery);
122 140
 
123 141
 }
124 142
 
... ...
@@ -134,21 +152,29 @@ static void window_load(Window *window) {
134 152
   Layer *window_layer = window_get_root_layer(window);
135 153
   GRect bounds = layer_get_bounds(window_layer);
136 154
 
137
-  time_text_layer = text_layer_create(GRect(0, 72, 144-0, 168-72));
155
+  time_text_layer = text_layer_create(GRect(0, 72, 144-0, 49));
138 156
   text_layer_set_background_color(time_text_layer, background_color);
139 157
   text_layer_set_text_color(time_text_layer, foreground_color);
140 158
   text_layer_set_font(time_text_layer, fonts_get_system_font(FONT_KEY_ROBOTO_BOLD_SUBSET_49));
141
-  text_layer_set_text(time_text_layer, "");
159
+  text_layer_set_text(time_text_layer, "--");
142 160
   text_layer_set_text_alignment(time_text_layer, GTextAlignmentCenter);
143 161
   layer_add_child(window_layer, text_layer_get_layer(time_text_layer));
144 162
 
145
-  date_text_layer = text_layer_create(GRect(0, 130, 144-0, 168-130));
163
+  date_text_layer = text_layer_create(GRect(0, 121, 144-0, 21));
146 164
   text_layer_set_background_color(date_text_layer, background_color);
147 165
   text_layer_set_text_color(date_text_layer, foreground_color);
148 166
   text_layer_set_font(date_text_layer, fonts_get_system_font(FONT_KEY_ROBOTO_CONDENSED_21));
149
-  text_layer_set_text(date_text_layer, "");
167
+  text_layer_set_text(date_text_layer, "--");
150 168
   text_layer_set_text_alignment(date_text_layer, GTextAlignmentCenter);
151 169
   layer_add_child(window_layer, text_layer_get_layer(date_text_layer));
170
+  
171
+  s_battery_layer = text_layer_create(GRect(102, 147, 42, 20));
172
+  text_layer_set_background_color(s_battery_layer, background_color);
173
+  text_layer_set_text_color(s_battery_layer, foreground_color);
174
+  text_layer_set_font(s_battery_layer, fonts_get_system_font(FONT_KEY_LECO_20_BOLD_NUMBERS));
175
+  text_layer_set_text(s_battery_layer, "---");
176
+  text_layer_set_text_alignment(s_battery_layer, GTextAlignmentRight);
177
+  layer_add_child(window_layer, text_layer_get_layer(s_battery_layer));
152 178
 
153 179
   canvas = layer_create(GRect(0, 0, bounds.size.w, bounds.size.h));
154 180
   layer_set_update_proc(canvas, draw_watch);
... ...
@@ -160,13 +186,14 @@ static void window_load(Window *window) {
160 186
   //----------------
161 187
   
162 188
   // Create the Battery icon GBitmap
163
-  s_battery_icon_bitmap = gbitmap_create_with_resource(RESOURCE_ID_BATTERY );
189
+  //s_battery_icon_bitmap = gbitmap_create_with_resource(RESOURCE_ID_BATTERY );
164 190
   
165 191
   // Create the BitmapLayer to display the GBitmap
192
+  /*
166 193
   s_battery_icon_layer = bitmap_layer_create(GRect( 127, 143, 14, 22));
167 194
   bitmap_layer_set_bitmap(s_battery_icon_layer, s_battery_icon_bitmap);
168 195
   layer_add_child(window_layer, bitmap_layer_get_layer(s_battery_icon_layer));
169
-  
196
+  */
170 197
   //----------------
171 198
   
172 199
   // Create the Bluetooth icon GBitmap
... ...
@@ -190,8 +217,8 @@ static void window_unload(Window *window) {
190 217
   gbitmap_destroy(image);
191 218
   gbitmap_destroy(s_bt_icon_bitmap);
192 219
   bitmap_layer_destroy(s_bt_icon_layer);
193
-  gbitmap_destroy(s_battery_icon_bitmap);
194
-  bitmap_layer_destroy(s_battery_icon_layer);
220
+  //gbitmap_destroy(s_battery_icon_bitmap);
221
+  text_layer_destroy(s_battery_layer);
195 222
 }
196 223
 
197 224
 static void init(void) {
Louis authored on25/07/2018 21:41:41
Showing1 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,239 @@
1
+#include <pebble.h>
2
+#include "config.h"
3
+
4
+#define STR_SIZE 20
5
+#define REDRAW_INTERVAL 1
6
+#define WIDTH 144
7
+#define HEIGHT 72
8
+
9
+static Window *window;
10
+static TextLayer *time_text_layer;
11
+static TextLayer *date_text_layer;
12
+
13
+static GBitmap *world_bitmap;
14
+
15
+static Layer *canvas;
16
+static GBitmap *image;
17
+static int redraw_counter;
18
+// BT connection & Battery icon declare
19
+static BitmapLayer *s_battery_icon_layer, *s_bt_icon_layer;
20
+static GBitmap *s_battery_icon_bitmap, *s_bt_icon_bitmap;
21
+static bool low_battery;
22
+static int s_battery_level;
23
+// s is set to memory of size STR_SIZE, and temporarily stores strings
24
+char *s;
25
+
26
+
27
+static void draw_earth() {
28
+  // ##### calculate the time
29
+  int now = (int)time(NULL);
30
+
31
+  float day_of_year; // value from 0 to 1 of progress through a year
32
+  float time_of_day; // value from 0 to 1 of progress through a day
33
+  // approx number of leap years since epoch
34
+  // = now / SECONDS_IN_YEAR * .24; (.24 = average rate of leap years)
35
+  int leap_years = (int)((float)now / 131487192.0);
36
+  // day_of_year is an estimate, but should be correct to within one day
37
+  day_of_year = now - (((int)((float)now / 31556926.0) * 365 + leap_years) * 86400);
38
+  day_of_year = day_of_year / 86400.0;
39
+  time_of_day = day_of_year - (int)day_of_year;
40
+  day_of_year = day_of_year / 365.0;
41
+  // ##### calculate the position of the sun
42
+  // left to right of world goes from 0 to 65536
43
+  int sun_x = (int)((float)TRIG_MAX_ANGLE * (1.0 - time_of_day));
44
+  // bottom to top of world goes from -32768 to 32768
45
+  // 0.2164 is march 20, the 79th day of the year, the march equinox
46
+  // Earth's inclination is 23.4 degrees, so sun should vary 23.4/90=.26 up and down
47
+  int sun_y = -sin_lookup((day_of_year - 0.2164) * TRIG_MAX_ANGLE) * .26 * .25;
48
+  // ##### draw the bitmap
49
+  int x, y;
50
+  for(x = 0; x < WIDTH; x++) {
51
+    int x_angle = (int)((float)TRIG_MAX_ANGLE * (float)x / (float)(WIDTH));
52
+    for(y = 0; y < HEIGHT; y++) {
53
+      int y_angle = (int)((float)TRIG_MAX_ANGLE * (float)y / (float)(HEIGHT * 2)) - TRIG_MAX_ANGLE/4;
54
+      // spherical law of cosines
55
+      float angle = ((float)sin_lookup(sun_y)/(float)TRIG_MAX_RATIO) * ((float)sin_lookup(y_angle)/(float)TRIG_MAX_RATIO);
56
+      angle = angle + ((float)cos_lookup(sun_y)/(float)TRIG_MAX_RATIO) * ((float)cos_lookup(y_angle)/(float)TRIG_MAX_RATIO) * ((float)cos_lookup(sun_x - x_angle)/(float)TRIG_MAX_RATIO);
57
+      int byte = y * gbitmap_get_bytes_per_row(image) + (int)(x / 8);
58
+      //if ((angle < 0) ^ (0x1 & (((char *)gbitmap_get_data(world_bitmap))[byte] >> (x % 8)))) {
59
+      if ( (angle < 0) ^ (0x1 & (((char *)gbitmap_get_data(world_bitmap))[byte] >> (x % 8)))) {
60
+        // white pixel
61
+        ((char *)gbitmap_get_data(image))[byte] = ((char *)gbitmap_get_data(image))[byte] | (0x1 << (x % 8));
62
+     } else {
63
+        // black pixel
64
+        ((char *)gbitmap_get_data(image))[byte] = ((char *)gbitmap_get_data(image))[byte] & ~(0x1 << (x % 8));
65
+      }
66
+    }
67
+  }
68
+  layer_mark_dirty(canvas);
69
+}
70
+
71
+static void draw_watch(struct Layer *layer, GContext *ctx) {
72
+  graphics_draw_bitmap_in_rect(ctx, image, gbitmap_get_bounds(image));
73
+}
74
+
75
+static void handle_minute_tick(struct tm *tick_time, TimeUnits units_changed) {
76
+  static char time_text[] = "00:00";
77
+  static char date_text[] = "Xxx, Xxx 00";
78
+
79
+  strftime(date_text, sizeof(date_text), "%a, %b %e", tick_time);
80
+  text_layer_set_text(date_text_layer, date_text);
81
+
82
+  if (clock_is_24h_style()) {
83
+    strftime(time_text, sizeof(time_text), "%R", tick_time);
84
+  } else {
85
+    strftime(time_text, sizeof(time_text), "%I:%M", tick_time);
86
+  }
87
+  if (!clock_is_24h_style() && (time_text[0] == '0')) {
88
+    memmove(time_text, &time_text[1], sizeof(time_text) - 1);
89
+  }
90
+  text_layer_set_text(time_text_layer, time_text);
91
+ 
92
+  redraw_counter++;
93
+  if (redraw_counter >= REDRAW_INTERVAL) {
94
+    draw_earth();
95
+    redraw_counter = 0;
96
+  }
97
+}
98
+
99
+
100
+static void bluetooth_callback(bool connected) {
101
+  // Show ko icon if disconnected
102
+  layer_set_hidden(bitmap_layer_get_layer(s_bt_icon_layer), connected);
103
+
104
+  if(!connected && !quiet_time_is_active ()) {
105
+    // Issue a vibrating alert
106
+    vibes_double_pulse();
107
+  }
108
+}
109
+
110
+static void battery_callback(BatteryChargeState state) {
111
+  // Record the new battery level
112
+  s_battery_level = state.charge_percent;
113
+  
114
+ // display icon and vibrate if low battery
115
+  if( s_battery_level < 11 && !low_battery && !state.is_charging ) {
116
+    vibes_double_pulse();
117
+    low_battery = true;
118
+  }else if (state.is_charging) {
119
+    low_battery = false;
120
+  }
121
+  layer_set_hidden(bitmap_layer_get_layer(s_battery_icon_layer), !low_battery);
122
+
123
+}
124
+
125
+static void window_load(Window *window) {
126
+#ifdef BLACK_ON_WHITE
127
+  GColor background_color = GColorWhite;
128
+  GColor foreground_color = GColorBlack;
129
+#else
130
+  GColor background_color = GColorBlack;
131
+  GColor foreground_color = GColorWhite;
132
+#endif
133
+  window_set_background_color(window, background_color);
134
+  Layer *window_layer = window_get_root_layer(window);
135
+  GRect bounds = layer_get_bounds(window_layer);
136
+
137
+  time_text_layer = text_layer_create(GRect(0, 72, 144-0, 168-72));
138
+  text_layer_set_background_color(time_text_layer, background_color);
139
+  text_layer_set_text_color(time_text_layer, foreground_color);
140
+  text_layer_set_font(time_text_layer, fonts_get_system_font(FONT_KEY_ROBOTO_BOLD_SUBSET_49));
141
+  text_layer_set_text(time_text_layer, "");
142
+  text_layer_set_text_alignment(time_text_layer, GTextAlignmentCenter);
143
+  layer_add_child(window_layer, text_layer_get_layer(time_text_layer));
144
+
145
+  date_text_layer = text_layer_create(GRect(0, 130, 144-0, 168-130));
146
+  text_layer_set_background_color(date_text_layer, background_color);
147
+  text_layer_set_text_color(date_text_layer, foreground_color);
148
+  text_layer_set_font(date_text_layer, fonts_get_system_font(FONT_KEY_ROBOTO_CONDENSED_21));
149
+  text_layer_set_text(date_text_layer, "");
150
+  text_layer_set_text_alignment(date_text_layer, GTextAlignmentCenter);
151
+  layer_add_child(window_layer, text_layer_get_layer(date_text_layer));
152
+
153
+  canvas = layer_create(GRect(0, 0, bounds.size.w, bounds.size.h));
154
+  layer_set_update_proc(canvas, draw_watch);
155
+  layer_add_child(window_layer, canvas);
156
+
157
+  image = gbitmap_create_blank(GSize(WIDTH, HEIGHT), GBitmapFormat1Bit);
158
+
159
+  draw_earth();
160
+  //----------------
161
+  
162
+  // Create the Battery icon GBitmap
163
+  s_battery_icon_bitmap = gbitmap_create_with_resource(RESOURCE_ID_BATTERY );
164
+  
165
+  // Create the BitmapLayer to display the GBitmap
166
+  s_battery_icon_layer = bitmap_layer_create(GRect( 127, 143, 14, 22));
167
+  bitmap_layer_set_bitmap(s_battery_icon_layer, s_battery_icon_bitmap);
168
+  layer_add_child(window_layer, bitmap_layer_get_layer(s_battery_icon_layer));
169
+  
170
+  //----------------
171
+  
172
+  // Create the Bluetooth icon GBitmap
173
+  s_bt_icon_bitmap = gbitmap_create_with_resource(RESOURCE_ID_BT_ko_icon );
174
+  
175
+  // Create the BitmapLayer to display the GBitmap
176
+  s_bt_icon_layer = bitmap_layer_create(GRect( 3, 143, 14, 22));
177
+  bitmap_layer_set_bitmap(s_bt_icon_layer, s_bt_icon_bitmap);
178
+  layer_add_child(window_layer, bitmap_layer_get_layer(s_bt_icon_layer));
179
+  
180
+  // show correct state of battery from start
181
+  battery_callback(battery_state_service_peek());
182
+  // Show the correct state of the BT connection from the start
183
+  bluetooth_callback(connection_service_peek_pebble_app_connection());
184
+}
185
+
186
+static void window_unload(Window *window) {
187
+  text_layer_destroy(time_text_layer);
188
+  text_layer_destroy(date_text_layer);
189
+  layer_destroy(canvas);
190
+  gbitmap_destroy(image);
191
+  gbitmap_destroy(s_bt_icon_bitmap);
192
+  bitmap_layer_destroy(s_bt_icon_layer);
193
+  gbitmap_destroy(s_battery_icon_bitmap);
194
+  bitmap_layer_destroy(s_battery_icon_layer);
195
+}
196
+
197
+static void init(void) {
198
+  redraw_counter = 0;
199
+
200
+
201
+  world_bitmap = gbitmap_create_with_resource(RESOURCE_ID_WORLD_MONO);
202
+
203
+  window = window_create();
204
+  window_set_window_handlers(window, (WindowHandlers) {
205
+    .load = window_load,
206
+    .unload = window_unload,
207
+  });
208
+  
209
+  const bool animated = true;
210
+  window_stack_push(window, animated);
211
+
212
+  s = malloc(STR_SIZE);
213
+  tick_timer_service_subscribe(MINUTE_UNIT, handle_minute_tick);
214
+  
215
+  // Subscribe to battery info
216
+  battery_state_service_subscribe(battery_callback);
217
+  
218
+  // Register for Bluetooth connection updates
219
+  connection_service_subscribe((ConnectionHandlers) {
220
+    .pebble_app_connection_handler = bluetooth_callback
221
+  });  
222
+}
223
+
224
+static void deinit(void) {
225
+  tick_timer_service_unsubscribe();
226
+  free(s);
227
+  window_destroy(window);
228
+  gbitmap_destroy(world_bitmap);
229
+
230
+}
231
+
232
+int main(void) {
233
+  init();
234
+
235
+  APP_LOG(APP_LOG_LEVEL_DEBUG, "Done initializing, pushed window: %p", window);
236
+
237
+  app_event_loop();
238
+  deinit();
239
+}