Domanda

So, I'm building a pebble app and to get my head around some things I'm first making some tests with the phone-to-pebble connection. Although, as always in C, I get stuck on the most basic parts. The initial text is not being displayed and the screen is just blank.

Here's the relevant code:

static void change_text(Layer *layer, GContext *ctx){
    text_layer_set_text(textLayer, textRecieved);
    APP_LOG(APP_LOG_LEVEL_DEBUG, "Changing to: %s", textRecieved);
}

static void window_load(Window *window){

    GRect bounds = layer_get_bounds(window_get_root_layer(window));
    textLayer = text_layer_create(GRect(5,5, bounds.size.w-10, 30));

    textRecieved = (char *) malloc(BUFFER_SIZE);
    memset(textRecieved,0,BUFFER_SIZE);
    strcpy(textRecieved, "Nothing yet.");

    text_layer_set_text(textLayer, textRecieved);
    layer_add_child(window_get_root_layer(window), text_layer_get_layer(textLayer));

    APP_LOG(APP_LOG_LEVEL_DEBUG, "Recieved: %s", textRecieved);
    layer_set_update_proc(text_layer_get_layer(textLayer), change_text);    
}

Relevant variable definitions (in the top-most of the file):

TextLayer* textLayer;
const int BUFFER_SIZE = 100;
char* textRecieved;

If I comment out the *layer_set_update_proc(...);* line I can see the text. I'm probably doing something wrong in the change_text method. (It gets called shortly after window_load.) The outputs from my debugging prints is:

[DEBUG] app_message.c:70: Recieved: Nothing yet.
[DEBUG] app_message.c:55: Changing to: Nothing yet.
È stato utile?

Soluzione

You should not call layer_set_update_proc(). By doing so you override the default text_layer drawing mechanism. That is why you are not seeing anything on the screen.

A few other comments:

  • Yes you can call text_layer_set_text() whenever you want
  • Use strncpy(textRecieved, "Nothing yet", sizeof(textRecieved)); instead of memset+strcpy
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top