1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
| #include "simple_analog.h"
#include "pebble.h"
static Window *window;
static Layer *s_simple_bg_layer, *s_date_layer, *s_hands_layer;
static TextLayer *s_date_label;
static TextLayer *battery_icon_layer;
static TextLayer *battery_percent_layer;
static TextLayer *bluetooth_icon_layer;
static BitmapLayer *s_background_layer;
static GBitmap *s_background_bitmap;
static GPath *s_tick_paths[NUM_CLOCK_TICKS];
static GPath *s_minute_arrow, *s_hour_arrow;
static char s_date_buffer[20];
static void bg_update_proc(Layer *layer, GContext *ctx) {
graphics_context_set_fill_color(ctx, GColorBlack);
graphics_fill_rect(ctx, layer_get_bounds(layer), 0, GCornerNone);
graphics_context_set_fill_color(ctx, GColorWhite);
for (int i = 0; i < NUM_CLOCK_TICKS; ++i) {
gpath_draw_filled(ctx, s_tick_paths[i]);
}
}
static void hands_update_proc(Layer *layer, GContext *ctx) {
GRect bounds = layer_get_bounds(layer);
GPoint center = grect_center_point(&bounds);
int16_t second_hand_length = bounds.size.w / 2;
time_t now = time(NULL);
struct tm *t = localtime(&now);
int32_t second_angle = TRIG_MAX_ANGLE * t->tm_sec / 60;
GPoint second_hand = {
.x = (int16_t)(sin_lookup(second_angle) * (int32_t)second_hand_length / TRIG_MAX_RATIO) + center.x,
.y = (int16_t)(-cos_lookup(second_angle) * (int32_t)second_hand_length / TRIG_MAX_RATIO) + center.y,
};
// second hand
graphics_context_set_stroke_color(ctx, GColorWhite);
graphics_draw_line(ctx, second_hand, center);
// minute/hour hand
graphics_context_set_fill_color(ctx, GColorWhite);
graphics_context_set_stroke_color(ctx, GColorBlack);
gpath_rotate_to(s_minute_arrow, TRIG_MAX_ANGLE * t->tm_min / 60);
gpath_draw_filled(ctx, s_minute_arrow);
gpath_draw_outline(ctx, s_minute_arrow);
gpath_rotate_to(s_hour_arrow, (TRIG_MAX_ANGLE * (((t->tm_hour % 12) * 6) + (t->tm_min / 10))) / (12 * 6));
gpath_draw_filled(ctx, s_hour_arrow);
gpath_draw_outline(ctx, s_hour_arrow);
// dot in the middle
graphics_context_set_fill_color(ctx, GColorBlack);
graphics_fill_rect(ctx, GRect(bounds.size.w / 2 - 1, bounds.size.h / 2 - 1, 3, 3), 0, GCornerNone);
}
static void date_update_proc(Layer *layer, GContext *ctx) {
time_t now = time(NULL);
struct tm *t = localtime(&now);
strftime(s_date_buffer, sizeof(s_date_buffer), "%a %d %b %y", t);
text_layer_set_text(s_date_label, s_date_buffer);
}
static void handle_second_tick(struct tm *tick_time, TimeUnits units_changed) {
layer_mark_dirty(window_get_root_layer(window));
}
static void handle_bluetooth(bool connected) {
static char bt_text[4];
static bool bt_connected= true;
if (connected)
{
strcpy(bt_text, "\xef\x84\x96");
if (!bt_connected)
{
bt_connected = true;
vibes_double_pulse();
}
}
else
{
strcpy(bt_text, "");
if (bt_connected)
{
bt_connected = false;
vibes_long_pulse();
}
}
text_layer_set_text(bluetooth_icon_layer, bt_text);
APP_LOG(APP_LOG_LEVEL_DEBUG, "Bluetooth change: connected=%i", connected);
}
static void handle_battery(BatteryChargeState charge_state)
{
static char battery_icon[4];
static char battery_text[5];
if (charge_state.charge_percent > 80)
{
strcpy(battery_icon, "\xef\x84\x93");
}
else if (charge_state.charge_percent > 50 && charge_state.charge_percent <= 80)
{
strcpy(battery_icon, "\xef\x84\x94");
}
else if (charge_state.charge_percent > 20 && charge_state.charge_percent <= 50)
{
strcpy(battery_icon, "\xef\x84\x95");
}
else
{
strcpy(battery_icon, "\xef\x84\x92");
}
APP_LOG(APP_LOG_LEVEL_DEBUG, "Battery change: %i remaining", charge_state.charge_percent);
snprintf(battery_text, sizeof(battery_text), "%d%%", charge_state.charge_percent);
text_layer_set_text(battery_icon_layer, battery_icon);
text_layer_set_text(battery_percent_layer, battery_text);
}
static void window_load(Window *window) {
Layer *window_layer = window_get_root_layer(window);
GRect bounds = layer_get_bounds(window_layer);
GFont status_font = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_STATUS_12));
GFont icon_font = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_ICONS_14));
s_simple_bg_layer = layer_create(bounds);
layer_set_update_proc(s_simple_bg_layer, bg_update_proc);
layer_add_child(window_layer, s_simple_bg_layer);
//Create GBitmap, then set to created BitmapLayer
s_background_bitmap = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_BACKGROUND);
s_background_layer = bitmap_layer_create(GRect(0, 0, 144, 168));
bitmap_layer_set_bitmap(s_background_layer, s_background_bitmap);
layer_add_child(window_get_root_layer(window), bitmap_layer_get_layer(s_background_layer));
s_date_layer = layer_create(bounds);
layer_set_update_proc(s_date_layer, date_update_proc);
layer_add_child(window_layer, s_date_layer);
s_date_label = text_layer_create(GRect(37, 118, 80, 20));
text_layer_set_text(s_date_label, s_date_buffer);
text_layer_set_background_color(s_date_label, GColorBlack);
text_layer_set_text_color(s_date_label, GColorWhite);
text_layer_set_font(s_date_label, fonts_get_system_font(FONT_KEY_GOTHIC_18));
layer_add_child(s_date_layer, text_layer_get_layer(s_date_label));
s_hands_layer = layer_create(bounds);
layer_set_update_proc(s_hands_layer, hands_update_proc);
layer_add_child(window_layer, s_hands_layer);
// Battery Percentage
battery_percent_layer = text_layer_create(GRect(108, 4, 33, 15));
text_layer_set_font(battery_percent_layer, status_font);
text_layer_set_text_color(battery_percent_layer, GColorWhite);
text_layer_set_background_color(battery_percent_layer, GColorClear);
text_layer_set_text_alignment(battery_percent_layer, GTextAlignmentRight);
layer_add_child(window_layer, text_layer_get_layer(battery_percent_layer));
// Battery Icon
battery_icon_layer = text_layer_create(GRect(122, 140, 15, 15));
text_layer_set_font(battery_icon_layer, icon_font);
text_layer_set_text_color(battery_icon_layer, GColorWhite);
text_layer_set_background_color(battery_icon_layer, GColorClear);
text_layer_set_text_alignment(battery_icon_layer, GTextAlignmentRight);
layer_add_child(window_layer, text_layer_get_layer(battery_icon_layer));
// Bluetooth Icon
bluetooth_icon_layer = text_layer_create(GRect(2, 140, 15, 15));
text_layer_set_font(bluetooth_icon_layer, icon_font);
text_layer_set_text_color(bluetooth_icon_layer, GColorWhite);
text_layer_set_background_color(bluetooth_icon_layer, GColorClear);
text_layer_set_text_alignment(bluetooth_icon_layer, GTextAlignmentRight);
layer_add_child(window_layer, text_layer_get_layer(bluetooth_icon_layer));
// Events
battery_state_service_subscribe(&handle_battery);
bluetooth_connection_service_subscribe(&handle_bluetooth);
time_t now = time(NULL);
struct tm *current_time = localtime(&now);
handle_battery(battery_state_service_peek());
handle_bluetooth(bluetooth_connection_service_peek());
}
static void window_unload(Window *window) {
layer_destroy(s_simple_bg_layer);
layer_destroy(s_date_layer);
text_layer_destroy(battery_percent_layer);
text_layer_destroy(battery_icon_layer);
text_layer_destroy(bluetooth_icon_layer);
text_layer_destroy(s_date_label);
layer_destroy(s_hands_layer);
}
static void init() {
window = window_create();
window_set_window_handlers(window, (WindowHandlers) {
.load = window_load,
.unload = window_unload,
});
window_stack_push(window, true);
s_date_buffer[0] = '\0';
// init hand paths
s_minute_arrow = gpath_create(&MINUTE_HAND_POINTS);
s_hour_arrow = gpath_create(&HOUR_HAND_POINTS);
Layer *window_layer = window_get_root_layer(window);
GRect bounds = layer_get_bounds(window_layer);
GPoint center = grect_center_point(&bounds);
gpath_move_to(s_minute_arrow, center);
gpath_move_to(s_hour_arrow, center);
for (int i = 0; i < NUM_CLOCK_TICKS; ++i) {
s_tick_paths[i] = gpath_create(&ANALOG_BG_POINTS[i]);
}
tick_timer_service_subscribe(SECOND_UNIT, handle_second_tick);
}
static void deinit() {
gpath_destroy(s_minute_arrow);
gpath_destroy(s_hour_arrow);
for (int i = 0; i < NUM_CLOCK_TICKS; ++i) {
gpath_destroy(s_tick_paths[i]);
}
tick_timer_service_unsubscribe();
window_destroy(window);
}
int main() {
init();
app_event_loop();
deinit();
} |
Partager