C Pebble秒表更改字体大小错误

C Pebble秒表更改字体大小错误,c,counter,stopwatch,pebble-sdk,cloudpebble,C,Counter,Stopwatch,Pebble Sdk,Cloudpebble,我已经试着让一个应用程序在我的鹅卵石上运行了一段时间,我终于让它工作了。这是一个体育记录仪,记录两个队的得分,中间还有一个内置的秒表。一切正常,但如果秒表运行42秒,计数器的字体就会变得非常小。我不知道发生了什么事 当我环顾四周时,有人说可能是内存泄漏,但这与我的问题不同 #include <pebble.h> #define COUNTER_FONT_49 RESOURCE_ID_MACHINE_GUN_49 #define STOPWATCH_FONT_24 RESOURCE_

我已经试着让一个应用程序在我的鹅卵石上运行了一段时间,我终于让它工作了。这是一个体育记录仪,记录两个队的得分,中间还有一个内置的秒表。一切正常,但如果秒表运行42秒,计数器的字体就会变得非常小。我不知道发生了什么事

当我环顾四周时,有人说可能是内存泄漏,但这与我的问题不同

#include <pebble.h>

#define COUNTER_FONT_49 RESOURCE_ID_MACHINE_GUN_49
#define STOPWATCH_FONT_24 RESOURCE_ID_SPORTS_WORLD_24
#define HOME_AWAY_FONT_18 FONT_KEY_GOTHIC_18_BOLD

//---Counter Constants---
#define COUNTER_START 0
#define COUNTER_MAX 9999
#define COUNTER_MIN -9999
#define MAX_DIGITS 4

//---Interface Variables---
Window* window;
static Layer* layer;
static GFont counterFont;
static GFont stopwatchFont;
static TextLayer* teamAScore_layer;
static TextLayer* teamBScore_layer;
static TextLayer* big_time_layer;
static TextLayer* seconds_time_layer;
static TextLayer* home_away_layer;


//---Counter Variables---
char teamA_counter_text[MAX_DIGITS + 2 /* sign & \0 */];
char teamB_counter_text[MAX_DIGITS + 2 /* sign & \0 */];
int teamACounter;
int teamBCounter;
int singleClickIncrement;
int longClickIncrement;
int doubleClickIncrement;

//The Time
static double elapsed_time = 0;
static bool started = false;
static AppTimer* update_timer = NULL;
static double start_time = 0;
static double pause_time = 0;
time_t time_seconds();
void stop_stopwatch();
void start_stopwatch();
void handle_timer(void* data);
void update_stopwatch();

double float_time_ms() {
    time_t seconds;
    uint16_t milliseconds;
    time_ms(&seconds, &milliseconds);
    return (double)seconds + ((double)milliseconds / 1000.0);
}

void stop_stopwatch() {
    started = false;
    pause_time = float_time_ms();
    if(update_timer != NULL) {
        app_timer_cancel(update_timer);
        update_timer = NULL;
    }
}

void start_stopwatch() {
    started = true;
    if(start_time == 0) {
        start_time = float_time_ms();
    } else if(pause_time != 0) {
        double interval = float_time_ms() - pause_time;
        start_time += interval;
    }
    update_timer = app_timer_register(100, handle_timer, NULL);
}

void update_stopwatch() {
    static char big_time[] = "00:00";
    static char deciseconds_time[] = ".0";
    static char seconds_time[] = ":00";

    // Now convert to hours/minutes/seconds.
    int tenths = (int)(elapsed_time * 10) % 10;
    int seconds = (int)elapsed_time % 60;
    int minutes = (int)elapsed_time / 60 % 60;
    int hours = (int)elapsed_time / 3600;

    // We can't fit three digit hours, so stop timing here.
    if(hours > 99) {
        stop_stopwatch();
        return;
    }

    if(hours < 1) {
        snprintf(big_time, 6, "%02d:%02d", minutes, seconds);
        snprintf(deciseconds_time, 3, ".%d", tenths);
    } else {
        snprintf(big_time, 6, "%02d:%02d", hours, minutes);
        snprintf(seconds_time, 4, ":%02d", seconds);
    }

    // Now draw the strings.
    text_layer_set_text(big_time_layer, big_time);
    text_layer_set_text(seconds_time_layer, hours < 1 ? deciseconds_time : seconds_time);
}

void select_click_long_handler(ClickRecognizerRef recognizer, Window *window) {           //pressed SELECT LONG
    bool is_running = started;
    stop_stopwatch();
    start_time = 0;
    elapsed_time = 0;
    if(is_running) start_stopwatch();
    update_stopwatch();
}

static void select_click_handler(ClickRecognizerRef recognizer, void *context) {         //pressed SELECT 
  if(started) {
        stop_stopwatch();
    } else {
        start_stopwatch();
    }
}

void handle_timer(void* data) {
    if(started) {
        double now = float_time_ms();
        elapsed_time = now - start_time;
        update_timer = app_timer_register(100, handle_timer, NULL);
    }
    update_stopwatch();
}

static int increment_value(int team_score, const int increment){
  if(team_score + increment <= COUNTER_MAX && team_score + increment >= COUNTER_MIN)
     team_score = team_score + increment;

  return team_score;
}

static void up_multi_click_handler(ClickRecognizerRef recognizer, void *context) {       //pressed UP MULTI
  teamACounter = increment_value(teamACounter, doubleClickIncrement);
  layer_mark_dirty(layer);
}

static void up_click_long_handler(ClickRecognizerRef recognizer, void *context) {        //pressed UP LONG
  teamACounter = increment_value(teamACounter, longClickIncrement);
  layer_mark_dirty(layer);
}

static void up_click_handler(ClickRecognizerRef recognizer, void *context) {             //pressed UP 
  teamACounter = increment_value(teamACounter, singleClickIncrement);
  layer_mark_dirty(layer);
}

static void down_multi_click_handler(ClickRecognizerRef recognizer, void *context) {     //pressed DOWN MULTI
  teamBCounter = increment_value(teamBCounter, doubleClickIncrement);
  layer_mark_dirty(layer);
}

static void down_click_long_handler(ClickRecognizerRef recognizer, void *context) {     //pressed DOWN LONG
  teamBCounter = increment_value(teamBCounter, longClickIncrement);
  layer_mark_dirty(layer);
}

static void down_click_handler(ClickRecognizerRef recognizer, void *context) {          //pressed DOWN
  teamBCounter = increment_value(teamBCounter, singleClickIncrement);
  layer_mark_dirty(layer);
}

static void click_config_provider(void *context) {
  window_single_click_subscribe(BUTTON_ID_SELECT, select_click_handler);
  window_long_click_subscribe(BUTTON_ID_SELECT, 700, (ClickHandler) select_click_long_handler, NULL);

  window_single_click_subscribe(BUTTON_ID_UP, up_click_handler);
  window_long_click_subscribe(BUTTON_ID_UP, 700, (ClickHandler)up_click_long_handler, NULL);
  window_multi_click_subscribe(BUTTON_ID_UP, 2, 10, 0, true, up_multi_click_handler);

  window_single_click_subscribe(BUTTON_ID_DOWN, down_click_handler);
  window_long_click_subscribe(BUTTON_ID_DOWN, 700, (ClickHandler)down_click_long_handler, NULL);
  window_multi_click_subscribe(BUTTON_ID_DOWN, 2, 10, 0, true, down_multi_click_handler);
}

static void update_layer(Layer *layer, GContext *ctx){
  GRect bounds = layer_get_frame(layer);
  counterFont = fonts_load_custom_font(resource_get_handle(COUNTER_FONT_49));

  graphics_context_set_text_color(ctx, GColorBlack);

  snprintf(teamA_counter_text, (MAX_DIGITS + 2)*sizeof(char), "%d", teamACounter);
  graphics_draw_text(ctx, 
                     teamA_counter_text, 
                     counterFont, 
                     GRect(0, 0, bounds.size.w - 10, 60),
                     GTextOverflowModeWordWrap, 
                     GTextAlignmentCenter, 
                     NULL);

  snprintf(teamB_counter_text, (MAX_DIGITS + 2)*sizeof(char), "%d", teamBCounter);
  graphics_draw_text(ctx, 
                     teamB_counter_text,
                     counterFont, 
                     GRect(0, 95, bounds.size.w - 10, 60), 
                     GTextOverflowModeWordWrap,
                     GTextAlignmentCenter, 
                     NULL);
}

static void window_load(Window *window) {
  // Get the root layer
  Layer *window_layer = window_get_root_layer(window);
  // Get the bounds of the window for sizing the text layer
  GRect bounds = layer_get_bounds(window_layer);

  layer = layer_create(bounds);
  layer_set_update_proc(layer, update_layer);
  layer_add_child(window_layer, layer);
}

static void window_unload(Window *window) {
  // Destroy TextLayer
  text_layer_destroy(teamAScore_layer);
  text_layer_destroy(teamBScore_layer);
  text_layer_destroy(big_time_layer);
  text_layer_destroy(seconds_time_layer);
}

void handle_init(void) {
  window = window_create();
  window_set_click_config_provider(window, click_config_provider);
  window_set_window_handlers(window, (WindowHandlers) {
      .load = window_load,
    .unload = window_unload,
  });

  //Set the counters start
  teamACounter = COUNTER_START;
  teamBCounter = COUNTER_START;
//---------------------------------------------TEMPERARY INCREMENT
  singleClickIncrement = 1;
  doubleClickIncrement = 2;
  longClickIncrement   = 3;

  Layer *root_layer = window_get_root_layer(window);
  stopwatchFont = fonts_load_custom_font(resource_get_handle(STOPWATCH_FONT_24));

  //-----Display Stop Watch-----
  big_time_layer = text_layer_create(GRect(0, 65, 86, 35));
  text_layer_set_text_alignment(big_time_layer, GTextAlignmentRight);
  text_layer_set_background_color(big_time_layer, GColorClear);
  text_layer_set_text(big_time_layer, "00:00");
  text_layer_set_font(big_time_layer, stopwatchFont);
  layer_add_child(root_layer, (Layer*)big_time_layer);

  seconds_time_layer = text_layer_create(GRect(86, 65, 49, 35));
  text_layer_set_text(seconds_time_layer, ".0");
  text_layer_set_background_color(seconds_time_layer, GColorClear);
  text_layer_set_font(seconds_time_layer, stopwatchFont);
  layer_add_child(root_layer, (Layer*)seconds_time_layer);

  //-----Display Home and Away-----
  home_away_layer = text_layer_create(GRect(124, 0, 20, 152));
  text_layer_set_text(home_away_layer, "\n H\n\n\n\n\n A");
  text_layer_set_background_color(home_away_layer, GColorBlack);
  text_layer_set_text_color(home_away_layer, GColorWhite);
  text_layer_set_font(home_away_layer, fonts_get_system_font(HOME_AWAY_FONT_18));
  layer_add_child(root_layer, (Layer*)home_away_layer);

  const bool animated = true;
  window_stack_push(window, animated);
}

void handle_deinit(void) {
  window_destroy(window);
}

int main(void) {
  handle_init();
  app_event_loop();
  handle_deinit();
}
#包括
#定义计数器\u字体\u 49资源\u ID\u机器\u枪\u 49
#定义秒表\u字体\u 24资源\u ID\u体育\u世界\u 24
#定义HOME\u AWAY\u FONT\u 18 FONT\u KEY\u GOTHIC\u 18\u BOLD
//---反常量---
#定义计数器\u开始0
#定义计数器_max9999
#定义计数器_MIN-9999
#定义最大数字4
//---接口变量---
窗口*窗口;
静态层*层;
静态GFont反字体;
静态GFont秒表字体;
静态文本层*团队核心层;
静态文本层*teamBScore\u层;
静态文本层*大时间层;
静态文本层*秒时间层;
静态文本层*home\u WANGE\u层;
//---计数器变量---
char teamA_counter_text[最大位数+2/*符号&\0*/];
char teamB_counter_text[最大位数+2/*符号&\0*/];
int teamACounter;
国际团队计数;
int单点击增量;
int-longClickIncrement;
int双击增量;
//时间
静态双运行时间=0;
静态bool start=false;
静态AppTimer*update_timer=NULL;
静态双启动时间=0;
静态双暂停时间=0;
time_t time_seconds();
作废秒表();
无效开始计时();
无效句柄\计时器(无效*数据);
void update_stopwatch();
双浮动时间(毫秒){
时间t秒;
uint16_t毫秒;
时间(毫秒、秒、毫秒);
返回(双)秒+((双)毫秒/1000.0);
}
无效秒表(){
开始=错误;
暂停时间=浮动时间();
如果(更新计时器!=NULL){
应用程序\定时器\取消(更新\定时器);
更新_定时器=空;
}
}
无效开始计时(){
开始=真;
如果(开始时间==0){
开始时间=浮动时间();
}否则如果(暂停时间!=0){
双间隔=浮动时间\u ms()-暂停时间;
开始时间+=间隔;
}
更新定时器=应用定时器寄存器(100,句柄定时器,空);
}
作废更新_秒表(){
静态字符big_time[]=“00:00”;
静态字符分秒\u时间[]=“.0”;
静态字符秒\u时间[]=“:00”;
//现在转换为小时/分钟/秒。
十分之一整数=(整数)(经过的时间*10)%10;
int秒=(int)已用时间%60;
int分钟=(int)经过的时间/60%60;
整数小时=(整数)经过的时间/3600;
//我们不能适应三位数的小时,所以停止计时。
如果(小时数>99){
秒表;
回来
}
如果(小时<1){
snprintf(大_时间,6,“%02d:%02d”,分,秒);
snprintf(分秒/单位时间,3,.%d),十分之一);
}否则{
snprintf(大_时间,6,“%02d:%02d”,小时,分钟);
snprintf(秒/单位时间,4“:%02d”,秒);
}
//现在画线。
文本\层\设置\文本(大\时间\层,大\时间);
文本层设置文本(秒时间层,小时<1?分秒时间:秒时间);
}
void select\u click\u long\u处理程序(ClickRecognizerRef recognizer,Window*Window){//按下select long
bool正在运行=已启动;
秒表;
开始时间=0;
已用时间=0;
如果(正在运行)启动秒表();
更新秒表();
}
静态无效选择\单击\处理程序(单击识别器REF识别器,无效*上下文){//按选择
如果(启动){
秒表;
}否则{
启动秒表();
}
}
无效句柄\计时器(无效*数据){
如果(启动){
现在加倍=浮动时间毫秒();
已用时间=现在-开始时间;
更新定时器=应用定时器寄存器(100,句柄定时器,空);
}
更新秒表();
}
静态整数增量值(整数团队分数,常数整数增量){
如果(团队得分+增量=计数器最小值)
团队得分=团队得分+增量;
返回队得分;
}
静态无效向上\u多\u单击\u处理程序(单击识别器参考识别器,无效*上下文){//按向上多个
teamACounter=增量值(teamACounter,双击增量);
层\标记\脏(层);
}
静态向上无效\u单击长\u处理程序(单击识别器参考识别器,void*上下文){//按向上长
teamACounter=增量值(teamACounter,longClickIncrement);
层\标记\脏(层);
}
静态向上作废\u单击\u处理程序(单击识别器参考识别器,作废*上下文){//向上按
teamACounter=增量值(teamACounter,singleClickIncrement);
层\标记\脏(层);
}
静态void down\u multi\u click\u处理程序(ClickRecognizerRef recognizer,void*上下文){//按下multi
teamBCounter=增量值(teamBCounter,双击增量);
层\标记\脏(层);
}
静态无效向下\单击长\处理程序(单击识别器参考识别器,无效*上下文){//按下长
teamBCounter=增量值(teamBCounter,longClickIncrement);
层\标记\脏(层);
}
静态void down\u单击\u处理程序(单击识别器ref识别器,void*上下文){//按下
teamBCounter=增量值(teamBCounter,singleClickIncrement);
层\标记\脏(层);
}
静态无效单击配置提供程序(无效*上下文){
窗口\单击\订阅(按钮\ ID \选择,选择\单击\处理程序);
窗口长点击订阅(按钮ID选择,700,(点击处理程序)选择长点击处理程序,空);
窗口单击订阅(按钮ID向上,向上单击处理程序);
窗口长点击订阅(按钮ID向上,700,(点击处理程序)向上点击长处理程序,空);
窗口\u多点单击\u订阅(按钮\u ID\u UP,2,10,0,true,UP\u多点单击\u处理程序);
窗口\单击\订阅(按钮\ ID \向下,向下\单击\处理程序);
窗口长单击订阅(按钮ID向下,700,(单击处理程序)向下单击长句柄