Wire up fuel gauge
This commit is contained in:
+70
-7
@@ -8,6 +8,8 @@ static BitmapLayer *s_time_mg_layers[4];
|
|||||||
static BitmapLayer *s_time_fg_layers[4];
|
static BitmapLayer *s_time_fg_layers[4];
|
||||||
static BitmapLayer *s_e_layer;
|
static BitmapLayer *s_e_layer;
|
||||||
static BitmapLayer *s_f_layer;
|
static BitmapLayer *s_f_layer;
|
||||||
|
static Layer *s_batt_mg_layer;
|
||||||
|
static Layer *s_batt_layer;
|
||||||
static const uint8_t s_x_r = (PBL_DISPLAY_WIDTH / 2) + 2;
|
static const uint8_t s_x_r = (PBL_DISPLAY_WIDTH / 2) + 2;
|
||||||
static const uint8_t s_x_l = (s_x_r - 2 * (69 / 2)) - 5;
|
static const uint8_t s_x_l = (s_x_r - 2 * (69 / 2)) - 5;
|
||||||
#if PBL_DISPLAY_HEIGHT == 260
|
#if PBL_DISPLAY_HEIGHT == 260
|
||||||
@@ -17,8 +19,11 @@ static const GRect s_time_grects[4] = {GRect(s_x_l, -22, 69, 110), GRect(s_x_r,
|
|||||||
#else
|
#else
|
||||||
static const GRect s_time_grects[4] = {GRect(s_x_l, 2, 69, 110), GRect(s_x_r, 2, 69, 110), GRect(s_x_l, 116, 69, 110), GRect(s_x_r, 116, 69, 110)};
|
static const GRect s_time_grects[4] = {GRect(s_x_l, 2, 69, 110), GRect(s_x_r, 2, 69, 110), GRect(s_x_l, 116, 69, 110), GRect(s_x_r, 116, 69, 110)};
|
||||||
#endif
|
#endif
|
||||||
|
static const uint16_t s_fuel_guage_x_pos = PBL_DISPLAY_WIDTH - 23;
|
||||||
|
|
||||||
// persist statics&defines
|
// persist statics&defines
|
||||||
|
static GColor8 s_mg_color;
|
||||||
|
static GColor8 s_fg_color;
|
||||||
#define storage_key_show_time_mg 0
|
#define storage_key_show_time_mg 0
|
||||||
#define storage_key_led_color 1
|
#define storage_key_led_color 1
|
||||||
#define storage_key_bg_color 2
|
#define storage_key_bg_color 2
|
||||||
@@ -30,6 +35,42 @@ static GBitmap *s_font_bitmaps[10];
|
|||||||
static GBitmap *s_e_icon;
|
static GBitmap *s_e_icon;
|
||||||
static GBitmap *s_f_icon;
|
static GBitmap *s_f_icon;
|
||||||
|
|
||||||
|
// tracking statics
|
||||||
|
static int s_batt_level;
|
||||||
|
|
||||||
|
// bar statics
|
||||||
|
static const uint8_t s_bar_height = 8;
|
||||||
|
static const uint8_t s_bar_width = 17;
|
||||||
|
static const uint8_t s_bar_spacing = s_bar_height + 1;
|
||||||
|
|
||||||
|
static void batt_update_proc(Layer *layer, GContext *ctx) {
|
||||||
|
graphics_context_set_fill_color(ctx, GColorWhite);
|
||||||
|
const uint8_t bar_count = s_batt_level / 5;
|
||||||
|
uint16_t bar_bottom_pos = 195;
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < bar_count; ++i) {
|
||||||
|
graphics_fill_rect(ctx, GRect(0, bar_bottom_pos, s_bar_width, s_bar_height), 0, GCornerNone);
|
||||||
|
bar_bottom_pos -= s_bar_spacing;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void batt_callback(BatteryChargeState state) {
|
||||||
|
s_batt_level = state.charge_percent;
|
||||||
|
layer_mark_dirty(s_batt_layer);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void batt_mg_update_proc(Layer *layer, GContext *ctx) {
|
||||||
|
GRect bounds = layer_get_bounds(layer);
|
||||||
|
graphics_context_set_fill_color(ctx, s_mg_color);
|
||||||
|
graphics_fill_rect(ctx, bounds, 0, GCornerNone);
|
||||||
|
graphics_context_set_stroke_color(ctx, GColorRed);
|
||||||
|
const uint16_t red_y = bounds.origin.y + 183;
|
||||||
|
graphics_draw_line(ctx, GPoint(bounds.origin.x + 1, red_y), GPoint(bounds.origin.x + s_bar_width, red_y));
|
||||||
|
graphics_context_set_stroke_color(ctx, GColorDukeBlue);
|
||||||
|
const uint16_t blue_y = bounds.origin.y + 1;
|
||||||
|
graphics_draw_line(ctx, GPoint(bounds.origin.x + 1, blue_y), GPoint(bounds.origin.x + s_bar_width, blue_y));
|
||||||
|
}
|
||||||
|
|
||||||
static void main_window_load() {
|
static void main_window_load() {
|
||||||
Layer *window_layer = window_get_root_layer(s_main_window);
|
Layer *window_layer = window_get_root_layer(s_main_window);
|
||||||
|
|
||||||
@@ -45,7 +86,10 @@ static void main_window_load() {
|
|||||||
window_set_background_color(s_main_window, GColorBlue);
|
window_set_background_color(s_main_window, GColorBlue);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (persist_exists(storage_key_mg_color)) {
|
s_batt_mg_layer = layer_create(GRect(s_fuel_guage_x_pos - 1, 21, s_bar_width + 2, PBL_DISPLAY_HEIGHT - 43));
|
||||||
|
layer_set_update_proc(s_batt_mg_layer, batt_mg_update_proc);
|
||||||
|
|
||||||
|
if (persist_read_bool(storage_key_show_time_mg)) {
|
||||||
for (int i = 0; i < 4; ++i) {
|
for (int i = 0; i < 4; ++i) {
|
||||||
s_time_mg_layers[i] = bitmap_layer_create(s_time_grects[i]);
|
s_time_mg_layers[i] = bitmap_layer_create(s_time_grects[i]);
|
||||||
bitmap_layer_set_compositing_mode(s_time_mg_layers[i], GCompOpSet);
|
bitmap_layer_set_compositing_mode(s_time_mg_layers[i], GCompOpSet);
|
||||||
@@ -53,19 +97,19 @@ static void main_window_load() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
s_e_layer = bitmap_layer_create(GRect(PBL_DISPLAY_WIDTH - 23, PBL_DISPLAY_HEIGHT - 17, 17, 15));
|
s_e_layer = bitmap_layer_create(GRect(s_fuel_guage_x_pos, PBL_DISPLAY_HEIGHT - 18, 17, 15));
|
||||||
s_f_layer = bitmap_layer_create(GRect(PBL_DISPLAY_WIDTH - 23, 2, 17, 13));
|
s_f_layer = bitmap_layer_create(GRect(s_fuel_guage_x_pos, 4, 17, 13));
|
||||||
bitmap_layer_set_bitmap(s_e_layer, s_e_icon);
|
bitmap_layer_set_bitmap(s_e_layer, s_e_icon);
|
||||||
bitmap_layer_set_bitmap(s_f_layer, s_f_icon);
|
bitmap_layer_set_bitmap(s_f_layer, s_f_icon);
|
||||||
bitmap_layer_set_compositing_mode(s_e_layer, GCompOpSet);
|
bitmap_layer_set_compositing_mode(s_e_layer, GCompOpSet);
|
||||||
bitmap_layer_set_compositing_mode(s_f_layer, GCompOpSet);
|
bitmap_layer_set_compositing_mode(s_f_layer, GCompOpSet);
|
||||||
|
|
||||||
if (persist_exists(storage_key_fg_color)) {
|
if (s_fg_color.argb != GColorWhite.argb) {
|
||||||
for (int i = 0; i < 10; ++i) {
|
for (int i = 0; i < 10; ++i) {
|
||||||
replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_font_bitmaps[i], NULL);
|
replace_gbitmap_color(GColorWhite, s_fg_color, s_font_bitmaps[i], NULL);
|
||||||
}
|
}
|
||||||
replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_e_icon, NULL);
|
replace_gbitmap_color(GColorWhite, s_fg_color, s_e_icon, NULL);
|
||||||
replace_gbitmap_color(GColorWhite, (GColor8){.argb = persist_read_int(storage_key_fg_color)}, s_e_icon, NULL);
|
replace_gbitmap_color(GColorWhite, s_fg_color, s_e_icon, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < 4; ++i) {
|
for (int i = 0; i < 4; ++i) {
|
||||||
@@ -73,9 +117,12 @@ static void main_window_load() {
|
|||||||
}
|
}
|
||||||
layer_add_child(window_layer, bitmap_layer_get_layer(s_e_layer));
|
layer_add_child(window_layer, bitmap_layer_get_layer(s_e_layer));
|
||||||
layer_add_child(window_layer, bitmap_layer_get_layer(s_f_layer));
|
layer_add_child(window_layer, bitmap_layer_get_layer(s_f_layer));
|
||||||
|
layer_add_child(window_layer, s_batt_mg_layer);
|
||||||
|
layer_add_child(window_layer, s_batt_layer);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void main_window_unload() {
|
static void main_window_unload() {
|
||||||
|
layer_destroy(s_batt_mg_layer);
|
||||||
bitmap_layer_destroy(s_f_layer);
|
bitmap_layer_destroy(s_f_layer);
|
||||||
bitmap_layer_destroy(s_e_layer);
|
bitmap_layer_destroy(s_e_layer);
|
||||||
for (int i = 0; i < 4; ++i) {
|
for (int i = 0; i < 4; ++i) {
|
||||||
@@ -114,6 +161,16 @@ static void minute_handler(struct tm *tick_time, TimeUnits units_changed) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void soft_reload(bool first_load) {
|
static void soft_reload(bool first_load) {
|
||||||
|
if (persist_exists(storage_key_mg_color)) {
|
||||||
|
s_mg_color = (GColor8){.argb = persist_read_int(storage_key_mg_color)};
|
||||||
|
} else {
|
||||||
|
s_mg_color = GColorLightGray;
|
||||||
|
}
|
||||||
|
if (persist_exists(storage_key_fg_color)) {
|
||||||
|
s_fg_color = (GColor8){.argb = persist_read_int(storage_key_fg_color)};
|
||||||
|
} else {
|
||||||
|
s_fg_color = GColorWhite;
|
||||||
|
}
|
||||||
if (!first_load) {
|
if (!first_load) {
|
||||||
window_stack_remove(s_main_window, false);
|
window_stack_remove(s_main_window, false);
|
||||||
window_destroy(s_main_window);
|
window_destroy(s_main_window);
|
||||||
@@ -144,14 +201,20 @@ static void init() {
|
|||||||
s_time_fg_layers[i] = bitmap_layer_create(s_time_grects[i]);
|
s_time_fg_layers[i] = bitmap_layer_create(s_time_grects[i]);
|
||||||
bitmap_layer_set_compositing_mode(s_time_fg_layers[i], GCompOpSet);
|
bitmap_layer_set_compositing_mode(s_time_fg_layers[i], GCompOpSet);
|
||||||
}
|
}
|
||||||
|
s_batt_layer = layer_create(GRect(s_fuel_guage_x_pos, 0, s_bar_width, PBL_DISPLAY_HEIGHT));
|
||||||
|
layer_set_update_proc(s_batt_layer, batt_update_proc);
|
||||||
|
|
||||||
update_minute_1();
|
update_minute_1();
|
||||||
|
batt_callback(battery_state_service_peek());
|
||||||
soft_reload(true);
|
soft_reload(true);
|
||||||
tick_timer_service_subscribe(MINUTE_UNIT, minute_handler);
|
tick_timer_service_subscribe(MINUTE_UNIT, minute_handler);
|
||||||
|
battery_state_service_subscribe(batt_callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void deinit() {
|
static void deinit() {
|
||||||
|
battery_state_service_unsubscribe();
|
||||||
tick_timer_service_unsubscribe();
|
tick_timer_service_unsubscribe();
|
||||||
|
layer_destroy(s_batt_layer);
|
||||||
gbitmap_destroy(s_f_icon);
|
gbitmap_destroy(s_f_icon);
|
||||||
gbitmap_destroy(s_e_icon);
|
gbitmap_destroy(s_e_icon);
|
||||||
for (int i = 0; i < 4; ++i) {
|
for (int i = 0; i < 4; ++i) {
|
||||||
|
|||||||
Reference in New Issue
Block a user