我正在制作一个非常简单的表盘我的Pebble。我有三个文本图层。一个显示时间,显示日期,一个显示当天的进度指示器。
每当我按下按钮进入菜单或转到另一个表盘时,应用程序崩溃。我想也许我并没有在deinit过程中摧毁一些东西,但是我已经仔细检查过,无法找到任何东西。这是代码(借口一些混乱,我正在调试其他东西):
#include <pebble.h>
static Window *s_main_window;
static TextLayer *s_time_layer;
static TextLayer *s_date_layer;
static TextLayer *s_progress_layer;
static void update_time() {
// Get a tm structure
time_t temp = time(NULL);
struct tm *tick_time = localtime(&temp);
// Create a long-lived buffer
static char buffer[] = "00:00";
static char date_buffer[] = "00/00/00";
static char progress_buffer[24];
int hour = 0;
if(tick_time->tm_hour == 0) { hour = 1; }
else if(tick_time->tm_hour == 1) { hour = 1; }
else if(tick_time->tm_hour == 2) { hour = 1; }
else if(tick_time->tm_hour == 3) { hour = 2; }
else if(tick_time->tm_hour == 4) { hour = 2; }
else if(tick_time->tm_hour == 5) { hour = 3; }
else if(tick_time->tm_hour == 6) { hour = 3; }
else if(tick_time->tm_hour == 7) { hour = 4; }
else if(tick_time->tm_hour == 8) { hour = 4; }
else if(tick_time->tm_hour == 9) { hour = 5; }
else if(tick_time->tm_hour == 10) { hour = 5; }
else if(tick_time->tm_hour == 11) { hour = 6; }
else if(tick_time->tm_hour == 12) { hour = 6; }
else if(tick_time->tm_hour == 13) { hour = 7; }
else if(tick_time->tm_hour == 14) { hour = 7; }
else if(tick_time->tm_hour == 15) { hour = 8; }
else if(tick_time->tm_hour == 16) { hour = 8; }
else if(tick_time->tm_hour == 17) { hour = 9; }
else if(tick_time->tm_hour == 18) { hour = 9; }
else if(tick_time->tm_hour == 19) { hour = 10; }
else if(tick_time->tm_hour == 20) { hour = 10; }
else if(tick_time->tm_hour == 21) { hour = 11; }
else if(tick_time->tm_hour == 22) { hour = 11; }
else if(tick_time->tm_hour == 23) { hour = 12; }
else if(tick_time->tm_hour == 24) { hour = 12; }
const char src1[] = "•";
const char src2[] = "• •";
const char src3[] = "• • •";
const char src4[] = "• • • •";
const char src5[] = "• • • • •";
const char src6[] = "• • • • • •";
const char src7[] = "• • • • • • •";
const char src8[] = "• • • • • • • •";
const char src9[] = "• • • • • • • • •";
const char src10[] = "• • • • • • • • • •";
const char src11[] = "• • • • • • • • • • •";
const char src12[] = "• • • • • • • • • • • •";
switch(hour){
case 1:
strcpy(progress_buffer, src1);
break;
case 2:
strcpy(progress_buffer, src2);
break;
case 3:
strcpy(progress_buffer, src3);
break;
case 4:
strcpy(progress_buffer, src4);
break;
case 5:
strcpy(progress_buffer, src5);
break;
case 6:
strcpy(progress_buffer, src6);
break;
case 7:
strcpy(progress_buffer, src7);
break;
case 8:
strcpy(progress_buffer, src8);
break;
case 9:
strcpy(progress_buffer, src9);
break;
case 10:
strcpy(progress_buffer, src10);
break;
case 11:
strcpy(progress_buffer, src10);
break;
case 12:
strcpy(progress_buffer, src10);
break;
default:
strcpy(progress_buffer, src1);
break;
}
// Write the current hours and minutes into the buffer
if(clock_is_24h_style() == true) {
// Use 24 hour format
strftime(buffer, sizeof("00:00"), "%H:%M", tick_time);
strftime(date_buffer, sizeof("00/00/00"), "%D", tick_time);
} else {
// Use 12 hour format
strftime(buffer, sizeof("00:00"), "%I:%M", tick_time);
strftime(date_buffer, sizeof("00/00/00"), "%D", tick_time);
}
if (!clock_is_24h_style() && (buffer[0] == '0')) {
memmove(buffer, &buffer[1], sizeof(buffer) - 1);
}
// Display this time on the TextLayer
text_layer_set_text(s_time_layer, buffer);
text_layer_set_text(s_date_layer, date_buffer);
text_layer_set_text(s_progress_layer, progress_buffer);
}
static void tick_handler(struct tm *tick_time, TimeUnits units_changed) {
update_time();
}
static void main_window_load(Window *window) {
window_set_background_color(s_main_window, GColorBlack);
// Create time TextLayer
s_time_layer = text_layer_create(GRect(0, 45, 144, 55));
text_layer_set_background_color(s_time_layer, GColorBlack);
text_layer_set_text_color(s_time_layer, GColorWhite);
// Improve the layout to be more like a watchface
text_layer_set_font(s_time_layer, fonts_get_system_font(FONT_KEY_BITHAM_42_BOLD));
text_layer_set_text_alignment(s_time_layer, GTextAlignmentCenter);
// Add it 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));
// Create date TextLayer
s_date_layer = text_layer_create(GRect(0, 95, 144, 40));
text_layer_set_background_color(s_date_layer, GColorBlack);
text_layer_set_text_color(s_date_layer, GColorWhite);
// Improve the layout to be more like a watchface
text_layer_set_font(s_date_layer, fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD));
text_layer_set_text_alignment(s_date_layer, GTextAlignmentCenter);
// Add it 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));
// Create progress TextLayer
s_progress_layer = text_layer_create(GRect(0, 143, 144, 35));
text_layer_set_background_color(s_progress_layer, GColorBlack);
text_layer_set_text_color(s_progress_layer, GColorWhite);
// Improve the layout to be more like a watchface
text_layer_set_font(s_progress_layer, fonts_get_system_font(FONT_KEY_GOTHIC_14));
text_layer_set_text_alignment(s_progress_layer, GTextAlignmentCenter);
// Add it as a child layer to the Window's root layer
layer_add_child(window_get_root_layer(window), text_layer_get_layer(s_progress_layer));
// Make sure the time is displayed from the start
update_time();
}
static void main_window_unload(Window *window) {
// Destroy TextLayer
text_layer_destroy(s_time_layer);
text_layer_destroy(s_date_layer);
text_layer_destroy(s_progress_layer);
}
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);
// Register with TickTimerService
tick_timer_service_subscribe(MINUTE_UNIT, tick_handler);
}
static void deinit() {
// Destroy Window
window_destroy(s_main_window);
tick_timer_service_unsubscribe();
}
int main(void) {
init();
app_event_loop();
deinit();
}
答案 0 :(得分:0)
(我无法弄清楚为什么会修复它,或者为什么它首先崩溃但是......)
如果您从window_destroy(s_main_window);
功能中移除deinit
,则不会再崩溃。
答案 1 :(得分:0)
我增加了progress_buffer的缓冲区大小,表盘停止了崩溃。我发现代码的值至少为37。我想缓冲区需要存储多于点,例如空白。