Reputation: 11
I'm trying to add a Weather text layer and the compilation is successful but for some reason it won't install on the emulator. This code is what you get when you finish the first two tutorials on the site and anything that refers to a Date (layer, font) is added by me. I've added it as a child layer to the Window already, yet nothing comes up.
#include <pebble.h>
static Window *s_main_window;
static TextLayer *s_time_layer;
static TextLayer *s_date_layer;
static GFont s_date_font;
static GFont s_time_font;
static BitmapLayer *s_background_layer;
static GBitmap *s_background_bitmap;
static void update_time() {
// Get a tm structure
time_t temp = time(NULL);
// time_t temp1 = date(NULL);
struct tm *tick_time = localtime(&temp);
// struct dt *tick_date = localdate(&temp1);
// Create a long-lived buffer
static char buffer[] = "00:00:00";
static char buffer1[] = "00:00:00";
// Write the current hours and minutes into the buffer
if(clock_is_24h_style() == true) {
// Use 24 hour format
strftime(buffer, sizeof("00:00:00"), "%H:%M:%S", tick_time);
} else {
// Use 12 hour format
strftime(buffer, sizeof("00:00:00"), "%I:%M:%S", tick_time);
}
// Display this time on the TextLayer
text_layer_set_text(s_time_layer, buffer);
strftime(buffer1, sizeof("mm/dd/yy"), "%m/%d/%y", tick_time);
// Display this date on the TextLayer
text_layer_set_text(s_date_layer, buffer1);
}
static void main_window_load(Window *window) {
// 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));
// Create time TextLayer
//s_time_layer = text_layer_create(GRect(0, 55, 144, 50));
s_time_layer = text_layer_create(GRect(4, 62, 139, 50));
text_layer_set_background_color(s_time_layer, GColorClear);
text_layer_set_text_color(s_time_layer, GColorBlack);
// Create date DateLayer
//s_time_layer = text_layer_create(GRect(0, 55, 144, 50));
s_date_layer = text_layer_create(GRect(8, 184, 139, 50));
text_layer_set_background_color(s_date_layer, GColorClear);
text_layer_set_text_color(s_date_layer, GColorWhite);
// Create GFont for Time and Date
s_time_font = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_PERFECT_DOS_30));
s_date_font = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_PERFECT_DOS_30));
// Apply to TextLayer
text_layer_set_font(s_time_layer, s_time_font);
//Apply to DateLayer
text_layer_set_font(s_date_layer, s_date_font);
//Test text on watchface
//text_layer_set_text(s_time_layer, "00:00");
// Improve the layout to be more like a watchface
//text_layer_set_font(s_time_layer, fonts_get_system_font(FONT_KEY_BITHAM_30_BLACK));
text_layer_set_text_alignment(s_time_layer, GTextAlignmentCenter);
text_layer_set_text_alignment(s_date_layer, GAlignBottom);
// Add time as a child layer to the Window's root layer
layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_time_layer));
//Add date as a child layer to the Window's root layer
layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_date_layer));
}
static void main_window_unload(Window *window) {
// Unload GFonts
fonts_unload_custom_font(s_time_font);
fonts_unload_custom_font(s_date_font);
// Destroy TextLayer
text_layer_destroy(s_time_layer);
//Destroy DateLayer
text_layer_destroy(s_date_layer);
// Destroy GBitmap
gbitmap_destroy(s_background_bitmap);
// Destroy BitmapLayer
bitmap_layer_destroy(s_background_layer);
}
static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
update_time();
}
static void init() {
// Create main Window element and assign to pointer
s_main_window = window_create();
// Set handlers to manage the elements inside the Window
window_set_window_handlers(s_main_window, (WindowHandlers) {
.load = main_window_load,
.unload = main_window_unload
});
// Show the Window on the watch, with animated=true
window_stack_push(s_main_window, true);
// Make sure the time and date is displayed from the start
update_time();
// Register with TickTimerService
tick_timer_service_subscribe(MINUTE_UNIT, tick_handler);
tick_timer_service_subscribe(SECOND_UNIT, tick_handler);
tick_timer_service_subscribe(DAY_UNIT, tick_handler);
tick_timer_service_subscribe(MONTH_UNIT, tick_handler);
tick_timer_service_subscribe(YEAR_UNIT, tick_handler);
}
static void deinit() {
// Destroy Window
window_destroy(s_main_window);
}
int main(void) {
init();
app_event_loop();
deinit();
}
Upvotes: 1
Views: 334
Reputation: 1
I tested it in CloudPebble with no problems (had to remove bg image and font you didn't include.)
you made following mistakes:
s_date_layer = text_layer_create(GRect(8, 184, 139, 50));
Your Y value 184 start below the bottom of screen (168)
text_layer_set_text_color(s_date_layer, GColorWhite);
It may draw white on white (didn't see your background)
tick_timer_service_subscribe(YEAR_UNIT, tick_handler);
Only last subscribe counts so either you do only following:(fit your usage)
tick_timer_service_subscribe(SECOND_UNIT, tick_handler);
or you add/or the values you need in one call for example:
tick_timer_service_subscribe(SECOND_UNIT | DAY_UNIT, tick_handler);
Upvotes: 0